2012-12-05 09:40:53 +01:00
|
|
|
/*
|
|
|
|
* 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:
|
2015-07-01 08:33:27 +02:00
|
|
|
Option() : nullableValue( CATCH_NULL ) {}
|
2012-12-05 09:40:53 +01:00
|
|
|
Option( T const& _value )
|
|
|
|
: nullableValue( new( storage ) T( _value ) )
|
|
|
|
{}
|
|
|
|
Option( Option const& _other )
|
2015-07-01 08:33:27 +02:00
|
|
|
: nullableValue( _other ? new( storage ) T( *_other ) : CATCH_NULL )
|
2012-12-05 09:40:53 +01:00
|
|
|
{}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-12-05 09:40:53 +01:00
|
|
|
~Option() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
Option& operator= ( Option const& _other ) {
|
2014-03-01 11:38:14 +01:00
|
|
|
if( &_other != this ) {
|
|
|
|
reset();
|
|
|
|
if( _other )
|
|
|
|
nullableValue = new( storage ) T( *_other );
|
|
|
|
}
|
2012-12-05 09:40:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2013-07-24 20:13:08 +02:00
|
|
|
Option& operator = ( T const& _value ) {
|
|
|
|
reset();
|
|
|
|
nullableValue = new( storage ) T( _value );
|
|
|
|
return *this;
|
|
|
|
}
|
2012-12-05 09:40:53 +01:00
|
|
|
|
|
|
|
void reset() {
|
|
|
|
if( nullableValue )
|
|
|
|
nullableValue->~T();
|
2015-07-01 08:33:27 +02:00
|
|
|
nullableValue = CATCH_NULL;
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
2013-07-24 20:13:08 +02:00
|
|
|
|
2012-12-05 09:40:53 +01:00
|
|
|
T& operator*() { return *nullableValue; }
|
2013-04-23 19:58:56 +02:00
|
|
|
T const& operator*() const { return *nullableValue; }
|
2012-12-05 09:40:53 +01:00
|
|
|
T* operator->() { return nullableValue; }
|
|
|
|
const T* operator->() const { return nullableValue; }
|
|
|
|
|
2013-06-06 23:54:42 +02:00
|
|
|
T valueOr( T const& defaultValue ) const {
|
|
|
|
return nullableValue ? *nullableValue : defaultValue;
|
|
|
|
}
|
|
|
|
|
2015-07-01 08:33:27 +02:00
|
|
|
bool some() const { return nullableValue != CATCH_NULL; }
|
|
|
|
bool none() const { return nullableValue == CATCH_NULL; }
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-07-01 08:33:27 +02:00
|
|
|
bool operator !() const { return nullableValue == CATCH_NULL; }
|
2012-12-05 09:40:53 +01:00
|
|
|
operator SafeBool::type() const {
|
|
|
|
return SafeBool::makeSafe( some() );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-04-21 08:18:46 +02:00
|
|
|
T *nullableValue;
|
2017-04-20 15:38:57 +02:00
|
|
|
union {
|
2017-04-21 08:18:46 +02:00
|
|
|
char storage[sizeof(T)];
|
|
|
|
|
|
|
|
// These are here to force alignment for the storage
|
|
|
|
long double dummy1;
|
2017-04-25 09:38:25 +02:00
|
|
|
void (*dummy2)();
|
|
|
|
long double dummy3;
|
|
|
|
#ifdef CATCH_CONFIG_CPP11_LONG_LONG
|
|
|
|
long long dummy4;
|
|
|
|
#endif
|
2017-04-20 15:38:57 +02:00
|
|
|
};
|
2012-12-05 09:40:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_OPTION_HPP_INCLUDED
|