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"
22 #if LLVM_HAS_RVALUE_REFERENCES
33 explicit Optional() : x(), hasVal(false) {}
34 Optional(const T &y) : x(y), hasVal(true) {}
36 #if LLVM_HAS_RVALUE_REFERENCES
37 Optional(T &&y) : x(std::forward<T>(y)), hasVal(true) {}
40 static inline Optional create(const T* y) {
41 return y ? Optional(*y) : Optional();
44 Optional &operator=(const T &y) {
50 const T* getPointer() const { assert(hasVal); return &x; }
51 const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return x; }
53 operator bool() const { return hasVal; }
54 bool hasValue() const { return hasVal; }
55 const T* operator->() const { return getPointer(); }
56 const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return x; }
58 #if LLVM_HAS_RVALUE_REFERENCE_THIS
59 T&& getValue() && { assert(hasVal); return std::move(x); }
60 T&& operator*() && { assert(hasVal); return std::move(x); }
64 template<typename T> struct simplify_type;
67 struct simplify_type<const Optional<T> > {
68 typedef const T* SimpleType;
69 static SimpleType getSimplifiedValue(const Optional<T> &Val) {
70 return Val.getPointer();
75 struct simplify_type<Optional<T> >
76 : public simplify_type<const Optional<T> > {};
78 /// \brief Poison comparison between two \c Optional objects. Clients needs to
79 /// explicitly compare the underlying values and account for empty \c Optional
82 /// This routine will never be defined. It returns \c void to help diagnose
83 /// errors at compile time.
84 template<typename T, typename U>
85 void operator==(const Optional<T> &X, const Optional<U> &Y);
87 /// \brief Poison comparison between two \c Optional objects. Clients needs to
88 /// explicitly compare the underlying values and account for empty \c Optional
91 /// This routine will never be defined. It returns \c void to help diagnose
92 /// errors at compile time.
93 template<typename T, typename U>
94 void operator!=(const Optional<T> &X, const Optional<U> &Y);
96 /// \brief Poison comparison between two \c Optional objects. Clients needs to
97 /// explicitly compare the underlying values and account for empty \c Optional
100 /// This routine will never be defined. It returns \c void to help diagnose
101 /// errors at compile time.
102 template<typename T, typename U>
103 void operator<(const Optional<T> &X, const Optional<U> &Y);
105 /// \brief Poison comparison between two \c Optional objects. Clients needs to
106 /// explicitly compare the underlying values and account for empty \c Optional
109 /// This routine will never be defined. It returns \c void to help diagnose
110 /// errors at compile time.
111 template<typename T, typename U>
112 void operator<=(const Optional<T> &X, const Optional<U> &Y);
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 } // end llvm namespace