144367d352d546f131739ec24b9be3ac87830cf7
[oota-llvm.git] / include / llvm / ADT / Optional.h
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 <utility>
24
25 namespace llvm {
26
27 template<typename T>
28 class Optional {
29   AlignedCharArrayUnion<T> storage;
30   bool hasVal;
31 public:
32   typedef T value_type;
33
34   Optional(NoneType) : hasVal(false) {}
35   explicit Optional() : hasVal(false) {}
36   Optional(const T &y) : hasVal(true) {
37     new (storage.buffer) T(y);
38   }
39   Optional(const Optional &O) : hasVal(O.hasVal) {
40     if (hasVal)
41       new (storage.buffer) T(*O);
42   }
43
44   Optional(T &&y) : hasVal(true) {
45     new (storage.buffer) T(std::forward<T>(y));
46   }
47   Optional(Optional<T> &&O) : hasVal(O) {
48     if (O) {
49       new (storage.buffer) T(std::move(*O));
50       O.reset();
51     }
52   }
53   Optional &operator=(T &&y) {
54     if (hasVal)
55       **this = std::move(y);
56     else {
57       new (storage.buffer) T(std::move(y));
58       hasVal = true;
59     }
60     return *this;
61   }
62   Optional &operator=(Optional &&O) {
63     if (!O)
64       reset();
65     else {
66       *this = std::move(*O);
67       O.reset();
68     }
69     return *this;
70   }
71
72   static inline Optional create(const T* y) {
73     return y ? Optional(*y) : Optional();
74   }
75
76   // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
77   // could be made more efficient by passing by value, possibly unifying them
78   // with the rvalue versions above - but this could place a different set of
79   // requirements (notably: the existence of a default ctor) when implemented
80   // in that way. Careful SFINAE to avoid such pitfalls would be required.
81   Optional &operator=(const T &y) {
82     if (hasVal)
83       **this = y;
84     else {
85       new (storage.buffer) T(y);
86       hasVal = true;
87     }
88     return *this;
89   }
90
91   Optional &operator=(const Optional &O) {
92     if (!O)
93       reset();
94     else
95       *this = *O;
96     return *this;
97   }
98
99   void reset() {
100     if (hasVal) {
101       (**this).~T();
102       hasVal = false;
103     }
104   }
105
106   ~Optional() {
107     reset();
108   }
109
110   const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
111   T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
112   const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
113   T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
114
115   LLVM_EXPLICIT operator bool() const { return hasVal; }
116   bool hasValue() const { return hasVal; }
117   const T* operator->() const { return getPointer(); }
118   T* operator->() { return getPointer(); }
119   const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
120   T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
121
122   template <typename U>
123   constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
124     return hasValue() ? getValue() : std::forward<U>(value);
125   }
126
127 #if LLVM_HAS_RVALUE_REFERENCE_THIS
128   T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
129   T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
130
131   template <typename U>
132   T getValueOr(U &&value) && {
133     return hasValue() ? std::move(getValue()) : std::forward<U>(value);
134   }
135 #endif
136 };
137
138 template <typename T> struct isPodLike;
139 template <typename T> struct isPodLike<Optional<T> > {
140   // An Optional<T> is pod-like if T is.
141   static const bool value = isPodLike<T>::value;
142 };
143
144 /// \brief Poison comparison between two \c Optional objects. Clients needs to
145 /// explicitly compare the underlying values and account for empty \c Optional
146 /// objects.
147 ///
148 /// This routine will never be defined. It returns \c void to help diagnose
149 /// errors at compile time.
150 template<typename T, typename U>
151 void operator==(const Optional<T> &X, const Optional<U> &Y);
152
153 /// \brief Poison comparison between two \c Optional objects. Clients needs to
154 /// explicitly compare the underlying values and account for empty \c Optional
155 /// objects.
156 ///
157 /// This routine will never be defined. It returns \c void to help diagnose
158 /// errors at compile time.
159 template<typename T, typename U>
160 void operator!=(const Optional<T> &X, const Optional<U> &Y);
161
162 /// \brief Poison comparison between two \c Optional objects. Clients needs to
163 /// explicitly compare the underlying values and account for empty \c Optional
164 /// objects.
165 ///
166 /// This routine will never be defined. It returns \c void to help diagnose
167 /// errors at compile time.
168 template<typename T, typename U>
169 void operator<(const Optional<T> &X, const Optional<U> &Y);
170
171 /// \brief Poison comparison between two \c Optional objects. Clients needs to
172 /// explicitly compare the underlying values and account for empty \c Optional
173 /// objects.
174 ///
175 /// This routine will never be defined. It returns \c void to help diagnose
176 /// errors at compile time.
177 template<typename T, typename U>
178 void operator<=(const Optional<T> &X, const Optional<U> &Y);
179
180 /// \brief Poison comparison between two \c Optional objects. Clients needs to
181 /// explicitly compare the underlying values and account for empty \c Optional
182 /// objects.
183 ///
184 /// This routine will never be defined. It returns \c void to help diagnose
185 /// errors at compile time.
186 template<typename T, typename U>
187 void operator>=(const Optional<T> &X, const Optional<U> &Y);
188
189 /// \brief Poison comparison between two \c Optional objects. Clients needs to
190 /// explicitly compare the underlying values and account for empty \c Optional
191 /// objects.
192 ///
193 /// This routine will never be defined. It returns \c void to help diagnose
194 /// errors at compile time.
195 template<typename T, typename U>
196 void operator>(const Optional<T> &X, const Optional<U> &Y);
197
198 } // end llvm namespace
199
200 #endif