6b49f1ab3b3a7507e3b16ad352e95f75b154e729
[folly.git] / folly / Optional.h
1 /*
2  * Copyright 2017 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 #pragma once
18
19 /*
20  * Optional - For conditional initialization of values, like boost::optional,
21  * but with support for move semantics and emplacement.  Reference type support
22  * has not been included due to limited use cases and potential confusion with
23  * semantics of assignment: Assigning to an optional reference could quite
24  * reasonably copy its value or redirect the reference.
25  *
26  * Optional can be useful when a variable might or might not be needed:
27  *
28  *  Optional<Logger> maybeLogger = ...;
29  *  if (maybeLogger) {
30  *    maybeLogger->log("hello");
31  *  }
32  *
33  * Optional enables a 'null' value for types which do not otherwise have
34  * nullability, especially useful for parameter passing:
35  *
36  * void testIterator(const unique_ptr<Iterator>& it,
37  *                   initializer_list<int> idsExpected,
38  *                   Optional<initializer_list<int>> ranksExpected = none) {
39  *   for (int i = 0; it->next(); ++i) {
40  *     EXPECT_EQ(it->doc().id(), idsExpected[i]);
41  *     if (ranksExpected) {
42  *       EXPECT_EQ(it->doc().rank(), (*ranksExpected)[i]);
43  *     }
44  *   }
45  * }
46  *
47  * Optional models OptionalPointee, so calling 'get_pointer(opt)' will return a
48  * pointer to nullptr if the 'opt' is empty, and a pointer to the value if it is
49  * not:
50  *
51  *  Optional<int> maybeInt = ...;
52  *  if (int* v = get_pointer(maybeInt)) {
53  *    cout << *v << endl;
54  *  }
55  */
56 #include <cstddef>
57 #include <functional>
58 #include <new>
59 #include <stdexcept>
60 #include <type_traits>
61 #include <utility>
62
63 #include <folly/Portability.h>
64
65 namespace folly {
66
67 namespace detail {
68 struct NoneHelper {};
69
70 // Allow each translation unit to control its own -fexceptions setting.
71 // If exceptions are disabled, std::terminate() will be called instead of
72 // throwing OptionalEmptyException when the condition fails.
73 [[noreturn]] void throw_optional_empty_exception();
74 }
75
76 typedef int detail::NoneHelper::*None;
77
78 const None none = nullptr;
79
80 class OptionalEmptyException : public std::runtime_error {
81  public:
82   OptionalEmptyException()
83       : std::runtime_error("Empty Optional cannot be unwrapped") {}
84 };
85
86 template<class Value>
87 class Optional {
88  public:
89   typedef Value value_type;
90
91   static_assert(!std::is_reference<Value>::value,
92                 "Optional may not be used with reference types");
93   static_assert(!std::is_abstract<Value>::value,
94                 "Optional may not be used with abstract types");
95
96   Optional() noexcept {
97   }
98
99   Optional(const Optional& src)
100     noexcept(std::is_nothrow_copy_constructible<Value>::value) {
101
102     if (src.hasValue()) {
103       construct(src.value());
104     }
105   }
106
107   Optional(Optional&& src)
108     noexcept(std::is_nothrow_move_constructible<Value>::value) {
109
110     if (src.hasValue()) {
111       construct(std::move(src.value()));
112       src.clear();
113     }
114   }
115
116   /* implicit */ Optional(const None&) noexcept {
117   }
118
119   /* implicit */ Optional(Value&& newValue)
120     noexcept(std::is_nothrow_move_constructible<Value>::value) {
121     construct(std::move(newValue));
122   }
123
124   /* implicit */ Optional(const Value& newValue)
125     noexcept(std::is_nothrow_copy_constructible<Value>::value) {
126     construct(newValue);
127   }
128
129   void assign(const None&) {
130     clear();
131   }
132
133   void assign(Optional&& src) {
134     if (this != &src) {
135       if (src.hasValue()) {
136         assign(std::move(src.value()));
137         src.clear();
138       } else {
139         clear();
140       }
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       storage_.value = std::move(newValue);
155     } else {
156       construct(std::move(newValue));
157     }
158   }
159
160   void assign(const Value& newValue) {
161     if (hasValue()) {
162       storage_.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     storage_.clear();
196   }
197
198   const Value& value() const& {
199     require_value();
200     return storage_.value;
201   }
202
203   Value& value() & {
204     require_value();
205     return storage_.value;
206   }
207
208   Value&& value() && {
209     require_value();
210     return std::move(storage_.value);
211   }
212
213   const Value&& value() const&& {
214     require_value();
215     return std::move(storage_.value);
216   }
217
218   const Value* get_pointer() const&  {
219     return storage_.hasValue ? &storage_.value : nullptr;
220   }
221   Value* get_pointer() & {
222     return storage_.hasValue ? &storage_.value : nullptr;
223   }
224   Value* get_pointer() && = delete;
225
226   bool hasValue() const { return storage_.hasValue; }
227
228   explicit operator bool() const {
229     return hasValue();
230   }
231
232   const Value& operator*()  const&  { return value(); }
233         Value& operator*()       &  { return value(); }
234   const Value&& operator*() const&& { return std::move(value()); }
235         Value&& operator*()      && { return std::move(value()); }
236
237   const Value* operator->() const { return &value(); }
238         Value* operator->()       { return &value(); }
239
240   // Return a copy of the value if set, or a given default if not.
241   template <class U>
242   Value value_or(U&& dflt) const& {
243     if (storage_.hasValue) {
244       return storage_.value;
245     }
246
247     return std::forward<U>(dflt);
248   }
249
250   template <class U>
251   Value value_or(U&& dflt) && {
252     if (storage_.hasValue) {
253       return std::move(storage_.value);
254     }
255
256     return std::forward<U>(dflt);
257   }
258
259  private:
260   void require_value() const {
261     if (!storage_.hasValue) {
262       detail::throw_optional_empty_exception();
263     }
264   }
265
266   template<class... Args>
267   void construct(Args&&... args) {
268     const void* ptr = &storage_.value;
269     // for supporting const types
270     new(const_cast<void*>(ptr)) Value(std::forward<Args>(args)...);
271     storage_.hasValue = true;
272   }
273
274   struct StorageTriviallyDestructible {
275     // The union trick allows to initialize the Optional's memory,
276     // so that compiler/tools don't complain about uninitialized memory,
277     // without actually calling Value's default constructor.
278     // The rest of the implementation enforces that hasValue/value are
279     // synchronized.
280     union {
281       bool hasValue;
282       struct {
283         bool paddingForHasValue_[1];
284         Value value;
285       };
286     };
287
288     StorageTriviallyDestructible() : hasValue{false} {}
289
290     void clear() {
291       hasValue = false;
292     }
293   };
294
295   struct StorageNonTriviallyDestructible {
296     // See StorageTriviallyDestructible's union
297     union {
298       bool hasValue;
299       struct {
300         bool paddingForHasValue_[1];
301         Value value;
302       };
303     };
304
305     FOLLY_PUSH_WARNING
306     // These are both informational warnings, but they trigger rare enough
307     // that we've left them enabled.
308     FOLLY_MSVC_DISABLE_WARNING(4587) // constructor of .value is not called
309     FOLLY_MSVC_DISABLE_WARNING(4588) // destructor of .value is not called
310     StorageNonTriviallyDestructible() : hasValue{false} {}
311     ~StorageNonTriviallyDestructible() {
312       clear();
313     }
314     FOLLY_POP_WARNING
315
316     void clear() {
317       if (hasValue) {
318         hasValue = false;
319         value.~Value();
320       }
321     }
322   };
323
324   using Storage =
325     typename std::conditional<std::is_trivially_destructible<Value>::value,
326                               StorageTriviallyDestructible,
327                               StorageNonTriviallyDestructible>::type;
328
329   Storage storage_;
330 };
331
332 template<class T>
333 const T* get_pointer(const Optional<T>& opt) {
334   return opt.get_pointer();
335 }
336
337 template<class T>
338 T* get_pointer(Optional<T>& opt) {
339   return opt.get_pointer();
340 }
341
342 template<class T>
343 void swap(Optional<T>& a, Optional<T>& b) {
344   if (a.hasValue() && b.hasValue()) {
345     // both full
346     using std::swap;
347     swap(a.value(), b.value());
348   } else if (a.hasValue() || b.hasValue()) {
349     std::swap(a, b); // fall back to default implementation if they're mixed.
350   }
351 }
352
353 template<class T,
354          class Opt = Optional<typename std::decay<T>::type>>
355 Opt make_optional(T&& v) {
356   return Opt(std::forward<T>(v));
357 }
358
359 ///////////////////////////////////////////////////////////////////////////////
360 // Comparisons.
361
362 template<class V>
363 bool operator==(const Optional<V>& a, const V& b) {
364   return a.hasValue() && a.value() == b;
365 }
366
367 template<class V>
368 bool operator!=(const Optional<V>& a, const V& b) {
369   return !(a == b);
370 }
371
372 template<class V>
373 bool operator==(const V& a, const Optional<V>& b) {
374   return b.hasValue() && b.value() == a;
375 }
376
377 template<class V>
378 bool operator!=(const V& a, const Optional<V>& b) {
379   return !(a == b);
380 }
381
382 template<class V>
383 bool operator==(const Optional<V>& a, const Optional<V>& b) {
384   if (a.hasValue() != b.hasValue()) { return false; }
385   if (a.hasValue())                 { return a.value() == b.value(); }
386   return true;
387 }
388
389 template<class V>
390 bool operator!=(const Optional<V>& a, const Optional<V>& b) {
391   return !(a == b);
392 }
393
394 template<class V>
395 bool operator< (const Optional<V>& a, const Optional<V>& b) {
396   if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
397   if (a.hasValue())                 { return a.value()    < b.value(); }
398   return false;
399 }
400
401 template<class V>
402 bool operator> (const Optional<V>& a, const Optional<V>& b) {
403   return b < a;
404 }
405
406 template<class V>
407 bool operator<=(const Optional<V>& a, const Optional<V>& b) {
408   return !(b < a);
409 }
410
411 template<class V>
412 bool operator>=(const Optional<V>& a, const Optional<V>& b) {
413   return !(a < b);
414 }
415
416 // Suppress comparability of Optional<T> with T, despite implicit conversion.
417 template<class V> bool operator< (const Optional<V>&, const V& other) = delete;
418 template<class V> bool operator<=(const Optional<V>&, const V& other) = delete;
419 template<class V> bool operator>=(const Optional<V>&, const V& other) = delete;
420 template<class V> bool operator> (const Optional<V>&, const V& other) = delete;
421 template<class V> bool operator< (const V& other, const Optional<V>&) = delete;
422 template<class V> bool operator<=(const V& other, const Optional<V>&) = delete;
423 template<class V> bool operator>=(const V& other, const Optional<V>&) = delete;
424 template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
425
426 ///////////////////////////////////////////////////////////////////////////////
427
428 } // namespace folly
429
430 // Allow usage of Optional<T> in std::unordered_map and std::unordered_set
431 FOLLY_NAMESPACE_STD_BEGIN
432 template <class T>
433 struct hash<folly::Optional<T>> {
434   size_t operator()(folly::Optional<T> const& obj) const {
435     if (!obj.hasValue()) {
436       return 0;
437     }
438     return hash<typename remove_const<T>::type>()(*obj);
439   }
440 };
441 FOLLY_NAMESPACE_STD_END