Commit b7e1266b authored by Kenton Varda's avatar Kenton Varda

Improve const-correctness of readers and builders. We enforce transitive…

Improve const-correctness of readers and builders.  We enforce transitive constness, meaning you should not be able to construct a mutable Builder from a const Builder (you can always convert them to Readers instead).
parent b4953e9d
...@@ -102,7 +102,7 @@ inline internal::StructSize structSizeFromSchema(StructSchema schema) { ...@@ -102,7 +102,7 @@ inline internal::StructSize structSizeFromSchema(StructSchema schema) {
// ======================================================================================= // =======================================================================================
kj::Maybe<EnumSchema::Enumerant> DynamicEnum::getEnumerant() { kj::Maybe<EnumSchema::Enumerant> DynamicEnum::getEnumerant() const {
auto enumerants = schema.getEnumerants(); auto enumerants = schema.getEnumerants();
if (value < enumerants.size()) { if (value < enumerants.size()) {
return enumerants[value]; return enumerants[value];
...@@ -111,7 +111,7 @@ kj::Maybe<EnumSchema::Enumerant> DynamicEnum::getEnumerant() { ...@@ -111,7 +111,7 @@ kj::Maybe<EnumSchema::Enumerant> DynamicEnum::getEnumerant() {
} }
} }
uint16_t DynamicEnum::asImpl(uint64_t requestedTypeId) { uint16_t DynamicEnum::asImpl(uint64_t requestedTypeId) const {
KJ_REQUIRE(requestedTypeId == schema.getProto().getId(), KJ_REQUIRE(requestedTypeId == schema.getProto().getId(),
"Type mismatch in DynamicEnum.as().") { "Type mismatch in DynamicEnum.as().") {
// use it anyway // use it anyway
...@@ -122,7 +122,7 @@ uint16_t DynamicEnum::asImpl(uint64_t requestedTypeId) { ...@@ -122,7 +122,7 @@ uint16_t DynamicEnum::asImpl(uint64_t requestedTypeId) {
// ======================================================================================= // =======================================================================================
DynamicStruct::Reader DynamicObject::as(StructSchema schema) { DynamicStruct::Reader DynamicObject::as(StructSchema schema) const {
if (reader.kind == internal::ObjectKind::NULL_POINTER) { if (reader.kind == internal::ObjectKind::NULL_POINTER) {
return DynamicStruct::Reader(schema, internal::StructReader()); return DynamicStruct::Reader(schema, internal::StructReader());
} }
...@@ -133,7 +133,7 @@ DynamicStruct::Reader DynamicObject::as(StructSchema schema) { ...@@ -133,7 +133,7 @@ DynamicStruct::Reader DynamicObject::as(StructSchema schema) {
return DynamicStruct::Reader(schema, reader.structReader); return DynamicStruct::Reader(schema, reader.structReader);
} }
DynamicList::Reader DynamicObject::as(ListSchema schema) { DynamicList::Reader DynamicObject::as(ListSchema schema) const {
if (reader.kind == internal::ObjectKind::NULL_POINTER) { if (reader.kind == internal::ObjectKind::NULL_POINTER) {
return DynamicList::Reader(schema, internal::ListReader()); return DynamicList::Reader(schema, internal::ListReader());
} }
...@@ -146,7 +146,7 @@ DynamicList::Reader DynamicObject::as(ListSchema schema) { ...@@ -146,7 +146,7 @@ DynamicList::Reader DynamicObject::as(ListSchema schema) {
// ======================================================================================= // =======================================================================================
kj::Maybe<StructSchema::Member> DynamicUnion::Reader::which() { kj::Maybe<StructSchema::Member> DynamicUnion::Reader::which() const {
auto members = schema.getMembers(); auto members = schema.getMembers();
uint16_t discrim = reader.getDataField<uint16_t>( uint16_t discrim = reader.getDataField<uint16_t>(
schema.getProto().getBody().getUnionMember().getDiscriminantOffset() * ELEMENTS); schema.getProto().getBody().getUnionMember().getDiscriminantOffset() * ELEMENTS);
...@@ -169,7 +169,7 @@ kj::Maybe<StructSchema::Member> DynamicUnion::Builder::which() { ...@@ -169,7 +169,7 @@ kj::Maybe<StructSchema::Member> DynamicUnion::Builder::which() {
} }
} }
DynamicValue::Reader DynamicUnion::Reader::get() { DynamicValue::Reader DynamicUnion::Reader::get() const {
KJ_IF_MAYBE(w, which()) { KJ_IF_MAYBE(w, which()) {
return DynamicValue::Reader(DynamicStruct::Reader::getImpl(reader, *w)); return DynamicValue::Reader(DynamicStruct::Reader::getImpl(reader, *w));
} else { } else {
...@@ -185,7 +185,7 @@ DynamicValue::Builder DynamicUnion::Builder::get() { ...@@ -185,7 +185,7 @@ DynamicValue::Builder DynamicUnion::Builder::get() {
} }
} }
void DynamicUnion::Builder::set(StructSchema::Member member, DynamicValue::Reader value) { void DynamicUnion::Builder::set(StructSchema::Member member, const DynamicValue::Reader& value) {
setDiscriminant(member); setDiscriminant(member);
DynamicStruct::Builder::setImpl(builder, member, value); DynamicStruct::Builder::setImpl(builder, member, value);
} }
...@@ -231,26 +231,26 @@ Data::Builder DynamicUnion::Builder::initObjectAsData(StructSchema::Member membe ...@@ -231,26 +231,26 @@ Data::Builder DynamicUnion::Builder::initObjectAsData(StructSchema::Member membe
return DynamicStruct::Builder::initFieldAsDataImpl(builder, member, size); return DynamicStruct::Builder::initFieldAsDataImpl(builder, member, size);
} }
void DynamicUnion::Builder::set(Text::Reader name, DynamicValue::Reader value) { void DynamicUnion::Builder::set(kj::StringPtr name, const DynamicValue::Reader& value) {
set(schema.getMemberByName(name), value); set(schema.getMemberByName(name), value);
} }
DynamicValue::Builder DynamicUnion::Builder::init(Text::Reader name) { DynamicValue::Builder DynamicUnion::Builder::init(kj::StringPtr name) {
return init(schema.getMemberByName(name)); return init(schema.getMemberByName(name));
} }
DynamicValue::Builder DynamicUnion::Builder::init(Text::Reader name, uint size) { DynamicValue::Builder DynamicUnion::Builder::init(kj::StringPtr name, uint size) {
return init(schema.getMemberByName(name), size); return init(schema.getMemberByName(name), size);
} }
DynamicStruct::Builder DynamicUnion::Builder::initObject(Text::Reader name, StructSchema type) { DynamicStruct::Builder DynamicUnion::Builder::initObject(kj::StringPtr name, StructSchema type) {
return initObject(schema.getMemberByName(name), type); return initObject(schema.getMemberByName(name), type);
} }
DynamicList::Builder DynamicUnion::Builder::initObject( DynamicList::Builder DynamicUnion::Builder::initObject(
Text::Reader name, ListSchema type, uint size) { kj::StringPtr name, ListSchema type, uint size) {
return initObject(schema.getMemberByName(name), type, size); return initObject(schema.getMemberByName(name), type, size);
} }
Text::Builder DynamicUnion::Builder::initObjectAsText(Text::Reader name, uint size) { Text::Builder DynamicUnion::Builder::initObjectAsText(kj::StringPtr name, uint size) {
return initObjectAsText(schema.getMemberByName(name), size); return initObjectAsText(schema.getMemberByName(name), size);
} }
Data::Builder DynamicUnion::Builder::initObjectAsData(Text::Reader name, uint size) { Data::Builder DynamicUnion::Builder::initObjectAsData(kj::StringPtr name, uint size) {
return initObjectAsData(schema.getMemberByName(name), size); return initObjectAsData(schema.getMemberByName(name), size);
} }
...@@ -285,7 +285,7 @@ void DynamicUnion::Builder::setObjectDiscriminant(StructSchema::Member member) { ...@@ -285,7 +285,7 @@ void DynamicUnion::Builder::setObjectDiscriminant(StructSchema::Member member) {
// ======================================================================================= // =======================================================================================
DynamicValue::Reader DynamicStruct::Reader::get(StructSchema::Member member) { DynamicValue::Reader DynamicStruct::Reader::get(StructSchema::Member member) const {
KJ_REQUIRE(member.getContainingStruct() == schema, "`member` is not a member of this struct."); KJ_REQUIRE(member.getContainingStruct() == schema, "`member` is not a member of this struct.");
return getImpl(reader, member); return getImpl(reader, member);
} }
...@@ -294,7 +294,7 @@ DynamicValue::Builder DynamicStruct::Builder::get(StructSchema::Member member) { ...@@ -294,7 +294,7 @@ DynamicValue::Builder DynamicStruct::Builder::get(StructSchema::Member member) {
return getImpl(builder, member); return getImpl(builder, member);
} }
bool DynamicStruct::Reader::has(StructSchema::Member member) { bool DynamicStruct::Reader::has(StructSchema::Member member) const {
KJ_REQUIRE(member.getContainingStruct() == schema, "`member` is not a member of this struct."); KJ_REQUIRE(member.getContainingStruct() == schema, "`member` is not a member of this struct.");
auto body = member.getProto().getBody(); auto body = member.getProto().getBody();
...@@ -427,7 +427,7 @@ bool DynamicStruct::Builder::has(StructSchema::Member member) { ...@@ -427,7 +427,7 @@ bool DynamicStruct::Builder::has(StructSchema::Member member) {
return false; return false;
} }
void DynamicStruct::Builder::set(StructSchema::Member member, DynamicValue::Reader value) { void DynamicStruct::Builder::set(StructSchema::Member member, const DynamicValue::Reader& value) {
KJ_REQUIRE(member.getContainingStruct() == schema, "`member` is not a member of this struct."); KJ_REQUIRE(member.getContainingStruct() == schema, "`member` is not a member of this struct.");
return setImpl(builder, member, value); return setImpl(builder, member, value);
} }
...@@ -591,56 +591,56 @@ Data::Builder DynamicStruct::Builder::initObjectAsData(StructSchema::Member memb ...@@ -591,56 +591,56 @@ Data::Builder DynamicStruct::Builder::initObjectAsData(StructSchema::Member memb
return Data::Builder(); return Data::Builder();
} }
DynamicValue::Reader DynamicStruct::Reader::get(Text::Reader name) { DynamicValue::Reader DynamicStruct::Reader::get(kj::StringPtr name) const {
return getImpl(reader, schema.getMemberByName(name)); return getImpl(reader, schema.getMemberByName(name));
} }
DynamicValue::Builder DynamicStruct::Builder::get(Text::Reader name) { DynamicValue::Builder DynamicStruct::Builder::get(kj::StringPtr name) {
return getImpl(builder, schema.getMemberByName(name)); return getImpl(builder, schema.getMemberByName(name));
} }
bool DynamicStruct::Reader::has(Text::Reader name) { bool DynamicStruct::Reader::has(kj::StringPtr name) const {
return has(schema.getMemberByName(name)); return has(schema.getMemberByName(name));
} }
bool DynamicStruct::Builder::has(Text::Reader name) { bool DynamicStruct::Builder::has(kj::StringPtr name) {
return has(schema.getMemberByName(name)); return has(schema.getMemberByName(name));
} }
void DynamicStruct::Builder::set(Text::Reader name, DynamicValue::Reader value) { void DynamicStruct::Builder::set(kj::StringPtr name, const DynamicValue::Reader& value) {
setImpl(builder, schema.getMemberByName(name), value); setImpl(builder, schema.getMemberByName(name), value);
} }
void DynamicStruct::Builder::set(Text::Reader name, void DynamicStruct::Builder::set(kj::StringPtr name,
std::initializer_list<DynamicValue::Reader> value) { std::initializer_list<DynamicValue::Reader> value) {
init(name, value.size()).as<DynamicList>().copyFrom(value); init(name, value.size()).as<DynamicList>().copyFrom(value);
} }
DynamicValue::Builder DynamicStruct::Builder::init(Text::Reader name) { DynamicValue::Builder DynamicStruct::Builder::init(kj::StringPtr name) {
return initImpl(builder, schema.getMemberByName(name)); return initImpl(builder, schema.getMemberByName(name));
} }
DynamicValue::Builder DynamicStruct::Builder::init(Text::Reader name, uint size) { DynamicValue::Builder DynamicStruct::Builder::init(kj::StringPtr name, uint size) {
return initImpl(builder, schema.getMemberByName(name), size); return initImpl(builder, schema.getMemberByName(name), size);
} }
DynamicStruct::Builder DynamicStruct::Builder::getObject( DynamicStruct::Builder DynamicStruct::Builder::getObject(
Text::Reader name, StructSchema type) { kj::StringPtr name, StructSchema type) {
return getObject(schema.getMemberByName(name), type); return getObject(schema.getMemberByName(name), type);
} }
DynamicList::Builder DynamicStruct::Builder::getObject(Text::Reader name, ListSchema type) { DynamicList::Builder DynamicStruct::Builder::getObject(kj::StringPtr name, ListSchema type) {
return getObject(schema.getMemberByName(name), type); return getObject(schema.getMemberByName(name), type);
} }
Text::Builder DynamicStruct::Builder::getObjectAsText(Text::Reader name) { Text::Builder DynamicStruct::Builder::getObjectAsText(kj::StringPtr name) {
return getObjectAsText(schema.getMemberByName(name)); return getObjectAsText(schema.getMemberByName(name));
} }
Data::Builder DynamicStruct::Builder::getObjectAsData(Text::Reader name) { Data::Builder DynamicStruct::Builder::getObjectAsData(kj::StringPtr name) {
return getObjectAsData(schema.getMemberByName(name)); return getObjectAsData(schema.getMemberByName(name));
} }
DynamicStruct::Builder DynamicStruct::Builder::initObject( DynamicStruct::Builder DynamicStruct::Builder::initObject(
Text::Reader name, StructSchema type) { kj::StringPtr name, StructSchema type) {
return initObject(schema.getMemberByName(name), type); return initObject(schema.getMemberByName(name), type);
} }
DynamicList::Builder DynamicStruct::Builder::initObject( DynamicList::Builder DynamicStruct::Builder::initObject(
Text::Reader name, ListSchema type, uint size) { kj::StringPtr name, ListSchema type, uint size) {
return initObject(schema.getMemberByName(name), type, size); return initObject(schema.getMemberByName(name), type, size);
} }
Text::Builder DynamicStruct::Builder::initObjectAsText(Text::Reader name, uint size) { Text::Builder DynamicStruct::Builder::initObjectAsText(kj::StringPtr name, uint size) {
return initObjectAsText(schema.getMemberByName(name), size); return initObjectAsText(schema.getMemberByName(name), size);
} }
Data::Builder DynamicStruct::Builder::initObjectAsData(Text::Reader name, uint size) { Data::Builder DynamicStruct::Builder::initObjectAsData(kj::StringPtr name, uint size) {
return initObjectAsData(schema.getMemberByName(name), size); return initObjectAsData(schema.getMemberByName(name), size);
} }
...@@ -873,7 +873,8 @@ Data::Builder DynamicStruct::Builder::getObjectAsDataImpl( ...@@ -873,7 +873,8 @@ Data::Builder DynamicStruct::Builder::getObjectAsDataImpl(
} }
void DynamicStruct::Builder::setImpl( void DynamicStruct::Builder::setImpl(
internal::StructBuilder builder, StructSchema::Member member, DynamicValue::Reader value) { internal::StructBuilder builder, StructSchema::Member member,
const DynamicValue::Reader& value) {
switch (member.getProto().getBody().which()) { switch (member.getProto().getBody().which()) {
case schema::StructNode::Member::Body::UNION_MEMBER: { case schema::StructNode::Member::Body::UNION_MEMBER: {
auto src = value.as<DynamicUnion>(); auto src = value.as<DynamicUnion>();
...@@ -1119,7 +1120,7 @@ DynamicValue::Reader DynamicList::Reader::operator[](uint index) const { ...@@ -1119,7 +1120,7 @@ DynamicValue::Reader DynamicList::Reader::operator[](uint index) const {
return nullptr; return nullptr;
} }
DynamicValue::Builder DynamicList::Builder::operator[](uint index) const { DynamicValue::Builder DynamicList::Builder::operator[](uint index) {
KJ_REQUIRE(index < size(), "List index out-of-bounds."); KJ_REQUIRE(index < size(), "List index out-of-bounds.");
switch (schema.whichElementType()) { switch (schema.whichElementType()) {
...@@ -1182,7 +1183,7 @@ DynamicValue::Builder DynamicList::Builder::operator[](uint index) const { ...@@ -1182,7 +1183,7 @@ DynamicValue::Builder DynamicList::Builder::operator[](uint index) const {
return nullptr; return nullptr;
} }
void DynamicList::Builder::set(uint index, DynamicValue::Reader value) { void DynamicList::Builder::set(uint index, const DynamicValue::Reader& value) {
KJ_REQUIRE(index < size(), "List index out-of-bounds.") { KJ_REQUIRE(index < size(), "List index out-of-bounds.") {
return; return;
} }
...@@ -1321,13 +1322,13 @@ void DynamicList::Builder::copyFrom(std::initializer_list<DynamicValue::Reader> ...@@ -1321,13 +1322,13 @@ void DynamicList::Builder::copyFrom(std::initializer_list<DynamicValue::Reader>
} }
} }
DynamicList::Reader DynamicList::Builder::asReader() { DynamicList::Reader DynamicList::Builder::asReader() const {
return DynamicList::Reader(schema, builder.asReader()); return DynamicList::Reader(schema, builder.asReader());
} }
// ======================================================================================= // =======================================================================================
DynamicValue::Reader DynamicValue::Builder::asReader() { DynamicValue::Reader DynamicValue::Builder::asReader() const {
switch (type) { switch (type) {
case UNKNOWN: return Reader(); case UNKNOWN: return Reader();
case VOID: return Reader(voidValue); case VOID: return Reader(voidValue);
...@@ -1399,7 +1400,7 @@ T checkRoundTrip(U value) { ...@@ -1399,7 +1400,7 @@ T checkRoundTrip(U value) {
} // namespace } // namespace
#define HANDLE_NUMERIC_TYPE(typeName, ifInt, ifUint, ifFloat) \ #define HANDLE_NUMERIC_TYPE(typeName, ifInt, ifUint, ifFloat) \
typeName DynamicValue::Reader::AsImpl<typeName>::apply(Reader reader) { \ typeName DynamicValue::Reader::AsImpl<typeName>::apply(const Reader& reader) { \
switch (reader.type) { \ switch (reader.type) { \
case INT: \ case INT: \
return ifInt<typeName>(reader.intValue); \ return ifInt<typeName>(reader.intValue); \
...@@ -1413,7 +1414,7 @@ typeName DynamicValue::Reader::AsImpl<typeName>::apply(Reader reader) { \ ...@@ -1413,7 +1414,7 @@ typeName DynamicValue::Reader::AsImpl<typeName>::apply(Reader reader) { \
} \ } \
} \ } \
} \ } \
typeName DynamicValue::Builder::AsImpl<typeName>::apply(Builder builder) { \ typeName DynamicValue::Builder::AsImpl<typeName>::apply(Builder& builder) { \
switch (builder.type) { \ switch (builder.type) { \
case INT: \ case INT: \
return ifInt<typeName>(builder.intValue); \ return ifInt<typeName>(builder.intValue); \
...@@ -1442,12 +1443,12 @@ HANDLE_NUMERIC_TYPE(double, kj::upcast, kj::upcast, kj::upcast) ...@@ -1442,12 +1443,12 @@ HANDLE_NUMERIC_TYPE(double, kj::upcast, kj::upcast, kj::upcast)
#undef HANDLE_NUMERIC_TYPE #undef HANDLE_NUMERIC_TYPE
#define HANDLE_TYPE(name, discrim, typeName) \ #define HANDLE_TYPE(name, discrim, typeName) \
ReaderFor<typeName> DynamicValue::Reader::AsImpl<typeName>::apply(Reader reader) { \ ReaderFor<typeName> DynamicValue::Reader::AsImpl<typeName>::apply(const Reader& reader) { \
KJ_REQUIRE(reader.type == discrim, \ KJ_REQUIRE(reader.type == discrim, \
"Type mismatch when using DynamicValue::Reader::as()."); \ "Type mismatch when using DynamicValue::Reader::as()."); \
return reader.name##Value; \ return reader.name##Value; \
} \ } \
BuilderFor<typeName> DynamicValue::Builder::AsImpl<typeName>::apply(Builder builder) { \ BuilderFor<typeName> DynamicValue::Builder::AsImpl<typeName>::apply(Builder& builder) { \
KJ_REQUIRE(builder.type == discrim, \ KJ_REQUIRE(builder.type == discrim, \
"Type mismatch when using DynamicValue::Builder::as()."); \ "Type mismatch when using DynamicValue::Builder::as()."); \
return builder.name##Value; \ return builder.name##Value; \
...@@ -1467,13 +1468,13 @@ HANDLE_TYPE(union, UNION, DynamicUnion) ...@@ -1467,13 +1468,13 @@ HANDLE_TYPE(union, UNION, DynamicUnion)
#undef HANDLE_TYPE #undef HANDLE_TYPE
// As in the header, HANDLE_TYPE(void, VOID, Void) crashes GCC 4.7. // As in the header, HANDLE_TYPE(void, VOID, Void) crashes GCC 4.7.
Void DynamicValue::Reader::AsImpl<Void>::apply(Reader reader) { Void DynamicValue::Reader::AsImpl<Void>::apply(const Reader& reader) {
KJ_REQUIRE(reader.type == VOID, "Type mismatch when using DynamicValue::Reader::as().") { KJ_REQUIRE(reader.type == VOID, "Type mismatch when using DynamicValue::Reader::as().") {
return Void(); return Void();
} }
return reader.voidValue; return reader.voidValue;
} }
Void DynamicValue::Builder::AsImpl<Void>::apply(Builder builder) { Void DynamicValue::Builder::AsImpl<Void>::apply(Builder& builder) {
KJ_REQUIRE(builder.type == VOID, "Type mismatch when using DynamicValue::Builder::as().") { KJ_REQUIRE(builder.type == VOID, "Type mismatch when using DynamicValue::Builder::as().") {
return Void(); return Void();
} }
...@@ -1509,7 +1510,7 @@ DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::getDynamic( ...@@ -1509,7 +1510,7 @@ DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::getDynamic(
index, structSizeFromSchema(schema), nullptr)); index, structSizeFromSchema(schema), nullptr));
} }
void PointerHelpers<DynamicStruct, Kind::UNKNOWN>::set( void PointerHelpers<DynamicStruct, Kind::UNKNOWN>::set(
StructBuilder builder, WirePointerCount index, DynamicStruct::Reader value) { StructBuilder builder, WirePointerCount index, const DynamicStruct::Reader& value) {
builder.setStructField(index, value.reader); builder.setStructField(index, value.reader);
} }
DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::init( DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::init(
...@@ -1536,7 +1537,7 @@ DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::getDynamic( ...@@ -1536,7 +1537,7 @@ DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::getDynamic(
} }
} }
void PointerHelpers<DynamicList, Kind::UNKNOWN>::set( void PointerHelpers<DynamicList, Kind::UNKNOWN>::set(
StructBuilder builder, WirePointerCount index, DynamicList::Reader value) { StructBuilder builder, WirePointerCount index, const DynamicList::Reader& value) {
builder.setListField(index, value.reader); builder.setListField(index, value.reader);
} }
DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::init( DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::init(
......
...@@ -109,17 +109,17 @@ public: ...@@ -109,17 +109,17 @@ public:
inline DynamicEnum(T&& value): DynamicEnum(toDynamic(value)) {} inline DynamicEnum(T&& value): DynamicEnum(toDynamic(value)) {}
template <typename T> template <typename T>
inline T as() { return static_cast<T>(asImpl(typeId<T>())); } inline T as() const { return static_cast<T>(asImpl(typeId<T>())); }
// Cast to a native enum type. // Cast to a native enum type.
inline EnumSchema getSchema() { return schema; } inline EnumSchema getSchema() const { return schema; }
kj::Maybe<EnumSchema::Enumerant> getEnumerant(); kj::Maybe<EnumSchema::Enumerant> getEnumerant() const;
// Get which enumerant this enum value represents. Returns nullptr if the numeric value does not // Get which enumerant this enum value represents. Returns nullptr if the numeric value does not
// correspond to any enumerant in the schema -- this can happen if the data was built using a // correspond to any enumerant in the schema -- this can happen if the data was built using a
// newer schema that has more values defined. // newer schema that has more values defined.
inline uint16_t getRaw() { return value; } inline uint16_t getRaw() const { return value; }
// Returns the raw underlying enum value. // Returns the raw underlying enum value.
private: private:
...@@ -129,7 +129,7 @@ private: ...@@ -129,7 +129,7 @@ private:
inline DynamicEnum(EnumSchema schema, uint16_t value) inline DynamicEnum(EnumSchema schema, uint16_t value)
: schema(schema), value(value) {} : schema(schema), value(value) {}
uint16_t asImpl(uint64_t requestedTypeId); uint16_t asImpl(uint64_t requestedTypeId) const;
friend struct DynamicStruct; friend struct DynamicStruct;
friend struct DynamicList; friend struct DynamicList;
...@@ -148,11 +148,11 @@ public: ...@@ -148,11 +148,11 @@ public:
DynamicObject() = default; DynamicObject() = default;
template <typename T> template <typename T>
inline typename T::Reader as() { return AsImpl<T>::apply(*this); } inline typename T::Reader as() const { return AsImpl<T>::apply(*this); }
// Convert the object to the given struct, list, or blob type. // Convert the object to the given struct, list, or blob type.
DynamicStruct::Reader as(StructSchema schema); DynamicStruct::Reader as(StructSchema schema) const;
DynamicList::Reader as(ListSchema schema); DynamicList::Reader as(ListSchema schema) const;
private: private:
internal::ObjectReader reader; internal::ObjectReader reader;
...@@ -173,13 +173,13 @@ class DynamicUnion::Reader { ...@@ -173,13 +173,13 @@ class DynamicUnion::Reader {
public: public:
Reader() = default; Reader() = default;
inline StructSchema::Union getSchema() { return schema; } inline StructSchema::Union getSchema() const { return schema; }
kj::Maybe<StructSchema::Member> which(); kj::Maybe<StructSchema::Member> which() const;
// Returns which field is set, or nullptr if an unknown field is set (i.e. the schema is old, and // Returns which field is set, or nullptr if an unknown field is set (i.e. the schema is old, and
// the underlying data has the union set to a member we don't know about). // the underlying data has the union set to a member we don't know about).
DynamicValue::Reader get(); DynamicValue::Reader get() const;
// Get the value of whichever field of the union is set. Throws an exception if which() returns // Get the value of whichever field of the union is set. Throws an exception if which() returns
// nullptr. // nullptr.
...@@ -200,14 +200,14 @@ class DynamicUnion::Builder { ...@@ -200,14 +200,14 @@ class DynamicUnion::Builder {
public: public:
Builder() = default; Builder() = default;
inline StructSchema::Union getSchema() { return schema; } inline StructSchema::Union getSchema() const { return schema; }
kj::Maybe<StructSchema::Member> which(); kj::Maybe<StructSchema::Member> which();
// Returns which field is set, or nullptr if an unknown field is set (i.e. the schema is old, and // Returns which field is set, or nullptr if an unknown field is set (i.e. the schema is old, and
// the underlying data has the union set to a member we don't know about). // the underlying data has the union set to a member we don't know about).
DynamicValue::Builder get(); DynamicValue::Builder get();
void set(StructSchema::Member member, DynamicValue::Reader value); void set(StructSchema::Member member, const DynamicValue::Reader& value);
DynamicValue::Builder init(StructSchema::Member member); DynamicValue::Builder init(StructSchema::Member member);
DynamicValue::Builder init(StructSchema::Member member, uint size); DynamicValue::Builder init(StructSchema::Member member, uint size);
...@@ -221,16 +221,16 @@ public: ...@@ -221,16 +221,16 @@ public:
Data::Builder initObjectAsData(StructSchema::Member member, uint size); Data::Builder initObjectAsData(StructSchema::Member member, uint size);
// Get/init an "Object" member. Must specify the type. // Get/init an "Object" member. Must specify the type.
void set(Text::Reader name, DynamicValue::Reader value); void set(kj::StringPtr name, const DynamicValue::Reader& value);
DynamicValue::Builder init(Text::Reader member); DynamicValue::Builder init(kj::StringPtr name);
DynamicValue::Builder init(Text::Reader name, uint size); DynamicValue::Builder init(kj::StringPtr name, uint size);
DynamicStruct::Builder initObject(Text::Reader name, StructSchema type); DynamicStruct::Builder initObject(kj::StringPtr name, StructSchema type);
DynamicList::Builder initObject(Text::Reader name, ListSchema type, uint size); DynamicList::Builder initObject(kj::StringPtr name, ListSchema type, uint size);
Text::Builder initObjectAsText(Text::Reader name, uint size); Text::Builder initObjectAsText(kj::StringPtr name, uint size);
Data::Builder initObjectAsData(Text::Reader name, uint size); Data::Builder initObjectAsData(kj::StringPtr name, uint size);
// Convenience methods that identify the member by text name. // Convenience methods that identify the member by text name.
Reader asReader(); Reader asReader() const;
private: private:
StructSchema::Union schema; StructSchema::Union schema;
...@@ -256,21 +256,21 @@ public: ...@@ -256,21 +256,21 @@ public:
inline Reader(T&& value): Reader(toDynamic(value)) {} inline Reader(T&& value): Reader(toDynamic(value)) {}
template <typename T> template <typename T>
typename T::Reader as(); typename T::Reader as() const;
// Convert the dynamic struct to its compiled-in type. // Convert the dynamic struct to its compiled-in type.
inline StructSchema getSchema() { return schema; } inline StructSchema getSchema() const { return schema; }
DynamicValue::Reader get(StructSchema::Member member); DynamicValue::Reader get(StructSchema::Member member) const;
// Read the given member value. // Read the given member value.
bool has(StructSchema::Member member); bool has(StructSchema::Member member) const;
// Tests whether the given member is set to its default value. For pointer values, this does // Tests whether the given member is set to its default value. For pointer values, this does
// not actually traverse the value comparing it with the default, but simply returns true if the // not actually traverse the value comparing it with the default, but simply returns true if the
// pointer is non-null. // pointer is non-null.
DynamicValue::Reader get(Text::Reader name); DynamicValue::Reader get(kj::StringPtr name) const;
bool has(Text::Reader name); bool has(kj::StringPtr name) const;
// Shortcuts to access members by name. These throw exceptions if no such member exists. // Shortcuts to access members by name. These throw exceptions if no such member exists.
private: private:
...@@ -307,7 +307,7 @@ public: ...@@ -307,7 +307,7 @@ public:
typename T::Builder as(); typename T::Builder as();
// Cast to a particular struct type. // Cast to a particular struct type.
inline StructSchema getSchema() { return schema; } inline StructSchema getSchema() const { return schema; }
DynamicValue::Builder get(StructSchema::Member member); DynamicValue::Builder get(StructSchema::Member member);
// Read the given member value. // Read the given member value.
...@@ -317,7 +317,7 @@ public: ...@@ -317,7 +317,7 @@ public:
// not actually traverse the value comparing it with the default, but simply returns true if the // not actually traverse the value comparing it with the default, but simply returns true if the
// pointer is non-null. // pointer is non-null.
void set(StructSchema::Member member, DynamicValue::Reader value); void set(StructSchema::Member member, const DynamicValue::Reader& value);
// Set the given member value. // Set the given member value.
DynamicValue::Builder init(StructSchema::Member member); DynamicValue::Builder init(StructSchema::Member member);
...@@ -336,23 +336,23 @@ public: ...@@ -336,23 +336,23 @@ public:
Data::Builder initObjectAsData(StructSchema::Member member, uint size); Data::Builder initObjectAsData(StructSchema::Member member, uint size);
// Init an object field. You must specify the type. // Init an object field. You must specify the type.
DynamicValue::Builder get(Text::Reader name); DynamicValue::Builder get(kj::StringPtr name);
bool has(Text::Reader name); bool has(kj::StringPtr name);
void set(Text::Reader name, DynamicValue::Reader value); void set(kj::StringPtr name, const DynamicValue::Reader& value);
void set(Text::Reader name, std::initializer_list<DynamicValue::Reader> value); void set(kj::StringPtr name, std::initializer_list<DynamicValue::Reader> value);
DynamicValue::Builder init(Text::Reader name); DynamicValue::Builder init(kj::StringPtr name);
DynamicValue::Builder init(Text::Reader name, uint size); DynamicValue::Builder init(kj::StringPtr name, uint size);
DynamicStruct::Builder getObject(Text::Reader name, StructSchema type); DynamicStruct::Builder getObject(kj::StringPtr name, StructSchema type);
DynamicList::Builder getObject(Text::Reader name, ListSchema type); DynamicList::Builder getObject(kj::StringPtr name, ListSchema type);
Text::Builder getObjectAsText(Text::Reader name); Text::Builder getObjectAsText(kj::StringPtr name);
Data::Builder getObjectAsData(Text::Reader name); Data::Builder getObjectAsData(kj::StringPtr name);
DynamicStruct::Builder initObject(Text::Reader name, StructSchema type); DynamicStruct::Builder initObject(kj::StringPtr name, StructSchema type);
DynamicList::Builder initObject(Text::Reader name, ListSchema type, uint size); DynamicList::Builder initObject(kj::StringPtr name, ListSchema type, uint size);
Text::Builder initObjectAsText(Text::Reader name, uint size); Text::Builder initObjectAsText(kj::StringPtr name, uint size);
Data::Builder initObjectAsData(Text::Reader name, uint size); Data::Builder initObjectAsData(kj::StringPtr name, uint size);
// Shortcuts to access members by name. These throw exceptions if no such member exists. // Shortcuts to access members by name. These throw exceptions if no such member exists.
Reader asReader(); Reader asReader() const;
private: private:
StructSchema schema; StructSchema schema;
...@@ -373,7 +373,8 @@ private: ...@@ -373,7 +373,8 @@ private:
internal::StructBuilder builder, StructSchema::Member field); internal::StructBuilder builder, StructSchema::Member field);
static void setImpl( static void setImpl(
internal::StructBuilder builder, StructSchema::Member member, DynamicValue::Reader value); internal::StructBuilder builder, StructSchema::Member member,
const DynamicValue::Reader& value);
static DynamicValue::Builder initImpl( static DynamicValue::Builder initImpl(
internal::StructBuilder builder, StructSchema::Member member, uint size); internal::StructBuilder builder, StructSchema::Member member, uint size);
...@@ -408,7 +409,7 @@ public: ...@@ -408,7 +409,7 @@ public:
inline Reader(T&& value): Reader(toDynamic(value)) {} inline Reader(T&& value): Reader(toDynamic(value)) {}
template <typename T> template <typename T>
typename T::Reader as(); typename T::Reader as() const;
// Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data // Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data
// can't possibly represent the requested type. // can't possibly represent the requested type.
...@@ -417,7 +418,7 @@ public: ...@@ -417,7 +418,7 @@ public:
inline uint size() const { return reader.size() / ELEMENTS; } inline uint size() const { return reader.size() / ELEMENTS; }
DynamicValue::Reader operator[](uint index) const; DynamicValue::Reader operator[](uint index) const;
typedef internal::IndexingIterator<Reader, DynamicValue::Reader> iterator; typedef internal::IndexingIterator<const Reader, DynamicValue::Reader> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -451,8 +452,8 @@ public: ...@@ -451,8 +452,8 @@ public:
inline ListSchema getSchema() const { return schema; } inline ListSchema getSchema() const { return schema; }
inline uint size() const { return builder.size() / ELEMENTS; } inline uint size() const { return builder.size() / ELEMENTS; }
DynamicValue::Builder operator[](uint index) const; DynamicValue::Builder operator[](uint index);
void set(uint index, DynamicValue::Reader value); void set(uint index, const DynamicValue::Reader& value);
DynamicValue::Builder init(uint index, uint size); DynamicValue::Builder init(uint index, uint size);
typedef internal::IndexingIterator<Builder, DynamicStruct::Builder> iterator; typedef internal::IndexingIterator<Builder, DynamicStruct::Builder> iterator;
...@@ -461,7 +462,7 @@ public: ...@@ -461,7 +462,7 @@ public:
void copyFrom(std::initializer_list<DynamicValue::Reader> value); void copyFrom(std::initializer_list<DynamicValue::Reader> value);
Reader asReader(); Reader asReader() const;
private: private:
ListSchema schema; ListSchema schema;
...@@ -528,19 +529,19 @@ public: ...@@ -528,19 +529,19 @@ public:
inline Reader(float value); inline Reader(float value);
inline Reader(double value); inline Reader(double value);
inline Reader(const char* value); // Text inline Reader(const char* value); // Text
inline Reader(Text::Reader value); inline Reader(const Text::Reader& value);
inline Reader(Data::Reader value); inline Reader(const Data::Reader& value);
inline Reader(DynamicList::Reader value); inline Reader(const DynamicList::Reader& value);
inline Reader(DynamicEnum value); inline Reader(DynamicEnum value);
inline Reader(DynamicStruct::Reader value); inline Reader(const DynamicStruct::Reader& value);
inline Reader(DynamicUnion::Reader value); inline Reader(const DynamicUnion::Reader& value);
inline Reader(DynamicObject value); inline Reader(DynamicObject value);
template <typename T, typename EnableIf = decltype(toDynamic(kj::instance<T>()))> template <typename T, typename EnableIf = decltype(toDynamic(kj::instance<T>()))>
inline Reader(T value): Reader(toDynamic(value)) {} inline Reader(T value): Reader(toDynamic(value)) {}
template <typename T> template <typename T>
inline ReaderFor<T> as() { return AsImpl<T>::apply(*this); } inline ReaderFor<T> as() const { return AsImpl<T>::apply(*this); }
// Use to interpret the value as some Cap'n Proto type. Allowed types are: // Use to interpret the value as some Cap'n Proto type. Allowed types are:
// - Void, bool, [u]int{8,16,32,64}_t, float, double, any enum: Returns the raw value. // - Void, bool, [u]int{8,16,32,64}_t, float, double, any enum: Returns the raw value.
// - Text, Data, any struct type: Returns the corresponding Reader. // - Text, Data, any struct type: Returns the corresponding Reader.
...@@ -561,7 +562,7 @@ public: ...@@ -561,7 +562,7 @@ public:
// //
// Any other conversion attempt will throw an exception. // Any other conversion attempt will throw an exception.
inline Type getType() { return type; } inline Type getType() const { return type; }
// Get the type of this value. // Get the type of this value.
private: private:
...@@ -623,7 +624,7 @@ public: ...@@ -623,7 +624,7 @@ public:
inline Type getType() { return type; } inline Type getType() { return type; }
// Get the type of this value. // Get the type of this value.
Reader asReader(); Reader asReader() const;
private: private:
Type type; Type type;
...@@ -648,16 +649,16 @@ private: ...@@ -648,16 +649,16 @@ private:
// specialization. Has a method apply() which does the work. // specialization. Has a method apply() which does the work.
}; };
kj::String KJ_STRINGIFY(DynamicValue::Reader value); kj::String KJ_STRINGIFY(const DynamicValue::Reader& value);
kj::String KJ_STRINGIFY(DynamicValue::Builder value); kj::String KJ_STRINGIFY(const DynamicValue::Builder& value);
kj::String KJ_STRINGIFY(DynamicEnum value); kj::String KJ_STRINGIFY(DynamicEnum value);
kj::String KJ_STRINGIFY(DynamicObject value); kj::String KJ_STRINGIFY(const DynamicObject& value);
kj::String KJ_STRINGIFY(DynamicUnion::Reader value); kj::String KJ_STRINGIFY(const DynamicUnion::Reader& value);
kj::String KJ_STRINGIFY(DynamicUnion::Builder value); kj::String KJ_STRINGIFY(const DynamicUnion::Builder& value);
kj::String KJ_STRINGIFY(DynamicStruct::Reader value); kj::String KJ_STRINGIFY(const DynamicStruct::Reader& value);
kj::String KJ_STRINGIFY(DynamicStruct::Builder value); kj::String KJ_STRINGIFY(const DynamicStruct::Builder& value);
kj::String KJ_STRINGIFY(DynamicList::Reader value); kj::String KJ_STRINGIFY(const DynamicList::Reader& value);
kj::String KJ_STRINGIFY(DynamicList::Builder value); kj::String KJ_STRINGIFY(const DynamicList::Builder& value);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Inject the ability to use DynamicStruct for message roots and Dynamic{Struct,List} for // Inject the ability to use DynamicStruct for message roots and Dynamic{Struct,List} for
...@@ -682,7 +683,7 @@ struct PointerHelpers<DynamicStruct, Kind::UNKNOWN> { ...@@ -682,7 +683,7 @@ struct PointerHelpers<DynamicStruct, Kind::UNKNOWN> {
static DynamicStruct::Builder getDynamic( static DynamicStruct::Builder getDynamic(
StructBuilder builder, WirePointerCount index, StructSchema schema); StructBuilder builder, WirePointerCount index, StructSchema schema);
static void set( static void set(
StructBuilder builder, WirePointerCount index, DynamicStruct::Reader value); StructBuilder builder, WirePointerCount index, const DynamicStruct::Reader& value);
static DynamicStruct::Builder init( static DynamicStruct::Builder init(
StructBuilder builder, WirePointerCount index, StructSchema schema); StructBuilder builder, WirePointerCount index, StructSchema schema);
}; };
...@@ -697,7 +698,7 @@ struct PointerHelpers<DynamicList, Kind::UNKNOWN> { ...@@ -697,7 +698,7 @@ struct PointerHelpers<DynamicList, Kind::UNKNOWN> {
static DynamicList::Builder getDynamic( static DynamicList::Builder getDynamic(
StructBuilder builder, WirePointerCount index, ListSchema schema); StructBuilder builder, WirePointerCount index, ListSchema schema);
static void set( static void set(
StructBuilder builder, WirePointerCount index, DynamicList::Reader value); StructBuilder builder, WirePointerCount index, const DynamicList::Reader& value);
static DynamicList::Builder init( static DynamicList::Builder init(
StructBuilder builder, WirePointerCount index, ListSchema schema, uint size); StructBuilder builder, WirePointerCount index, ListSchema schema, uint size);
}; };
...@@ -709,20 +710,20 @@ struct PointerHelpers<DynamicList, Kind::UNKNOWN> { ...@@ -709,20 +710,20 @@ struct PointerHelpers<DynamicList, Kind::UNKNOWN> {
template <typename T> template <typename T>
struct ToDynamic_<T, Kind::STRUCT> { struct ToDynamic_<T, Kind::STRUCT> {
static inline DynamicStruct::Reader apply(typename T::Reader value) { static inline DynamicStruct::Reader apply(const typename T::Reader& value) {
return DynamicStruct::Reader(Schema::from<T>(), value._reader); return DynamicStruct::Reader(Schema::from<T>(), value._reader);
} }
static inline DynamicStruct::Builder apply(typename T::Builder value) { static inline DynamicStruct::Builder apply(typename T::Builder& value) {
return DynamicStruct::Builder(Schema::from<T>(), value._builder); return DynamicStruct::Builder(Schema::from<T>(), value._builder);
} }
}; };
template <typename T> template <typename T>
struct ToDynamic_<T, Kind::LIST> { struct ToDynamic_<T, Kind::LIST> {
static inline DynamicList::Reader apply(typename T::Reader value) { static inline DynamicList::Reader apply(const typename T::Reader& value) {
return DynamicList::Reader(Schema::from<T>(), value.reader); return DynamicList::Reader(Schema::from<T>(), value.reader);
} }
static inline DynamicList::Builder apply(typename T::Builder value) { static inline DynamicList::Builder apply(typename T::Builder& value) {
return DynamicList::Builder(Schema::from<T>(), value.builder); return DynamicList::Builder(Schema::from<T>(), value.builder);
} }
}; };
...@@ -769,7 +770,7 @@ CAPNPROTO_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicObject, OBJECT, object); ...@@ -769,7 +770,7 @@ CAPNPROTO_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicObject, OBJECT, object);
#undef CAPNPROTO_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR #undef CAPNPROTO_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR
#define CAPNPROTO_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \ #define CAPNPROTO_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \
inline DynamicValue::Reader::Reader(cppType::Reader value) \ inline DynamicValue::Reader::Reader(const cppType::Reader& value) \
: type(typeTag), fieldName##Value(value) {} \ : type(typeTag), fieldName##Value(value) {} \
inline DynamicValue::Builder::Builder(cppType::Builder value) \ inline DynamicValue::Builder::Builder(cppType::Builder value) \
: type(typeTag), fieldName##Value(value) {} : type(typeTag), fieldName##Value(value) {}
...@@ -787,11 +788,11 @@ inline DynamicValue::Reader::Reader(const char* value): Reader(Text::Reader(valu ...@@ -787,11 +788,11 @@ inline DynamicValue::Reader::Reader(const char* value): Reader(Text::Reader(valu
#define CAPNPROTO_DECLARE_TYPE(name, discrim, typeName) \ #define CAPNPROTO_DECLARE_TYPE(name, discrim, typeName) \
template <> \ template <> \
struct DynamicValue::Reader::AsImpl<typeName> { \ struct DynamicValue::Reader::AsImpl<typeName> { \
static ReaderFor<typeName> apply(Reader reader); \ static ReaderFor<typeName> apply(const Reader& reader); \
}; \ }; \
template <> \ template <> \
struct DynamicValue::Builder::AsImpl<typeName> { \ struct DynamicValue::Builder::AsImpl<typeName> { \
static BuilderFor<typeName> apply(Builder builder); \ static BuilderFor<typeName> apply(Builder& builder); \
}; };
//CAPNPROTO_DECLARE_TYPE(void, VOID, Void) //CAPNPROTO_DECLARE_TYPE(void, VOID, Void)
...@@ -820,48 +821,48 @@ CAPNPROTO_DECLARE_TYPE(union, UNION, DynamicUnion) ...@@ -820,48 +821,48 @@ CAPNPROTO_DECLARE_TYPE(union, UNION, DynamicUnion)
// ReaderFor<> and BuilderFor<> wrappers, it works. // ReaderFor<> and BuilderFor<> wrappers, it works.
template <> template <>
struct DynamicValue::Reader::AsImpl<Void> { struct DynamicValue::Reader::AsImpl<Void> {
static Void apply(Reader reader); static Void apply(const Reader& reader);
}; };
template <> template <>
struct DynamicValue::Builder::AsImpl<Void> { struct DynamicValue::Builder::AsImpl<Void> {
static Void apply(Builder builder); static Void apply(Builder& builder);
}; };
template <typename T> template <typename T>
struct DynamicValue::Reader::AsImpl<T, Kind::ENUM> { struct DynamicValue::Reader::AsImpl<T, Kind::ENUM> {
static T apply(Reader reader) { static T apply(const Reader& reader) {
return reader.as<DynamicEnum>().as<T>(); return reader.as<DynamicEnum>().as<T>();
} }
}; };
template <typename T> template <typename T>
struct DynamicValue::Builder::AsImpl<T, Kind::ENUM> { struct DynamicValue::Builder::AsImpl<T, Kind::ENUM> {
static T apply(Builder builder) { static T apply(Builder& builder) {
return builder.as<DynamicEnum>().as<T>(); return builder.as<DynamicEnum>().as<T>();
} }
}; };
template <typename T> template <typename T>
struct DynamicValue::Reader::AsImpl<T, Kind::STRUCT> { struct DynamicValue::Reader::AsImpl<T, Kind::STRUCT> {
static typename T::Reader apply(Reader reader) { static typename T::Reader apply(const Reader& reader) {
return reader.as<DynamicStruct>().as<T>(); return reader.as<DynamicStruct>().as<T>();
} }
}; };
template <typename T> template <typename T>
struct DynamicValue::Builder::AsImpl<T, Kind::STRUCT> { struct DynamicValue::Builder::AsImpl<T, Kind::STRUCT> {
static typename T::Builder apply(Builder builder) { static typename T::Builder apply(Builder& builder) {
return builder.as<DynamicStruct>().as<T>(); return builder.as<DynamicStruct>().as<T>();
} }
}; };
template <typename T> template <typename T>
struct DynamicValue::Reader::AsImpl<T, Kind::LIST> { struct DynamicValue::Reader::AsImpl<T, Kind::LIST> {
static typename T::Reader apply(Reader reader) { static typename T::Reader apply(const Reader& reader) {
return reader.as<DynamicList>().as<T>(); return reader.as<DynamicList>().as<T>();
} }
}; };
template <typename T> template <typename T>
struct DynamicValue::Builder::AsImpl<T, Kind::LIST> { struct DynamicValue::Builder::AsImpl<T, Kind::LIST> {
static typename T::Builder apply(Builder builder) { static typename T::Builder apply(Builder& builder) {
return builder.as<DynamicList>().as<T>(); return builder.as<DynamicList>().as<T>();
} }
}; };
...@@ -884,14 +885,14 @@ struct DynamicObject::AsImpl<T, Kind::LIST> { ...@@ -884,14 +885,14 @@ struct DynamicObject::AsImpl<T, Kind::LIST> {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
inline DynamicUnion::Reader DynamicUnion::Builder::asReader() { inline DynamicUnion::Reader DynamicUnion::Builder::asReader() const {
return DynamicUnion::Reader(schema, builder.asReader()); return DynamicUnion::Reader(schema, builder.asReader());
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
template <typename T> template <typename T>
typename T::Reader DynamicStruct::Reader::as() { typename T::Reader DynamicStruct::Reader::as() const {
static_assert(kind<T>() == Kind::STRUCT, static_assert(kind<T>() == Kind::STRUCT,
"DynamicStruct::Reader::as<T>() can only convert to struct types."); "DynamicStruct::Reader::as<T>() can only convert to struct types.");
schema.requireUsableAs<T>(); schema.requireUsableAs<T>();
...@@ -906,7 +907,7 @@ typename T::Builder DynamicStruct::Builder::as() { ...@@ -906,7 +907,7 @@ typename T::Builder DynamicStruct::Builder::as() {
} }
template <> template <>
inline DynamicStruct::Reader DynamicStruct::Reader::as<DynamicStruct>() { inline DynamicStruct::Reader DynamicStruct::Reader::as<DynamicStruct>() const {
return *this; return *this;
} }
template <> template <>
...@@ -914,14 +915,14 @@ inline DynamicStruct::Builder DynamicStruct::Builder::as<DynamicStruct>() { ...@@ -914,14 +915,14 @@ inline DynamicStruct::Builder DynamicStruct::Builder::as<DynamicStruct>() {
return *this; return *this;
} }
inline DynamicStruct::Reader DynamicStruct::Builder::asReader() { inline DynamicStruct::Reader DynamicStruct::Builder::asReader() const {
return DynamicStruct::Reader(schema, builder.asReader()); return DynamicStruct::Reader(schema, builder.asReader());
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
template <typename T> template <typename T>
typename T::Reader DynamicList::Reader::as() { typename T::Reader DynamicList::Reader::as() const {
static_assert(kind<T>() == Kind::LIST, static_assert(kind<T>() == Kind::LIST,
"DynamicStruct::Reader::as<T>() can only convert to list types."); "DynamicStruct::Reader::as<T>() can only convert to list types.");
schema.requireUsableAs<T>(); schema.requireUsableAs<T>();
...@@ -936,7 +937,7 @@ typename T::Builder DynamicList::Builder::as() { ...@@ -936,7 +937,7 @@ typename T::Builder DynamicList::Builder::as() {
} }
template <> template <>
inline DynamicList::Reader DynamicList::Reader::as<DynamicList>() { inline DynamicList::Reader DynamicList::Reader::as<DynamicList>() const {
return *this; return *this;
} }
template <> template <>
......
...@@ -1825,89 +1825,89 @@ StructBuilder StructBuilder::getRoot( ...@@ -1825,89 +1825,89 @@ StructBuilder StructBuilder::getRoot(
} }
StructBuilder StructBuilder::initStructField( StructBuilder StructBuilder::initStructField(
WirePointerCount ptrIndex, StructSize size) const { WirePointerCount ptrIndex, StructSize size) {
return WireHelpers::initStructPointer(pointers + ptrIndex, segment, size); return WireHelpers::initStructPointer(pointers + ptrIndex, segment, size);
} }
StructBuilder StructBuilder::getStructField( StructBuilder StructBuilder::getStructField(
WirePointerCount ptrIndex, StructSize size, const word* defaultValue) const { WirePointerCount ptrIndex, StructSize size, const word* defaultValue) {
return WireHelpers::getWritableStructPointer( return WireHelpers::getWritableStructPointer(
pointers + ptrIndex, segment, size, defaultValue); pointers + ptrIndex, segment, size, defaultValue);
} }
ListBuilder StructBuilder::initListField( ListBuilder StructBuilder::initListField(
WirePointerCount ptrIndex, FieldSize elementSize, ElementCount elementCount) const { WirePointerCount ptrIndex, FieldSize elementSize, ElementCount elementCount) {
return WireHelpers::initListPointer( return WireHelpers::initListPointer(
pointers + ptrIndex, segment, pointers + ptrIndex, segment,
elementCount, elementSize); elementCount, elementSize);
} }
ListBuilder StructBuilder::initStructListField( ListBuilder StructBuilder::initStructListField(
WirePointerCount ptrIndex, ElementCount elementCount, StructSize elementSize) const { WirePointerCount ptrIndex, ElementCount elementCount, StructSize elementSize) {
return WireHelpers::initStructListPointer( return WireHelpers::initStructListPointer(
pointers + ptrIndex, segment, elementCount, elementSize); pointers + ptrIndex, segment, elementCount, elementSize);
} }
ListBuilder StructBuilder::getListField( ListBuilder StructBuilder::getListField(
WirePointerCount ptrIndex, FieldSize elementSize, const word* defaultValue) const { WirePointerCount ptrIndex, FieldSize elementSize, const word* defaultValue) {
return WireHelpers::getWritableListPointer( return WireHelpers::getWritableListPointer(
pointers + ptrIndex, segment, elementSize, defaultValue); pointers + ptrIndex, segment, elementSize, defaultValue);
} }
ListBuilder StructBuilder::getStructListField( ListBuilder StructBuilder::getStructListField(
WirePointerCount ptrIndex, StructSize elementSize, const word* defaultValue) const { WirePointerCount ptrIndex, StructSize elementSize, const word* defaultValue) {
return WireHelpers::getWritableStructListPointer( return WireHelpers::getWritableStructListPointer(
pointers + ptrIndex, segment, elementSize, defaultValue); pointers + ptrIndex, segment, elementSize, defaultValue);
} }
template <> template <>
Text::Builder StructBuilder::initBlobField<Text>(WirePointerCount ptrIndex, ByteCount size) const { Text::Builder StructBuilder::initBlobField<Text>(WirePointerCount ptrIndex, ByteCount size) {
return WireHelpers::initTextPointer(pointers + ptrIndex, segment, size); return WireHelpers::initTextPointer(pointers + ptrIndex, segment, size);
} }
template <> template <>
void StructBuilder::setBlobField<Text>(WirePointerCount ptrIndex, Text::Reader value) const { void StructBuilder::setBlobField<Text>(WirePointerCount ptrIndex, Text::Reader value) {
WireHelpers::setTextPointer(pointers + ptrIndex, segment, value); WireHelpers::setTextPointer(pointers + ptrIndex, segment, value);
} }
template <> template <>
Text::Builder StructBuilder::getBlobField<Text>( Text::Builder StructBuilder::getBlobField<Text>(
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const { WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) {
return WireHelpers::getWritableTextPointer( return WireHelpers::getWritableTextPointer(
pointers + ptrIndex, segment, defaultValue, defaultSize); pointers + ptrIndex, segment, defaultValue, defaultSize);
} }
template <> template <>
Data::Builder StructBuilder::initBlobField<Data>(WirePointerCount ptrIndex, ByteCount size) const { Data::Builder StructBuilder::initBlobField<Data>(WirePointerCount ptrIndex, ByteCount size) {
return WireHelpers::initDataPointer(pointers + ptrIndex, segment, size); return WireHelpers::initDataPointer(pointers + ptrIndex, segment, size);
} }
template <> template <>
void StructBuilder::setBlobField<Data>(WirePointerCount ptrIndex, Data::Reader value) const { void StructBuilder::setBlobField<Data>(WirePointerCount ptrIndex, Data::Reader value) {
WireHelpers::setDataPointer(pointers + ptrIndex, segment, value); WireHelpers::setDataPointer(pointers + ptrIndex, segment, value);
} }
template <> template <>
Data::Builder StructBuilder::getBlobField<Data>( Data::Builder StructBuilder::getBlobField<Data>(
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const { WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) {
return WireHelpers::getWritableDataPointer( return WireHelpers::getWritableDataPointer(
pointers + ptrIndex, segment, defaultValue, defaultSize); pointers + ptrIndex, segment, defaultValue, defaultSize);
} }
ObjectBuilder StructBuilder::getObjectField( ObjectBuilder StructBuilder::getObjectField(
WirePointerCount ptrIndex, const word* defaultValue) const { WirePointerCount ptrIndex, const word* defaultValue) {
return WireHelpers::getWritableObjectPointer(segment, pointers + ptrIndex, defaultValue); return WireHelpers::getWritableObjectPointer(segment, pointers + ptrIndex, defaultValue);
} }
void StructBuilder::setStructField(WirePointerCount ptrIndex, StructReader value) const { void StructBuilder::setStructField(WirePointerCount ptrIndex, StructReader value) {
return WireHelpers::setStructPointer(segment, pointers + ptrIndex, value); return WireHelpers::setStructPointer(segment, pointers + ptrIndex, value);
} }
void StructBuilder::setListField(WirePointerCount ptrIndex, ListReader value) const { void StructBuilder::setListField(WirePointerCount ptrIndex, ListReader value) {
return WireHelpers::setListPointer(segment, pointers + ptrIndex, value); return WireHelpers::setListPointer(segment, pointers + ptrIndex, value);
} }
void StructBuilder::setObjectField(WirePointerCount ptrIndex, ObjectReader value) const { void StructBuilder::setObjectField(WirePointerCount ptrIndex, ObjectReader value) {
return WireHelpers::setObjectPointer(segment, pointers + ptrIndex, value); return WireHelpers::setObjectPointer(segment, pointers + ptrIndex, value);
} }
bool StructBuilder::isPointerFieldNull(WirePointerCount ptrIndex) const { bool StructBuilder::isPointerFieldNull(WirePointerCount ptrIndex) {
return (pointers + ptrIndex)->isNull(); return (pointers + ptrIndex)->isNull();
} }
...@@ -2027,7 +2027,7 @@ Data::Builder ListBuilder::asData() { ...@@ -2027,7 +2027,7 @@ Data::Builder ListBuilder::asData() {
return Data::Builder(reinterpret_cast<byte*>(ptr), elementCount / ELEMENTS); return Data::Builder(reinterpret_cast<byte*>(ptr), elementCount / ELEMENTS);
} }
StructBuilder ListBuilder::getStructElement(ElementCount index) const { StructBuilder ListBuilder::getStructElement(ElementCount index) {
BitCount64 indexBit = ElementCount64(index) * step; BitCount64 indexBit = ElementCount64(index) * step;
byte* structData = ptr + indexBit / BITS_PER_BYTE; byte* structData = ptr + indexBit / BITS_PER_BYTE;
return StructBuilder(segment, structData, return StructBuilder(segment, structData,
...@@ -2036,75 +2036,75 @@ StructBuilder ListBuilder::getStructElement(ElementCount index) const { ...@@ -2036,75 +2036,75 @@ StructBuilder ListBuilder::getStructElement(ElementCount index) const {
} }
ListBuilder ListBuilder::initListElement( ListBuilder ListBuilder::initListElement(
ElementCount index, FieldSize elementSize, ElementCount elementCount) const { ElementCount index, FieldSize elementSize, ElementCount elementCount) {
return WireHelpers::initListPointer( return WireHelpers::initListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE),
segment, elementCount, elementSize); segment, elementCount, elementSize);
} }
ListBuilder ListBuilder::initStructListElement( ListBuilder ListBuilder::initStructListElement(
ElementCount index, ElementCount elementCount, StructSize elementSize) const { ElementCount index, ElementCount elementCount, StructSize elementSize) {
return WireHelpers::initStructListPointer( return WireHelpers::initStructListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE),
segment, elementCount, elementSize); segment, elementCount, elementSize);
} }
ListBuilder ListBuilder::getListElement(ElementCount index, FieldSize elementSize) const { ListBuilder ListBuilder::getListElement(ElementCount index, FieldSize elementSize) {
return WireHelpers::getWritableListPointer( return WireHelpers::getWritableListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment,
elementSize, nullptr); elementSize, nullptr);
} }
ListBuilder ListBuilder::getStructListElement(ElementCount index, StructSize elementSize) const { ListBuilder ListBuilder::getStructListElement(ElementCount index, StructSize elementSize) {
return WireHelpers::getWritableStructListPointer( return WireHelpers::getWritableStructListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment,
elementSize, nullptr); elementSize, nullptr);
} }
template <> template <>
Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) const { Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) {
return WireHelpers::initTextPointer( return WireHelpers::initTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size); reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size);
} }
template <> template <>
void ListBuilder::setBlobElement<Text>(ElementCount index, Text::Reader value) const { void ListBuilder::setBlobElement<Text>(ElementCount index, Text::Reader value) {
WireHelpers::setTextPointer( WireHelpers::setTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value); reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value);
} }
template <> template <>
Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) const { Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) {
return WireHelpers::getWritableTextPointer( return WireHelpers::getWritableTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, "", 0 * BYTES); reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, "", 0 * BYTES);
} }
template <> template <>
Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) const { Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) {
return WireHelpers::initDataPointer( return WireHelpers::initDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size); reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size);
} }
template <> template <>
void ListBuilder::setBlobElement<Data>(ElementCount index, Data::Reader value) const { void ListBuilder::setBlobElement<Data>(ElementCount index, Data::Reader value) {
WireHelpers::setDataPointer( WireHelpers::setDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value); reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value);
} }
template <> template <>
Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) const { Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) {
return WireHelpers::getWritableDataPointer( return WireHelpers::getWritableDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr,
0 * BYTES); 0 * BYTES);
} }
ObjectBuilder ListBuilder::getObjectElement(ElementCount index) const { ObjectBuilder ListBuilder::getObjectElement(ElementCount index) {
return WireHelpers::getWritableObjectPointer( return WireHelpers::getWritableObjectPointer(
segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), nullptr); segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), nullptr);
} }
void ListBuilder::setListElement(ElementCount index, ListReader value) const { void ListBuilder::setListElement(ElementCount index, ListReader value) {
return WireHelpers::setListPointer( return WireHelpers::setListPointer(
segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), value); segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), value);
} }
void ListBuilder::setObjectElement(ElementCount index, ObjectReader value) const { void ListBuilder::setObjectElement(ElementCount index, ObjectReader value) {
return WireHelpers::setObjectPointer( return WireHelpers::setObjectPointer(
segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), value); segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), value);
} }
......
...@@ -51,6 +51,16 @@ class SegmentBuilder; ...@@ -51,6 +51,16 @@ class SegmentBuilder;
// ============================================================================= // =============================================================================
struct DisallowConstCopy {
DisallowConstCopy() = default;
DisallowConstCopy(DisallowConstCopy&);
DisallowConstCopy(DisallowConstCopy&&) = default;
DisallowConstCopy& operator=(DisallowConstCopy&);
DisallowConstCopy& operator=(DisallowConstCopy&&) = default;
};
inline DisallowConstCopy::DisallowConstCopy(DisallowConstCopy&) = default;
inline DisallowConstCopy& DisallowConstCopy::operator=(DisallowConstCopy&) = default;
enum class FieldSize: uint8_t { enum class FieldSize: uint8_t {
// TODO(cleanup): Rename to FieldLayout or maybe ValueLayout. // TODO(cleanup): Rename to FieldLayout or maybe ValueLayout.
...@@ -290,7 +300,7 @@ private: ...@@ -290,7 +300,7 @@ private:
T value; T value;
}; };
class StructBuilder { class StructBuilder: public DisallowConstCopy {
public: public:
inline StructBuilder(): segment(nullptr), data(nullptr), pointers(nullptr), bit0Offset(0) {} inline StructBuilder(): segment(nullptr), data(nullptr), pointers(nullptr), bit0Offset(0) {}
...@@ -303,85 +313,85 @@ public: ...@@ -303,85 +313,85 @@ public:
inline Data::Builder getDataSectionAsBlob(); inline Data::Builder getDataSectionAsBlob();
template <typename T> template <typename T>
KJ_ALWAYS_INLINE(T getDataField(ElementCount offset) const); KJ_ALWAYS_INLINE(T getDataField(ElementCount offset));
// Gets the data field value of the given type at the given offset. The offset is measured in // Gets the data field value of the given type at the given offset. The offset is measured in
// multiples of the field size, determined by the type. // multiples of the field size, determined by the type.
template <typename T> template <typename T>
KJ_ALWAYS_INLINE(T getDataField(ElementCount offset, Mask<T> mask) const); KJ_ALWAYS_INLINE(T getDataField(ElementCount offset, Mask<T> mask));
// Like getDataField() but applies the given XOR mask to the data on load. Used for reading // Like getDataField() but applies the given XOR mask to the data on load. Used for reading
// fields with non-zero default values. // fields with non-zero default values.
template <typename T> template <typename T>
KJ_ALWAYS_INLINE(void setDataField( KJ_ALWAYS_INLINE(void setDataField(
ElementCount offset, kj::NoInfer<T> value) const); ElementCount offset, kj::NoInfer<T> value));
// Sets the data field value at the given offset. // Sets the data field value at the given offset.
template <typename T> template <typename T>
KJ_ALWAYS_INLINE(void setDataField( KJ_ALWAYS_INLINE(void setDataField(
ElementCount offset, kj::NoInfer<T> value, Mask<T> mask) const); ElementCount offset, kj::NoInfer<T> value, Mask<T> mask));
// Like setDataField() but applies the given XOR mask before storing. Used for writing fields // Like setDataField() but applies the given XOR mask before storing. Used for writing fields
// with non-zero default values. // with non-zero default values.
StructBuilder initStructField(WirePointerCount ptrIndex, StructSize size) const; StructBuilder initStructField(WirePointerCount ptrIndex, StructSize size);
// Initializes the struct field at the given index in the pointer segment. If it is already // Initializes the struct field at the given index in the pointer segment. If it is already
// initialized, the previous value is discarded or overwritten. The struct is initialized to // initialized, the previous value is discarded or overwritten. The struct is initialized to
// the type's default state (all-zero). Use getStructField() if you want the struct to be // the type's default state (all-zero). Use getStructField() if you want the struct to be
// initialized as a copy of the field's default value (which may have non-null pointers). // initialized as a copy of the field's default value (which may have non-null pointers).
StructBuilder getStructField(WirePointerCount ptrIndex, StructSize size, StructBuilder getStructField(WirePointerCount ptrIndex, StructSize size,
const word* defaultValue) const; const word* defaultValue);
// Gets the struct field at the given index in the pointer segment. If the field is not already // Gets the struct field at the given index in the pointer segment. If the field is not already
// initialized, it is initialized as a deep copy of the given default value (a flat message), // initialized, it is initialized as a deep copy of the given default value (a flat message),
// or to the empty state if defaultValue is nullptr. // or to the empty state if defaultValue is nullptr.
ListBuilder initListField(WirePointerCount ptrIndex, FieldSize elementSize, ListBuilder initListField(WirePointerCount ptrIndex, FieldSize elementSize,
ElementCount elementCount) const; ElementCount elementCount);
// Allocates a new list of the given size for the field at the given index in the pointer // Allocates a new list of the given size for the field at the given index in the pointer
// segment, and return a pointer to it. All elements are initialized to zero. // segment, and return a pointer to it. All elements are initialized to zero.
ListBuilder initStructListField(WirePointerCount ptrIndex, ElementCount elementCount, ListBuilder initStructListField(WirePointerCount ptrIndex, ElementCount elementCount,
StructSize size) const; StructSize size);
// Allocates a new list of the given size for the field at the given index in the pointer // Allocates a new list of the given size for the field at the given index in the pointer
// segment, and return a pointer to it. Each element is initialized to its empty state. // segment, and return a pointer to it. Each element is initialized to its empty state.
ListBuilder getListField(WirePointerCount ptrIndex, FieldSize elementSize, ListBuilder getListField(WirePointerCount ptrIndex, FieldSize elementSize,
const word* defaultValue) const; const word* defaultValue);
// Gets the already-allocated list field for the given pointer index, ensuring that the list is // Gets the already-allocated list field for the given pointer index, ensuring that the list is
// suitable for storing non-struct elements of the given size. If the list is not already // suitable for storing non-struct elements of the given size. If the list is not already
// allocated, it is allocated as a deep copy of the given default value (a flat message). If // allocated, it is allocated as a deep copy of the given default value (a flat message). If
// the default value is null, an empty list is used. // the default value is null, an empty list is used.
ListBuilder getStructListField(WirePointerCount ptrIndex, StructSize elementSize, ListBuilder getStructListField(WirePointerCount ptrIndex, StructSize elementSize,
const word* defaultValue) const; const word* defaultValue);
// Gets the already-allocated list field for the given pointer index, ensuring that the list // Gets the already-allocated list field for the given pointer index, ensuring that the list
// is suitable for storing struct elements of the given size. If the list is not // is suitable for storing struct elements of the given size. If the list is not
// already allocated, it is allocated as a deep copy of the given default value (a flat // already allocated, it is allocated as a deep copy of the given default value (a flat
// message). If the default value is null, an empty list is used. // message). If the default value is null, an empty list is used.
template <typename T> template <typename T>
typename T::Builder initBlobField(WirePointerCount ptrIndex, ByteCount size) const; typename T::Builder initBlobField(WirePointerCount ptrIndex, ByteCount size);
// Initialize a Text or Data field to the given size in bytes (not including NUL terminator for // Initialize a Text or Data field to the given size in bytes (not including NUL terminator for
// Text) and return a Text::Builder which can be used to fill in the content. // Text) and return a Text::Builder which can be used to fill in the content.
template <typename T> template <typename T>
void setBlobField(WirePointerCount ptrIndex, typename T::Reader value) const; void setBlobField(WirePointerCount ptrIndex, typename T::Reader value);
// Set the blob field to a copy of the given blob. // Set the blob field to a copy of the given blob.
template <typename T> template <typename T>
typename T::Builder getBlobField(WirePointerCount ptrIndex, typename T::Builder getBlobField(WirePointerCount ptrIndex,
const void* defaultValue, ByteCount defaultSize) const; const void* defaultValue, ByteCount defaultSize);
// Get the blob field. If it is not initialized, initialize it to a copy of the given default. // Get the blob field. If it is not initialized, initialize it to a copy of the given default.
ObjectBuilder getObjectField(WirePointerCount ptrIndex, const word* defaultValue) const; ObjectBuilder getObjectField(WirePointerCount ptrIndex, const word* defaultValue);
// Read a pointer of arbitrary type. // Read a pointer of arbitrary type.
void setStructField(WirePointerCount ptrIndex, StructReader value) const; void setStructField(WirePointerCount ptrIndex, StructReader value);
void setListField(WirePointerCount ptrIndex, ListReader value) const; void setListField(WirePointerCount ptrIndex, ListReader value);
void setObjectField(WirePointerCount ptrIndex, ObjectReader value) const; void setObjectField(WirePointerCount ptrIndex, ObjectReader value);
// Sets a pointer field to a deep copy of the given value. // Sets a pointer field to a deep copy of the given value.
bool isPointerFieldNull(WirePointerCount ptrIndex) const; bool isPointerFieldNull(WirePointerCount ptrIndex);
StructReader asReader() const; StructReader asReader() const;
// Gets a StructReader pointing at the same memory. // Gets a StructReader pointing at the same memory.
...@@ -510,7 +520,7 @@ private: ...@@ -510,7 +520,7 @@ private:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
class ListBuilder { class ListBuilder: public DisallowConstCopy {
public: public:
inline ListBuilder() inline ListBuilder()
: segment(nullptr), ptr(nullptr), elementCount(0 * ELEMENTS), : segment(nullptr), ptr(nullptr), elementCount(0 * ELEMENTS),
...@@ -524,55 +534,55 @@ public: ...@@ -524,55 +534,55 @@ public:
// Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized. // Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized.
template <typename T> template <typename T>
KJ_ALWAYS_INLINE(T getDataElement(ElementCount index) const); KJ_ALWAYS_INLINE(T getDataElement(ElementCount index));
// Get the element of the given type at the given index. // Get the element of the given type at the given index.
template <typename T> template <typename T>
KJ_ALWAYS_INLINE(void setDataElement( KJ_ALWAYS_INLINE(void setDataElement(
ElementCount index, kj::NoInfer<T> value) const); ElementCount index, kj::NoInfer<T> value));
// Set the element at the given index. // Set the element at the given index.
StructBuilder getStructElement(ElementCount index) const; StructBuilder getStructElement(ElementCount index);
// Get the struct element at the given index. // Get the struct element at the given index.
ListBuilder initListElement( ListBuilder initListElement(
ElementCount index, FieldSize elementSize, ElementCount elementCount) const; ElementCount index, FieldSize elementSize, ElementCount elementCount);
// Create a new list element of the given size at the given index. All elements are initialized // Create a new list element of the given size at the given index. All elements are initialized
// to zero. // to zero.
ListBuilder initStructListElement(ElementCount index, ElementCount elementCount, ListBuilder initStructListElement(ElementCount index, ElementCount elementCount,
StructSize size) const; StructSize size);
// Allocates a new list of the given size for the field at the given index in the pointer // Allocates a new list of the given size for the field at the given index in the pointer
// segment, and return a pointer to it. Each element is initialized to its empty state. // segment, and return a pointer to it. Each element is initialized to its empty state.
ListBuilder getListElement(ElementCount index, FieldSize elementSize) const; ListBuilder getListElement(ElementCount index, FieldSize elementSize);
// Get the existing list element at the given index, making sure it is suitable for storing // Get the existing list element at the given index, making sure it is suitable for storing
// non-struct elements of the given size. Returns an empty list if the element is not // non-struct elements of the given size. Returns an empty list if the element is not
// initialized. // initialized.
ListBuilder getStructListElement(ElementCount index, StructSize elementSize) const; ListBuilder getStructListElement(ElementCount index, StructSize elementSize);
// Get the existing list element at the given index, making sure it is suitable for storing // Get the existing list element at the given index, making sure it is suitable for storing
// struct elements of the given size. Returns an empty list if the element is not // struct elements of the given size. Returns an empty list if the element is not
// initialized. // initialized.
template <typename T> template <typename T>
typename T::Builder initBlobElement(ElementCount index, ByteCount size) const; typename T::Builder initBlobElement(ElementCount index, ByteCount size);
// Initialize a Text or Data element to the given size in bytes (not including NUL terminator for // Initialize a Text or Data element to the given size in bytes (not including NUL terminator for
// Text) and return a Text::Builder which can be used to fill in the content. // Text) and return a Text::Builder which can be used to fill in the content.
template <typename T> template <typename T>
void setBlobElement(ElementCount index, typename T::Reader value) const; void setBlobElement(ElementCount index, typename T::Reader value);
// Set the blob element to a copy of the given blob. // Set the blob element to a copy of the given blob.
template <typename T> template <typename T>
typename T::Builder getBlobElement(ElementCount index) const; typename T::Builder getBlobElement(ElementCount index);
// Get the blob element. If it is not initialized, return an empty blob builder. // Get the blob element. If it is not initialized, return an empty blob builder.
ObjectBuilder getObjectElement(ElementCount index) const; ObjectBuilder getObjectElement(ElementCount index);
// Gets a pointer element of arbitrary type. // Gets a pointer element of arbitrary type.
void setListElement(ElementCount index, ListReader value) const; void setListElement(ElementCount index, ListReader value);
void setObjectElement(ElementCount index, ObjectReader value) const; void setObjectElement(ElementCount index, ObjectReader value);
// Sets a pointer element to a deep copy of the given value. // Sets a pointer element to a deep copy of the given value.
ListReader asReader() const; ListReader asReader() const;
...@@ -716,12 +726,12 @@ inline Data::Builder StructBuilder::getDataSectionAsBlob() { ...@@ -716,12 +726,12 @@ inline Data::Builder StructBuilder::getDataSectionAsBlob() {
} }
template <typename T> template <typename T>
inline T StructBuilder::getDataField(ElementCount offset) const { inline T StructBuilder::getDataField(ElementCount offset) {
return reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].get(); return reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].get();
} }
template <> template <>
inline bool StructBuilder::getDataField<bool>(ElementCount offset) const { inline bool StructBuilder::getDataField<bool>(ElementCount offset) {
// This branch should be compiled out whenever this is inlined with a constant offset. // This branch should be compiled out whenever this is inlined with a constant offset.
BitCount boffset = (offset == 0 * ELEMENTS) ? BitCount boffset = (offset == 0 * ELEMENTS) ?
BitCount(bit0Offset) : offset * (1 * BITS / ELEMENTS); BitCount(bit0Offset) : offset * (1 * BITS / ELEMENTS);
...@@ -730,23 +740,22 @@ inline bool StructBuilder::getDataField<bool>(ElementCount offset) const { ...@@ -730,23 +740,22 @@ inline bool StructBuilder::getDataField<bool>(ElementCount offset) const {
} }
template <> template <>
inline Void StructBuilder::getDataField<Void>(ElementCount offset) const { inline Void StructBuilder::getDataField<Void>(ElementCount offset) {
return Void::VOID; return Void::VOID;
} }
template <typename T> template <typename T>
inline T StructBuilder::getDataField(ElementCount offset, Mask<T> mask) const { inline T StructBuilder::getDataField(ElementCount offset, Mask<T> mask) {
return unmask<T>(getDataField<Mask<T> >(offset), mask); return unmask<T>(getDataField<Mask<T> >(offset), mask);
} }
template <typename T> template <typename T>
inline void StructBuilder::setDataField( inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value) {
ElementCount offset, kj::NoInfer<T> value) const {
reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].set(value); reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].set(value);
} }
template <> template <>
inline void StructBuilder::setDataField<bool>(ElementCount offset, bool value) const { inline void StructBuilder::setDataField<bool>(ElementCount offset, bool value) {
// This branch should be compiled out whenever this is inlined with a constant offset. // This branch should be compiled out whenever this is inlined with a constant offset.
BitCount boffset = (offset == 0 * ELEMENTS) ? BitCount boffset = (offset == 0 * ELEMENTS) ?
BitCount(bit0Offset) : offset * (1 * BITS / ELEMENTS); BitCount(bit0Offset) : offset * (1 * BITS / ELEMENTS);
...@@ -757,11 +766,10 @@ inline void StructBuilder::setDataField<bool>(ElementCount offset, bool value) c ...@@ -757,11 +766,10 @@ inline void StructBuilder::setDataField<bool>(ElementCount offset, bool value) c
} }
template <> template <>
inline void StructBuilder::setDataField<Void>(ElementCount offset, Void value) const {} inline void StructBuilder::setDataField<Void>(ElementCount offset, Void value) {}
template <typename T> template <typename T>
inline void StructBuilder::setDataField( inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value, Mask<T> m) {
ElementCount offset, kj::NoInfer<T> value, Mask<T> m) const {
setDataField<Mask<T> >(offset, mask<T>(value, m)); setDataField<Mask<T> >(offset, mask<T>(value, m));
} }
...@@ -810,7 +818,7 @@ T StructReader::getDataField(ElementCount offset, Mask<T> mask) const { ...@@ -810,7 +818,7 @@ T StructReader::getDataField(ElementCount offset, Mask<T> mask) const {
inline ElementCount ListBuilder::size() const { return elementCount; } inline ElementCount ListBuilder::size() const { return elementCount; }
template <typename T> template <typename T>
inline T ListBuilder::getDataElement(ElementCount index) const { inline T ListBuilder::getDataElement(ElementCount index) {
return reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get(); return reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get();
// TODO(soon): Benchmark this alternate implementation, which I suspect may make better use of // TODO(soon): Benchmark this alternate implementation, which I suspect may make better use of
...@@ -822,7 +830,7 @@ inline T ListBuilder::getDataElement(ElementCount index) const { ...@@ -822,7 +830,7 @@ inline T ListBuilder::getDataElement(ElementCount index) const {
} }
template <> template <>
inline bool ListBuilder::getDataElement<bool>(ElementCount index) const { inline bool ListBuilder::getDataElement<bool>(ElementCount index) {
// Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists. // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists.
BitCount bindex = index * step; BitCount bindex = index * step;
byte* b = ptr + bindex / BITS_PER_BYTE; byte* b = ptr + bindex / BITS_PER_BYTE;
...@@ -830,17 +838,17 @@ inline bool ListBuilder::getDataElement<bool>(ElementCount index) const { ...@@ -830,17 +838,17 @@ inline bool ListBuilder::getDataElement<bool>(ElementCount index) const {
} }
template <> template <>
inline Void ListBuilder::getDataElement<Void>(ElementCount index) const { inline Void ListBuilder::getDataElement<Void>(ElementCount index) {
return Void::VOID; return Void::VOID;
} }
template <typename T> template <typename T>
inline void ListBuilder::setDataElement(ElementCount index, kj::NoInfer<T> value) const { inline void ListBuilder::setDataElement(ElementCount index, kj::NoInfer<T> value) {
reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->set(value); reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->set(value);
} }
template <> template <>
inline void ListBuilder::setDataElement<bool>(ElementCount index, bool value) const { inline void ListBuilder::setDataElement<bool>(ElementCount index, bool value) {
// Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists. // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists.
BitCount bindex = index * (1 * BITS / ELEMENTS); BitCount bindex = index * (1 * BITS / ELEMENTS);
byte* b = ptr + bindex / BITS_PER_BYTE; byte* b = ptr + bindex / BITS_PER_BYTE;
...@@ -850,7 +858,7 @@ inline void ListBuilder::setDataElement<bool>(ElementCount index, bool value) co ...@@ -850,7 +858,7 @@ inline void ListBuilder::setDataElement<bool>(ElementCount index, bool value) co
} }
template <> template <>
inline void ListBuilder::setDataElement<Void>(ElementCount index, Void value) const {} inline void ListBuilder::setDataElement<Void>(ElementCount index, Void value) {}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
...@@ -875,22 +883,22 @@ inline Void ListReader::getDataElement<Void>(ElementCount index) const { ...@@ -875,22 +883,22 @@ inline Void ListReader::getDataElement<Void>(ElementCount index) const {
} }
// These are defined in the source file. // These are defined in the source file.
template <> typename Text::Builder StructBuilder::initBlobField<Text>(WirePointerCount ptrIndex, ByteCount size) const; template <> typename Text::Builder StructBuilder::initBlobField<Text>(WirePointerCount ptrIndex, ByteCount size);
template <> void StructBuilder::setBlobField<Text>(WirePointerCount ptrIndex, typename Text::Reader value) const; template <> void StructBuilder::setBlobField<Text>(WirePointerCount ptrIndex, typename Text::Reader value);
template <> typename Text::Builder StructBuilder::getBlobField<Text>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const; template <> typename Text::Builder StructBuilder::getBlobField<Text>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize);
template <> typename Text::Reader StructReader::getBlobField<Text>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const; template <> typename Text::Reader StructReader::getBlobField<Text>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) const; template <> typename Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size);
template <> void ListBuilder::setBlobElement<Text>(ElementCount index, typename Text::Reader value) const; template <> void ListBuilder::setBlobElement<Text>(ElementCount index, typename Text::Reader value);
template <> typename Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) const; template <> typename Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index);
template <> typename Text::Reader ListReader::getBlobElement<Text>(ElementCount index) const; template <> typename Text::Reader ListReader::getBlobElement<Text>(ElementCount index) const;
template <> typename Data::Builder StructBuilder::initBlobField<Data>(WirePointerCount ptrIndex, ByteCount size) const; template <> typename Data::Builder StructBuilder::initBlobField<Data>(WirePointerCount ptrIndex, ByteCount size);
template <> void StructBuilder::setBlobField<Data>(WirePointerCount ptrIndex, typename Data::Reader value) const; template <> void StructBuilder::setBlobField<Data>(WirePointerCount ptrIndex, typename Data::Reader value);
template <> typename Data::Builder StructBuilder::getBlobField<Data>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const; template <> typename Data::Builder StructBuilder::getBlobField<Data>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize);
template <> typename Data::Reader StructReader::getBlobField<Data>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const; template <> typename Data::Reader StructReader::getBlobField<Data>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) const; template <> typename Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size);
template <> void ListBuilder::setBlobElement<Data>(ElementCount index, typename Data::Reader value) const; template <> void ListBuilder::setBlobElement<Data>(ElementCount index, typename Data::Reader value);
template <> typename Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) const; template <> typename Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index);
template <> typename Data::Reader ListReader::getBlobElement<Data>(ElementCount index) const; template <> typename Data::Reader ListReader::getBlobElement<Data>(ElementCount index) const;
} // namespace internal } // namespace internal
......
...@@ -167,11 +167,11 @@ public: ...@@ -167,11 +167,11 @@ public:
inline bool operator> (const IndexingIterator& other) const { return index > other.index; } inline bool operator> (const IndexingIterator& other) const { return index > other.index; }
private: private:
const Container* container; Container* container;
uint index; uint index;
friend Container; friend Container;
inline IndexingIterator(const Container* container, uint index) inline IndexingIterator(Container* container, uint index)
: container(container), index(index) {} : container(container), index(index) {}
}; };
...@@ -193,7 +193,7 @@ struct List<T, Kind::PRIMITIVE> { ...@@ -193,7 +193,7 @@ struct List<T, Kind::PRIMITIVE> {
return reader.template getDataElement<T>(index * ELEMENTS); return reader.template getDataElement<T>(index * ELEMENTS);
} }
typedef internal::IndexingIterator<Reader, T> iterator; typedef internal::IndexingIterator<const Reader, T> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -216,7 +216,7 @@ struct List<T, Kind::PRIMITIVE> { ...@@ -216,7 +216,7 @@ struct List<T, Kind::PRIMITIVE> {
inline Reader asReader() { return Reader(builder.asReader()); } inline Reader asReader() { return Reader(builder.asReader()); }
inline uint size() const { return builder.size() / ELEMENTS; } inline uint size() const { return builder.size() / ELEMENTS; }
inline T operator[](uint index) const { inline T operator[](uint index) {
return builder.template getDataElement<T>(index * ELEMENTS); return builder.template getDataElement<T>(index * ELEMENTS);
} }
inline void set(uint index, T value) { inline void set(uint index, T value) {
...@@ -230,8 +230,8 @@ struct List<T, Kind::PRIMITIVE> { ...@@ -230,8 +230,8 @@ struct List<T, Kind::PRIMITIVE> {
} }
typedef internal::IndexingIterator<Builder, T> iterator; typedef internal::IndexingIterator<Builder, T> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() { return iterator(this, size()); }
private: private:
internal::ListBuilder builder; internal::ListBuilder builder;
...@@ -239,12 +239,12 @@ struct List<T, Kind::PRIMITIVE> { ...@@ -239,12 +239,12 @@ struct List<T, Kind::PRIMITIVE> {
private: private:
inline static internal::ListBuilder initAsElementOf( inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) { internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement( return builder.initListElement(
index * ELEMENTS, internal::FieldSizeForType<T>::value, size * ELEMENTS); index * ELEMENTS, internal::FieldSizeForType<T>::value, size * ELEMENTS);
} }
inline static internal::ListBuilder getAsElementOf( inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) { internal::ListBuilder& builder, uint index) {
return builder.getListElement(index * ELEMENTS, internal::FieldSizeForType<T>::value); return builder.getListElement(index * ELEMENTS, internal::FieldSizeForType<T>::value);
} }
inline static internal::ListReader getAsElementOf( inline static internal::ListReader getAsElementOf(
...@@ -261,7 +261,7 @@ private: ...@@ -261,7 +261,7 @@ private:
return builder.getListField(index, internal::FieldSizeForType<T>::value, defaultValue); return builder.getListField(index, internal::FieldSizeForType<T>::value, defaultValue);
} }
inline static internal::ListReader getAsFieldOf( inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) { const internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSizeForType<T>::value, defaultValue); return reader.getListField(index, internal::FieldSizeForType<T>::value, defaultValue);
} }
...@@ -290,7 +290,7 @@ struct List<T, Kind::STRUCT> { ...@@ -290,7 +290,7 @@ struct List<T, Kind::STRUCT> {
return typename T::Reader(reader.getStructElement(index * ELEMENTS)); return typename T::Reader(reader.getStructElement(index * ELEMENTS));
} }
typedef internal::IndexingIterator<Reader, typename T::Reader> iterator; typedef internal::IndexingIterator<const Reader, typename T::Reader> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -313,7 +313,7 @@ struct List<T, Kind::STRUCT> { ...@@ -313,7 +313,7 @@ struct List<T, Kind::STRUCT> {
inline Reader asReader() { return Reader(builder.asReader()); } inline Reader asReader() { return Reader(builder.asReader()); }
inline uint size() const { return builder.size() / ELEMENTS; } inline uint size() const { return builder.size() / ELEMENTS; }
inline typename T::Builder operator[](uint index) const { inline typename T::Builder operator[](uint index) {
return typename T::Builder(builder.getStructElement(index * ELEMENTS)); return typename T::Builder(builder.getStructElement(index * ELEMENTS));
} }
...@@ -323,8 +323,8 @@ struct List<T, Kind::STRUCT> { ...@@ -323,8 +323,8 @@ struct List<T, Kind::STRUCT> {
// of teh protocol. // of teh protocol.
typedef internal::IndexingIterator<Builder, typename T::Builder> iterator; typedef internal::IndexingIterator<Builder, typename T::Builder> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() { return iterator(this, size()); }
private: private:
internal::ListBuilder builder; internal::ListBuilder builder;
...@@ -332,12 +332,12 @@ struct List<T, Kind::STRUCT> { ...@@ -332,12 +332,12 @@ struct List<T, Kind::STRUCT> {
private: private:
inline static internal::ListBuilder initAsElementOf( inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) { internal::ListBuilder& builder, uint index, uint size) {
return builder.initStructListElement( return builder.initStructListElement(
index * ELEMENTS, size * ELEMENTS, internal::structSize<T>()); index * ELEMENTS, size * ELEMENTS, internal::structSize<T>());
} }
inline static internal::ListBuilder getAsElementOf( inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) { internal::ListBuilder& builder, uint index) {
return builder.getStructListElement(index * ELEMENTS, internal::structSize<T>()); return builder.getStructListElement(index * ELEMENTS, internal::structSize<T>());
} }
inline static internal::ListReader getAsElementOf( inline static internal::ListReader getAsElementOf(
...@@ -354,7 +354,7 @@ private: ...@@ -354,7 +354,7 @@ private:
return builder.getStructListField(index, internal::structSize<T>(), defaultValue); return builder.getStructListField(index, internal::structSize<T>(), defaultValue);
} }
inline static internal::ListReader getAsFieldOf( inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) { const internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::INLINE_COMPOSITE, defaultValue); return reader.getListField(index, internal::FieldSize::INLINE_COMPOSITE, defaultValue);
} }
...@@ -380,7 +380,7 @@ struct List<List<T>, Kind::LIST> { ...@@ -380,7 +380,7 @@ struct List<List<T>, Kind::LIST> {
return typename List<T>::Reader(List<T>::getAsElementOf(reader, index)); return typename List<T>::Reader(List<T>::getAsElementOf(reader, index));
} }
typedef internal::IndexingIterator<Reader, typename List<T>::Reader> iterator; typedef internal::IndexingIterator<const Reader, typename List<T>::Reader> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -403,7 +403,7 @@ struct List<List<T>, Kind::LIST> { ...@@ -403,7 +403,7 @@ struct List<List<T>, Kind::LIST> {
inline Reader asReader() { return Reader(builder.asReader()); } inline Reader asReader() { return Reader(builder.asReader()); }
inline uint size() const { return builder.size() / ELEMENTS; } inline uint size() const { return builder.size() / ELEMENTS; }
inline typename List<T>::Builder operator[](uint index) const { inline typename List<T>::Builder operator[](uint index) {
return typename List<T>::Builder(List<T>::getAsElementOf(builder, index)); return typename List<T>::Builder(List<T>::getAsElementOf(builder, index));
} }
inline typename List<T>::Builder init(uint index, uint size) { inline typename List<T>::Builder init(uint index, uint size) {
...@@ -421,8 +421,8 @@ struct List<List<T>, Kind::LIST> { ...@@ -421,8 +421,8 @@ struct List<List<T>, Kind::LIST> {
} }
typedef internal::IndexingIterator<Builder, typename List<T>::Builder> iterator; typedef internal::IndexingIterator<Builder, typename List<T>::Builder> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() { return iterator(this, size()); }
private: private:
internal::ListBuilder builder; internal::ListBuilder builder;
...@@ -430,12 +430,12 @@ struct List<List<T>, Kind::LIST> { ...@@ -430,12 +430,12 @@ struct List<List<T>, Kind::LIST> {
private: private:
inline static internal::ListBuilder initAsElementOf( inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) { internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement( return builder.initListElement(
index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS); index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS);
} }
inline static internal::ListBuilder getAsElementOf( inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) { internal::ListBuilder& builder, uint index) {
return builder.getListElement(index * ELEMENTS, internal::FieldSize::POINTER); return builder.getListElement(index * ELEMENTS, internal::FieldSize::POINTER);
} }
inline static internal::ListReader getAsElementOf( inline static internal::ListReader getAsElementOf(
...@@ -452,7 +452,7 @@ private: ...@@ -452,7 +452,7 @@ private:
return builder.getListField(index, internal::FieldSize::POINTER, defaultValue); return builder.getListField(index, internal::FieldSize::POINTER, defaultValue);
} }
inline static internal::ListReader getAsFieldOf( inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) { const internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::POINTER, defaultValue); return reader.getListField(index, internal::FieldSize::POINTER, defaultValue);
} }
...@@ -476,7 +476,7 @@ struct List<T, Kind::BLOB> { ...@@ -476,7 +476,7 @@ struct List<T, Kind::BLOB> {
return reader.getBlobElement<T>(index * ELEMENTS); return reader.getBlobElement<T>(index * ELEMENTS);
} }
typedef internal::IndexingIterator<Reader, typename T::Reader> iterator; typedef internal::IndexingIterator<const Reader, typename T::Reader> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -499,7 +499,7 @@ struct List<T, Kind::BLOB> { ...@@ -499,7 +499,7 @@ struct List<T, Kind::BLOB> {
inline Reader asReader() { return Reader(builder.asReader()); } inline Reader asReader() { return Reader(builder.asReader()); }
inline uint size() const { return builder.size() / ELEMENTS; } inline uint size() const { return builder.size() / ELEMENTS; }
inline typename T::Builder operator[](uint index) const { inline typename T::Builder operator[](uint index) {
return builder.getBlobElement<T>(index * ELEMENTS); return builder.getBlobElement<T>(index * ELEMENTS);
} }
inline void set(uint index, typename T::Reader value) { inline void set(uint index, typename T::Reader value) {
...@@ -510,8 +510,8 @@ struct List<T, Kind::BLOB> { ...@@ -510,8 +510,8 @@ struct List<T, Kind::BLOB> {
} }
typedef internal::IndexingIterator<Builder, typename T::Builder> iterator; typedef internal::IndexingIterator<Builder, typename T::Builder> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() { return iterator(this, size()); }
private: private:
internal::ListBuilder builder; internal::ListBuilder builder;
...@@ -519,12 +519,12 @@ struct List<T, Kind::BLOB> { ...@@ -519,12 +519,12 @@ struct List<T, Kind::BLOB> {
private: private:
inline static internal::ListBuilder initAsElementOf( inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) { internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement( return builder.initListElement(
index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS); index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS);
} }
inline static internal::ListBuilder getAsElementOf( inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) { internal::ListBuilder& builder, uint index) {
return builder.getListElement(index * ELEMENTS, internal::FieldSize::POINTER); return builder.getListElement(index * ELEMENTS, internal::FieldSize::POINTER);
} }
inline static internal::ListReader getAsElementOf( inline static internal::ListReader getAsElementOf(
...@@ -541,7 +541,7 @@ private: ...@@ -541,7 +541,7 @@ private:
return builder.getListField(index, internal::FieldSize::POINTER, defaultValue); return builder.getListField(index, internal::FieldSize::POINTER, defaultValue);
} }
inline static internal::ListReader getAsFieldOf( inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) { const internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::POINTER, defaultValue); return reader.getListField(index, internal::FieldSize::POINTER, defaultValue);
} }
......
...@@ -36,11 +36,11 @@ class SchemaLoader::Impl { ...@@ -36,11 +36,11 @@ class SchemaLoader::Impl {
public: public:
Impl(); Impl();
internal::RawSchema* load(schema::Node::Reader reader); internal::RawSchema* load(const schema::Node::Reader& reader);
internal::RawSchema* loadNative(const internal::RawSchema* nativeSchema); internal::RawSchema* loadNative(const internal::RawSchema* nativeSchema);
internal::RawSchema* loadEmpty(uint64_t id, Text::Reader name, schema::Node::Body::Which kind); internal::RawSchema* loadEmpty(uint64_t id, kj::StringPtr name, schema::Node::Body::Which kind);
// Create a dummy empty schema of the given kind for the given id and load it. // Create a dummy empty schema of the given kind for the given id and load it.
internal::RawSchema* tryGet(uint64_t typeId) const; internal::RawSchema* tryGet(uint64_t typeId) const;
...@@ -79,7 +79,7 @@ class SchemaLoader::Validator { ...@@ -79,7 +79,7 @@ class SchemaLoader::Validator {
public: public:
Validator(SchemaLoader::Impl& loader): loader(loader) {} Validator(SchemaLoader::Impl& loader): loader(loader) {}
bool validate(schema::Node::Reader node) { bool validate(const schema::Node::Reader& node) {
isValid = true; isValid = true;
nodeName = node.getDisplayName(); nodeName = node.getDisplayName();
dependencies.clear(); dependencies.clear();
...@@ -149,11 +149,11 @@ private: ...@@ -149,11 +149,11 @@ private:
#define FAIL_VALIDATE_SCHEMA(...) \ #define FAIL_VALIDATE_SCHEMA(...) \
KJ_FAIL_REQUIRE(__VA_ARGS__) { isValid = false; return; } KJ_FAIL_REQUIRE(__VA_ARGS__) { isValid = false; return; }
void validate(schema::FileNode::Reader fileNode) { void validate(const schema::FileNode::Reader& fileNode) {
// Nothing needs validation. // Nothing needs validation.
} }
void validate(schema::StructNode::Reader structNode) { void validate(const schema::StructNode::Reader& structNode) {
uint dataSizeInBits; uint dataSizeInBits;
uint pointerCount; uint pointerCount;
...@@ -223,13 +223,13 @@ private: ...@@ -223,13 +223,13 @@ private:
} }
} }
void validateMemberName(Text::Reader name, uint unionIndex, uint index) { void validateMemberName(kj::StringPtr name, uint unionIndex, uint index) {
bool isNewName = members.insert(std::make_pair( bool isNewName = members.insert(std::make_pair(
std::pair<uint, Text::Reader>(unionIndex, name), index)).second; std::pair<uint, Text::Reader>(unionIndex, name), index)).second;
VALIDATE_SCHEMA(isNewName, "duplicate name", name); VALIDATE_SCHEMA(isNewName, "duplicate name", name);
} }
void validate(schema::StructNode::Member::Reader member, void validate(const schema::StructNode::Member::Reader& member,
kj::ArrayPtr<bool> sawCodeOrder, kj::ArrayPtr<bool> sawOrdinal, kj::ArrayPtr<bool> sawCodeOrder, kj::ArrayPtr<bool> sawOrdinal,
uint dataSizeInBits, uint pointerCount, uint dataSizeInBits, uint pointerCount,
uint unionIndex, uint index) { uint unionIndex, uint index) {
...@@ -282,7 +282,7 @@ private: ...@@ -282,7 +282,7 @@ private:
} }
} }
void validate(schema::EnumNode::Reader enumNode) { void validate(const schema::EnumNode::Reader& enumNode) {
auto enumerants = enumNode.getEnumerants(); auto enumerants = enumNode.getEnumerants();
KJ_STACK_ARRAY(bool, sawCodeOrder, enumerants.size(), 32, 256); KJ_STACK_ARRAY(bool, sawCodeOrder, enumerants.size(), 32, 256);
...@@ -299,7 +299,7 @@ private: ...@@ -299,7 +299,7 @@ private:
} }
} }
void validate(schema::InterfaceNode::Reader interfaceNode) { void validate(const schema::InterfaceNode::Reader& interfaceNode) {
auto methods = interfaceNode.getMethods(); auto methods = interfaceNode.getMethods();
KJ_STACK_ARRAY(bool, sawCodeOrder, methods.size(), 32, 256); KJ_STACK_ARRAY(bool, sawCodeOrder, methods.size(), 32, 256);
...@@ -329,17 +329,17 @@ private: ...@@ -329,17 +329,17 @@ private:
} }
} }
void validate(schema::ConstNode::Reader constNode) { void validate(const schema::ConstNode::Reader& constNode) {
uint dummy1; uint dummy1;
bool dummy2; bool dummy2;
validate(constNode.getType(), constNode.getValue(), &dummy1, &dummy2); validate(constNode.getType(), constNode.getValue(), &dummy1, &dummy2);
} }
void validate(schema::AnnotationNode::Reader annotationNode) { void validate(const schema::AnnotationNode::Reader& annotationNode) {
validate(annotationNode.getType()); validate(annotationNode.getType());
} }
void validate(schema::Type::Reader type, schema::Value::Reader value, void validate(const schema::Type::Reader& type, const schema::Value::Reader& value,
uint* dataSizeInBits, bool* isPointer) { uint* dataSizeInBits, bool* isPointer) {
validate(type); validate(type);
...@@ -379,7 +379,7 @@ private: ...@@ -379,7 +379,7 @@ private:
} }
} }
void validate(schema::Type::Reader type) { void validate(const schema::Type::Reader& type) {
switch (type.getBody().which()) { switch (type.getBody().which()) {
case schema::Type::Body::VOID_TYPE: case schema::Type::Body::VOID_TYPE:
case schema::Type::Body::BOOL_TYPE: case schema::Type::Body::BOOL_TYPE:
...@@ -442,7 +442,8 @@ class SchemaLoader::CompatibilityChecker { ...@@ -442,7 +442,8 @@ class SchemaLoader::CompatibilityChecker {
public: public:
CompatibilityChecker(SchemaLoader::Impl& loader): loader(loader) {} CompatibilityChecker(SchemaLoader::Impl& loader): loader(loader) {}
bool shouldReplace(schema::Node::Reader existingNode, schema::Node::Reader replacement, bool shouldReplace(const schema::Node::Reader& existingNode,
const schema::Node::Reader& replacement,
bool replacementIsNative) { bool replacementIsNative) {
KJ_CONTEXT("checking compatibility with previously-loaded node of the same id", KJ_CONTEXT("checking compatibility with previously-loaded node of the same id",
existingNode.getDisplayName()); existingNode.getDisplayName());
...@@ -508,7 +509,8 @@ private: ...@@ -508,7 +509,8 @@ private:
} }
} }
void checkCompatibility(schema::Node::Reader node, schema::Node::Reader replacement) { void checkCompatibility(const schema::Node::Reader& node,
const schema::Node::Reader& replacement) {
// Returns whether `replacement` is equivalent, older than, newer than, or incompatible with // Returns whether `replacement` is equivalent, older than, newer than, or incompatible with
// `node`. If exceptions are enabled, this will throw an exception on INCOMPATIBLE. // `node`. If exceptions are enabled, this will throw an exception on INCOMPATIBLE.
...@@ -547,12 +549,13 @@ private: ...@@ -547,12 +549,13 @@ private:
} }
} }
void checkCompatibility(schema::FileNode::Reader file, schema::FileNode::Reader replacement) { void checkCompatibility(const schema::FileNode::Reader& file,
const schema::FileNode::Reader& replacement) {
// Nothing to compare. // Nothing to compare.
} }
void checkCompatibility(schema::StructNode::Reader structNode, void checkCompatibility(const schema::StructNode::Reader& structNode,
schema::StructNode::Reader replacement) { const schema::StructNode::Reader& replacement) {
if (replacement.getDataSectionWordSize() > structNode.getDataSectionWordSize()) { if (replacement.getDataSectionWordSize() > structNode.getDataSectionWordSize()) {
replacementIsNewer(); replacementIsNewer();
} else if (replacement.getDataSectionWordSize() < structNode.getDataSectionWordSize()) { } else if (replacement.getDataSectionWordSize() < structNode.getDataSectionWordSize()) {
...@@ -591,8 +594,8 @@ private: ...@@ -591,8 +594,8 @@ private:
} }
} }
void checkCompatibility(schema::StructNode::Member::Reader member, void checkCompatibility(const schema::StructNode::Member::Reader& member,
schema::StructNode::Member::Reader replacement) { const schema::StructNode::Member::Reader& replacement) {
KJ_CONTEXT("comparing struct member", member.getName()); KJ_CONTEXT("comparing struct member", member.getName());
switch (member.getBody().which()) { switch (member.getBody().which()) {
...@@ -634,8 +637,8 @@ private: ...@@ -634,8 +637,8 @@ private:
} }
} }
void checkCompatibility(schema::EnumNode::Reader enumNode, void checkCompatibility(const schema::EnumNode::Reader& enumNode,
schema::EnumNode::Reader replacement) { const schema::EnumNode::Reader& replacement) {
uint size = enumNode.getEnumerants().size(); uint size = enumNode.getEnumerants().size();
uint replacementSize = replacement.getEnumerants().size(); uint replacementSize = replacement.getEnumerants().size();
if (replacementSize > size) { if (replacementSize > size) {
...@@ -645,8 +648,8 @@ private: ...@@ -645,8 +648,8 @@ private:
} }
} }
void checkCompatibility(schema::InterfaceNode::Reader interfaceNode, void checkCompatibility(const schema::InterfaceNode::Reader& interfaceNode,
schema::InterfaceNode::Reader replacement) { const schema::InterfaceNode::Reader& replacement) {
auto methods = interfaceNode.getMethods(); auto methods = interfaceNode.getMethods();
auto replacementMethods = replacement.getMethods(); auto replacementMethods = replacement.getMethods();
...@@ -663,8 +666,8 @@ private: ...@@ -663,8 +666,8 @@ private:
} }
} }
void checkCompatibility(schema::InterfaceNode::Method::Reader method, void checkCompatibility(const schema::InterfaceNode::Method::Reader& method,
schema::InterfaceNode::Method::Reader replacement) { const schema::InterfaceNode::Method::Reader& replacement) {
KJ_CONTEXT("comparing method", method.getName()); KJ_CONTEXT("comparing method", method.getName());
auto params = method.getParams(); auto params = method.getParams();
...@@ -703,13 +706,13 @@ private: ...@@ -703,13 +706,13 @@ private:
ALLOW_UPGRADE_TO_STRUCT); ALLOW_UPGRADE_TO_STRUCT);
} }
void checkCompatibility(schema::ConstNode::Reader constNode, void checkCompatibility(const schema::ConstNode::Reader& constNode,
schema::ConstNode::Reader replacement) { const schema::ConstNode::Reader& replacement) {
// Who cares? These don't appear on the wire. // Who cares? These don't appear on the wire.
} }
void checkCompatibility(schema::AnnotationNode::Reader annotationNode, void checkCompatibility(const schema::AnnotationNode::Reader& annotationNode,
schema::AnnotationNode::Reader replacement) { const schema::AnnotationNode::Reader& replacement) {
// Who cares? These don't appear on the wire. // Who cares? These don't appear on the wire.
} }
...@@ -718,8 +721,8 @@ private: ...@@ -718,8 +721,8 @@ private:
NO_UPGRADE_TO_STRUCT NO_UPGRADE_TO_STRUCT
}; };
void checkCompatibility(schema::Type::Reader type, void checkCompatibility(const schema::Type::Reader& type,
schema::Type::Reader replacement, const schema::Type::Reader& replacement,
UpgradeToStructMode upgradeToStructMode) { UpgradeToStructMode upgradeToStructMode) {
if (replacement.getBody().which() != type.getBody().which()) { if (replacement.getBody().which() != type.getBody().which()) {
// Check for allowed "upgrade" to Data or Object. // Check for allowed "upgrade" to Data or Object.
...@@ -773,9 +776,10 @@ private: ...@@ -773,9 +776,10 @@ private:
return; return;
case schema::Type::Body::LIST_TYPE: case schema::Type::Body::LIST_TYPE:
return checkCompatibility(type.getBody().getListType(), checkCompatibility(type.getBody().getListType(),
replacement.getBody().getListType(), replacement.getBody().getListType(),
ALLOW_UPGRADE_TO_STRUCT); ALLOW_UPGRADE_TO_STRUCT);
return;
case schema::Type::Body::ENUM_TYPE: case schema::Type::Body::ENUM_TYPE:
VALIDATE_SCHEMA(replacement.getBody().getEnumType() == type.getBody().getEnumType(), VALIDATE_SCHEMA(replacement.getBody().getEnumType() == type.getBody().getEnumType(),
...@@ -804,7 +808,7 @@ private: ...@@ -804,7 +808,7 @@ private:
// We assume unknown types (from newer versions of Cap'n Proto?) are equivalent. // We assume unknown types (from newer versions of Cap'n Proto?) are equivalent.
} }
void checkUpgradeToStruct(schema::Type::Reader type, uint64_t structTypeId) { void checkUpgradeToStruct(const schema::Type::Reader& type, uint64_t structTypeId) {
// We can't just look up the target struct and check it because it may not have been loaded // We can't just look up the target struct and check it because it may not have been loaded
// yet. Instead, we contrive a struct that looks like what we want and load() that, which // yet. Instead, we contrive a struct that looks like what we want and load() that, which
// guarantees that any incompatibility will be caught either now or when the real version of // guarantees that any incompatibility will be caught either now or when the real version of
...@@ -884,7 +888,7 @@ private: ...@@ -884,7 +888,7 @@ private:
loader.load(node); loader.load(node);
} }
bool canUpgradeToData(schema::Type::Reader type) { bool canUpgradeToData(const schema::Type::Reader& type) {
if (type.getBody().which() == schema::Type::Body::TEXT_TYPE) { if (type.getBody().which() == schema::Type::Body::TEXT_TYPE) {
return true; return true;
} else if (type.getBody().which() == schema::Type::Body::LIST_TYPE) { } else if (type.getBody().which() == schema::Type::Body::LIST_TYPE) {
...@@ -900,7 +904,7 @@ private: ...@@ -900,7 +904,7 @@ private:
} }
} }
bool canUpgradeToObject(schema::Type::Reader type) { bool canUpgradeToObject(const schema::Type::Reader& type) {
switch (type.getBody().which()) { switch (type.getBody().which()) {
case schema::Type::Body::VOID_TYPE: case schema::Type::Body::VOID_TYPE:
case schema::Type::Body::BOOL_TYPE: case schema::Type::Body::BOOL_TYPE:
...@@ -930,8 +934,8 @@ private: ...@@ -930,8 +934,8 @@ private:
return true; return true;
} }
void checkDefaultCompatibility(schema::Value::Reader value, void checkDefaultCompatibility(const schema::Value::Reader& value,
schema::Value::Reader replacement) { const schema::Value::Reader& replacement) {
// Note that we test default compatibility only after testing type compatibility, and default // Note that we test default compatibility only after testing type compatibility, and default
// values have already been validated as matching their types, so this should pass. // values have already been validated as matching their types, so this should pass.
KJ_ASSERT(value.getBody().which() == replacement.getBody().which()) { KJ_ASSERT(value.getBody().which() == replacement.getBody().which()) {
...@@ -980,7 +984,7 @@ SchemaLoader::Impl::Impl() ...@@ -980,7 +984,7 @@ SchemaLoader::Impl::Impl()
: arena(allocator.arena()), : arena(allocator.arena()),
segment(allocator.getRootSegment()) {} segment(allocator.getRootSegment()) {}
internal::RawSchema* SchemaLoader::Impl::load(schema::Node::Reader reader) { internal::RawSchema* SchemaLoader::Impl::load(const schema::Node::Reader& reader) {
// Make a copy of the node which can be used unchecked. // Make a copy of the node which can be used unchecked.
size_t size = reader.totalSizeInWords() + 1; size_t size = reader.totalSizeInWords() + 1;
word* validated = allocate<word>(size); word* validated = allocate<word>(size);
...@@ -1064,7 +1068,7 @@ internal::RawSchema* SchemaLoader::Impl::loadNative(const internal::RawSchema* n ...@@ -1064,7 +1068,7 @@ internal::RawSchema* SchemaLoader::Impl::loadNative(const internal::RawSchema* n
} }
internal::RawSchema* SchemaLoader::Impl::loadEmpty( internal::RawSchema* SchemaLoader::Impl::loadEmpty(
uint64_t id, Text::Reader name, schema::Node::Body::Which kind) { uint64_t id, kj::StringPtr name, schema::Node::Body::Which kind) {
word scratch[32]; word scratch[32];
memset(scratch, 0, sizeof(scratch)); memset(scratch, 0, sizeof(scratch));
MallocMessageBuilder builder(kj::arrayPtr(scratch, sizeof(scratch))); MallocMessageBuilder builder(kj::arrayPtr(scratch, sizeof(scratch)));
...@@ -1124,7 +1128,7 @@ kj::Maybe<Schema> SchemaLoader::tryGet(uint64_t id) const { ...@@ -1124,7 +1128,7 @@ kj::Maybe<Schema> SchemaLoader::tryGet(uint64_t id) const {
} }
} }
Schema SchemaLoader::load(schema::Node::Reader reader) { Schema SchemaLoader::load(const schema::Node::Reader& reader) {
return Schema(impl->load(reader)); return Schema(impl->load(reader));
} }
......
...@@ -44,7 +44,7 @@ public: ...@@ -44,7 +44,7 @@ public:
kj::Maybe<Schema> tryGet(uint64_t id) const; kj::Maybe<Schema> tryGet(uint64_t id) const;
// Like get() but doesn't throw. // Like get() but doesn't throw.
Schema load(schema::Node::Reader reader); Schema load(const schema::Node::Reader& reader);
// Loads the given schema node. Validates the node and throws an exception if invalid. This // Loads the given schema node. Validates the node and throws an exception if invalid. This
// makes a copy of the schema, so the object passed in can be destroyed after this returns. // makes a copy of the schema, so the object passed in can be destroyed after this returns.
// //
......
...@@ -76,7 +76,7 @@ InterfaceSchema Schema::asInterface() const { ...@@ -76,7 +76,7 @@ InterfaceSchema Schema::asInterface() const {
return InterfaceSchema(raw); return InterfaceSchema(raw);
} }
void Schema::requireUsableAs(const internal::RawSchema* expected) { void Schema::requireUsableAs(const internal::RawSchema* expected) const {
KJ_REQUIRE(raw == expected || KJ_REQUIRE(raw == expected ||
(raw != nullptr && expected != nullptr && raw->canCastTo == expected), (raw != nullptr && expected != nullptr && raw->canCastTo == expected),
"This schema is not compatible with the requested native type."); "This schema is not compatible with the requested native type.");
...@@ -87,7 +87,7 @@ void Schema::requireUsableAs(const internal::RawSchema* expected) { ...@@ -87,7 +87,7 @@ void Schema::requireUsableAs(const internal::RawSchema* expected) {
namespace { namespace {
template <typename List> template <typename List>
auto findSchemaMemberByName(const internal::RawSchema* raw, Text::Reader name, auto findSchemaMemberByName(const internal::RawSchema* raw, kj::StringPtr name,
uint unionIndex, List&& list) uint unionIndex, List&& list)
-> kj::Maybe<kj::RemoveReference<decltype(list[0])>> { -> kj::Maybe<kj::RemoveReference<decltype(list[0])>> {
uint lower = 0; uint lower = 0;
...@@ -100,7 +100,7 @@ auto findSchemaMemberByName(const internal::RawSchema* raw, Text::Reader name, ...@@ -100,7 +100,7 @@ auto findSchemaMemberByName(const internal::RawSchema* raw, Text::Reader name,
if (member.unionIndex == unionIndex) { if (member.unionIndex == unionIndex) {
auto candidate = list[member.index]; auto candidate = list[member.index];
Text::Reader candidateName = candidate.getProto().getName(); kj::StringPtr candidateName = candidate.getProto().getName();
if (candidateName == name) { if (candidateName == name) {
return candidate; return candidate;
} else if (candidateName < name) { } else if (candidateName < name) {
...@@ -124,11 +124,11 @@ StructSchema::MemberList StructSchema::getMembers() const { ...@@ -124,11 +124,11 @@ StructSchema::MemberList StructSchema::getMembers() const {
return MemberList(*this, 0, getProto().getBody().getStructNode().getMembers()); return MemberList(*this, 0, getProto().getBody().getStructNode().getMembers());
} }
kj::Maybe<StructSchema::Member> StructSchema::findMemberByName(Text::Reader name) const { kj::Maybe<StructSchema::Member> StructSchema::findMemberByName(kj::StringPtr name) const {
return findSchemaMemberByName(raw, name, 0, getMembers()); return findSchemaMemberByName(raw, name, 0, getMembers());
} }
StructSchema::Member StructSchema::getMemberByName(Text::Reader name) const { StructSchema::Member StructSchema::getMemberByName(kj::StringPtr name) const {
KJ_IF_MAYBE(member, findMemberByName(name)) { KJ_IF_MAYBE(member, findMemberByName(name)) {
return *member; return *member;
} else { } else {
...@@ -152,11 +152,11 @@ StructSchema::MemberList StructSchema::Union::getMembers() const { ...@@ -152,11 +152,11 @@ StructSchema::MemberList StructSchema::Union::getMembers() const {
return MemberList(parent, index + 1, proto.getBody().getUnionMember().getMembers()); return MemberList(parent, index + 1, proto.getBody().getUnionMember().getMembers());
} }
kj::Maybe<StructSchema::Member> StructSchema::Union::findMemberByName(Text::Reader name) const { kj::Maybe<StructSchema::Member> StructSchema::Union::findMemberByName(kj::StringPtr name) const {
return findSchemaMemberByName(parent.raw, name, index + 1, getMembers()); return findSchemaMemberByName(parent.raw, name, index + 1, getMembers());
} }
StructSchema::Member StructSchema::Union::getMemberByName(Text::Reader name) const { StructSchema::Member StructSchema::Union::getMemberByName(kj::StringPtr name) const {
KJ_IF_MAYBE(member, findMemberByName(name)) { KJ_IF_MAYBE(member, findMemberByName(name)) {
return *member; return *member;
} else { } else {
...@@ -170,11 +170,11 @@ EnumSchema::EnumerantList EnumSchema::getEnumerants() const { ...@@ -170,11 +170,11 @@ EnumSchema::EnumerantList EnumSchema::getEnumerants() const {
return EnumerantList(*this, getProto().getBody().getEnumNode().getEnumerants()); return EnumerantList(*this, getProto().getBody().getEnumNode().getEnumerants());
} }
kj::Maybe<EnumSchema::Enumerant> EnumSchema::findEnumerantByName(Text::Reader name) const { kj::Maybe<EnumSchema::Enumerant> EnumSchema::findEnumerantByName(kj::StringPtr name) const {
return findSchemaMemberByName(raw, name, 0, getEnumerants()); return findSchemaMemberByName(raw, name, 0, getEnumerants());
} }
EnumSchema::Enumerant EnumSchema::getEnumerantByName(Text::Reader name) const { EnumSchema::Enumerant EnumSchema::getEnumerantByName(kj::StringPtr name) const {
KJ_IF_MAYBE(enumerant, findEnumerantByName(name)) { KJ_IF_MAYBE(enumerant, findEnumerantByName(name)) {
return *enumerant; return *enumerant;
} else { } else {
...@@ -188,11 +188,11 @@ InterfaceSchema::MethodList InterfaceSchema::getMethods() const { ...@@ -188,11 +188,11 @@ InterfaceSchema::MethodList InterfaceSchema::getMethods() const {
return MethodList(*this, getProto().getBody().getInterfaceNode().getMethods()); return MethodList(*this, getProto().getBody().getInterfaceNode().getMethods());
} }
kj::Maybe<InterfaceSchema::Method> InterfaceSchema::findMethodByName(Text::Reader name) const { kj::Maybe<InterfaceSchema::Method> InterfaceSchema::findMethodByName(kj::StringPtr name) const {
return findSchemaMemberByName(raw, name, 0, getMethods()); return findSchemaMemberByName(raw, name, 0, getMethods());
} }
InterfaceSchema::Method InterfaceSchema::getMethodByName(Text::Reader name) const { InterfaceSchema::Method InterfaceSchema::getMethodByName(kj::StringPtr name) const {
KJ_IF_MAYBE(method, findMethodByName(name)) { KJ_IF_MAYBE(method, findMethodByName(name)) {
return *method; return *method;
} else { } else {
...@@ -299,7 +299,7 @@ ListSchema ListSchema::getListElementType() const { ...@@ -299,7 +299,7 @@ ListSchema ListSchema::getListElementType() const {
return ListSchema(elementType, nestingDepth - 1, elementSchema); return ListSchema(elementType, nestingDepth - 1, elementSchema);
} }
void ListSchema::requireUsableAs(ListSchema expected) { void ListSchema::requireUsableAs(ListSchema expected) const {
KJ_REQUIRE(elementType == expected.elementType && nestingDepth == expected.nestingDepth, KJ_REQUIRE(elementType == expected.elementType && nestingDepth == expected.nestingDepth,
"This schema is not compatible with the requested native type."); "This schema is not compatible with the requested native type.");
elementSchema.requireUsableAs(expected.elementSchema.raw); elementSchema.requireUsableAs(expected.elementSchema.raw);
......
...@@ -76,7 +76,7 @@ public: ...@@ -76,7 +76,7 @@ public:
// it), compare their IDs instead. // it), compare their IDs instead.
template <typename T> template <typename T>
void requireUsableAs(); void requireUsableAs() const;
// Throws an exception if a value with this Schema cannot safely be cast to a native value of // Throws an exception if a value with this Schema cannot safely be cast to a native value of
// the given type. This passes if either: // the given type. This passes if either:
// - *this == from<T>() // - *this == from<T>()
...@@ -92,7 +92,7 @@ private: ...@@ -92,7 +92,7 @@ private:
return Schema(&internal::rawSchema<T>()); return Schema(&internal::rawSchema<T>());
} }
void requireUsableAs(const internal::RawSchema* expected); void requireUsableAs(const internal::RawSchema* expected) const;
friend class StructSchema; friend class StructSchema;
friend class EnumSchema; friend class EnumSchema;
...@@ -113,9 +113,9 @@ public: ...@@ -113,9 +113,9 @@ public:
MemberList getMembers() const; MemberList getMembers() const;
kj::Maybe<Member> findMemberByName(Text::Reader name) const; kj::Maybe<Member> findMemberByName(kj::StringPtr name) const;
Member getMemberByName(Text::Reader name) const; Member getMemberByName(kj::StringPtr name) const;
// Like findMemberByName() but throws an exception on failure. // Like findMemberByName() but throws an exception on failure.
private: private:
...@@ -169,9 +169,9 @@ public: ...@@ -169,9 +169,9 @@ public:
MemberList getMembers() const; MemberList getMembers() const;
kj::Maybe<Member> findMemberByName(Text::Reader name) const; kj::Maybe<Member> findMemberByName(kj::StringPtr name) const;
Member getMemberByName(Text::Reader name) const; Member getMemberByName(kj::StringPtr name) const;
// Like findMemberByName() but throws an exception on failure. // Like findMemberByName() but throws an exception on failure.
private: private:
...@@ -185,7 +185,7 @@ public: ...@@ -185,7 +185,7 @@ public:
inline uint size() const { return list.size(); } inline uint size() const { return list.size(); }
inline Member operator[](uint index) const { return Member(parent, unionIndex, index, list[index]); } inline Member operator[](uint index) const { return Member(parent, unionIndex, index, list[index]); }
typedef internal::IndexingIterator<MemberList, Member> iterator; typedef internal::IndexingIterator<const MemberList, Member> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -212,9 +212,9 @@ public: ...@@ -212,9 +212,9 @@ public:
EnumerantList getEnumerants() const; EnumerantList getEnumerants() const;
kj::Maybe<Enumerant> findEnumerantByName(Text::Reader name) const; kj::Maybe<Enumerant> findEnumerantByName(kj::StringPtr name) const;
Enumerant getEnumerantByName(Text::Reader name) const; Enumerant getEnumerantByName(kj::StringPtr name) const;
// Like findEnumerantByName() but throws an exception on failure. // Like findEnumerantByName() but throws an exception on failure.
private: private:
...@@ -254,7 +254,7 @@ public: ...@@ -254,7 +254,7 @@ public:
inline uint size() const { return list.size(); } inline uint size() const { return list.size(); }
inline Enumerant operator[](uint index) const { return Enumerant(parent, index, list[index]); } inline Enumerant operator[](uint index) const { return Enumerant(parent, index, list[index]); }
typedef internal::IndexingIterator<EnumerantList, Enumerant> iterator; typedef internal::IndexingIterator<const EnumerantList, Enumerant> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -279,9 +279,9 @@ public: ...@@ -279,9 +279,9 @@ public:
MethodList getMethods() const; MethodList getMethods() const;
kj::Maybe<Method> findMethodByName(Text::Reader name) const; kj::Maybe<Method> findMethodByName(kj::StringPtr name) const;
Method getMethodByName(Text::Reader name) const; Method getMethodByName(kj::StringPtr name) const;
// Like findMethodByName() but throws an exception on failure. // Like findMethodByName() but throws an exception on failure.
private: private:
...@@ -322,7 +322,7 @@ public: ...@@ -322,7 +322,7 @@ public:
inline uint size() const { return list.size(); } inline uint size() const { return list.size(); }
inline Method operator[](uint index) const { return Method(parent, index, list[index]); } inline Method operator[](uint index) const { return Method(parent, index, list[index]); }
typedef internal::IndexingIterator<MethodList, Method> iterator; typedef internal::IndexingIterator<const MethodList, Method> iterator;
inline iterator begin() const { return iterator(this, 0); } inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); } inline iterator end() const { return iterator(this, size()); }
...@@ -372,7 +372,7 @@ public: ...@@ -372,7 +372,7 @@ public:
inline bool operator!=(const ListSchema& other) const { return !(*this == other); } inline bool operator!=(const ListSchema& other) const { return !(*this == other); }
template <typename T> template <typename T>
void requireUsableAs(); void requireUsableAs() const;
private: private:
schema::Type::Body::Which elementType; schema::Type::Body::Which elementType;
...@@ -393,7 +393,7 @@ private: ...@@ -393,7 +393,7 @@ private:
return FromImpl<T>::get(); return FromImpl<T>::get();
} }
void requireUsableAs(ListSchema expected); void requireUsableAs(ListSchema expected) const;
friend class Schema; friend class Schema;
}; };
...@@ -417,7 +417,7 @@ template <> inline schema::Type::Body::Which Schema::from<Text>() { return schem ...@@ -417,7 +417,7 @@ template <> inline schema::Type::Body::Which Schema::from<Text>() { return schem
template <> inline schema::Type::Body::Which Schema::from<Data>() { return schema::Type::Body::DATA_TYPE; } template <> inline schema::Type::Body::Which Schema::from<Data>() { return schema::Type::Body::DATA_TYPE; }
template <typename T> template <typename T>
inline void Schema::requireUsableAs() { inline void Schema::requireUsableAs() const {
requireUsableAs(&internal::rawSchema<T>()); requireUsableAs(&internal::rawSchema<T>());
} }
...@@ -455,7 +455,7 @@ inline bool ListSchema::operator==(const ListSchema& other) const { ...@@ -455,7 +455,7 @@ inline bool ListSchema::operator==(const ListSchema& other) const {
} }
template <typename T> template <typename T>
inline void ListSchema::requireUsableAs() { inline void ListSchema::requireUsableAs() const {
static_assert(kind<T>() == Kind::LIST, static_assert(kind<T>() == Kind::LIST,
"ListSchema::requireUsableAs<T>() requires T is a list type."); "ListSchema::requireUsableAs<T>() requires T is a list type.");
requireUsableAs(Schema::from<T>()); requireUsableAs(Schema::from<T>());
......
...@@ -34,7 +34,7 @@ namespace { ...@@ -34,7 +34,7 @@ namespace {
static const char HEXDIGITS[] = "0123456789abcdef"; static const char HEXDIGITS[] = "0123456789abcdef";
static void print(std::ostream& os, DynamicValue::Reader value, static void print(std::ostream& os, const DynamicValue::Reader& value,
schema::Type::Body::Which which) { schema::Type::Body::Which which) {
// Print an arbitrary message via the dynamic API by // Print an arbitrary message via the dynamic API by
// iterating over the schema. Look at the handling // iterating over the schema. Look at the handling
...@@ -189,16 +189,16 @@ kj::String stringify(DynamicValue::Reader value) { ...@@ -189,16 +189,16 @@ kj::String stringify(DynamicValue::Reader value) {
} // namespace } // namespace
kj::String KJ_STRINGIFY(DynamicValue::Reader value) { return stringify(value); } kj::String KJ_STRINGIFY(const DynamicValue::Reader& value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicValue::Builder value) { return stringify(value.asReader()); } kj::String KJ_STRINGIFY(const DynamicValue::Builder& value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(DynamicEnum value) { return stringify(value); } kj::String KJ_STRINGIFY(DynamicEnum value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicObject value) { return stringify(value); } kj::String KJ_STRINGIFY(const DynamicObject& value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicUnion::Reader value) { return stringify(value); } kj::String KJ_STRINGIFY(const DynamicUnion::Reader& value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicUnion::Builder value) { return stringify(value.asReader()); } kj::String KJ_STRINGIFY(const DynamicUnion::Builder& value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(DynamicStruct::Reader value) { return stringify(value); } kj::String KJ_STRINGIFY(const DynamicStruct::Reader& value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicStruct::Builder value) { return stringify(value.asReader()); } kj::String KJ_STRINGIFY(const DynamicStruct::Builder& value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(DynamicList::Reader value) { return stringify(value); } kj::String KJ_STRINGIFY(const DynamicList::Reader& value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicList::Builder value) { return stringify(value.asReader()); } kj::String KJ_STRINGIFY(const DynamicList::Builder& value) { return stringify(value.asReader()); }
namespace internal { namespace internal {
......
...@@ -150,35 +150,35 @@ public: ...@@ -150,35 +150,35 @@ public:
inline explicit Reader(::capnproto::internal::StructReader base): _reader(base) {} inline explicit Reader(::capnproto::internal::StructReader base): _reader(base) {}
{{#typeStruct}} {{#typeStruct}}
inline size_t totalSizeInWords() { inline size_t totalSizeInWords() const {
return _reader.totalSize() / ::capnproto::WORDS; return _reader.totalSize() / ::capnproto::WORDS;
} }
{{#structUnions}} {{#structUnions}}
// {{unionDecl}} // {{unionDecl}}
inline {{unionTitleCase}}::Reader get{{unionTitleCase}}(); inline {{unionTitleCase}}::Reader get{{unionTitleCase}}() const;
{{/structUnions}} {{/structUnions}}
{{/typeStruct}} {{/typeStruct}}
{{#typeUnion}} {{#typeUnion}}
inline Which which(); inline Which which() const;
{{/typeUnion}} {{/typeUnion}}
{{#typeFields}} {{#typeFields}}
// {{fieldDecl}} // {{fieldDecl}}
{{#fieldIsPrimitive}} {{#fieldIsPrimitive}}
inline {{fieldType}} get{{fieldTitleCase}}(); inline {{fieldType}} get{{fieldTitleCase}}() const;
{{/fieldIsPrimitive}} {{/fieldIsPrimitive}}
{{^fieldIsPrimitive}} {{^fieldIsPrimitive}}
inline bool has{{fieldTitleCase}}(); inline bool has{{fieldTitleCase}}() const;
{{^fieldIsGenericObject}} {{^fieldIsGenericObject}}
inline {{fieldType}}::Reader get{{fieldTitleCase}}(); inline {{fieldType}}::Reader get{{fieldTitleCase}}() const;
{{/fieldIsGenericObject}} {{/fieldIsGenericObject}}
{{/fieldIsPrimitive}} {{/fieldIsPrimitive}}
{{#fieldIsGenericObject}} {{#fieldIsGenericObject}}
template <typename T> inline typename T::Reader get{{fieldTitleCase}}(); template <typename T> inline typename T::Reader get{{fieldTitleCase}}() const;
template <typename T, typename Param> inline typename T::Reader template <typename T, typename Param> inline typename T::Reader
get{{fieldTitleCase}}(Param&& param); get{{fieldTitleCase}}(Param&& param) const;
{{/fieldIsGenericObject}} {{/fieldIsGenericObject}}
{{/typeFields}} {{/typeFields}}
private: private:
...@@ -208,8 +208,8 @@ public: ...@@ -208,8 +208,8 @@ public:
Builder() = default; Builder() = default;
inline explicit Builder(::capnproto::internal::StructBuilder base): _builder(base) {} inline explicit Builder(::capnproto::internal::StructBuilder base): _builder(base) {}
inline operator Reader() { return Reader(_builder.asReader()); } inline operator Reader() const { return Reader(_builder.asReader()); }
inline Reader asReader() { return *this; } inline Reader asReader() const { return *this; }
{{#typeStruct}} {{#typeStruct}}
inline size_t totalSizeInWords() { return asReader().totalSizeInWords(); } inline size_t totalSizeInWords() { return asReader().totalSizeInWords(); }
...@@ -281,7 +281,7 @@ inline ::kj::String KJ_STRINGIFY({{typeFullName}}::Builder builder) { ...@@ -281,7 +281,7 @@ inline ::kj::String KJ_STRINGIFY({{typeFullName}}::Builder builder) {
{{#typeStruct}} {{#typeStruct}}
{{#structUnions}} {{#structUnions}}
inline {{unionFullName}}::Reader {{structFullName}}::Reader::get{{unionTitleCase}}() { inline {{unionFullName}}::Reader {{structFullName}}::Reader::get{{unionTitleCase}}() const {
return {{unionFullName}}::Reader(_reader); return {{unionFullName}}::Reader(_reader);
} }
...@@ -293,7 +293,7 @@ inline {{unionFullName}}::Builder {{structFullName}}::Builder::get{{unionTitleCa ...@@ -293,7 +293,7 @@ inline {{unionFullName}}::Builder {{structFullName}}::Builder::get{{unionTitleCa
{{#typeUnion}} {{#typeUnion}}
// {{unionFullName}} // {{unionFullName}}
inline {{unionFullName}}::Which {{unionFullName}}::Reader::which() { inline {{unionFullName}}::Which {{unionFullName}}::Reader::which() const {
return _reader.getDataField<Which>({{unionTagOffset}} * ::capnproto::ELEMENTS); return _reader.getDataField<Which>({{unionTagOffset}} * ::capnproto::ELEMENTS);
} }
...@@ -307,7 +307,7 @@ inline {{unionFullName}}::Which {{unionFullName}}::Builder::which() { ...@@ -307,7 +307,7 @@ inline {{unionFullName}}::Which {{unionFullName}}::Builder::which() {
// {{typeFullName}}::{{fieldDecl}} // {{typeFullName}}::{{fieldDecl}}
{{! ------------------------------------------------------------------------------------------- }} {{! ------------------------------------------------------------------------------------------- }}
{{#fieldIsPrimitive}} {{#fieldIsPrimitive}}
inline {{fieldType}} {{typeFullName}}::Reader::get{{fieldTitleCase}}() { inline {{fieldType}} {{typeFullName}}::Reader::get{{fieldTitleCase}}() const {
{{#fieldUnion}} {{#fieldUnion}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}}, KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member."); "Must check which() before get()ing a union member.");
...@@ -335,7 +335,7 @@ inline void {{typeFullName}}::Builder::set{{fieldTitleCase}}({{fieldType}} value ...@@ -335,7 +335,7 @@ inline void {{typeFullName}}::Builder::set{{fieldTitleCase}}({{fieldType}} value
{{/fieldIsPrimitive}} {{/fieldIsPrimitive}}
{{! ------------------------------------------------------------------------------------------- }} {{! ------------------------------------------------------------------------------------------- }}
{{^fieldIsPrimitive}} {{^fieldIsPrimitive}}
inline bool {{typeFullName}}::Reader::has{{fieldTitleCase}}() { inline bool {{typeFullName}}::Reader::has{{fieldTitleCase}}() const {
return !_reader.isPointerFieldNull({{fieldOffset}} * ::capnproto::POINTERS); return !_reader.isPointerFieldNull({{fieldOffset}} * ::capnproto::POINTERS);
} }
...@@ -344,7 +344,7 @@ inline bool {{typeFullName}}::Builder::has{{fieldTitleCase}}() { ...@@ -344,7 +344,7 @@ inline bool {{typeFullName}}::Builder::has{{fieldTitleCase}}() {
} }
{{^fieldIsGenericObject}} {{^fieldIsGenericObject}}
inline {{fieldType}}::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() { inline {{fieldType}}::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() const {
{{#fieldUnion}} {{#fieldUnion}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}}, KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member."); "Must check which() before get()ing a union member.");
...@@ -411,7 +411,7 @@ inline {{fieldType}}::Builder {{typeFullName}}::Builder::init{{fieldTitleCase}}( ...@@ -411,7 +411,7 @@ inline {{fieldType}}::Builder {{typeFullName}}::Builder::init{{fieldTitleCase}}(
{{! ------------------------------------------------------------------------------------------- }} {{! ------------------------------------------------------------------------------------------- }}
{{#fieldIsGenericObject}} {{#fieldIsGenericObject}}
template <typename T> template <typename T>
inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() { inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() const {
{{#fieldUnion}} {{#fieldUnion}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}}, KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member."); "Must check which() before get()ing a union member.");
...@@ -431,7 +431,7 @@ inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}() { ...@@ -431,7 +431,7 @@ inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}() {
} }
template <typename T, typename Param> template <typename T, typename Param>
inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}(Param&& param) { inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}(Param&& param) const {
{{#fieldUnion}} {{#fieldUnion}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}}, KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member."); "Must check which() before get()ing a union member.");
......
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