Add explicit assignment operator definitions to Optional
[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 namespace folly {
65
66 namespace detail { struct NoneHelper {}; }
67
68 typedef int detail::NoneHelper::*None;
69
70 const None none = nullptr;
71
72 /**
73  * gcc-4.7 warns about use of uninitialized memory around the use of storage_
74  * even though this is explicitly initialized at each point.
75  */
76 #if defined(__GNUC__) && !defined(__clang__)
77 # pragma GCC diagnostic push
78 # pragma GCC diagnostic ignored "-Wuninitialized"
79 # pragma GCC diagnostic ignored "-Wpragmas"
80 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
81 #endif // __GNUC__
82
83 template<class Value>
84 class Optional : boost::totally_ordered<Optional<Value>,
85                  boost::totally_ordered<Optional<Value>, Value>> {
86   typedef void (Optional::*bool_type)() const;
87   void truthy() const {};
88  public:
89   static_assert(!std::is_reference<Value>::value,
90                 "Optional may not be used with reference types");
91
92   Optional()
93     : hasValue_(false) {
94   }
95
96   Optional(const Optional& src) {
97     if (src.hasValue()) {
98       construct(src.value());
99     } else {
100       hasValue_ = false;
101     }
102   }
103
104   Optional(Optional&& src) {
105     if (src.hasValue()) {
106       construct(std::move(src.value()));
107       src.clear();
108     } else {
109       hasValue_ = false;
110     }
111   }
112
113   /* implicit */ Optional(const None& empty)
114     : hasValue_(false) {
115   }
116
117   /* implicit */ Optional(Value&& newValue) {
118     construct(std::move(newValue));
119   }
120
121   /* implicit */ Optional(const Value& newValue) {
122     construct(newValue);
123   }
124
125   ~Optional() {
126     clear();
127   }
128
129   void assign(const None&) {
130     clear();
131   }
132
133   void assign(Optional&& src) {
134     if (src.hasValue()) {
135       assign(std::move(src.value()));
136       src.clear();
137     } else {
138       clear();
139     }
140   }
141
142   void assign(const Optional& src) {
143     if (src.hasValue()) {
144       assign(src.value());
145     } else {
146       clear();
147     }
148   }
149
150   void assign(Value&& newValue) {
151     if (hasValue()) {
152       value_ = std::move(newValue);
153     } else {
154       construct(std::move(newValue));
155     }
156   }
157
158   void assign(const Value& newValue) {
159     if (hasValue()) {
160       value_ = newValue;
161     } else {
162       construct(newValue);
163     }
164   }
165
166   template<class Arg>
167   Optional& operator=(Arg&& arg) {
168     assign(std::forward<Arg>(arg));
169     return *this;
170   }
171
172   Optional& operator=(Optional &&other) {
173     assign(std::move(other));
174     return *this;
175   }
176
177   Optional& operator=(const Optional &other) {
178     assign(other);
179     return *this;
180   }
181
182   bool operator<(const Optional& other) const {
183     if (hasValue() != other.hasValue()) {
184       return hasValue() < other.hasValue();
185     }
186     if (hasValue()) {
187       return value() < other.value();
188     }
189     return false; // both empty
190   }
191
192   bool operator<(const Value& other) const {
193     return !hasValue() || value() < other;
194   }
195
196   bool operator==(const Optional& other) const {
197     if (hasValue()) {
198       return other.hasValue() && value() == other.value();
199     } else {
200       return !other.hasValue();
201     }
202   }
203
204   bool operator==(const Value& other) const {
205     return hasValue() && value() == other;
206   }
207
208   template<class... Args>
209   void emplace(Args&&... args) {
210     clear();
211     construct(std::forward<Args>(args)...);
212   }
213
214   void clear() {
215     if (hasValue()) {
216       hasValue_ = false;
217       value_.~Value();
218     }
219   }
220
221   const Value& value() const {
222     assert(hasValue());
223     return value_;
224   }
225
226   Value& value() {
227     assert(hasValue());
228     return value_;
229   }
230
231   bool hasValue() const { return hasValue_; }
232
233   /* safe bool idiom */
234   operator bool_type() const {
235     return hasValue() ? &Optional::truthy : nullptr;
236   }
237
238   const Value& operator*() const { return value(); }
239         Value& operator*()       { return value(); }
240
241   const Value* operator->() const { return &value(); }
242         Value* operator->()       { return &value(); }
243
244  private:
245   template<class... Args>
246   void construct(Args&&... args) {
247     const void* ptr = &value_;
248     // for supporting const types
249     new(const_cast<void*>(ptr)) Value(std::forward<Args>(args)...);
250     hasValue_ = true;
251   }
252
253   // uninitialized
254   union { Value value_; };
255   bool hasValue_;
256 };
257
258 #if defined(__GNUC__) && !defined(__clang__)
259 #pragma GCC diagnostic pop
260 #endif
261
262 template<class T>
263 const T* get_pointer(const Optional<T>& opt) {
264   return opt ? &opt.value() : nullptr;
265 }
266
267 template<class T>
268 T* get_pointer(Optional<T>& opt) {
269   return opt ? &opt.value() : nullptr;
270 }
271
272 template<class T>
273 void swap(Optional<T>& a, Optional<T>& b) {
274   if (a.hasValue() && b.hasValue()) {
275     // both full
276     using std::swap;
277     swap(a.value(), b.value());
278   } else if (a.hasValue() || b.hasValue()) {
279     std::swap(a, b); // fall back to default implementation if they're mixed.
280   }
281 }
282
283 template<class T,
284          class Opt = Optional<typename std::decay<T>::type>>
285 Opt make_optional(T&& v) {
286   return Opt(std::forward<T>(v));
287 }
288
289 } // namespace folly
290
291 #endif//FOLLY_OPTIONAL_H_