Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2023-05-06 14:31:29 +0200
committerMartin Hořeňovský2023-05-06 15:22:02 +0200
commitc57b5cdf432b1983c6f1b1fd9fb1f7ff11397391 (patch)
tree265388000fe49146a56ac24e81dbe42560cd6ce4 /src
parentd84777c9cbf5b1b7d0e9ef2e14606ece254adf7a (diff)
downloadcatch3-c57b5cdf432b1983c6f1b1fd9fb1f7ff11397391.tar.gz
Move-enable Catch::optional
This avoids copies in couple places through Catch2, e.g. reporter spec handling, and moving around `AssertionResult` in `RunContext`.
Diffstat (limited to 'src')
-rw-r--r--src/catch2/internal/catch_optional.hpp55
1 files changed, 36 insertions, 19 deletions
diff --git a/src/catch2/internal/catch_optional.hpp b/src/catch2/internal/catch_optional.hpp
index ac3714ee..d1e953ad 100644
--- a/src/catch2/internal/catch_optional.hpp
+++ b/src/catch2/internal/catch_optional.hpp
@@ -8,6 +8,8 @@
#ifndef CATCH_OPTIONAL_HPP_INCLUDED
#define CATCH_OPTIONAL_HPP_INCLUDED
+#include <catch2/internal/catch_move_and_forward.hpp>
+
#include <cassert>
namespace Catch {
@@ -16,35 +18,50 @@ namespace Catch {
template<typename T>
class Optional {
public:
- Optional() : nullableValue( nullptr ) {}
- Optional( T const& _value )
- : nullableValue( new( storage ) T( _value ) )
- {}
- Optional( Optional const& _other )
- : nullableValue( _other ? new( storage ) T( *_other ) : nullptr )
- {}
-
- ~Optional() {
+ Optional(): nullableValue( nullptr ) {}
+ ~Optional() { reset(); }
+
+ Optional( T const& _value ):
+ nullableValue( new ( storage ) T( _value ) ) {}
+ Optional( T&& _value ):
+ nullableValue( new ( storage ) T( CATCH_MOVE( _value ) ) ) {}
+
+ Optional& operator=( T const& _value ) {
reset();
+ nullableValue = new ( storage ) T( _value );
+ return *this;
}
+ Optional& operator=( T&& _value ) {
+ reset();
+ nullableValue = new ( storage ) T( CATCH_MOVE( _value ) );
+ return *this;
+ }
+
+ Optional( Optional const& _other ):
+ nullableValue( _other ? new ( storage ) T( *_other ) : nullptr ) {}
+ Optional( Optional&& _other ):
+ nullableValue( _other ? new ( storage ) T( CATCH_MOVE( *_other ) )
+ : nullptr ) {}
- Optional& operator= ( Optional const& _other ) {
- if( &_other != this ) {
+ Optional& operator=( Optional const& _other ) {
+ if ( &_other != this ) {
reset();
- if( _other )
- nullableValue = new( storage ) T( *_other );
+ if ( _other ) { nullableValue = new ( storage ) T( *_other ); }
}
return *this;
}
- Optional& operator = ( T const& _value ) {
- reset();
- nullableValue = new( storage ) T( _value );
+ Optional& operator=( Optional&& _other ) {
+ if ( &_other != this ) {
+ reset();
+ if ( _other ) {
+ nullableValue = new ( storage ) T( CATCH_MOVE( *_other ) );
+ }
+ }
return *this;
}
void reset() {
- if( nullableValue )
- nullableValue->~T();
+ if ( nullableValue ) { nullableValue->~T(); }
nullableValue = nullptr;
}
@@ -91,7 +108,7 @@ namespace Catch {
}
private:
- T *nullableValue;
+ T* nullableValue;
alignas(alignof(T)) char storage[sizeof(T)];
};