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/Support/Compiler.h"
20 #include "llvm/Support/AlignOf.h"
23 #if LLVM_HAS_RVALUE_REFERENCES
31 AlignedCharArrayUnion<T> storage;
34 explicit Optional() : hasVal(false) {}
35 Optional(const T &y) : hasVal(true) {
36 new (storage.buffer) T(y);
38 Optional(const Optional &O) : hasVal(O.hasVal) {
40 new (storage.buffer) T(*O);
43 #if LLVM_HAS_RVALUE_REFERENCES
44 Optional(T &&y) : hasVal(true) {
45 new (storage.buffer) T(std::forward<T>(y));
49 static inline Optional create(const T* y) {
50 return y ? Optional(*y) : Optional();
53 Optional &operator=(const T &y) {
57 new (storage.buffer) T(y);
63 Optional &operator=(const Optional &O) {
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(); }
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(); }
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()); }
100 template<typename T> struct simplify_type;
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();
110 template <typename T>
111 struct simplify_type<Optional<T> >
112 : public simplify_type<const Optional<T> > {};
114 /// \brief Poison comparison between two \c Optional objects. Clients needs to
115 /// explicitly compare the underlying values and account for empty \c Optional
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);
123 /// \brief Poison comparison between two \c Optional objects. Clients needs to
124 /// explicitly compare the underlying values and account for empty \c Optional
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);
132 /// \brief Poison comparison between two \c Optional objects. Clients needs to
133 /// explicitly compare the underlying values and account for empty \c Optional
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);
141 /// \brief Poison comparison between two \c Optional objects. Clients needs to
142 /// explicitly compare the underlying values and account for empty \c Optional
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);
150 /// \brief Poison comparison between two \c Optional objects. Clients needs to
151 /// explicitly compare the underlying values and account for empty \c Optional
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);
159 /// \brief Poison comparison between two \c Optional objects. Clients needs to
160 /// explicitly compare the underlying values and account for empty \c Optional
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);
168 } // end llvm namespace