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