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