mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-04 05:09:53 +01:00
79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
/*
|
|
* Created by Phil on 02/12/2012.
|
|
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
|
|
*
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
*/
|
|
#ifndef TWOBLUECUBES_CATCH_OPTION_HPP_INCLUDED
|
|
#define TWOBLUECUBES_CATCH_OPTION_HPP_INCLUDED
|
|
|
|
#include "catch_common.h"
|
|
|
|
namespace Catch {
|
|
|
|
// An optional type
|
|
template<typename T>
|
|
class Option {
|
|
public:
|
|
Option() : nullableValue( CATCH_NULL ) {}
|
|
Option( T const& _value )
|
|
: nullableValue( new( storage ) T( _value ) )
|
|
{}
|
|
Option( Option const& _other )
|
|
: nullableValue( _other ? new( storage ) T( *_other ) : CATCH_NULL )
|
|
{}
|
|
|
|
~Option() {
|
|
reset();
|
|
}
|
|
|
|
Option& operator= ( Option const& _other ) {
|
|
if( &_other != this ) {
|
|
reset();
|
|
if( _other )
|
|
nullableValue = new( storage ) T( *_other );
|
|
}
|
|
return *this;
|
|
}
|
|
Option& operator = ( T const& _value ) {
|
|
reset();
|
|
nullableValue = new( storage ) T( _value );
|
|
return *this;
|
|
}
|
|
|
|
void reset() {
|
|
if( nullableValue )
|
|
nullableValue->~T();
|
|
nullableValue = CATCH_NULL;
|
|
}
|
|
|
|
T& operator*() { return *nullableValue; }
|
|
T const& operator*() const { return *nullableValue; }
|
|
T* operator->() { return nullableValue; }
|
|
const T* operator->() const { return nullableValue; }
|
|
|
|
T valueOr( T const& defaultValue ) const {
|
|
return nullableValue ? *nullableValue : defaultValue;
|
|
}
|
|
|
|
bool some() const { return nullableValue != CATCH_NULL; }
|
|
bool none() const { return nullableValue == CATCH_NULL; }
|
|
|
|
bool operator !() const { return nullableValue == CATCH_NULL; }
|
|
operator SafeBool::type() const {
|
|
return SafeBool::makeSafe( some() );
|
|
}
|
|
|
|
private:
|
|
union {
|
|
T *nullableValue;
|
|
long double _; // Forces alignment for the storage, following
|
|
};
|
|
char storage[sizeof(T)];
|
|
};
|
|
|
|
} // end namespace Catch
|
|
|
|
#endif // TWOBLUECUBES_CATCH_OPTION_HPP_INCLUDED
|