Add "typedef T value_type;" to llvm::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/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 #if LLVM_HAS_RVALUE_REFERENCE_THIS
123   T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
124   T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
125 #endif
126 };
127
128 template <typename T> struct isPodLike;
129 template <typename T> struct isPodLike<Optional<T> > {
130   // An Optional<T> is pod-like if T is.
131   static const bool value = isPodLike<T>::value;
132 };
133
134 /// \brief Poison comparison between two \c Optional objects. Clients needs to
135 /// explicitly compare the underlying values and account for empty \c Optional
136 /// objects.
137 ///
138 /// This routine will never be defined. It returns \c void to help diagnose
139 /// errors at compile time.
140 template<typename T, typename U>
141 void operator==(const Optional<T> &X, const Optional<U> &Y);
142
143 /// \brief Poison comparison between two \c Optional objects. Clients needs to
144 /// explicitly compare the underlying values and account for empty \c Optional
145 /// objects.
146 ///
147 /// This routine will never be defined. It returns \c void to help diagnose
148 /// errors at compile time.
149 template<typename T, typename U>
150 void operator!=(const Optional<T> &X, const Optional<U> &Y);
151
152 /// \brief Poison comparison between two \c Optional objects. Clients needs to
153 /// explicitly compare the underlying values and account for empty \c Optional
154 /// objects.
155 ///
156 /// This routine will never be defined. It returns \c void to help diagnose
157 /// errors at compile time.
158 template<typename T, typename U>
159 void operator<(const Optional<T> &X, const Optional<U> &Y);
160
161 /// \brief Poison comparison between two \c Optional objects. Clients needs to
162 /// explicitly compare the underlying values and account for empty \c Optional
163 /// objects.
164 ///
165 /// This routine will never be defined. It returns \c void to help diagnose
166 /// errors at compile time.
167 template<typename T, typename U>
168 void operator<=(const Optional<T> &X, const Optional<U> &Y);
169
170 /// \brief Poison comparison between two \c Optional objects. Clients needs to
171 /// explicitly compare the underlying values and account for empty \c Optional
172 /// objects.
173 ///
174 /// This routine will never be defined. It returns \c void to help diagnose
175 /// errors at compile time.
176 template<typename T, typename U>
177 void operator>=(const Optional<T> &X, const Optional<U> &Y);
178
179 /// \brief Poison comparison between two \c Optional objects. Clients needs to
180 /// explicitly compare the underlying values and account for empty \c Optional
181 /// objects.
182 ///
183 /// This routine will never be defined. It returns \c void to help diagnose
184 /// errors at compile time.
185 template<typename T, typename U>
186 void operator>(const Optional<T> &X, const Optional<U> &Y);
187
188 } // end llvm namespace
189
190 #endif