13 #include <type_traits>
35 template<
typename ContentType >
40 typedef ExceptionPtr ErrorType;
55 Nullable (
const ContentType& content );
57 Nullable (
const std::string& error );
61 template<
typename ExceptionType >
62 Nullable ( std::shared_ptr< ExceptionType > error );
65 std::string GetErrorReason ();
66 ErrorType GetError ();
68 bool operator== (
const ContentType & that );
69 bool operator!= (
const ContentType & that );
72 const ContentType & Get ()
const;
73 operator const ContentType & ()
const;
76 operator ContentType & ();
80 static Nullable FromError (
const ErrorType& error );
81 static Nullable FromError (
const std::string& reason );
93 typedef ExceptionPtr ErrorType;
104 Nullable (
const ErrorType& error );
105 Nullable (
const std::string& error );
107 template<
typename ExceptionType >
108 Nullable ( std::shared_ptr< ExceptionType > error );
111 std::string GetErrorReason ();
112 ErrorType GetError ();
126 template<
typename ContentType >
134 template<
typename ContentType >
136 : m_isValid( true ), Content(
std::move( content ) )
141 template<
typename ContentType >
143 : m_isValid( true ), Content( content )
148 template<
typename ContentType >
150 : m_isValid( false ), Error( error )
155 template<
typename ContentType >
157 : m_isValid( false ), Error(
std::static_pointer_cast< Exception >(
std::make_shared< RuntimeException >( error ) ) )
162 template<
typename ContentType >
164 : m_isValid( that.m_isValid )
167 new( &Content ) ContentType( that.Content );
169 new( &Error ) ErrorType( that.Error );
174 template<
typename ContentType >
175 template<
typename ExceptionType >
177 : m_isValid( false ), Error(
std::static_pointer_cast< Exception >( error ) )
179 static_assert( std::is_base_of< typename ErrorType::element_type, ExceptionType >::value,
"ExceptionType should be derived from ErrorType" );
184 template<
typename ContentType >
188 Content.~ContentType();
195 template<
typename ContentType >
203 template<
typename ContentType >
206 return Error->ErrorMessage();
211 template<
typename ContentType >
221 template<
typename ContentType >
224 return m_isValid && Content == that;
229 template<
typename ContentType >
232 return !( *
this == that );
237 template<
typename ContentType >
241 Content.~ContentType();
245 m_isValid = that.m_isValid;
248 new( &Content ) ContentType( that.Content );
250 new( &Error ) ErrorType( that.Error );
257 template<
typename ContentType >
267 template<
typename ContentType >
277 template<
typename ContentType >
285 template<
typename ContentType >
295 template<
typename ContentType >
303 template<
typename ContentType >
306 return FromError( std::make_shared< std::runtime_error >( reason ) );
309 template<
typename ContentType >
331 : m_isValid( false ), Error( error )
336 template<
typename ExceptionType >
338 : m_isValid( false ), Error(
std::static_pointer_cast< Exception >( error ) )
340 static_assert( std::is_base_of< typename ErrorType::element_type, ExceptionType >::value,
"ExceptionType should be derived from ErrorType" );
346 : m_isValid( false ), Error(
std::static_pointer_cast< Exception >(
std::make_shared< RuntimeException >( error ) ) )
352 : m_isValid( result ==
Result::Success ), Error( nullptr )
367 return Error->ErrorMessage();
Definition: Exception.h:11
Returns value or error.
Definition: Nullable.h:19
Result
Enumeration for nullable for creating valid and invalid object.
Definition: Nullable.h:25
Alexandrescu Expected type for error handling.
Definition: Nullable.h:36
Alexandrescu Expected type for error handling.
Definition: Nullable.h:89