diff options
| author | Martin Hořeňovský | 2023-12-26 11:57:38 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2023-12-26 15:41:07 +0100 |
| commit | 5d5f42f99b7409b6b051182fae69fd22f8877a6d (patch) | |
| tree | 1a17ece45bac0d154f5be17a36cae032af21eba6 | |
| parent | c57e349d1d62fcdf96aef576a5c200ae2078daad (diff) | |
| download | catch3-5d5f42f99b7409b6b051182fae69fd22f8877a6d.tar.gz | |
Enable moving Opts into Parser
This also required keeping the reference type in the fluent interface
for Opts.
Removes another ~40 allocations.
| -rw-r--r-- | src/catch2/internal/catch_clara.hpp | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/catch2/internal/catch_clara.hpp b/src/catch2/internal/catch_clara.hpp index a9413810..4bcef406 100644 --- a/src/catch2/internal/catch_clara.hpp +++ b/src/catch2/internal/catch_clara.hpp @@ -495,10 +495,14 @@ namespace Catch { m_ref( std::make_shared<BoundLambda<LambdaT>>( ref ) ), m_hint( hint ) {} - auto operator()( StringRef description ) -> DerivedT& { + DerivedT& operator()( StringRef description ) & { m_description = description; return static_cast<DerivedT&>( *this ); } + DerivedT&& operator()( StringRef description ) && { + m_description = description; + return static_cast<DerivedT&&>( *this ); + } auto optional() -> DerivedT& { m_optionality = Optionality::Optional; @@ -567,10 +571,14 @@ namespace Catch { Opt( T& ref, StringRef hint ): ParserRefImpl( ref, hint ) {} - auto operator[](std::string const& optName) -> Opt& { + Opt& operator[]( std::string const& optName ) & { m_optNames.push_back(optName); return *this; } + Opt&& operator[]( std::string const& optName ) && { + m_optNames.push_back( optName ); + return CATCH_MOVE(*this); + } Detail::HelpColumns getHelpColumns() const; @@ -628,21 +636,25 @@ namespace Catch { return *this; } - auto operator|=(Opt const& opt) -> Parser& { - m_options.push_back(opt); - return *this; + friend Parser& operator|=( Parser& p, Opt const& opt ) { + p.m_options.push_back( opt ); + return p; + } + friend Parser& operator|=( Parser& p, Opt&& opt ) { + p.m_options.push_back( CATCH_MOVE(opt) ); + return p; } Parser& operator|=(Parser const& other); template <typename T> - friend Parser operator|(Parser const& p, T const& rhs) { - return Parser( p ) |= rhs; + friend Parser operator|( Parser const& p, T&& rhs ) { + return Parser( p ) |= CATCH_FORWARD(rhs); } template <typename T> - friend Parser operator|( Parser&& p, T const& rhs ) { - p |= rhs; + friend Parser operator|( Parser&& p, T&& rhs ) { + p |= CATCH_FORWARD(rhs); return CATCH_MOVE(p); } |