Move-enable various parts of TextFlow

This removes about 200 pointless copies from printing the help
message (the original motivation for the change), and also nicely
improves performance of the various reporters that depend on
TextFlow.
This commit is contained in:
Martin Hořeňovský 2023-12-27 21:43:40 +01:00
parent eaafd07674
commit b52d97855d
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
4 changed files with 54 additions and 22 deletions

View File

@ -366,8 +366,8 @@ namespace Catch {
optWidth = ( std::min )( optWidth, consoleWidth / 2 );
for ( auto const& cols : rows ) {
auto row = TextFlow::Column( cols.left )
for ( auto& cols : rows ) {
auto row = TextFlow::Column( CATCH_MOVE(cols.left) )
.width( optWidth )
.indent( 2 ) +
TextFlow::Spacer( 4 ) +

View File

@ -233,23 +233,36 @@ namespace Catch {
return os;
}
Columns Column::operator+( Column const& other ) {
Columns operator+(Column const& lhs, Column const& rhs) {
Columns cols;
cols += *this;
cols += other;
cols += lhs;
cols += rhs;
return cols;
}
Columns operator+(Column&& lhs, Column&& rhs) {
Columns cols;
cols += CATCH_MOVE( lhs );
cols += CATCH_MOVE( rhs );
return cols;
}
Columns& Columns::operator+=( Column const& col ) {
m_columns.push_back( col );
return *this;
Columns& operator+=(Columns& lhs, Column const& rhs) {
lhs.m_columns.push_back( rhs );
return lhs;
}
Columns Columns::operator+( Column const& col ) {
Columns combined = *this;
combined += col;
Columns& operator+=(Columns& lhs, Column&& rhs) {
lhs.m_columns.push_back( CATCH_MOVE(rhs) );
return lhs;
}
Columns operator+( Columns const& lhs, Column const& rhs ) {
auto combined( lhs );
combined += rhs;
return combined;
}
Columns operator+( Columns&& lhs, Column&& rhs ) {
lhs += CATCH_MOVE( rhs );
return CATCH_MOVE( lhs );
}
} // namespace TextFlow
} // namespace Catch

View File

@ -8,8 +8,10 @@
#ifndef CATCH_TEXTFLOW_HPP_INCLUDED
#define CATCH_TEXTFLOW_HPP_INCLUDED
#include <cassert>
#include <catch2/internal/catch_console_width.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <cassert>
#include <string>
#include <vector>
@ -37,7 +39,7 @@ namespace Catch {
public:
/**
* Iterates "lines" in `Column` and return sthem
* Iterates "lines" in `Column` and returns them
*/
class const_iterator {
friend Column;
@ -91,20 +93,35 @@ namespace Catch {
using iterator = const_iterator;
explicit Column( std::string const& text ): m_string( text ) {}
explicit Column( std::string&& text ):
m_string( CATCH_MOVE(text)) {}
Column& width( size_t newWidth ) {
Column& width( size_t newWidth ) & {
assert( newWidth > 0 );
m_width = newWidth;
return *this;
}
Column& indent( size_t newIndent ) {
Column&& width( size_t newWidth ) && {
assert( newWidth > 0 );
m_width = newWidth;
return CATCH_MOVE( *this );
}
Column& indent( size_t newIndent ) & {
m_indent = newIndent;
return *this;
}
Column& initialIndent( size_t newIndent ) {
Column&& indent( size_t newIndent ) && {
m_indent = newIndent;
return CATCH_MOVE( *this );
}
Column& initialIndent( size_t newIndent ) & {
m_initialIndent = newIndent;
return *this;
}
Column&& initialIndent( size_t newIndent ) && {
m_initialIndent = newIndent;
return CATCH_MOVE( *this );
}
size_t width() const { return m_width; }
const_iterator begin() const { return const_iterator( *this ); }
@ -113,7 +130,8 @@ namespace Catch {
friend std::ostream& operator<<( std::ostream& os,
Column const& col );
Columns operator+( Column const& other );
friend Columns operator+( Column const& lhs, Column const& rhs );
friend Columns operator+( Column&& lhs, Column&& rhs );
};
//! Creates a column that serves as an empty space of specific width
@ -157,8 +175,10 @@ namespace Catch {
iterator begin() const { return iterator( *this ); }
iterator end() const { return { *this, iterator::EndTag() }; }
Columns& operator+=( Column const& col );
Columns operator+( Column const& col );
friend Columns& operator+=( Columns& lhs, Column const& rhs );
friend Columns& operator+=( Columns& lhs, Column&& rhs );
friend Columns operator+( Columns const& lhs, Column const& rhs );
friend Columns operator+( Columns&& lhs, Column&& rhs );
friend std::ostream& operator<<( std::ostream& os,
Columns const& cols );

View File

@ -315,11 +315,10 @@ public:
*this << RowBreak();
TextFlow::Columns headerCols;
auto spacer = TextFlow::Spacer(2);
for (auto const& info : m_columnInfos) {
assert(info.width > 2);
headerCols += TextFlow::Column(info.name).width(info.width - 2);
headerCols += spacer;
headerCols += TextFlow::Spacer( 2 );
}
m_os << headerCols << '\n';