1 //===-- Optional.h - Simple variant for passing optional values ---*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ADT_OPTIONAL_H
17 #define LLVM_ADT_OPTIONAL_H
19 #include "llvm/ADT/None.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/AlignOf.h"
24 #if LLVM_HAS_RVALUE_REFERENCES
32 AlignedCharArrayUnion<T> storage;
35 Optional(NoneType) : hasVal(false) {}
36 explicit Optional() : hasVal(false) {}
37 Optional(const T &y) : hasVal(true) {
38 new (storage.buffer) T(y);
40 Optional(const Optional &O) : hasVal(O.hasVal) {
42 new (storage.buffer) T(*O);
45 #if LLVM_HAS_RVALUE_REFERENCES
46 Optional(T &&y) : hasVal(true) {
47 new (storage.buffer) T(std::forward<T>(y));
51 static inline Optional create(const T* y) {
52 return y ? Optional(*y) : Optional();
55 Optional &operator=(const T &y) {
59 new (storage.buffer) T(y);
65 Optional &operator=(const Optional &O) {
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(); }
89 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(); }
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()); }
102 template<typename T> struct simplify_type;
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();
112 template <typename T>
113 struct simplify_type<Optional<T> >
114 : public simplify_type<const Optional<T> > {};
116 /// \brief Poison comparison between two \c Optional objects. Clients needs to
117 /// explicitly compare the underlying values and account for empty \c Optional
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);
125 /// \brief Poison comparison between two \c Optional objects. Clients needs to
126 /// explicitly compare the underlying values and account for empty \c Optional
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);
134 /// \brief Poison comparison between two \c Optional objects. Clients needs to
135 /// explicitly compare the underlying values and account for empty \c Optional
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);
143 /// \brief Poison comparison between two \c Optional objects. Clients needs to
144 /// explicitly compare the underlying values and account for empty \c Optional
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);
152 /// \brief Poison comparison between two \c Optional objects. Clients needs to
153 /// explicitly compare the underlying values and account for empty \c Optional
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);
161 /// \brief Poison comparison between two \c Optional objects. Clients needs to
162 /// explicitly compare the underlying values and account for empty \c Optional
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);
170 } // end llvm namespace