Add support for getting the current thread's name
[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 { struct NoneHelper {}; }
68
69 typedef int detail::NoneHelper::*None;
70
71 const None none = nullptr;
72
73 class OptionalEmptyException : public std::runtime_error {
74  public:
75   OptionalEmptyException()
76       : std::runtime_error("Empty Optional cannot be unwrapped") {}
77 };
78
79 template<class Value>
80 class Optional {
81  public:
82   typedef Value value_type;
83
84   static_assert(!std::is_reference<Value>::value,
85                 "Optional may not be used with reference types");
86   static_assert(!std::is_abstract<Value>::value,
87                 "Optional may not be used with abstract types");
88
89   Optional() noexcept {
90   }
91
92   Optional(const Optional& src)
93     noexcept(std::is_nothrow_copy_constructible<Value>::value) {
94
95     if (src.hasValue()) {
96       construct(src.value());
97     }
98   }
99
100   Optional(Optional&& src)
101     noexcept(std::is_nothrow_move_constructible<Value>::value) {
102
103     if (src.hasValue()) {
104       construct(std::move(src.value()));
105       src.clear();
106     }
107   }
108
109   /* implicit */ Optional(const None&) noexcept {
110   }
111
112   /* implicit */ Optional(Value&& newValue)
113     noexcept(std::is_nothrow_move_constructible<Value>::value) {
114     construct(std::move(newValue));
115   }
116
117   /* implicit */ Optional(const Value& newValue)
118     noexcept(std::is_nothrow_copy_constructible<Value>::value) {
119     construct(newValue);
120   }
121
122   void assign(const None&) {
123     clear();
124   }
125
126   void assign(Optional&& src) {
127     if (this != &src) {
128       if (src.hasValue()) {
129         assign(std::move(src.value()));
130         src.clear();
131       } else {
132         clear();
133       }
134     }
135   }
136
137   void assign(const Optional& src) {
138     if (src.hasValue()) {
139       assign(src.value());
140     } else {
141       clear();
142     }
143   }
144
145   void assign(Value&& newValue) {
146     if (hasValue()) {
147       storage_.value = std::move(newValue);
148     } else {
149       construct(std::move(newValue));
150     }
151   }
152
153   void assign(const Value& newValue) {
154     if (hasValue()) {
155       storage_.value = newValue;
156     } else {
157       construct(newValue);
158     }
159   }
160
161   template<class Arg>
162   Optional& operator=(Arg&& arg) {
163     assign(std::forward<Arg>(arg));
164     return *this;
165   }
166
167   Optional& operator=(Optional &&other)
168     noexcept (std::is_nothrow_move_assignable<Value>::value) {
169
170     assign(std::move(other));
171     return *this;
172   }
173
174   Optional& operator=(const Optional &other)
175     noexcept (std::is_nothrow_copy_assignable<Value>::value) {
176
177     assign(other);
178     return *this;
179   }
180
181   template<class... Args>
182   void emplace(Args&&... args) {
183     clear();
184     construct(std::forward<Args>(args)...);
185   }
186
187   void clear() {
188     storage_.clear();
189   }
190
191   const Value& value() const& {
192     require_value();
193     return storage_.value;
194   }
195
196   Value& value() & {
197     require_value();
198     return storage_.value;
199   }
200
201   Value&& value() && {
202     require_value();
203     return std::move(storage_.value);
204   }
205
206   const Value&& value() const&& {
207     require_value();
208     return std::move(storage_.value);
209   }
210
211   const Value* get_pointer() const&  {
212     return storage_.hasValue ? &storage_.value : nullptr;
213   }
214   Value* get_pointer() & {
215     return storage_.hasValue ? &storage_.value : nullptr;
216   }
217   Value* get_pointer() && = delete;
218
219   bool hasValue() const { return storage_.hasValue; }
220
221   explicit operator bool() const {
222     return hasValue();
223   }
224
225   const Value& operator*()  const&  { return value(); }
226         Value& operator*()       &  { return value(); }
227   const Value&& operator*() const&& { return std::move(value()); }
228         Value&& operator*()      && { return std::move(value()); }
229
230   const Value* operator->() const { return &value(); }
231         Value* operator->()       { return &value(); }
232
233   // Return a copy of the value if set, or a given default if not.
234   template <class U>
235   Value value_or(U&& dflt) const& {
236     if (storage_.hasValue) {
237       return storage_.value;
238     }
239
240     return std::forward<U>(dflt);
241   }
242
243   template <class U>
244   Value value_or(U&& dflt) && {
245     if (storage_.hasValue) {
246       return std::move(storage_.value);
247     }
248
249     return std::forward<U>(dflt);
250   }
251
252  private:
253   void require_value() const {
254     if (!storage_.hasValue) {
255       throw OptionalEmptyException();
256     }
257   }
258
259   template<class... Args>
260   void construct(Args&&... args) {
261     const void* ptr = &storage_.value;
262     // for supporting const types
263     new(const_cast<void*>(ptr)) Value(std::forward<Args>(args)...);
264     storage_.hasValue = true;
265   }
266
267   struct StorageTriviallyDestructible {
268     // The union trick allows to initialize the Optional's memory,
269     // so that compiler/tools don't complain about uninitialized memory,
270     // without actually calling Value's default constructor.
271     // The rest of the implementation enforces that hasValue/value are
272     // synchronized.
273     union {
274       bool hasValue;
275       struct {
276         bool paddingForHasValue_[1];
277         Value value;
278       };
279     };
280
281     StorageTriviallyDestructible() : hasValue{false} {}
282
283     void clear() {
284       hasValue = false;
285     }
286   };
287
288   struct StorageNonTriviallyDestructible {
289     // See StorageTriviallyDestructible's union
290     union {
291       bool hasValue;
292       struct {
293         bool paddingForHasValue_[1];
294         Value value;
295       };
296     };
297
298     FOLLY_PUSH_WARNING
299     // These are both informational warnings, but they trigger rare enough
300     // that we've left them enabled.
301     FOLLY_MSVC_DISABLE_WARNING(4587) // constructor of .value is not called
302     FOLLY_MSVC_DISABLE_WARNING(4588) // destructor of .value is not called
303     StorageNonTriviallyDestructible() : hasValue{false} {}
304     ~StorageNonTriviallyDestructible() {
305       clear();
306     }
307     FOLLY_POP_WARNING
308
309     void clear() {
310       if (hasValue) {
311         hasValue = false;
312         value.~Value();
313       }
314     }
315   };
316
317   using Storage =
318     typename std::conditional<std::is_trivially_destructible<Value>::value,
319                               StorageTriviallyDestructible,
320                               StorageNonTriviallyDestructible>::type;
321
322   Storage storage_;
323 };
324
325 template<class T>
326 const T* get_pointer(const Optional<T>& opt) {
327   return opt.get_pointer();
328 }
329
330 template<class T>
331 T* get_pointer(Optional<T>& opt) {
332   return opt.get_pointer();
333 }
334
335 template<class T>
336 void swap(Optional<T>& a, Optional<T>& b) {
337   if (a.hasValue() && b.hasValue()) {
338     // both full
339     using std::swap;
340     swap(a.value(), b.value());
341   } else if (a.hasValue() || b.hasValue()) {
342     std::swap(a, b); // fall back to default implementation if they're mixed.
343   }
344 }
345
346 template<class T,
347          class Opt = Optional<typename std::decay<T>::type>>
348 Opt make_optional(T&& v) {
349   return Opt(std::forward<T>(v));
350 }
351
352 ///////////////////////////////////////////////////////////////////////////////
353 // Comparisons.
354
355 template<class V>
356 bool operator==(const Optional<V>& a, const V& b) {
357   return a.hasValue() && a.value() == b;
358 }
359
360 template<class V>
361 bool operator!=(const Optional<V>& a, const V& b) {
362   return !(a == b);
363 }
364
365 template<class V>
366 bool operator==(const V& a, const Optional<V>& b) {
367   return b.hasValue() && b.value() == a;
368 }
369
370 template<class V>
371 bool operator!=(const V& a, const Optional<V>& b) {
372   return !(a == b);
373 }
374
375 template<class V>
376 bool operator==(const Optional<V>& a, const Optional<V>& b) {
377   if (a.hasValue() != b.hasValue()) { return false; }
378   if (a.hasValue())                 { return a.value() == b.value(); }
379   return true;
380 }
381
382 template<class V>
383 bool operator!=(const Optional<V>& a, const Optional<V>& b) {
384   return !(a == b);
385 }
386
387 template<class V>
388 bool operator< (const Optional<V>& a, const Optional<V>& b) {
389   if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
390   if (a.hasValue())                 { return a.value()    < b.value(); }
391   return false;
392 }
393
394 template<class V>
395 bool operator> (const Optional<V>& a, const Optional<V>& b) {
396   return b < a;
397 }
398
399 template<class V>
400 bool operator<=(const Optional<V>& a, const Optional<V>& b) {
401   return !(b < a);
402 }
403
404 template<class V>
405 bool operator>=(const Optional<V>& a, const Optional<V>& b) {
406   return !(a < b);
407 }
408
409 // Suppress comparability of Optional<T> with T, despite implicit conversion.
410 template<class V> bool operator< (const Optional<V>&, const V& other) = delete;
411 template<class V> bool operator<=(const Optional<V>&, const V& other) = delete;
412 template<class V> bool operator>=(const Optional<V>&, const V& other) = delete;
413 template<class V> bool operator> (const Optional<V>&, const V& other) = delete;
414 template<class V> bool operator< (const V& other, const Optional<V>&) = delete;
415 template<class V> bool operator<=(const V& other, const Optional<V>&) = delete;
416 template<class V> bool operator>=(const V& other, const Optional<V>&) = delete;
417 template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
418
419 ///////////////////////////////////////////////////////////////////////////////
420
421 } // namespace folly
422
423 // Allow usage of Optional<T> in std::unordered_map and std::unordered_set
424 FOLLY_NAMESPACE_STD_BEGIN
425 template <class T>
426 struct hash<folly::Optional<T>> {
427   size_t operator()(folly::Optional<T> const& obj) const {
428     if (!obj.hasValue()) {
429       return 0;
430     }
431     return hash<typename remove_const<T>::type>()(*obj);
432   }
433 };
434 FOLLY_NAMESPACE_STD_END