Commit 754477a8 authored by Kenton Varda's avatar Kenton Varda

Merge pull request #169 from jparyani/DynamicStructAsAnyStruct

Allow DynamicStruct::Reader/Builder.as<AnyStruct>
parents c3b6b203 e80357ef
......@@ -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());
......
......@@ -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;
......
......@@ -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); \
......
......@@ -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,
......
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