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
daa62fd1
Commit
daa62fd1
authored
Aug 23, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update sample code for new union syntax.
parent
066ce2e5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
50 deletions
+22
-50
addressbook.c++
c++/samples/addressbook.c++
+6
-20
addressbook.capnp
c++/samples/addressbook.capnp
+5
-5
cxx.md
doc/cxx.md
+11
-25
No files found.
c++/samples/addressbook.c++
View file @
daa62fd1
...
...
@@ -98,7 +98,6 @@ using ::capnp::DynamicValue;
using
::
capnp
::
DynamicStruct
;
using
::
capnp
::
DynamicEnum
;
using
::
capnp
::
DynamicList
;
using
::
capnp
::
DynamicUnion
;
using
::
capnp
::
List
;
using
::
capnp
::
Schema
;
using
::
capnp
::
StructSchema
;
...
...
@@ -133,7 +132,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
auto
phone0
=
alicePhones
[
0
].
as
<
DynamicStruct
>
();
phone0
.
set
(
"number"
,
"555-1212"
);
phone0
.
set
(
"type"
,
"mobile"
);
alice
.
get
(
"employment"
).
as
<
Dynamic
Union
>
()
alice
.
get
(
"employment"
).
as
<
Dynamic
Struct
>
()
.
set
(
"school"
,
"MIT"
);
auto
bob
=
people
[
1
].
as
<
DynamicStruct
>
();
...
...
@@ -149,7 +148,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
bobPhones
[
0
].
setType
(
Person
::
PhoneNumber
::
Type
::
HOME
);
bobPhones
[
1
].
setNumber
(
"555-7654"
);
bobPhones
[
1
].
setType
(
Person
::
PhoneNumber
::
Type
::
WORK
);
bob
.
get
(
"employment"
).
as
<
Dynamic
Union
>
()
bob
.
get
(
"employment"
).
as
<
Dynamic
Struct
>
()
.
set
(
"unemployed"
,
::
capnp
::
VOID
);
writePackedMessageToFd
(
fd
,
message
);
...
...
@@ -208,33 +207,20 @@ void dynamicPrintValue(DynamicValue::Reader value) {
std
::
cout
<<
"("
;
auto
structValue
=
value
.
as
<
DynamicStruct
>
();
bool
first
=
true
;
for
(
auto
member
:
structValue
.
getSchema
().
getMembers
())
{
for
(
auto
field
:
structValue
.
getSchema
().
getFields
())
{
if
(
!
structValue
.
has
(
field
))
continue
;
if
(
first
)
{
first
=
false
;
}
else
{
std
::
cout
<<
", "
;
}
std
::
cout
<<
member
.
getProto
().
getName
().
cStr
()
std
::
cout
<<
field
.
getProto
().
getName
().
cStr
()
<<
" = "
;
dynamicPrintValue
(
structValue
.
get
(
member
));
dynamicPrintValue
(
structValue
.
get
(
field
));
}
std
::
cout
<<
")"
;
break
;
}
case
DynamicValue
:
:
UNION
:
{
auto
unionValue
=
value
.
as
<
DynamicUnion
>
();
KJ_IF_MAYBE
(
tag
,
unionValue
.
which
())
{
std
::
cout
<<
tag
->
getProto
().
getName
().
cStr
()
<<
"("
;
dynamicPrintValue
(
unionValue
.
get
());
std
::
cout
<<
")"
;
}
else
{
// Unknown union member; must have come from newer
// version of the protocol.
std
::
cout
<<
"?"
;
}
break
;
}
default:
// There are other types, we aren't handling them.
std
::
cout
<<
"?"
;
...
...
c++/samples/addressbook.capnp
View file @
daa62fd1
...
...
@@ -20,11 +20,11 @@ struct Person {
}
}
employment
@4
union {
unemployed @
5
:Void;
employer @
6
:Text;
school @
7
:Text;
selfEmployed @
8
:Void;
employment
:
union {
unemployed @
4
:Void;
employer @
5
:Text;
school @
6
:Text;
selfEmployed @
7
:Void;
# We assume that a person is only one of these.
}
}
...
...
doc/cxx.md
View file @
daa62fd1
...
...
@@ -30,11 +30,11 @@ struct Person {
}
}
employment
@4
union {
unemployed @
5
:Void;
employer @
6
:Text;
school @
7
:Text;
selfEmployed @
8
:Void;
employment
:
union {
unemployed @
4
:Void;
employer @
5
:Text;
school @
6
:Text;
selfEmployed @
7
:Void;
# We assume that a person is only one of these.
}
}
...
...
@@ -391,7 +391,6 @@ using ::capnp::DynamicValue;
using ::capnp::DynamicStruct;
using ::capnp::DynamicEnum;
using ::capnp::DynamicList;
using ::capnp::DynamicUnion;
using ::capnp::List;
using ::capnp::Schema;
using ::capnp::StructSchema;
...
...
@@ -426,7 +425,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
auto phone0 = alicePhones
[
0
]
.as
<DynamicStruct>
();
phone0.set("number", "555-1212");
phone0.set("type", "mobile");
alice.get("employment").as
<Dynamic
Union
>
()
alice.get("employment").as
<Dynamic
Struct
>
()
.set("school", "MIT");
auto bob = people
[
1
]
.as
<DynamicStruct>
();
...
...
@@ -442,7 +441,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
bobPhones
[
0
]
.setType(Person::PhoneNumber::Type::HOME);
bobPhones
[
1
]
.setNumber("555-7654");
bobPhones
[
1
]
.setType(Person::PhoneNumber::Type::WORK);
bob.get("employment").as
<Dynamic
Union
>
()
bob.get("employment").as
<Dynamic
Struct
>
()
.set("unemployed", ::capnp::VOID);
writePackedMessageToFd(fd, message);
...
...
@@ -501,33 +500,20 @@ void dynamicPrintValue(DynamicValue::Reader value) {
std::cout << "(";
auto structValue = value.as
<DynamicStruct>
();
bool first = true;
for (auto
member:
structValue.getSchema().getMembers()) {
for (auto
field: structValue.getSchema().getFields()) {
if (!structValue.has(field)) continue;
if (first) {
first = false;
} else {
std::cout << ", ";
}
std::cout <<
member
.getProto().getName().cStr()
std::cout <<
field
.getProto().getName().cStr()
<< " = ";
dynamicPrintValue(structValue.get(
member
));
dynamicPrintValue(structValue.get(
field
));
}
std::cout << ")";
break;
}
case DynamicValue::UNION: {
auto unionValue = value.as
<DynamicUnion>
();
KJ_IF_MAYBE(tag, unionValue.which()) {
std::cout
<
<
tag-
>
getProto().getName().cStr() << "(";
dynamicPrintValue(unionValue.get());
std::cout << ")";
} else {
// Unknown union member; must have come from newer
// version of the protocol.
std::cout << "?";
}
break;
}
default:
// There are other types, we aren't handling them.
std::cout << "?";
...
...
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