Skip to content

Commit ccc025b

Browse files
author
Enrico Granata
committed
Add an Either<T,U> type to lldb_utility which represents a type-safe payload of either one type or another, à la Haskell
llvm-svn: 242867
1 parent 4112ab9 commit ccc025b

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

lldb/include/lldb/Utility/Either.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===-- Either.h -----------------------------------------------*- 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+
#ifndef liblldb_Either_h_
11+
#define liblldb_Either_h_
12+
13+
#include "llvm/ADT/Optional.h"
14+
15+
namespace lldb_utility {
16+
template <typename T1, typename T2>
17+
class Either {
18+
private:
19+
enum class Selected {
20+
One, Two
21+
};
22+
23+
Selected m_selected;
24+
union {
25+
T1 m_t1;
26+
T2 m_t2;
27+
};
28+
29+
public:
30+
Either(const T1& t1)
31+
{
32+
m_t1 = t1;
33+
m_selected = Selected::One;
34+
}
35+
36+
Either(const T2& t2)
37+
{
38+
m_t2 = t2;
39+
m_selected = Selected::Two;
40+
}
41+
42+
template <class X, typename std::enable_if<std::is_same<T1,X>::value>::type * = nullptr >
43+
llvm::Optional<T1>
44+
GetAs()
45+
{
46+
switch (m_selected)
47+
{
48+
case Selected::One:
49+
return m_t1;
50+
default:
51+
return llvm::Optional<T1>();
52+
}
53+
}
54+
55+
template <class X, typename std::enable_if<std::is_same<T2,X>::value>::type * = nullptr >
56+
llvm::Optional<T2>
57+
GetAs()
58+
{
59+
switch (m_selected)
60+
{
61+
case Selected::Two:
62+
return m_t2;
63+
default:
64+
return llvm::Optional<T2>();
65+
}
66+
}
67+
};
68+
69+
} // namespace lldb_utility
70+
71+
#endif // #ifndef liblldb_Either_h_
72+

lldb/lldb.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,7 @@
24312431
9475C18D14E5F834001BFC6D /* SBTypeNameSpecifier.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBTypeNameSpecifier.cpp; path = source/API/SBTypeNameSpecifier.cpp; sourceTree = "<group>"; };
24322432
947A1D621616476A0017C8D1 /* CommandObjectPlugin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CommandObjectPlugin.cpp; path = source/Commands/CommandObjectPlugin.cpp; sourceTree = "<group>"; };
24332433
947A1D631616476A0017C8D1 /* CommandObjectPlugin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommandObjectPlugin.h; path = source/Commands/CommandObjectPlugin.h; sourceTree = "<group>"; };
2434+
9481FE6B1B5F2D9200DED357 /* Either.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Either.h; path = include/lldb/Utility/Either.h; sourceTree = "<group>"; };
24342435
9492E2A41A8AC11000295BBD /* CoreMedia.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CoreMedia.cpp; path = source/DataFormatters/CoreMedia.cpp; sourceTree = "<group>"; };
24352436
949ADF001406F62E004833E1 /* ValueObjectConstResultImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ValueObjectConstResultImpl.h; path = include/lldb/Core/ValueObjectConstResultImpl.h; sourceTree = "<group>"; };
24362437
949ADF021406F648004833E1 /* ValueObjectConstResultImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ValueObjectConstResultImpl.cpp; path = source/Core/ValueObjectConstResultImpl.cpp; sourceTree = "<group>"; };
@@ -3532,6 +3533,7 @@
35323533
264723A511FA076E00DE380C /* CleanUp.h */,
35333534
3F81691B1ABA242B001DA9DF /* ConvertEnum.h */,
35343535
3F8169171ABA2419001DA9DF /* ConvertEnum.cpp */,
3536+
9481FE6B1B5F2D9200DED357 /* Either.h */,
35353537
4C73152119B7D71700F865A4 /* Iterable.h */,
35363538
942829541A89614000521B30 /* JSON.h */,
35373539
942829551A89614C00521B30 /* JSON.cpp */,

0 commit comments

Comments
 (0)