OnixS BME SENAF Handler C++ library  2.2.1
API documentation
Optional.h
Go to the documentation of this file.
1 /*
2  * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3  *
4  * This software owned by Onix Solutions Limited [OnixS] and is protected by
5  * copyright law and international copyright treaties.
6  *
7  * Access to and use of the software is governed by the terms of the applicable
8  * ONIXS Software Services Agreement (the Agreement) and Customer end user
9  * license agreements granting a non-assignable, non-transferable and
10  * non-exclusive license to use the software for it's own data processing
11  * purposes under the terms defined in the Agreement.
12  *
13  * Except as otherwise granted within the terms of the Agreement, copying or
14  * reproduction of any part of this source code or associated reference material
15  * to any other location for further reproduction or redistribution, and any
16  * amendments to this copyright notice, are expressly prohibited.
17  *
18  * Any reproduction or redistribution for sale or hiring of the Software not in
19  * accordance with the terms of the Agreement is a violation of copyright law.
20  */
21 
22 #pragma once
23 
24 #include <OnixS/Senaf/MarketData/Export.h>
25 
26 #include <new>
27 
28 namespace OnixS { namespace Senaf { namespace MarketData {
29 
30 template <typename T>
31 class Optional
32 {
33 public:
34  /// Uninitialized instance.
36  : init_(false)
37  {
38  }
39 
40  /// Constructs the optional object.
41  Optional(const T& val)
42  : init_(true)
43  {
44  construct(val);
45  }
46 
47  /**
48  * Initializes the contained value by copying the contained value of other,
49  * but only if other is engaged. other is still in engaged state if it was
50  * in engaged state before the call.
51  */
52  Optional(const Optional<T>& opt)
53  : init_(opt.init_)
54  {
55  if (init_)
56  {
57  construct(opt.get());
58  }
59  }
60 
61  /// Assigns contents.
63  {
64  destruct();
65 
66  if ((init_ = opt.init_))
67  {
68  construct(opt.get());
69  }
70 
71  return *this;
72  }
73 
74  /// Assigns contents.
75  Optional& operator=(const T& val)
76  {
77  destruct();
78 
79  init_ = true;
80 
81  construct(val);
82 
83  return *this;
84  }
85 
86  /// If engaged, destroys the contained value.
88  {
89  destruct();
90  }
91 
92  /// Checks whether the object is in engaged state
93  operator bool() const
94  {
95  return init_;
96  }
97 
98  /// Returns a reference to the contained value.
99  T& operator*()
100  {
101  return get(); // undefined if not initialized
102  }
103 
104  /// Returns a const reference to the contained value.
105  const T& operator*() const
106  {
107  return get(); // undefined if not initialized
108  }
109 
110  /// A reference to the contained value.
111  T& value()
112  {
113  return **this;
114  }
115 
116  /// A const reference to the contained value.
117  const T& value() const
118  {
119  return **this;
120  }
121 
122  /// Reset stored value and set state to Uninitialized
123  void reset()
124  {
125  destruct();
126 
127  init_ = false;
128  }
129 
130 protected:
131  void* storage()
132  {
133  return data_;
134  }
135 
136  const void* storage() const
137  {
138  return data_;
139  }
140 
141  T& get()
142  {
143  return *static_cast<T*>(storage());
144  }
145 
146  const T& get() const
147  {
148  return *static_cast<const T*>(storage());
149  }
150 
151  void destruct()
152  {
153  if (init_)
154  {
155  get().~T();
156  }
157  init_ = false;
158  }
159 
160  void construct(const T& data)
161  {
162  new (storage()) T(data);
163  }
164 
165  char data_[sizeof(T)];
166  bool init_;
167 };
168 
169 }}} // namespace OnixS::Senaf::MarketData
const void * storage() const
Definition: Optional.h:136
Optional & operator=(const T &val)
Assigns contents.
Definition: Optional.h:75
Optional()
Uninitialized instance.
Definition: Optional.h:35
Optional(const T &val)
Constructs the optional object.
Definition: Optional.h:41
const T & value() const
A const reference to the contained value.
Definition: Optional.h:117
T & value()
A reference to the contained value.
Definition: Optional.h:111
Optional(const Optional< T > &opt)
Definition: Optional.h:52
~Optional()
If engaged, destroys the contained value.
Definition: Optional.h:87
void reset()
Reset stored value and set state to Uninitialized.
Definition: Optional.h:123
const T & operator*() const
Returns a const reference to the contained value.
Definition: Optional.h:105
Optional & operator=(const Optional< T > &opt)
Assigns contents.
Definition: Optional.h:62
void construct(const T &data)
Definition: Optional.h:160
T & operator*()
Returns a reference to the contained value.
Definition: Optional.h:99