Skip to content

Commit e9cc773

Browse files
removed std::optional<bool>
1 parent 9644b99 commit e9cc773

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

pub/cppast.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include <cstdint>
4444
#include <list>
4545
#include <memory>
46-
#include <optional>
4746
#include <string>
4847
#include <utility>
4948
#include <vector>
@@ -936,8 +935,8 @@ struct CppCompound : public CppObj, public AttribSpecified
936935
const CppConstructor* moveCtor_ {nullptr};
937936
const CppDestructor* dtor_ {nullptr};
938937

939-
mutable std::optional<bool> hasVirtual_;
940-
mutable std::optional<bool> hasPureVirtual_;
938+
mutable TriStateBool hasVirtual_ = TriStateBool::Unknown;
939+
mutable TriStateBool hasPureVirtual_ = TriStateBool::Unknown;
941940
};
942941

943942
using CppCompoundEPtr = CppEasyPtr<CppCompound>;
@@ -1220,8 +1219,8 @@ struct CppConstructor : public CppFuncCtorBase
12201219
bool isMoveConstructor() const;
12211220

12221221
private:
1223-
mutable std::optional<bool> isCopyConstructor_;
1224-
mutable std::optional<bool> isMoveConstructor_;
1222+
mutable TriStateBool isCopyConstructor_ = TriStateBool::Unknown;
1223+
mutable TriStateBool isMoveConstructor_ = TriStateBool::Unknown;
12251224
};
12261225

12271226
using CppConstructorEPtr = CppEasyPtr<CppConstructor>;

pub/cppconst.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525

2626
#include <cstdint>
2727

28+
enum class TriStateBool : std::int8_t
29+
{
30+
Unknown = -1,
31+
False = 0,
32+
True = 1
33+
};
34+
2835
enum class CppObjType : std::uint8_t
2936
{
3037
kUnknown = 0x0000,

src/cppast.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131

3232
bool CppConstructor::isCopyConstructor() const
3333
{
34-
if (isCopyConstructor_)
35-
return *isCopyConstructor_;
34+
if (isCopyConstructor_ != TriStateBool::Unknown)
35+
return isCopyConstructor_ == TriStateBool::True;
3636

37-
isCopyConstructor_ = false;
37+
isCopyConstructor_ = TriStateBool::False;
3838
if (!params_ || (params_->size() != 1))
3939
return false;
4040
const auto& param = params_->front();
@@ -58,16 +58,17 @@ bool CppConstructor::isCopyConstructor() const
5858
{
5959
return false;
6060
}
61-
isCopyConstructor_ = true;
62-
return *isCopyConstructor_;
61+
isCopyConstructor_ = TriStateBool::True;
62+
63+
return true;
6364
}
6465

6566
bool CppConstructor::isMoveConstructor() const
6667
{
67-
if (isMoveConstructor_)
68-
return *isMoveConstructor_;
68+
if (isMoveConstructor_ != TriStateBool::Unknown)
69+
return isMoveConstructor_ == TriStateBool::True;
6970

70-
isMoveConstructor_ = false;
71+
isMoveConstructor_ = TriStateBool::False;
7172
if (!params_ || (params_->size() != 1))
7273
return false;
7374
const auto& param = params_->front();
@@ -93,46 +94,50 @@ bool CppConstructor::isMoveConstructor() const
9394
{
9495
return false;
9596
}
96-
isMoveConstructor_ = true;
97-
return *isMoveConstructor_;
97+
isMoveConstructor_ = TriStateBool::True;
98+
99+
return true;
98100
}
99101

100102
bool CppCompound::hasPublicVirtualMethod() const
101103
{
102104
if (!isClassLike(this))
103105
return false;
104-
if (hasVirtual_)
105-
return *hasVirtual_;
106-
hasVirtual_ = false;
106+
if (hasVirtual_ != TriStateBool::Unknown)
107+
return hasVirtual_ == TriStateBool::True;
108+
109+
hasVirtual_ = TriStateBool::False;
107110
forEachMember(this, [&](const CppObj* mem) {
108111
if (isFunction(mem) && isPublic(mem))
109112
{
110113
auto func = (CppFunction*) mem;
111114
if (func->attr() & (kVirtual | kOverride))
112115
{
113-
hasVirtual_ = true;
116+
hasVirtual_ = TriStateBool::True;
114117
return true;
115118
}
116119
}
117120
return false;
118121
});
119-
return *hasVirtual_;
122+
123+
return hasVirtual_ == TriStateBool::True;
120124
}
121125

122126
bool CppCompound::hasPureVirtual() const
123127
{
124128
if (!isClassLike(this))
125129
return false;
126-
if (hasPureVirtual_)
127-
return *hasPureVirtual_;
128-
hasPureVirtual_ = false;
130+
if (hasPureVirtual_ != TriStateBool::Unknown)
131+
return hasPureVirtual_ == TriStateBool::True;
132+
133+
hasPureVirtual_ = TriStateBool::False;
129134
forEachMember(this, [&](const CppObj* mem) {
130135
if (isFunction(mem))
131136
{
132137
auto func = static_cast<const CppFunction*>(mem);
133138
if (isPureVirtual(func))
134139
{
135-
hasPureVirtual_ = true;
140+
hasPureVirtual_ = TriStateBool::True;
136141
return true;
137142
}
138143
}
@@ -141,14 +146,15 @@ bool CppCompound::hasPureVirtual() const
141146
auto dtor = static_cast<const CppDestructor*>(mem);
142147
if (isPureVirtual(dtor))
143148
{
144-
hasPureVirtual_ = true;
149+
hasPureVirtual_ = TriStateBool::True;
145150
return true;
146151
}
147152
}
148153

149154
return false;
150155
});
151-
return *hasPureVirtual_;
156+
157+
return hasPureVirtual_ == TriStateBool::True;
152158
}
153159

154160
bool CppCompound::triviallyConstructable() const

0 commit comments

Comments
 (0)