Clean up extraneous copies in Messages

This removes 109 allocations from running `tests/SelfTest`
(437057 -> 436948).
This commit is contained in:
Martin Hořeňovský 2023-01-28 22:01:34 +01:00
parent 356dfc1439
commit 906552f8c8
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
6 changed files with 11 additions and 13 deletions

View File

@ -19,8 +19,8 @@ namespace Catch {
////////////////////////////////////////////////////////////////////////////
ScopedMessage::ScopedMessage( MessageBuilder const& builder ):
m_info( builder.m_info ) {
ScopedMessage::ScopedMessage( MessageBuilder&& builder ):
m_info( CATCH_MOVE(builder.m_info) ) {
m_info.message = builder.m_stream.str();
getResultCapture().pushScopedMessage( m_info );
}

View File

@ -39,11 +39,10 @@ namespace Catch {
ResultWas::OfType type ):
m_info(macroName, lineInfo, type) {}
template<typename T>
MessageBuilder& operator << ( T const& value ) {
MessageBuilder&& operator << ( T const& value ) && {
m_stream << value;
return *this;
return CATCH_MOVE(*this);
}
MessageInfo m_info;
@ -51,7 +50,7 @@ namespace Catch {
class ScopedMessage {
public:
explicit ScopedMessage( MessageBuilder const& builder );
explicit ScopedMessage( MessageBuilder&& builder );
ScopedMessage( ScopedMessage& duplicate ) = delete;
ScopedMessage( ScopedMessage&& old ) noexcept;
~ScopedMessage();

View File

@ -65,7 +65,7 @@ namespace Catch {
virtual void pushScopedMessage( MessageInfo const& message ) = 0;
virtual void popScopedMessage( MessageInfo const& message ) = 0;
virtual void emplaceUnscopedMessage( MessageBuilder const& builder ) = 0;
virtual void emplaceUnscopedMessage( MessageBuilder&& builder ) = 0;
virtual void handleFatalErrorCondition( StringRef message ) = 0;

View File

@ -60,10 +60,9 @@ namespace Catch {
// Copy message into messages list.
// !TBD This should have been done earlier, somewhere
MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() );
builder << assertionResult.getMessage();
builder.m_info.message = builder.m_stream.str();
builder.m_info.message = static_cast<std::string>(assertionResult.getMessage());
infoMessages.push_back( builder.m_info );
infoMessages.push_back( CATCH_MOVE(builder.m_info) );
}
}

View File

@ -402,8 +402,8 @@ namespace Catch {
m_messages.erase(std::remove(m_messages.begin(), m_messages.end(), message), m_messages.end());
}
void RunContext::emplaceUnscopedMessage( MessageBuilder const& builder ) {
m_messageScopes.emplace_back( builder );
void RunContext::emplaceUnscopedMessage( MessageBuilder&& builder ) {
m_messageScopes.emplace_back( CATCH_MOVE(builder) );
}
std::string RunContext::getCurrentTestName() const {

View File

@ -90,7 +90,7 @@ namespace Catch {
void pushScopedMessage( MessageInfo const& message ) override;
void popScopedMessage( MessageInfo const& message ) override;
void emplaceUnscopedMessage( MessageBuilder const& builder ) override;
void emplaceUnscopedMessage( MessageBuilder&& builder ) override;
std::string getCurrentTestName() const override;