OnixS BME SENAF Handler C++ library  2.2.0
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 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 <OnixS/Senaf/MarketData/Export.h>
23 
24 #include <new>
25 
26 namespace OnixS { namespace Senaf { namespace MarketData {
27 
28 template <typename T>
29 class Optional
30 {
31 public:
32  /// Uninitialized instance.
33  Optional() : init_(false) {}
34 
35  /// Constructs the optional object.
36  Optional(const T& val) : init_(true)
37  {
38  construct(val);
39  }
40 
41  /**
42  * Initializes the contained value by copying the contained value of other,
43  * but only if other is engaged. other is still in engaged state if it was
44  * in engaged state before the call.
45  */
46  Optional(const Optional<T>& opt)
47  : init_(opt.init_)
48  {
49  if (init_)
50  construct(opt.get());
51  }
52 
53  /// Assigns contents.
55  {
56  destruct();
57 
58  if ((init_ = opt.init_))
59  construct(opt.get());
60 
61  return *this;
62  }
63 
64  /// Assigns contents.
65  Optional& operator=(const T& val)
66  {
67  destruct();
68 
69  init_ = true;
70 
71  construct(val);
72 
73  return *this;
74  }
75 
76  /// If engaged, destroys the contained value.
78  {
79  destruct();
80  }
81 
82  /// Checks whether the object is in engaged state
83  operator bool() const
84  {
85  return init_;
86  }
87 
88  /// Returns a reference to the contained value.
89  T& operator*()
90  {
91  return get(); // undefined if not initialized
92  }
93 
94  /// Returns a const reference to the contained value.
95  const T& operator*() const
96  {
97  return get(); // undefined if not initialized
98  }
99 
100  /// A reference to the contained value.
101  T& value()
102  {
103  return **this;
104  }
105 
106  /// A const reference to the contained value.
107  const T& value() const
108  {
109  return **this;
110  }
111 
112  /// Reset stored value and set state to Uninitialized
113  void reset()
114  {
115  destruct();
116 
117  init_ = false;
118  }
119 
120 protected:
121  void* storage()
122  {
123  return data_;
124  }
125 
126  const void* storage() const
127  {
128  return data_;
129  }
130 
131  T& get()
132  {
133  return *static_cast<T*>(storage());
134  }
135 
136  const T& get() const
137  {
138  return *static_cast<const T*>(storage());
139  };
140 
141  void destruct()
142  {
143  if (init_) get().~T();
144  init_ = false;
145  }
146 
147  void construct(const T& data)
148  {
149  new (storage()) T(data);
150  }
151 
152  char data_[sizeof(T)];
153  bool init_;
154 };
155 
156 }}} // namespace MarketData, Senaf, OnixS
const void * storage() const
Definition: Optional.h:126
Optional & operator=(const T &val)
Assigns contents.
Definition: Optional.h:65
Optional()
Uninitialized instance.
Definition: Optional.h:33
Optional(const T &val)
Constructs the optional object.
Definition: Optional.h:36
const T & value() const
A const reference to the contained value.
Definition: Optional.h:107
T & value()
A reference to the contained value.
Definition: Optional.h:101
Optional(const Optional< T > &opt)
Definition: Optional.h:46
~Optional()
If engaged, destroys the contained value.
Definition: Optional.h:77
void reset()
Reset stored value and set state to Uninitialized.
Definition: Optional.h:113
const T & operator*() const
Returns a const reference to the contained value.
Definition: Optional.h:95
Optional & operator=(const Optional< T > &opt)
Assigns contents.
Definition: Optional.h:54
void construct(const T &data)
Definition: Optional.h:147
T & operator*()
Returns a reference to the contained value.
Definition: Optional.h:89