Sleeping Wombat Common Library  0.50.0
swCommonLibrary
Nullable.h
1 #pragma once
2 
3 #include <string>
4 
5 
11 enum class NullableInit
12 { Valid };
13 
14 
15 
18 template< typename ResultType >
19 struct Nullable
20 {
21  ResultType Value;
22  bool IsValid;
23  std::string ErrorString;
24 
25 
26 
29  : IsValid( false )
30  , Value( ResultType() )
31  {}
32 
33 
35  explicit Nullable( NullableInit )
36  : IsValid( true )
37  , Value( ResultType() )
38  {}
39 
41  Nullable( std::string&& error )
42  : ErrorString( std::move( error ) )
43  , IsValid( false )
44  , Value( ResultType() )
45  {}
46 
48  Nullable( ResultType&& result )
49  : Value( std::move( result ) )
50  , IsValid( true )
51  {}
52 
53  Nullable( const Nullable< ResultType >& other )
54  : ErrorString( other.ErrorString )
55  , IsValid( other.IsValid )
56  , Value( other.Value )
57  {}
58 
60  : ErrorString( std::move( other.ErrorString ) )
61  , IsValid( other.IsValid )
62  , Value( std::move( other.Value ) )
63  {}
64 
65  void operator=( const Nullable< ResultType >& other )
66  {
67  Value = other.Value;
68  IsValid = other.IsValid;
69  ErrorString = other.ErrorString;
70  }
71 
72  void operator=( Nullable< ResultType >&& other )
73  {
74  Value = std::move( other.Value );
75  IsValid = other.IsValid;
76  ErrorString = std::move( other.ErrorString );
77  }
78 
79 
80  bool operator!()
81  {
82  return !IsValid;
83  }
84 
85 };
86 
87 #define ReturnIfInvalid( nullable ) if( !nullable.IsValid ) return std::move( nullable.ErrorString );
88 
Nullable(std::string &&error)
Creates invalid object and sets error string. Value fieled is default contructed. ...
Definition: Nullable.h:41
Returns value or error.
Definition: Nullable.h:19
Nullable(NullableInit)
Creates valid object. Value fieled is default contructed.
Definition: Nullable.h:35
Nullable()
Creates invalid object. Value fieled is default contructed.
Definition: Nullable.h:28
Nullable(ResultType &&result)
Creates valid object.
Definition: Nullable.h:48