1 //===-- Optional.h - Simple variant for passing optional values ---*- C++ -*-=// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file provides Optional, a template class modeled in the spirit of 11 // OCaml's 'opt' variant. The idea is to strongly type whether or not 12 // a value can be optional. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_ADT_OPTIONAL_H 17 #define LLVM_ADT_OPTIONAL_H 18 19 #include "llvm/ADT/None.h" 20 #include "llvm/Support/AlignOf.h" 21 #include "llvm/Support/Compiler.h" 22 #include <cassert> 23 #include <new> 24 #include <utility> 25 26 namespace llvm { 27 28 template<typename T> 29 class Optional { 30 AlignedCharArrayUnion<T> storage; 31 bool hasVal; 32 public: 33 typedef T value_type; 34 Optional(NoneType)35 Optional(NoneType) : hasVal(false) {} Optional()36 explicit Optional() : hasVal(false) {} Optional(const T & y)37 Optional(const T &y) : hasVal(true) { 38 new (storage.buffer) T(y); 39 } Optional(const Optional & O)40 Optional(const Optional &O) : hasVal(O.hasVal) { 41 if (hasVal) 42 new (storage.buffer) T(*O); 43 } 44 Optional(T && y)45 Optional(T &&y) : hasVal(true) { 46 new (storage.buffer) T(std::forward<T>(y)); 47 } Optional(Optional<T> && O)48 Optional(Optional<T> &&O) : hasVal(O) { 49 if (O) { 50 new (storage.buffer) T(std::move(*O)); 51 O.reset(); 52 } 53 } 54 Optional &operator=(T &&y) { 55 if (hasVal) 56 **this = std::move(y); 57 else { 58 new (storage.buffer) T(std::move(y)); 59 hasVal = true; 60 } 61 return *this; 62 } 63 Optional &operator=(Optional &&O) { 64 if (!O) 65 reset(); 66 else { 67 *this = std::move(*O); 68 O.reset(); 69 } 70 return *this; 71 } 72 73 #if LLVM_HAS_VARIADIC_TEMPLATES 74 75 /// Create a new object by constructing it in place with the given arguments. 76 template<typename ...ArgTypes> emplace(ArgTypes &&...Args)77 void emplace(ArgTypes &&...Args) { 78 reset(); 79 hasVal = true; 80 new (storage.buffer) T(std::forward<ArgTypes>(Args)...); 81 } 82 83 #else 84 85 /// Create a new object by default-constructing it in place. emplace()86 void emplace() { 87 reset(); 88 hasVal = true; 89 new (storage.buffer) T(); 90 } 91 92 /// Create a new object by constructing it in place with the given arguments. 93 template<typename T1> emplace(T1 && A1)94 void emplace(T1 &&A1) { 95 reset(); 96 hasVal = true; 97 new (storage.buffer) T(std::forward<T1>(A1)); 98 } 99 100 /// Create a new object by constructing it in place with the given arguments. 101 template<typename T1, typename T2> emplace(T1 && A1,T2 && A2)102 void emplace(T1 &&A1, T2 &&A2) { 103 reset(); 104 hasVal = true; 105 new (storage.buffer) T(std::forward<T1>(A1), std::forward<T2>(A2)); 106 } 107 108 /// Create a new object by constructing it in place with the given arguments. 109 template<typename T1, typename T2, typename T3> emplace(T1 && A1,T2 && A2,T3 && A3)110 void emplace(T1 &&A1, T2 &&A2, T3 &&A3) { 111 reset(); 112 hasVal = true; 113 new (storage.buffer) T(std::forward<T1>(A1), std::forward<T2>(A2), 114 std::forward<T3>(A3)); 115 } 116 117 /// Create a new object by constructing it in place with the given arguments. 118 template<typename T1, typename T2, typename T3, typename T4> emplace(T1 && A1,T2 && A2,T3 && A3,T4 && A4)119 void emplace(T1 &&A1, T2 &&A2, T3 &&A3, T4 &&A4) { 120 reset(); 121 hasVal = true; 122 new (storage.buffer) T(std::forward<T1>(A1), std::forward<T2>(A2), 123 std::forward<T3>(A3), std::forward<T4>(A4)); 124 } 125 126 #endif // LLVM_HAS_VARIADIC_TEMPLATES 127 create(const T * y)128 static inline Optional create(const T* y) { 129 return y ? Optional(*y) : Optional(); 130 } 131 132 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors) 133 // could be made more efficient by passing by value, possibly unifying them 134 // with the rvalue versions above - but this could place a different set of 135 // requirements (notably: the existence of a default ctor) when implemented 136 // in that way. Careful SFINAE to avoid such pitfalls would be required. 137 Optional &operator=(const T &y) { 138 if (hasVal) 139 **this = y; 140 else { 141 new (storage.buffer) T(y); 142 hasVal = true; 143 } 144 return *this; 145 } 146 147 Optional &operator=(const Optional &O) { 148 if (!O) 149 reset(); 150 else 151 *this = *O; 152 return *this; 153 } 154 reset()155 void reset() { 156 if (hasVal) { 157 (**this).~T(); 158 hasVal = false; 159 } 160 } 161 ~Optional()162 ~Optional() { 163 reset(); 164 } 165 getPointer()166 const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); } getPointer()167 T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); } getValue()168 const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } getValue()169 T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } 170 171 LLVM_EXPLICIT operator bool() const { return hasVal; } hasValue()172 bool hasValue() const { return hasVal; } 173 const T* operator->() const { return getPointer(); } 174 T* operator->() { return getPointer(); } 175 const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } 176 T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } 177 178 template <typename U> getValueOr(U && value)179 LLVM_CONSTEXPR T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION { 180 return hasValue() ? getValue() : std::forward<U>(value); 181 } 182 183 #if LLVM_HAS_RVALUE_REFERENCE_THIS getValue()184 T&& getValue() && { assert(hasVal); return std::move(*getPointer()); } 185 T&& operator*() && { assert(hasVal); return std::move(*getPointer()); } 186 187 template <typename U> getValueOr(U && value)188 T getValueOr(U &&value) && { 189 return hasValue() ? std::move(getValue()) : std::forward<U>(value); 190 } 191 #endif 192 }; 193 194 template <typename T> struct isPodLike; 195 template <typename T> struct isPodLike<Optional<T> > { 196 // An Optional<T> is pod-like if T is. 197 static const bool value = isPodLike<T>::value; 198 }; 199 200 /// \brief Poison comparison between two \c Optional objects. Clients needs to 201 /// explicitly compare the underlying values and account for empty \c Optional 202 /// objects. 203 /// 204 /// This routine will never be defined. It returns \c void to help diagnose 205 /// errors at compile time. 206 template<typename T, typename U> 207 void operator==(const Optional<T> &X, const Optional<U> &Y); 208 209 /// \brief Poison comparison between two \c Optional objects. Clients needs to 210 /// explicitly compare the underlying values and account for empty \c Optional 211 /// objects. 212 /// 213 /// This routine will never be defined. It returns \c void to help diagnose 214 /// errors at compile time. 215 template<typename T, typename U> 216 void operator!=(const Optional<T> &X, const Optional<U> &Y); 217 218 /// \brief Poison comparison between two \c Optional objects. Clients needs to 219 /// explicitly compare the underlying values and account for empty \c Optional 220 /// objects. 221 /// 222 /// This routine will never be defined. It returns \c void to help diagnose 223 /// errors at compile time. 224 template<typename T, typename U> 225 void operator<(const Optional<T> &X, const Optional<U> &Y); 226 227 /// \brief Poison comparison between two \c Optional objects. Clients needs to 228 /// explicitly compare the underlying values and account for empty \c Optional 229 /// objects. 230 /// 231 /// This routine will never be defined. It returns \c void to help diagnose 232 /// errors at compile time. 233 template<typename T, typename U> 234 void operator<=(const Optional<T> &X, const Optional<U> &Y); 235 236 /// \brief Poison comparison between two \c Optional objects. Clients needs to 237 /// explicitly compare the underlying values and account for empty \c Optional 238 /// objects. 239 /// 240 /// This routine will never be defined. It returns \c void to help diagnose 241 /// errors at compile time. 242 template<typename T, typename U> 243 void operator>=(const Optional<T> &X, const Optional<U> &Y); 244 245 /// \brief Poison comparison between two \c Optional objects. Clients needs to 246 /// explicitly compare the underlying values and account for empty \c Optional 247 /// objects. 248 /// 249 /// This routine will never be defined. It returns \c void to help diagnose 250 /// errors at compile time. 251 template<typename T, typename U> 252 void operator>(const Optional<T> &X, const Optional<U> &Y); 253 254 } // end llvm namespace 255 256 #endif 257