move equals / == / != from functions to methods

parent 1cc41227
......@@ -266,49 +266,51 @@ TEST(Any, AnyStructListCapInSchema) {
TEST(Any, Equals) {
MallocMessageBuilder builderA;
auto rootA = builderA.getRoot<test::TestAllTypes>();
auto anyA = builderA.getRoot<AnyPointer>();
initTestMessage(rootA);
MallocMessageBuilder builderB;
auto rootB = builderB.getRoot<test::TestAllTypes>();
auto anyB = builderB.getRoot<AnyPointer>();
initTestMessage(rootB);
EXPECT_EQ(StructEqualityResult::EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::EQUAL, anyA.equals(anyB));
rootA.setBoolField(false);
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, anyA.equals(anyB));
rootB.setBoolField(false);
EXPECT_EQ(StructEqualityResult::EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::EQUAL, anyA.equals(anyB));
rootB.setEnumField(test::TestEnum::GARPLY);
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, anyA.equals(anyB));
rootA.setEnumField(test::TestEnum::GARPLY);
EXPECT_EQ(StructEqualityResult::EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::EQUAL, anyA.equals(anyB));
rootA.getStructField().setTextField("buzz");
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, anyA.equals(anyB));
rootB.getStructField().setTextField("buzz");
EXPECT_EQ(StructEqualityResult::EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::EQUAL, anyA.equals(anyB));
rootA.initVoidList(3);
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, anyA.equals(anyB));
rootB.initVoidList(3);
EXPECT_EQ(StructEqualityResult::EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::EQUAL, anyA.equals(anyB));
rootA.getBoolList().set(2, true);
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, anyA.equals(anyB));
rootB.getBoolList().set(2, true);
EXPECT_EQ(StructEqualityResult::EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::EQUAL, anyA.equals(anyB));
rootB.getStructList()[1].setTextField("my NEW structlist 2");
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::NOT_EQUAL, anyA.equals(anyB));
rootA.getStructList()[1].setTextField("my NEW structlist 2");
EXPECT_EQ(StructEqualityResult::EQUAL, equal(builderA.getRoot<AnyPointer>(), builderB.getRoot<AnyPointer>()));
EXPECT_EQ(StructEqualityResult::EQUAL, anyA.equals(anyB));
}
} // namespace
......
......@@ -79,8 +79,8 @@ kj::Own<ClientHook> AnyPointer::Pipeline::asCap() {
#endif // !CAPNP_LITE
StructEqualityResult equal(AnyStruct::Reader left, AnyStruct::Reader right) {
auto dataL = left.getDataSection();
StructEqualityResult AnyStruct::Reader::equals(AnyStruct::Reader right) {
auto dataL = getDataSection();
size_t dataSizeL = dataL.size();
while(dataSizeL > 0 && dataL[dataSizeL - 1] == 0) {
-- dataSizeL;
......@@ -100,7 +100,7 @@ StructEqualityResult equal(AnyStruct::Reader left, AnyStruct::Reader right) {
return StructEqualityResult::NOT_EQUAL;
}
auto ptrsL = left.getPointerSection();
auto ptrsL = getPointerSection();
auto ptrsR = right.getPointerSection();
size_t i = 0;
......@@ -108,7 +108,7 @@ StructEqualityResult equal(AnyStruct::Reader left, AnyStruct::Reader right) {
for(; i < kj::min(ptrsL.size(), ptrsR.size()); i++) {
auto l = ptrsL[i];
auto r = ptrsR[i];
switch(equal(l, r)) {
switch(l.equals(r)) {
case StructEqualityResult::EQUAL:
break;
case StructEqualityResult::NOT_EQUAL:
......@@ -133,19 +133,19 @@ kj::StringPtr KJ_STRINGIFY(StructEqualityResult res) {
}
}
StructEqualityResult equal(AnyList::Reader left, AnyList::Reader right) {
if(left.size() != right.size()) {
StructEqualityResult AnyList::Reader::equals(AnyList::Reader right) {
if(size() != right.size()) {
return StructEqualityResult::NOT_EQUAL;
}
switch(left.getElementSize()) {
switch(getElementSize()) {
case ElementSize::VOID:
case ElementSize::BIT:
case ElementSize::BYTE:
case ElementSize::TWO_BYTES:
case ElementSize::FOUR_BYTES:
case ElementSize::EIGHT_BYTES:
if(left.getElementSize() == right.getElementSize()) {
if(memcmp(left.getRawBytes().begin(), right.getRawBytes().begin(), left.getRawBytes().size()) == 0) {
if(getElementSize() == right.getElementSize()) {
if(memcmp(getRawBytes().begin(), right.getRawBytes().begin(), getRawBytes().size()) == 0) {
return StructEqualityResult::EQUAL;
} else {
return StructEqualityResult::NOT_EQUAL;
......@@ -155,10 +155,10 @@ StructEqualityResult equal(AnyList::Reader left, AnyList::Reader right) {
}
case ElementSize::POINTER:
case ElementSize::INLINE_COMPOSITE: {
auto llist = left.as<List<AnyStruct>>();
auto llist = as<List<AnyStruct>>();
auto rlist = right.as<List<AnyStruct>>();
for(size_t i = 0; i < left.size(); i++) {
switch(equal(llist[i], rlist[i])) {
for(size_t i = 0; i < size(); i++) {
switch(llist[i].equals(rlist[i])) {
case StructEqualityResult::EQUAL:
break;
case StructEqualityResult::NOT_EQUAL:
......@@ -172,29 +172,29 @@ StructEqualityResult equal(AnyList::Reader left, AnyList::Reader right) {
}
}
StructEqualityResult equal(AnyPointer::Reader left, AnyPointer::Reader right) {
StructEqualityResult AnyPointer::Reader::equals(AnyPointer::Reader right) {
if(right.isCapability()) {
return StructEqualityResult::UNKNOWN_CONTAINS_CAPS;
}
if(left.isNull()) {
if(isNull()) {
if(right.isNull()) {
return StructEqualityResult::EQUAL;
} else {
return StructEqualityResult::NOT_EQUAL;
}
} else if(left.isStruct()) {
} else if(isStruct()) {
if(right.isStruct()) {
return equal(left.getAs<AnyStruct>(), right.getAs<AnyStruct>());
return getAs<AnyStruct>().equals(right.getAs<AnyStruct>());
} else {
return StructEqualityResult::NOT_EQUAL;
}
} else if(left.isList()) {
} else if(isList()) {
if(right.isList()) {
return equal(left.getAs<AnyList>(), right.getAs<AnyList>());
return getAs<AnyList>().equals(right.getAs<AnyList>());
} else {
return StructEqualityResult::NOT_EQUAL;
}
} else if(left.isCapability()) {
} else if(isCapability()) {
return StructEqualityResult::UNKNOWN_CONTAINS_CAPS;
} else {
// There aren't currently any other types of pointers
......@@ -202,8 +202,30 @@ StructEqualityResult equal(AnyPointer::Reader left, AnyPointer::Reader right) {
}
}
bool operator ==(AnyPointer::Reader left, AnyPointer::Reader right) {
switch(equal(left, right)) {
bool AnyPointer::Reader::operator ==(AnyPointer::Reader right) {
switch(equals(right)) {
case StructEqualityResult::EQUAL:
return true;
case StructEqualityResult::NOT_EQUAL:
return false;
case StructEqualityResult::UNKNOWN_CONTAINS_CAPS:
KJ_FAIL_REQUIRE();
}
}
bool AnyStruct::Reader::operator ==(AnyStruct::Reader right) {
switch(equals(right)) {
case StructEqualityResult::EQUAL:
return true;
case StructEqualityResult::NOT_EQUAL:
return false;
case StructEqualityResult::UNKNOWN_CONTAINS_CAPS:
KJ_FAIL_REQUIRE();
}
}
bool AnyList::Reader::operator ==(AnyList::Reader right) {
switch(equals(right)) {
case StructEqualityResult::EQUAL:
return true;
case StructEqualityResult::NOT_EQUAL:
......
......@@ -74,6 +74,14 @@ template <> struct Kind_<AnyList> { static constexpr Kind kind = Kind::OTHER; };
// =======================================================================================
// AnyPointer!
enum class StructEqualityResult {
NOT_EQUAL,
EQUAL,
UNKNOWN_CONTAINS_CAPS
};
kj::StringPtr KJ_STRINGIFY(StructEqualityResult res);
struct AnyPointer {
// Reader/Builder for the `AnyPointer` field type, i.e. a pointer that can point to an arbitrary
// object.
......@@ -97,6 +105,12 @@ struct AnyPointer {
inline bool isList() const { return getPointerType() == PointerType::LIST; }
inline bool isCapability() const { return getPointerType() == PointerType::CAPABILITY; }
StructEqualityResult equals(AnyPointer::Reader right);
bool operator ==(AnyPointer::Reader right);
inline bool operator !=(AnyPointer::Reader right) {
return !(*this == right);
}
template <typename T>
inline ReaderFor<T> getAs() const;
// Valid for T = any generated struct type, interface type, List<U>, Text, or Data.
......@@ -144,6 +158,16 @@ struct AnyPointer {
inline bool isList() { return getPointerType() == PointerType::LIST; }
inline bool isCapability() { return getPointerType() == PointerType::CAPABILITY; }
inline StructEqualityResult equals(AnyPointer::Reader right) {
return asReader().equals(right);
}
inline bool operator ==(AnyPointer::Reader right) {
return asReader() == right;
}
inline bool operator !=(AnyPointer::Reader right) {
return !(*this == right);
}
inline void clear();
// Set to null.
......@@ -434,6 +458,12 @@ public:
return List<AnyPointer>::Reader(_reader.getPointerSectionAsList());
}
StructEqualityResult equals(AnyStruct::Reader right);
bool operator ==(AnyStruct::Reader right);
inline bool operator !=(AnyStruct::Reader right) {
return !(*this == right);
}
template <typename T>
ReaderFor<T> as() const {
// T must be a struct type.
......@@ -464,6 +494,16 @@ public:
return List<AnyPointer>::Builder(_builder.getPointerSectionAsList());
}
inline StructEqualityResult equals(AnyStruct::Reader right) {
return asReader().equals(right);
}
inline bool operator ==(AnyStruct::Reader right) {
return asReader() == right;
}
inline bool operator !=(AnyStruct::Reader right) {
return !(*this == right);
}
inline operator Reader() const { return Reader(_builder.asReader()); }
inline Reader asReader() const { return Reader(_builder.asReader()); }
......@@ -573,6 +613,12 @@ public:
inline kj::ArrayPtr<const byte> getRawBytes() { return _reader.asRawBytes(); }
StructEqualityResult equals(AnyList::Reader right);
inline bool operator ==(AnyList::Reader right);
inline bool operator !=(AnyList::Reader right) {
return !(*this == right);
}
template <typename T> ReaderFor<T> as() {
// T must be List<U>.
return ReaderFor<T>(_reader);
......@@ -598,6 +644,14 @@ public:
inline ElementSize getElementSize() { return _builder.getElementSize(); }
inline uint size() { return _builder.size() / ELEMENTS; }
StructEqualityResult equals(AnyList::Reader right);
inline bool operator ==(AnyList::Reader right) {
return asReader() == right;
}
inline bool operator !=(AnyList::Reader right) {
return !(*this == right);
}
template <typename T> BuilderFor<T> as() {
// T must be List<U>.
return BuilderFor<T>(_builder);
......@@ -806,22 +860,6 @@ inline Orphan<AnyPointer> Orphan<AnyPointer>::releaseAs() {
return kj::mv(*this);
}
enum class StructEqualityResult {
NOT_EQUAL,
EQUAL,
UNKNOWN_CONTAINS_CAPS
};
kj::StringPtr KJ_STRINGIFY(StructEqualityResult res);
StructEqualityResult equal(AnyStruct::Reader left, AnyStruct::Reader right);
StructEqualityResult equal(List<AnyStruct>::Reader left, List<AnyStruct>::Reader right);
StructEqualityResult equal(AnyPointer::Reader left, AnyPointer::Reader right);
bool operator ==(AnyPointer::Reader left, AnyPointer::Reader right);
inline bool operator !=(AnyPointer::Reader left, AnyPointer::Reader right) {
return !(left == right);
}
namespace _ { // private
// Specialize PointerHelpers for AnyPointer.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment