Skip to content

Commit d13ca03

Browse files
change all enum to enum class
1 parent 2df83a9 commit d13ca03

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ using TCppFuncAddr_t = void*;
4444
using TInterp_t = void*;
4545
using TCppObject_t = void*;
4646

47-
enum Operator : unsigned char {
47+
template <typename T> struct TruthValue {
48+
T t;
49+
constexpr TruthValue(T t) : t(t) {}
50+
constexpr operator T() const { return t; }
51+
constexpr explicit operator bool() const { return underlying(t); }
52+
};
53+
54+
template <typename T> using Underlying = std::underlying_type_t<T>;
55+
template <typename T> constexpr Underlying<T> underlying(T t) {
56+
return Underlying<T>(t);
57+
}
58+
59+
enum class Operator : unsigned char {
4860
OP_None,
4961
OP_New,
5062
OP_Delete,
@@ -93,18 +105,31 @@ enum Operator : unsigned char {
93105
OP_Coawait,
94106
};
95107

96-
enum OperatorArity : unsigned char { kUnary = 1, kBinary, kBoth };
108+
enum class OperatorArity : unsigned char { kUnary = 1, kBinary, kBoth };
109+
110+
constexpr TruthValue<OperatorArity> operator&(OperatorArity l,
111+
OperatorArity r) {
112+
return OperatorArity(underlying(l) & underlying(r));
113+
}
114+
115+
constexpr TruthValue<OperatorArity> operator|(OperatorArity l,
116+
OperatorArity r) {
117+
return OperatorArity(underlying(l) | underlying(r));
118+
}
97119

98120
/// Enum modelling CVR qualifiers.
99-
enum QualKind : unsigned char {
121+
enum class QualKind : unsigned char {
100122
Const = 1 << 0,
101123
Volatile = 1 << 1,
102124
Restrict = 1 << 2
103125
};
104126

105-
inline QualKind operator|(QualKind a, QualKind b) {
106-
return static_cast<QualKind>(static_cast<unsigned char>(a) |
107-
static_cast<unsigned char>(b));
127+
constexpr TruthValue<QualKind> operator&(QualKind l, QualKind r) {
128+
return QualKind(underlying(l) & underlying(r));
129+
}
130+
131+
constexpr TruthValue<QualKind> operator|(QualKind l, QualKind r) {
132+
return QualKind(underlying(l) | underlying(r));
108133
}
109134

110135
/// A class modeling function calls for functions produced by the interpreter
@@ -686,7 +711,7 @@ CPPINTEROP_API OperatorArity GetOperatorArity(TCppFunction_t op);
686711
///\returns list of operator overloads
687712
CPPINTEROP_API void GetOperator(TCppScope_t scope, Operator op,
688713
std::vector<TCppFunction_t>& operators,
689-
OperatorArity kind = kBoth);
714+
OperatorArity kind = OperatorArity::kBoth);
690715

691716
/// Creates an owned instance of the interpreter we need for the various interop
692717
/// services and pushes it onto a stack.

lib/CppInterOp/CppInterOp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,11 +3874,11 @@ OperatorArity GetOperatorArity(TCppFunction_t op) {
38743874
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
38753875
case OO_##Name: \
38763876
if ((Unary) && (Binary)) \
3877-
return kBoth; \
3877+
return OperatorArity::kBoth; \
38783878
if (Unary) \
3879-
return kUnary; \
3879+
return OperatorArity::kUnary; \
38803880
if (Binary) \
3881-
return kBinary; \
3881+
return OperatorArity::kBinary; \
38823882
break;
38833883
#include "clang/Basic/OperatorKinds.def"
38843884
default:

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,7 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestGetFunctionCallWrapper) {
18581858

18591859
EXPECT_TRUE(toperator);
18601860
std::vector<Cpp::TCppScope_t> operators;
1861-
Cpp::GetOperator(TOperator, Cpp::OP_Less, operators);
1861+
Cpp::GetOperator(TOperator, Cpp::Operator::OP_Less, operators);
18621862
EXPECT_EQ(operators.size(), 1);
18631863

18641864
Cpp::TCppScope_t op_templated = operators[0];
@@ -1901,8 +1901,8 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestGetFunctionCallWrapper) {
19011901
Cpp::TCppType_t K1 = Cpp::GetTypeFromScope(Cpp::GetNamed("K1"));
19021902
Cpp::TCppType_t K2 = Cpp::GetTypeFromScope(Cpp::GetNamed("K2"));
19031903
operators.clear();
1904-
Cpp::GetOperator(Cpp::GetScope("N2", Cpp::GetScope("N1")), Cpp::OP_Plus,
1905-
operators);
1904+
Cpp::GetOperator(Cpp::GetScope("N2", Cpp::GetScope("N1")),
1905+
Cpp::Operator::OP_Plus, operators);
19061906
EXPECT_EQ(operators.size(), 1);
19071907
Cpp::TCppFunction_t kop =
19081908
Cpp::BestOverloadFunctionMatch(operators, {}, {K1, K2});
@@ -2132,7 +2132,7 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestGetFunctionCallWrapper) {
21322132
EXPECT_TRUE(KlassProduct_float);
21332133

21342134
operators.clear();
2135-
Cpp::GetOperator(KlassProduct_int, Cpp::OP_Star, operators);
2135+
Cpp::GetOperator(KlassProduct_int, Cpp::Operator::OP_Star, operators);
21362136
EXPECT_EQ(operators.size(), 2);
21372137

21382138
op = Cpp::BestOverloadFunctionMatch(

unittests/CppInterOp/TypeReflectionTest.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,11 @@ TYPED_TEST(CppInterOpTest, TypeReflectionTestIsFunctionPointerType) {
614614
}
615615

616616
TYPED_TEST(CppInterOpTest, TypeReflectionTestOperatorSpelling) {
617-
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Less), "<");
618-
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Plus), "+");
619-
EXPECT_EQ(Cpp::GetOperatorFromSpelling("->"), Cpp::OP_Arrow);
620-
EXPECT_EQ(Cpp::GetOperatorFromSpelling("()"), Cpp::OP_Call);
621-
EXPECT_EQ(Cpp::GetOperatorFromSpelling("invalid"), Cpp::OP_None);
617+
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::Operator::OP_Less), "<");
618+
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::Operator::OP_Plus), "+");
619+
EXPECT_EQ(Cpp::GetOperatorFromSpelling("->"), Cpp::Operator::OP_Arrow);
620+
EXPECT_EQ(Cpp::GetOperatorFromSpelling("()"), Cpp::Operator::OP_Call);
621+
EXPECT_EQ(Cpp::GetOperatorFromSpelling("invalid"), Cpp::Operator::OP_None);
622622
}
623623

624624
TYPED_TEST(CppInterOpTest, TypeReflectionTestTypeQualifiers) {

0 commit comments

Comments
 (0)