OnixS ICE iMpact Multicast Price Feed Handler C++ library 8.18.0
Users' manual and API documentation
Loading...
Searching...
No Matches
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
27namespace OnixS { namespace ICE { namespace iMpact { namespace MarketData {
28
29template <typename T>
31{
32public:
35 : init_(false)
36 {
37 }
38
40 Optional(const T& val)
41 : init_(true)
42 {
43 construct(val);
44 }
45
52 : init_(opt.init_)
53 {
54 if (init_)
55 {
56 construct(opt.get());
57 }
58 }
59
62 {
63 destruct();
64
65 if ((init_ = opt.init_))
66 {
67 construct(opt.get());
68 }
69
70 return *this;
71 }
72
74 Optional& operator=(const T& val)
75 {
76 destruct();
77
78 init_ = true;
79
80 construct(val);
81
82 return *this;
83 }
84
87 {
88 destruct();
89 }
90
92 operator bool() const
93 {
94 return init_;
95 }
96
99 {
100 return get(); // undefined if not initialized
101 }
102
104 const T& operator*() const
105 {
106 return get(); // undefined if not initialized
107 }
108
110 T& value()
111 {
112 return **this;
113 }
114
116 const T& value() const
117 {
118 return **this;
119 }
120
122 void reset()
123 {
124 destruct();
125
126 init_ = false;
127 }
128
129protected:
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
169template <typename T>
170std::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
const T & operator*() const
Returns a const reference to the contained value.
Definition Optional.h:104
Optional & operator=(const Optional< T > &opt)
Assigns contents.
Definition Optional.h:61
T & operator*()
Returns a reference to the contained value.
Definition Optional.h:98
Optional(const T &val)
Constructs the optional object.
Definition Optional.h:40
Optional()
Uninitialized instance.
Definition Optional.h:34
Optional(const Optional< T > &opt)
Definition Optional.h:51
T & value()
A reference to the contained value.
Definition Optional.h:110
const T & value() const
A const reference to the contained value.
Definition Optional.h:116
Optional & operator=(const T &val)
Assigns contents.
Definition Optional.h:74
~Optional()
If engaged, destroys the contained value.
Definition Optional.h:86
void reset()
Reset stored value and set state to Uninitialized.
Definition Optional.h:122
ONIXS_ICEMDH_EXPORT std::ostream & operator<<(std::ostream &, const Error &)
Make it printable to formatted C++ I/O streams.