f0d21c982b3c7dbad2244062d302b05c1048c45c
[folly.git] / folly / Optional.h
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef FOLLY_OPTIONAL_H_
18 #define FOLLY_OPTIONAL_H_
19
20 /*
21  * Optional - For conditional initialization of values, like boost::optional,
22  * but with support for move semantics and emplacement.  Reference type support
23  * has not been included due to limited use cases and potential confusion with
24  * semantics of assignment: Assigning to an optional reference could quite
25  * reasonably copy its value or redirect the reference.
26  *
27  * Optional can be useful when a variable might or might not be needed:
28  *
29  *  Optional<Logger> maybeLogger = ...;
30  *  if (maybeLogger) {
31  *    maybeLogger->log("hello");
32  *  }
33  *
34  * Optional enables a 'null' value for types which do not otherwise have
35  * nullability, especially useful for parameter passing:
36  *
37  * void testIterator(const unique_ptr<Iterator>& it,
38  *                   initializer_list<int> idsExpected,
39  *                   Optional<initializer_list<int>> ranksExpected = none) {
40  *   for (int i = 0; it->next(); ++i) {
41  *     EXPECT_EQ(it->doc().id(), idsExpected[i]);
42  *     if (ranksExpected) {
43  *       EXPECT_EQ(it->doc().rank(), (*ranksExpected)[i]);
44  *     }
45  *   }
46  * }
47  *
48  * Optional models OptionalPointee, so calling 'get_pointer(opt)' will return a
49  * pointer to nullptr if the 'opt' is empty, and a pointer to the value if it is
50  * not:
51  *
52  *  Optional<int> maybeInt = ...;
53  *  if (int* v = get_pointer(maybeInt)) {
54  *    cout << *v << endl;
55  *  }
56  */
57 #include <utility>
58 #include <cassert>
59 #include <cstddef>
60 #include <type_traits>
61
62 #include <boost/operators.hpp>
63
64
65 namespace folly {
66
67 namespace detail { struct NoneHelper {}; }
68
69 typedef int detail::NoneHelper::*None;
70
71 const None none = nullptr;
72
73 /**
74  * gcc-4.7 warns about use of uninitialized memory around the use of storage_
75  * even though this is explicitly initialized at each point.
76  */
77 #if defined(__GNUC__) && !defined(__clang__)
78 # pragma GCC diagnostic push
79 # pragma GCC diagnostic ignored "-Wuninitialized"
80 # pragma GCC diagnostic ignored "-Wpragmas"
81 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
82 #endif // __GNUC__
83
84 template<class Value>
85 class Optional {
86  public:
87   static_assert(!std::is_reference<Value>::value,
88                 "Optional may not be used with reference types");
89
90   Optional()
91     : hasValue_(false) {
92   }
93
94   Optional(const Optional& src)
95     noexcept(std::is_nothrow_copy_constructible<Value>::value) {
96
97     if (src.hasValue()) {
98       construct(src.value());
99     } else {
100       hasValue_ = false;
101     }
102   }
103
104   Optional(Optional&& src)
105     noexcept(std::is_nothrow_move_constructible<Value>::value) {
106
107     if (src.hasValue()) {
108       construct(std::move(src.value()));
109       src.clear();
110     } else {
111       hasValue_ = false;
112     }
113   }
114
115   /* implicit */ Optional(const None&)
116     : hasValue_(false) {
117   }
118
119   /* implicit */ Optional(Value&& newValue) {
120     construct(std::move(newValue));
121   }
122
123   /* implicit */ Optional(const Value& newValue) {
124     construct(newValue);
125   }
126
127   ~Optional() noexcept {
128     clear();
129   }
130
131   void assign(const None&) {
132     clear();
133   }
134
135   void assign(Optional&& src) {
136     if (src.hasValue()) {
137       assign(std::move(src.value()));
138       src.clear();
139     } else {
140       clear();
141     }
142   }
143
144   void assign(const Optional& src) {
145     if (src.hasValue()) {
146       assign(src.value());
147     } else {
148       clear();
149     }
150   }
151
152   void assign(Value&& newValue) {
153     if (hasValue()) {
154       value_ = std::move(newValue);
155     } else {
156       construct(std::move(newValue));
157     }
158   }
159
160   void assign(const Value& newValue) {
161     if (hasValue()) {
162       value_ = newValue;
163     } else {
164       construct(newValue);
165     }
166   }
167
168   template<class Arg>
169   Optional& operator=(Arg&& arg) {
170     assign(std::forward<Arg>(arg));
171     return *this;
172   }
173
174   Optional& operator=(Optional &&other)
175     noexcept (std::is_nothrow_move_assignable<Value>::value) {
176
177     assign(std::move(other));
178     return *this;
179   }
180
181   Optional& operator=(const Optional &other)
182     noexcept (std::is_nothrow_copy_assignable<Value>::value) {
183
184     assign(other);
185     return *this;
186   }
187
188   template<class... Args>
189   void emplace(Args&&... args) {
190     clear();
191     construct(std::forward<Args>(args)...);
192   }
193
194   void clear() {
195     if (hasValue()) {
196       hasValue_ = false;
197       value_.~Value();
198     }
199   }
200
201   const Value& value() const {
202     assert(hasValue());
203     return value_;
204   }
205
206   Value& value() {
207     assert(hasValue());
208     return value_;
209   }
210
211   bool hasValue() const { return hasValue_; }
212
213   explicit operator bool() const {
214     return hasValue();
215   }
216
217   const Value& operator*() const { return value(); }
218         Value& operator*()       { return value(); }
219
220   const Value* operator->() const { return &value(); }
221         Value* operator->()       { return &value(); }
222
223  private:
224   template<class... Args>
225   void construct(Args&&... args) {
226     const void* ptr = &value_;
227     // for supporting const types
228     new(const_cast<void*>(ptr)) Value(std::forward<Args>(args)...);
229     hasValue_ = true;
230   }
231
232   // uninitialized
233   union { Value value_; };
234   bool hasValue_;
235 };
236
237 #if defined(__GNUC__) && !defined(__clang__)
238 #pragma GCC diagnostic pop
239 #endif
240
241 template<class T>
242 const T* get_pointer(const Optional<T>& opt) {
243   return opt ? &opt.value() : nullptr;
244 }
245
246 template<class T>
247 T* get_pointer(Optional<T>& opt) {
248   return opt ? &opt.value() : nullptr;
249 }
250
251 template<class T>
252 void swap(Optional<T>& a, Optional<T>& b) {
253   if (a.hasValue() && b.hasValue()) {
254     // both full
255     using std::swap;
256     swap(a.value(), b.value());
257   } else if (a.hasValue() || b.hasValue()) {
258     std::swap(a, b); // fall back to default implementation if they're mixed.
259   }
260 }
261
262 template<class T,
263          class Opt = Optional<typename std::decay<T>::type>>
264 Opt make_optional(T&& v) {
265   return Opt(std::forward<T>(v));
266 }
267
268 template<class V>
269 bool operator< (const Optional<V>& a, const Optional<V>& b) {
270   if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
271   if (a.hasValue())                 { return a.value()    < b.value(); }
272   return false;
273 }
274
275 template<class V>
276 bool operator==(const Optional<V>& a, const Optional<V>& b) {
277   if (a.hasValue() != b.hasValue()) { return false; }
278   if (a.hasValue())                 { return a.value() == b.value(); }
279   return true;
280 }
281
282 template<class V>
283 bool operator<=(const Optional<V>& a, const Optional<V>& b) {
284   return !(b < a);
285 }
286
287 template<class V>
288 bool operator!=(const Optional<V>& a, const Optional<V>& b) {
289   return !(b == a);
290 }
291
292 template<class V>
293 bool operator>=(const Optional<V>& a, const Optional<V>& b) {
294   return !(a < b);
295 }
296
297 template<class V>
298 bool operator> (const Optional<V>& a, const Optional<V>& b) {
299   return b < a;
300 }
301
302 // To supress comparability of Optional<T> with T, despite implicit conversion.
303 template<class V> bool operator< (const Optional<V>&, const V& other) = delete;
304 template<class V> bool operator<=(const Optional<V>&, const V& other) = delete;
305 template<class V> bool operator==(const Optional<V>&, const V& other) = delete;
306 template<class V> bool operator!=(const Optional<V>&, const V& other) = delete;
307 template<class V> bool operator>=(const Optional<V>&, const V& other) = delete;
308 template<class V> bool operator> (const Optional<V>&, const V& other) = delete;
309 template<class V> bool operator< (const V& other, const Optional<V>&) = delete;
310 template<class V> bool operator<=(const V& other, const Optional<V>&) = delete;
311 template<class V> bool operator==(const V& other, const Optional<V>&) = delete;
312 template<class V> bool operator!=(const V& other, const Optional<V>&) = delete;
313 template<class V> bool operator>=(const V& other, const Optional<V>&) = delete;
314 template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
315
316 } // namespace folly
317
318 #endif//FOLLY_OPTIONAL_H_