1 //===-- Either.h -----------------------------------------------*- 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 #ifndef liblldb_Either_h_
11 #define liblldb_Either_h_
13 #include "llvm/ADT/Optional.h"
17 namespace lldb_utility {
18 template <typename T1, typename T2> class Either {
20 enum class Selected { One, Two };
29 Either(const T1 &t1) {
31 m_selected = Selected::One;
34 Either(const T2 &t2) {
36 m_selected = Selected::Two;
39 Either(const Either<T1, T2> &rhs) {
40 switch (rhs.m_selected) {
42 m_t1 = rhs.GetAs<T1>().getValue();
43 m_selected = Selected::One;
46 m_t2 = rhs.GetAs<T2>().getValue();
47 m_selected = Selected::Two;
52 template <class X, typename std::enable_if<std::is_same<T1, X>::value>::type
54 llvm::Optional<T1> GetAs() const {
59 return llvm::Optional<T1>();
63 template <class X, typename std::enable_if<std::is_same<T2, X>::value>::type
65 llvm::Optional<T2> GetAs() const {
70 return llvm::Optional<T2>();
74 template <class ResultType>
75 ResultType Apply(std::function<ResultType(T1)> if_T1,
76 std::function<ResultType(T2)> if_T2) const {
85 bool operator==(const Either<T1, T2> &rhs) {
86 return (GetAs<T1>() == rhs.GetAs<T1>()) && (GetAs<T2>() == rhs.GetAs<T2>());
89 explicit operator bool() {
98 Either<T1, T2> &operator=(const Either<T1, T2> &rhs) {
99 switch (rhs.m_selected) {
101 m_t1 = rhs.GetAs<T1>().getValue();
102 m_selected = Selected::One;
105 m_t2 = rhs.GetAs<T2>().getValue();
106 m_selected = Selected::Two;
113 switch (m_selected) {
124 } // namespace lldb_utility
126 #endif // #ifndef liblldb_Either_h_