Rename llvm::Optional<T>::Reset to 'reset' as per LLVM naming conventions.
[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/Support/Compiler.h"
20 #include "llvm/Support/AlignOf.h"
21 #include <cassert>
22
23 #if LLVM_HAS_RVALUE_REFERENCES
24 #include <utility>
25 #endif
26
27 namespace llvm {
28
29 template<typename T>
30 class Optional {
31   AlignedCharArrayUnion<T> storage;
32   bool hasVal;
33 public:
34   explicit Optional() : hasVal(false) {}
35   Optional(const T &y) : hasVal(true) {
36     new (storage.buffer) T(y);
37   }
38   Optional(const Optional &O) : hasVal(O.hasVal) {
39     if (hasVal)
40       new (storage.buffer) T(*O);
41   }
42
43 #if LLVM_HAS_RVALUE_REFERENCES
44   Optional(T &&y) : hasVal(true) {
45     new (storage.buffer) T(std::forward<T>(y));
46   }
47 #endif
48
49   static inline Optional create(const T* y) {
50     return y ? Optional(*y) : Optional();
51   }
52
53   Optional &operator=(const T &y) {
54     if (hasVal)
55       **this = y;
56     else {
57       new (storage.buffer) T(y);
58       hasVal = true;
59     }
60     return *this;
61   }
62
63   Optional &operator=(const Optional &O) {
64     if (!O)
65       reset();
66     else
67       *this = *O;
68     return *this;
69   }
70
71   void reset() {
72     if (hasVal) {
73       (*this)->~T();
74       hasVal = false;
75     }
76   }
77
78   ~Optional() {
79     reset();
80   }
81   
82   const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
83   T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
84   const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
85   T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
86
87   operator bool() const { return hasVal; }
88   bool hasValue() const { return hasVal; }
89   const T* operator->() const { return getPointer(); }
90   T* operator->() { return getPointer(); }
91   const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
92   T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
93
94 #if LLVM_HAS_RVALUE_REFERENCE_THIS
95   T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
96   T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
97 #endif
98 };
99
100 template<typename T> struct simplify_type;
101
102 template <typename T>
103 struct simplify_type<const Optional<T> > {
104   typedef const T* SimpleType;
105   static SimpleType getSimplifiedValue(const Optional<T> &Val) {
106     return Val.getPointer();
107   }
108 };
109
110 template <typename T>
111 struct simplify_type<Optional<T> >
112   : public simplify_type<const Optional<T> > {};
113
114 /// \brief Poison comparison between two \c Optional objects. Clients needs to
115 /// explicitly compare the underlying values and account for empty \c Optional
116 /// objects.
117 ///
118 /// This routine will never be defined. It returns \c void to help diagnose 
119 /// errors at compile time.
120 template<typename T, typename U>
121 void operator==(const Optional<T> &X, const Optional<U> &Y);
122
123 /// \brief Poison comparison between two \c Optional objects. Clients needs to
124 /// explicitly compare the underlying values and account for empty \c Optional
125 /// objects.
126 ///
127 /// This routine will never be defined. It returns \c void to help diagnose 
128 /// errors at compile time.
129 template<typename T, typename U>
130 void operator!=(const Optional<T> &X, const Optional<U> &Y);
131
132 /// \brief Poison comparison between two \c Optional objects. Clients needs to
133 /// explicitly compare the underlying values and account for empty \c Optional
134 /// objects.
135 ///
136 /// This routine will never be defined. It returns \c void to help diagnose 
137 /// errors at compile time.
138 template<typename T, typename U>
139 void operator<(const Optional<T> &X, const Optional<U> &Y);
140
141 /// \brief Poison comparison between two \c Optional objects. Clients needs to
142 /// explicitly compare the underlying values and account for empty \c Optional
143 /// objects.
144 ///
145 /// This routine will never be defined. It returns \c void to help diagnose 
146 /// errors at compile time.
147 template<typename T, typename U>
148 void operator<=(const Optional<T> &X, const Optional<U> &Y);
149
150 /// \brief Poison comparison between two \c Optional objects. Clients needs to
151 /// explicitly compare the underlying values and account for empty \c Optional
152 /// objects.
153 ///
154 /// This routine will never be defined. It returns \c void to help diagnose 
155 /// errors at compile time.
156 template<typename T, typename U>
157 void operator>=(const Optional<T> &X, const Optional<U> &Y);
158
159 /// \brief Poison comparison between two \c Optional objects. Clients needs to
160 /// explicitly compare the underlying values and account for empty \c Optional
161 /// objects.
162 ///
163 /// This routine will never be defined. It returns \c void to help diagnose 
164 /// errors at compile time.
165 template<typename T, typename U>
166 void operator>(const Optional<T> &X, const Optional<U> &Y);
167
168 } // end llvm namespace
169
170 #endif