2 * Copyright 2013 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_OPTIONAL_H_
18 #define FOLLY_OPTIONAL_H_
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.
27 * Optional can be useful when a variable might or might not be needed:
29 * Optional<Logger> maybeLogger = ...;
31 * maybeLogger->log("hello");
34 * Optional enables a 'null' value for types which do not otherwise have
35 * nullability, especially useful for parameter passing:
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]);
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
52 * Optional<int> maybeInt = ...;
53 * if (int* v = get_pointer(maybeInt)) {
60 #include <type_traits>
62 #include <boost/operators.hpp>
66 namespace detail { struct NoneHelper {}; }
68 typedef int detail::NoneHelper::*None;
70 const None none = nullptr;
73 class Optional : boost::totally_ordered<Optional<Value>,
74 boost::totally_ordered<Optional<Value>, Value>> {
75 typedef void (Optional::*bool_type)() const;
76 void truthy() const {};
78 static_assert(!std::is_reference<Value>::value,
79 "Optional may not be used with reference types");
85 Optional(const Optional& src) {
87 construct(src.value());
93 Optional(Optional&& src) {
95 construct(std::move(src.value()));
102 /* implicit */ Optional(const None& empty)
106 /* implicit */ Optional(Value&& newValue) {
107 construct(std::move(newValue));
110 /* implicit */ Optional(const Value& newValue) {
118 void assign(const None&) {
122 void assign(Optional&& src) {
123 if (src.hasValue()) {
124 assign(std::move(src.value()));
131 void assign(const Optional& src) {
132 if (src.hasValue()) {
139 void assign(Value&& newValue) {
141 value_ = std::move(newValue);
143 construct(std::move(newValue));
147 void assign(const Value& newValue) {
156 Optional& operator=(Arg&& arg) {
157 assign(std::forward<Arg>(arg));
161 bool operator<(const Optional& other) const {
162 if (hasValue() != other.hasValue()) {
163 return hasValue() < other.hasValue();
166 return value() < other.value();
168 return false; // both empty
171 bool operator<(const Value& other) const {
172 return !hasValue() || value() < other;
175 bool operator==(const Optional& other) const {
177 return other.hasValue() && value() == other.value();
179 return !other.hasValue();
183 bool operator==(const Value& other) const {
184 return hasValue() && value() == other;
187 template<class... Args>
188 void emplace(Args&&... args) {
190 construct(std::forward<Args>(args)...);
200 const Value& value() const {
210 bool hasValue() const { return hasValue_; }
212 /* safe bool idiom */
213 operator bool_type() const {
214 return hasValue() ? &Optional::truthy : nullptr;
217 const Value& operator*() const { return value(); }
218 Value& operator*() { return value(); }
220 const Value* operator->() const { return &value(); }
221 Value* operator->() { return &value(); }
224 template<class... Args>
225 void construct(Args&&... args) {
226 const void* ptr = &value_;
227 // for supporting const types
228 new(const_cast<void*>(ptr)) Value(std::forward<Args>(args)...);
233 union { Value value_; };
238 const T* get_pointer(const Optional<T>& opt) {
239 return opt ? &opt.value() : nullptr;
243 T* get_pointer(Optional<T>& opt) {
244 return opt ? &opt.value() : nullptr;
248 void swap(Optional<T>& a, Optional<T>& b) {
249 if (a.hasValue() && b.hasValue()) {
252 swap(a.value(), b.value());
253 } else if (a.hasValue() || b.hasValue()) {
254 std::swap(a, b); // fall back to default implementation if they're mixed.
260 #endif//FOLLY_OPTIONAL_H_