OnixS BME SENAF Handler C++ library 2.3.0
API documentation
Loading...
Searching...
No Matches
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
28namespace OnixS { namespace Senaf { namespace MarketData {
29
30template <typename T>
32{
33public:
36 : init_(false)
37 {
38 }
39
41 Optional(const T& val)
42 : init_(true)
43 {
44 construct(val);
45 }
46
53 : init_(opt.init_)
54 {
55 if (init_)
56 {
57 construct(opt.get());
58 }
59 }
60
63 {
64 destruct();
65
66 if ((init_ = opt.init_))
67 {
68 construct(opt.get());
69 }
70
71 return *this;
72 }
73
75 Optional& operator=(const T& val)
76 {
77 destruct();
78
79 init_ = true;
80
81 construct(val);
82
83 return *this;
84 }
85
88 {
89 destruct();
90 }
91
93 operator bool() const
94 {
95 return init_;
96 }
97
100 {
101 return get(); // undefined if not initialized
102 }
103
105 const T& operator*() const
106 {
107 return get(); // undefined if not initialized
108 }
109
111 T& value()
112 {
113 return **this;
114 }
115
117 const T& value() const
118 {
119 return **this;
120 }
121
123 void reset()
124 {
125 destruct();
126
127 init_ = false;
128 }
129
130protected:
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 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
T & operator*()
Returns a reference to the contained value.
Definition Optional.h:99
Optional(const T &val)
Constructs the optional object.
Definition Optional.h:41
const void * storage() const
Definition Optional.h:136
Optional()
Uninitialized instance.
Definition Optional.h:35
Optional(const Optional< T > &opt)
Definition Optional.h:52
void construct(const T &data)
Definition Optional.h:160
T & value()
A reference to the contained value.
Definition Optional.h:111
const T & value() const
A const reference to the contained value.
Definition Optional.h:117
Optional & operator=(const T &val)
Assigns contents.
Definition Optional.h:75
~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