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
754477a8
Commit
754477a8
authored
Jan 27, 2015
by
Kenton Varda
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #169 from jparyani/DynamicStructAsAnyStruct
Allow DynamicStruct::Reader/Builder.as<AnyStruct>
parents
c3b6b203
e80357ef
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
2 deletions
+36
-2
any-test.c++
c++/src/capnp/any-test.c++
+6
-0
any.h
c++/src/capnp/any.h
+10
-2
dynamic-test.c++
c++/src/capnp/dynamic-test.c++
+9
-0
dynamic.h
c++/src/capnp/dynamic.h
+11
-0
No files found.
c++/src/capnp/any-test.c++
View file @
754477a8
...
...
@@ -114,6 +114,12 @@ TEST(Any, AnyStruct) {
checkTestMessage
(
root
.
getAnyPointerField
().
getAs
<
TestAllTypes
>
());
checkTestMessage
(
root
.
asReader
().
getAnyPointerField
().
getAs
<
TestAllTypes
>
());
auto
allTypes
=
root
.
getAnyPointerField
().
getAs
<
AnyStruct
>
().
as
<
TestAllTypes
>
();
auto
allTypesReader
=
root
.
getAnyPointerField
().
getAs
<
AnyStruct
>
().
asReader
().
as
<
TestAllTypes
>
();
allTypes
.
setInt32Field
(
100
);
EXPECT_EQ
(
100
,
allTypes
.
getInt32Field
());
EXPECT_EQ
(
100
,
allTypesReader
.
getInt32Field
());
EXPECT_EQ
(
48
,
root
.
getAnyPointerField
().
getAs
<
AnyStruct
>
().
getDataSection
().
size
());
EXPECT_EQ
(
20
,
root
.
getAnyPointerField
().
getAs
<
AnyStruct
>
().
getPointerSection
().
size
());
...
...
c++/src/capnp/any.h
View file @
754477a8
...
...
@@ -436,8 +436,10 @@ public:
}
template
<
typename
T
>
ReaderFor
<
T
>
as
();
// T must be a struct type.
ReaderFor
<
T
>
as
()
const
{
// T must be a struct type.
return
typename
T
::
Reader
(
_reader
);
}
private
:
_
::
StructReader
_reader
;
...
...
@@ -465,6 +467,12 @@ public:
inline
operator
Reader
()
const
{
return
Reader
(
_builder
.
asReader
());
}
inline
Reader
asReader
()
const
{
return
Reader
(
_builder
.
asReader
());
}
template
<
typename
T
>
BuilderFor
<
T
>
as
()
{
// T must be a struct type.
return
typename
T
::
Builder
(
_builder
);
}
private
:
_
::
StructBuilder
_builder
;
friend
class
Orphanage
;
...
...
c++/src/capnp/dynamic-test.c++
View file @
754477a8
...
...
@@ -222,6 +222,15 @@ TEST(DynamicApi, DynamicAnyPointers) {
}
}
TEST
(
DynamicApi
,
DynamicAnyStructs
)
{
MallocMessageBuilder
builder
;
auto
root
=
builder
.
initRoot
<
DynamicStruct
>
(
Schema
::
from
<
TestAllTypes
>
());
root
.
as
<
AnyStruct
>
().
as
<
TestAllTypes
>
().
setInt8Field
(
123
);
EXPECT_EQ
(
root
.
get
(
"int8Field"
).
as
<
int8_t
>
(),
123
);
EXPECT_EQ
(
root
.
asReader
().
as
<
AnyStruct
>
().
as
<
TestAllTypes
>
().
getInt8Field
(),
123
);
}
#define EXPECT_MAYBE_EQ(name, exp, expected, actual) \
KJ_IF_MAYBE(name, exp) { \
EXPECT_EQ(expected, actual); \
...
...
c++/src/capnp/dynamic.h
View file @
754477a8
...
...
@@ -1435,6 +1435,7 @@ typename T::Reader DynamicStruct::Reader::as() const {
schema
.
requireUsableAs
<
T
>
();
return
typename
T
::
Reader
(
reader
);
}
template
<
typename
T
>
typename
T
::
Builder
DynamicStruct
::
Builder
::
as
()
{
static_assert
(
kind
<
T
>
()
==
Kind
::
STRUCT
,
...
...
@@ -1456,6 +1457,16 @@ inline DynamicStruct::Reader DynamicStruct::Builder::asReader() const {
return
DynamicStruct
::
Reader
(
schema
,
builder
.
asReader
());
}
template
<>
inline
AnyStruct
::
Reader
DynamicStruct
::
Reader
::
as
<
AnyStruct
>
()
const
{
return
AnyStruct
::
Reader
(
reader
);
}
template
<>
inline
AnyStruct
::
Builder
DynamicStruct
::
Builder
::
as
<
AnyStruct
>
()
{
return
AnyStruct
::
Builder
(
builder
);
}
template
<
typename
T
>
typename
T
::
Pipeline
DynamicStruct
::
Pipeline
::
releaseAs
()
{
static_assert
(
kind
<
T
>
()
==
Kind
::
STRUCT
,
...
...
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