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