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