Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
96eb7cec
Commit
96eb7cec
authored
Apr 04, 2015
by
joshuawarner32@gmail.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move equals / == / != from functions to methods
parent
1cc41227
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
112 additions
and
50 deletions
+112
-50
any-test.c++
c++/src/capnp/any-test.c++
+15
-13
any.c++
c++/src/capnp/any.c++
+43
-21
any.h
c++/src/capnp/any.h
+54
-16
No files found.
c++/src/capnp/any-test.c++
View file @
96eb7cec
...
...
@@ -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
...
...
c++/src/capnp/any.c++
View file @
96eb7cec
...
...
@@ -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
:
...
...
c++/src/capnp/any.h
View file @
96eb7cec
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment