Make Optional<T>'s operator bool 'explicit' in C++11
[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 #endif
50
51   static inline Optional create(const T* y) {
52     return y ? Optional(*y) : Optional();
53   }
54
55   Optional &operator=(const T &y) {
56     if (hasVal)
57       **this = y;
58     else {
59       new (storage.buffer) T(y);
60       hasVal = true;
61     }
62     return *this;
63   }
64
65   Optional &operator=(const Optional &O) {
66     if (!O)
67       reset();
68     else
69       *this = *O;
70     return *this;
71   }
72
73   void reset() {
74     if (hasVal) {
75       (**this).~T();
76       hasVal = false;
77     }
78   }
79
80   ~Optional() {
81     reset();
82   }
83
84   const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
85   T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
86   const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
87   T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
88
89   LLVM_EXPLICIT operator bool() const { return hasVal; }
90   bool hasValue() const { return hasVal; }
91   const T* operator->() const { return getPointer(); }
92   T* operator->() { return getPointer(); }
93   const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
94   T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
95
96 #if LLVM_HAS_RVALUE_REFERENCE_THIS
97   T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
98   T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
99 #endif
100 };
101
102 template<typename T> struct simplify_type;
103
104 template <typename T>
105 struct simplify_type<const Optional<T> > {
106   typedef const T* SimpleType;
107   static SimpleType getSimplifiedValue(const Optional<T> &Val) {
108     return Val.getPointer();
109   }
110 };
111
112 template <typename T>
113 struct simplify_type<Optional<T> >
114   : public simplify_type<const Optional<T> > {};
115
116 /// \brief Poison comparison between two \c Optional objects. Clients needs to
117 /// explicitly compare the underlying values and account for empty \c Optional
118 /// objects.
119 ///
120 /// This routine will never be defined. It returns \c void to help diagnose
121 /// errors at compile time.
122 template<typename T, typename U>
123 void operator==(const Optional<T> &X, const Optional<U> &Y);
124
125 /// \brief Poison comparison between two \c Optional objects. Clients needs to
126 /// explicitly compare the underlying values and account for empty \c Optional
127 /// objects.
128 ///
129 /// This routine will never be defined. It returns \c void to help diagnose
130 /// errors at compile time.
131 template<typename T, typename U>
132 void operator!=(const Optional<T> &X, const Optional<U> &Y);
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 } // end llvm namespace
171
172 #endif