fix Baton.h typo
[folly.git] / folly / Optional.h
1 /*
2  * Copyright 2015 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 <cassert>
58 #include <cstddef>
59 #include <type_traits>
60 #include <utility>
61
62 #include <boost/operators.hpp>
63
64 #include <folly/Portability.h>
65
66 namespace folly {
67
68 namespace detail { struct NoneHelper {}; }
69
70 typedef int detail::NoneHelper::*None;
71
72 const None none = nullptr;
73
74 /**
75  * gcc-4.7 warns about use of uninitialized memory around the use of storage_
76  * even though this is explicitly initialized at each point.
77  */
78 #if defined(__GNUC__) && !defined(__clang__)
79 # pragma GCC diagnostic push
80 # pragma GCC diagnostic ignored "-Wuninitialized"
81 # pragma GCC diagnostic ignored "-Wpragmas"
82 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
83 #endif // __GNUC__
84
85 template<class Value>
86 class Optional {
87  public:
88   static_assert(!std::is_reference<Value>::value,
89                 "Optional may not be used with reference types");
90   static_assert(!std::is_abstract<Value>::value,
91                 "Optional may not be used with abstract types");
92
93   Optional()
94     : hasValue_(false) {
95   }
96
97   Optional(const Optional& src)
98     noexcept(std::is_nothrow_copy_constructible<Value>::value) {
99
100     if (src.hasValue()) {
101       construct(src.value());
102     } else {
103       hasValue_ = false;
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     } else {
114       hasValue_ = false;
115     }
116   }
117
118   /* implicit */ Optional(const None&) noexcept
119     : hasValue_(false) {
120   }
121
122   /* implicit */ Optional(Value&& newValue)
123     noexcept(std::is_nothrow_move_constructible<Value>::value) {
124     construct(std::move(newValue));
125   }
126
127   /* implicit */ Optional(const Value& newValue)
128     noexcept(std::is_nothrow_copy_constructible<Value>::value) {
129     construct(newValue);
130   }
131
132   ~Optional() noexcept {
133     clear();
134   }
135
136   void assign(const None&) {
137     clear();
138   }
139
140   void assign(Optional&& src) {
141     if (this != &src) {
142       if (src.hasValue()) {
143         assign(std::move(src.value()));
144         src.clear();
145       } else {
146         clear();
147       }
148     }
149   }
150
151   void assign(const Optional& src) {
152     if (src.hasValue()) {
153       assign(src.value());
154     } else {
155       clear();
156     }
157   }
158
159   void assign(Value&& newValue) {
160     if (hasValue()) {
161       value_ = std::move(newValue);
162     } else {
163       construct(std::move(newValue));
164     }
165   }
166
167   void assign(const Value& newValue) {
168     if (hasValue()) {
169       value_ = newValue;
170     } else {
171       construct(newValue);
172     }
173   }
174
175   template<class Arg>
176   Optional& operator=(Arg&& arg) {
177     assign(std::forward<Arg>(arg));
178     return *this;
179   }
180
181   Optional& operator=(Optional &&other)
182     noexcept (std::is_nothrow_move_assignable<Value>::value) {
183
184     assign(std::move(other));
185     return *this;
186   }
187
188   Optional& operator=(const Optional &other)
189     noexcept (std::is_nothrow_copy_assignable<Value>::value) {
190
191     assign(other);
192     return *this;
193   }
194
195   template<class... Args>
196   void emplace(Args&&... args) {
197     clear();
198     construct(std::forward<Args>(args)...);
199   }
200
201   void clear() {
202     if (hasValue()) {
203       hasValue_ = false;
204       value_.~Value();
205     }
206   }
207
208   const Value& value() const {
209     assert(hasValue());
210     return value_;
211   }
212
213   Value& value() {
214     assert(hasValue());
215     return value_;
216   }
217
218   bool hasValue() const { return 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
227   const Value* operator->() const { return &value(); }
228         Value* operator->()       { return &value(); }
229
230   // Return a copy of the value if set, or a given default if not.
231   template <class U>
232   Value value_or(U&& dflt) const& {
233     return hasValue_ ? value_ : std::forward<U>(dflt);
234   }
235
236   template <class U>
237   Value value_or(U&& dflt) && {
238     return hasValue_ ? std::move(value_) : std::forward<U>(dflt);
239   }
240
241  private:
242   template<class... Args>
243   void construct(Args&&... args) {
244     const void* ptr = &value_;
245     // for supporting const types
246     new(const_cast<void*>(ptr)) Value(std::forward<Args>(args)...);
247     hasValue_ = true;
248   }
249
250   // uninitialized
251   union { Value value_; };
252   bool hasValue_;
253 };
254
255 #if defined(__GNUC__) && !defined(__clang__)
256 #pragma GCC diagnostic pop
257 #endif
258
259 template<class T>
260 const T* get_pointer(const Optional<T>& opt) {
261   return opt ? &opt.value() : nullptr;
262 }
263
264 template<class T>
265 T* get_pointer(Optional<T>& opt) {
266   return opt ? &opt.value() : nullptr;
267 }
268
269 template<class T>
270 void swap(Optional<T>& a, Optional<T>& b) {
271   if (a.hasValue() && b.hasValue()) {
272     // both full
273     using std::swap;
274     swap(a.value(), b.value());
275   } else if (a.hasValue() || b.hasValue()) {
276     std::swap(a, b); // fall back to default implementation if they're mixed.
277   }
278 }
279
280 template<class T,
281          class Opt = Optional<typename std::decay<T>::type>>
282 Opt make_optional(T&& v) {
283   return Opt(std::forward<T>(v));
284 }
285
286 ///////////////////////////////////////////////////////////////////////////////
287 // Comparisons.
288
289 template<class V>
290 bool operator==(const Optional<V>& a, const V& b) {
291   return a.hasValue() && a.value() == b;
292 }
293
294 template<class V>
295 bool operator!=(const Optional<V>& a, const V& b) {
296   return !(a == b);
297 }
298
299 template<class V>
300 bool operator==(const V& a, const Optional<V>& b) {
301   return b.hasValue() && b.value() == a;
302 }
303
304 template<class V>
305 bool operator!=(const V& a, const Optional<V>& b) {
306   return !(a == b);
307 }
308
309 template<class V>
310 bool operator==(const Optional<V>& a, const Optional<V>& b) {
311   if (a.hasValue() != b.hasValue()) { return false; }
312   if (a.hasValue())                 { return a.value() == b.value(); }
313   return true;
314 }
315
316 template<class V>
317 bool operator!=(const Optional<V>& a, const Optional<V>& b) {
318   return !(a == b);
319 }
320
321 template<class V>
322 bool operator< (const Optional<V>& a, const Optional<V>& b) {
323   if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
324   if (a.hasValue())                 { return a.value()    < b.value(); }
325   return false;
326 }
327
328 template<class V>
329 bool operator> (const Optional<V>& a, const Optional<V>& b) {
330   return b < a;
331 }
332
333 template<class V>
334 bool operator<=(const Optional<V>& a, const Optional<V>& b) {
335   return !(b < a);
336 }
337
338 template<class V>
339 bool operator>=(const Optional<V>& a, const Optional<V>& b) {
340   return !(a < b);
341 }
342
343 // Suppress comparability of Optional<T> with T, despite implicit conversion.
344 template<class V> bool operator< (const Optional<V>&, const V& other) = delete;
345 template<class V> bool operator<=(const Optional<V>&, const V& other) = delete;
346 template<class V> bool operator>=(const Optional<V>&, const V& other) = delete;
347 template<class V> bool operator> (const Optional<V>&, const V& other) = delete;
348 template<class V> bool operator< (const V& other, const Optional<V>&) = delete;
349 template<class V> bool operator<=(const V& other, const Optional<V>&) = delete;
350 template<class V> bool operator>=(const V& other, const Optional<V>&) = delete;
351 template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
352
353 ///////////////////////////////////////////////////////////////////////////////
354
355 } // namespace folly
356
357 #endif // FOLLY_OPTIONAL_H_