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
This diff is collapsed.
This diff is collapsed.
......@@ -1825,89 +1825,89 @@ StructBuilder StructBuilder::getRoot(
}
StructBuilder StructBuilder::initStructField(
WirePointerCount ptrIndex, StructSize size) const {
WirePointerCount ptrIndex, StructSize size) {
return WireHelpers::initStructPointer(pointers + ptrIndex, segment, size);
}
StructBuilder StructBuilder::getStructField(
WirePointerCount ptrIndex, StructSize size, const word* defaultValue) const {
WirePointerCount ptrIndex, StructSize size, const word* defaultValue) {
return WireHelpers::getWritableStructPointer(
pointers + ptrIndex, segment, size, defaultValue);
}
ListBuilder StructBuilder::initListField(
WirePointerCount ptrIndex, FieldSize elementSize, ElementCount elementCount) const {
WirePointerCount ptrIndex, FieldSize elementSize, ElementCount elementCount) {
return WireHelpers::initListPointer(
pointers + ptrIndex, segment,
elementCount, elementSize);
}
ListBuilder StructBuilder::initStructListField(
WirePointerCount ptrIndex, ElementCount elementCount, StructSize elementSize) const {
WirePointerCount ptrIndex, ElementCount elementCount, StructSize elementSize) {
return WireHelpers::initStructListPointer(
pointers + ptrIndex, segment, elementCount, elementSize);
}
ListBuilder StructBuilder::getListField(
WirePointerCount ptrIndex, FieldSize elementSize, const word* defaultValue) const {
WirePointerCount ptrIndex, FieldSize elementSize, const word* defaultValue) {
return WireHelpers::getWritableListPointer(
pointers + ptrIndex, segment, elementSize, defaultValue);
}
ListBuilder StructBuilder::getStructListField(
WirePointerCount ptrIndex, StructSize elementSize, const word* defaultValue) const {
WirePointerCount ptrIndex, StructSize elementSize, const word* defaultValue) {
return WireHelpers::getWritableStructListPointer(
pointers + ptrIndex, segment, elementSize, defaultValue);
}
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);
}
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);
}
template <>
Text::Builder StructBuilder::getBlobField<Text>(
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const {
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) {
return WireHelpers::getWritableTextPointer(
pointers + ptrIndex, segment, defaultValue, defaultSize);
}
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);
}
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);
}
template <>
Data::Builder StructBuilder::getBlobField<Data>(
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const {
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) {
return WireHelpers::getWritableDataPointer(
pointers + ptrIndex, segment, defaultValue, defaultSize);
}
ObjectBuilder StructBuilder::getObjectField(
WirePointerCount ptrIndex, const word* defaultValue) const {
WirePointerCount ptrIndex, const word* 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);
}
void StructBuilder::setListField(WirePointerCount ptrIndex, ListReader value) const {
void StructBuilder::setListField(WirePointerCount ptrIndex, ListReader 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);
}
bool StructBuilder::isPointerFieldNull(WirePointerCount ptrIndex) const {
bool StructBuilder::isPointerFieldNull(WirePointerCount ptrIndex) {
return (pointers + ptrIndex)->isNull();
}
......@@ -2027,7 +2027,7 @@ Data::Builder ListBuilder::asData() {
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;
byte* structData = ptr + indexBit / BITS_PER_BYTE;
return StructBuilder(segment, structData,
......@@ -2036,75 +2036,75 @@ StructBuilder ListBuilder::getStructElement(ElementCount index) const {
}
ListBuilder ListBuilder::initListElement(
ElementCount index, FieldSize elementSize, ElementCount elementCount) const {
ElementCount index, FieldSize elementSize, ElementCount elementCount) {
return WireHelpers::initListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE),
segment, elementCount, elementSize);
}
ListBuilder ListBuilder::initStructListElement(
ElementCount index, ElementCount elementCount, StructSize elementSize) const {
ElementCount index, ElementCount elementCount, StructSize elementSize) {
return WireHelpers::initStructListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE),
segment, elementCount, elementSize);
}
ListBuilder ListBuilder::getListElement(ElementCount index, FieldSize elementSize) const {
ListBuilder ListBuilder::getListElement(ElementCount index, FieldSize elementSize) {
return WireHelpers::getWritableListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment,
elementSize, nullptr);
}
ListBuilder ListBuilder::getStructListElement(ElementCount index, StructSize elementSize) const {
ListBuilder ListBuilder::getStructListElement(ElementCount index, StructSize elementSize) {
return WireHelpers::getWritableStructListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment,
elementSize, nullptr);
}
template <>
Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) const {
Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) {
return WireHelpers::initTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size);
}
template <>
void ListBuilder::setBlobElement<Text>(ElementCount index, Text::Reader value) const {
void ListBuilder::setBlobElement<Text>(ElementCount index, Text::Reader value) {
WireHelpers::setTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value);
}
template <>
Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) const {
Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) {
return WireHelpers::getWritableTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, "", 0 * BYTES);
}
template <>
Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) const {
Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) {
return WireHelpers::initDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size);
}
template <>
void ListBuilder::setBlobElement<Data>(ElementCount index, Data::Reader value) const {
void ListBuilder::setBlobElement<Data>(ElementCount index, Data::Reader value) {
WireHelpers::setDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value);
}
template <>
Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) const {
Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) {
return WireHelpers::getWritableDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr,
0 * BYTES);
}
ObjectBuilder ListBuilder::getObjectElement(ElementCount index) const {
ObjectBuilder ListBuilder::getObjectElement(ElementCount index) {
return WireHelpers::getWritableObjectPointer(
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(
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(
segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), value);
}
......
This diff is collapsed.
......@@ -167,11 +167,11 @@ public:
inline bool operator> (const IndexingIterator& other) const { return index > other.index; }
private:
const Container* container;
Container* container;
uint index;
friend Container;
inline IndexingIterator(const Container* container, uint index)
inline IndexingIterator(Container* container, uint index)
: container(container), index(index) {}
};
......@@ -193,7 +193,7 @@ struct List<T, Kind::PRIMITIVE> {
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 end() const { return iterator(this, size()); }
......@@ -216,7 +216,7 @@ struct List<T, Kind::PRIMITIVE> {
inline Reader asReader() { return Reader(builder.asReader()); }
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);
}
inline void set(uint index, T value) {
......@@ -230,8 +230,8 @@ struct List<T, Kind::PRIMITIVE> {
}
typedef internal::IndexingIterator<Builder, T> iterator;
inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); }
inline iterator begin() { return iterator(this, 0); }
inline iterator end() { return iterator(this, size()); }
private:
internal::ListBuilder builder;
......@@ -239,12 +239,12 @@ struct List<T, Kind::PRIMITIVE> {
private:
inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) {
internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement(
index * ELEMENTS, internal::FieldSizeForType<T>::value, size * ELEMENTS);
}
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);
}
inline static internal::ListReader getAsElementOf(
......@@ -261,7 +261,7 @@ private:
return builder.getListField(index, internal::FieldSizeForType<T>::value, defaultValue);
}
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);
}
......@@ -290,7 +290,7 @@ struct List<T, Kind::STRUCT> {
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 end() const { return iterator(this, size()); }
......@@ -313,7 +313,7 @@ struct List<T, Kind::STRUCT> {
inline Reader asReader() { return Reader(builder.asReader()); }
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));
}
......@@ -323,8 +323,8 @@ struct List<T, Kind::STRUCT> {
// of teh protocol.
typedef internal::IndexingIterator<Builder, typename T::Builder> iterator;
inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); }
inline iterator begin() { return iterator(this, 0); }
inline iterator end() { return iterator(this, size()); }
private:
internal::ListBuilder builder;
......@@ -332,12 +332,12 @@ struct List<T, Kind::STRUCT> {
private:
inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) {
internal::ListBuilder& builder, uint index, uint size) {
return builder.initStructListElement(
index * ELEMENTS, size * ELEMENTS, internal::structSize<T>());
}
inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) {
internal::ListBuilder& builder, uint index) {
return builder.getStructListElement(index * ELEMENTS, internal::structSize<T>());
}
inline static internal::ListReader getAsElementOf(
......@@ -354,7 +354,7 @@ private:
return builder.getStructListField(index, internal::structSize<T>(), defaultValue);
}
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);
}
......@@ -380,7 +380,7 @@ struct List<List<T>, Kind::LIST> {
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 end() const { return iterator(this, size()); }
......@@ -403,7 +403,7 @@ struct List<List<T>, Kind::LIST> {
inline Reader asReader() { return Reader(builder.asReader()); }
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));
}
inline typename List<T>::Builder init(uint index, uint size) {
......@@ -421,8 +421,8 @@ struct List<List<T>, Kind::LIST> {
}
typedef internal::IndexingIterator<Builder, typename List<T>::Builder> iterator;
inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); }
inline iterator begin() { return iterator(this, 0); }
inline iterator end() { return iterator(this, size()); }
private:
internal::ListBuilder builder;
......@@ -430,12 +430,12 @@ struct List<List<T>, Kind::LIST> {
private:
inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) {
internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement(
index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS);
}
inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) {
internal::ListBuilder& builder, uint index) {
return builder.getListElement(index * ELEMENTS, internal::FieldSize::POINTER);
}
inline static internal::ListReader getAsElementOf(
......@@ -452,7 +452,7 @@ private:
return builder.getListField(index, internal::FieldSize::POINTER, defaultValue);
}
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);
}
......@@ -476,7 +476,7 @@ struct List<T, Kind::BLOB> {
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 end() const { return iterator(this, size()); }
......@@ -499,7 +499,7 @@ struct List<T, Kind::BLOB> {
inline Reader asReader() { return Reader(builder.asReader()); }
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);
}
inline void set(uint index, typename T::Reader value) {
......@@ -510,8 +510,8 @@ struct List<T, Kind::BLOB> {
}
typedef internal::IndexingIterator<Builder, typename T::Builder> iterator;
inline iterator begin() const { return iterator(this, 0); }
inline iterator end() const { return iterator(this, size()); }
inline iterator begin() { return iterator(this, 0); }
inline iterator end() { return iterator(this, size()); }
private:
internal::ListBuilder builder;
......@@ -519,12 +519,12 @@ struct List<T, Kind::BLOB> {
private:
inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) {
internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement(
index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS);
}
inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) {
internal::ListBuilder& builder, uint index) {
return builder.getListElement(index * ELEMENTS, internal::FieldSize::POINTER);
}
inline static internal::ListReader getAsElementOf(
......@@ -541,7 +541,7 @@ private:
return builder.getListField(index, internal::FieldSize::POINTER, defaultValue);
}
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);
}
......
This diff is collapsed.
......@@ -44,7 +44,7 @@ public:
kj::Maybe<Schema> tryGet(uint64_t id) const;
// 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
// 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 {
return InterfaceSchema(raw);
}
void Schema::requireUsableAs(const internal::RawSchema* expected) {
void Schema::requireUsableAs(const internal::RawSchema* expected) const {
KJ_REQUIRE(raw == expected ||
(raw != nullptr && expected != nullptr && raw->canCastTo == expected),
"This schema is not compatible with the requested native type.");
......@@ -87,7 +87,7 @@ void Schema::requireUsableAs(const internal::RawSchema* expected) {
namespace {
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)
-> kj::Maybe<kj::RemoveReference<decltype(list[0])>> {
uint lower = 0;
......@@ -100,7 +100,7 @@ auto findSchemaMemberByName(const internal::RawSchema* raw, Text::Reader name,
if (member.unionIndex == unionIndex) {
auto candidate = list[member.index];
Text::Reader candidateName = candidate.getProto().getName();
kj::StringPtr candidateName = candidate.getProto().getName();
if (candidateName == name) {
return candidate;
} else if (candidateName < name) {
......@@ -124,11 +124,11 @@ StructSchema::MemberList StructSchema::getMembers() const {
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());
}
StructSchema::Member StructSchema::getMemberByName(Text::Reader name) const {
StructSchema::Member StructSchema::getMemberByName(kj::StringPtr name) const {
KJ_IF_MAYBE(member, findMemberByName(name)) {
return *member;
} else {
......@@ -152,11 +152,11 @@ StructSchema::MemberList StructSchema::Union::getMembers() const {
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());
}
StructSchema::Member StructSchema::Union::getMemberByName(Text::Reader name) const {
StructSchema::Member StructSchema::Union::getMemberByName(kj::StringPtr name) const {
KJ_IF_MAYBE(member, findMemberByName(name)) {
return *member;
} else {
......@@ -170,11 +170,11 @@ EnumSchema::EnumerantList EnumSchema::getEnumerants() const {
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());
}
EnumSchema::Enumerant EnumSchema::getEnumerantByName(Text::Reader name) const {
EnumSchema::Enumerant EnumSchema::getEnumerantByName(kj::StringPtr name) const {
KJ_IF_MAYBE(enumerant, findEnumerantByName(name)) {
return *enumerant;
} else {
......@@ -188,11 +188,11 @@ InterfaceSchema::MethodList InterfaceSchema::getMethods() const {
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());
}
InterfaceSchema::Method InterfaceSchema::getMethodByName(Text::Reader name) const {
InterfaceSchema::Method InterfaceSchema::getMethodByName(kj::StringPtr name) const {
KJ_IF_MAYBE(method, findMethodByName(name)) {
return *method;
} else {
......@@ -299,7 +299,7 @@ ListSchema ListSchema::getListElementType() const {
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,
"This schema is not compatible with the requested native type.");
elementSchema.requireUsableAs(expected.elementSchema.raw);
......
......@@ -76,7 +76,7 @@ public:
// it), compare their IDs instead.
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
// the given type. This passes if either:
// - *this == from<T>()
......@@ -92,7 +92,7 @@ private:
return Schema(&internal::rawSchema<T>());
}
void requireUsableAs(const internal::RawSchema* expected);
void requireUsableAs(const internal::RawSchema* expected) const;
friend class StructSchema;
friend class EnumSchema;
......@@ -113,9 +113,9 @@ public:
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.
private:
......@@ -169,9 +169,9 @@ public:
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.
private:
......@@ -185,7 +185,7 @@ public:
inline uint size() const { return list.size(); }
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 end() const { return iterator(this, size()); }
......@@ -212,9 +212,9 @@ public:
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.
private:
......@@ -254,7 +254,7 @@ public:
inline uint size() const { return list.size(); }
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 end() const { return iterator(this, size()); }
......@@ -279,9 +279,9 @@ public:
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.
private:
......@@ -322,7 +322,7 @@ public:
inline uint size() const { return list.size(); }
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 end() const { return iterator(this, size()); }
......@@ -372,7 +372,7 @@ public:
inline bool operator!=(const ListSchema& other) const { return !(*this == other); }
template <typename T>
void requireUsableAs();
void requireUsableAs() const;
private:
schema::Type::Body::Which elementType;
......@@ -393,7 +393,7 @@ private:
return FromImpl<T>::get();
}
void requireUsableAs(ListSchema expected);
void requireUsableAs(ListSchema expected) const;
friend class Schema;
};
......@@ -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 <typename T>
inline void Schema::requireUsableAs() {
inline void Schema::requireUsableAs() const {
requireUsableAs(&internal::rawSchema<T>());
}
......@@ -455,7 +455,7 @@ inline bool ListSchema::operator==(const ListSchema& other) const {
}
template <typename T>
inline void ListSchema::requireUsableAs() {
inline void ListSchema::requireUsableAs() const {
static_assert(kind<T>() == Kind::LIST,
"ListSchema::requireUsableAs<T>() requires T is a list type.");
requireUsableAs(Schema::from<T>());
......
......@@ -34,7 +34,7 @@ namespace {
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) {
// Print an arbitrary message via the dynamic API by
// iterating over the schema. Look at the handling
......@@ -189,16 +189,16 @@ kj::String stringify(DynamicValue::Reader value) {
} // namespace
kj::String KJ_STRINGIFY(DynamicValue::Reader value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicValue::Builder value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(const DynamicValue::Reader& value) { return stringify(value); }
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(DynamicObject value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicUnion::Reader value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicUnion::Builder value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(DynamicStruct::Reader value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicStruct::Builder value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(DynamicList::Reader value) { return stringify(value); }
kj::String KJ_STRINGIFY(DynamicList::Builder value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(const DynamicObject& value) { return stringify(value); }
kj::String KJ_STRINGIFY(const DynamicUnion::Reader& value) { return stringify(value); }
kj::String KJ_STRINGIFY(const DynamicUnion::Builder& value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(const DynamicStruct::Reader& value) { return stringify(value); }
kj::String KJ_STRINGIFY(const DynamicStruct::Builder& value) { return stringify(value.asReader()); }
kj::String KJ_STRINGIFY(const DynamicList::Reader& value) { return stringify(value); }
kj::String KJ_STRINGIFY(const DynamicList::Builder& value) { return stringify(value.asReader()); }
namespace internal {
......
......@@ -150,35 +150,35 @@ public:
inline explicit Reader(::capnproto::internal::StructReader base): _reader(base) {}
{{#typeStruct}}
inline size_t totalSizeInWords() {
inline size_t totalSizeInWords() const {
return _reader.totalSize() / ::capnproto::WORDS;
}
{{#structUnions}}
// {{unionDecl}}
inline {{unionTitleCase}}::Reader get{{unionTitleCase}}();
inline {{unionTitleCase}}::Reader get{{unionTitleCase}}() const;
{{/structUnions}}
{{/typeStruct}}
{{#typeUnion}}
inline Which which();
inline Which which() const;
{{/typeUnion}}
{{#typeFields}}
// {{fieldDecl}}
{{#fieldIsPrimitive}}
inline {{fieldType}} get{{fieldTitleCase}}();
inline {{fieldType}} get{{fieldTitleCase}}() const;
{{/fieldIsPrimitive}}
{{^fieldIsPrimitive}}
inline bool has{{fieldTitleCase}}();
inline bool has{{fieldTitleCase}}() const;
{{^fieldIsGenericObject}}
inline {{fieldType}}::Reader get{{fieldTitleCase}}();
inline {{fieldType}}::Reader get{{fieldTitleCase}}() const;
{{/fieldIsGenericObject}}
{{/fieldIsPrimitive}}
{{#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
get{{fieldTitleCase}}(Param&& param);
get{{fieldTitleCase}}(Param&& param) const;
{{/fieldIsGenericObject}}
{{/typeFields}}
private:
......@@ -208,8 +208,8 @@ public:
Builder() = default;
inline explicit Builder(::capnproto::internal::StructBuilder base): _builder(base) {}
inline operator Reader() { return Reader(_builder.asReader()); }
inline Reader asReader() { return *this; }
inline operator Reader() const { return Reader(_builder.asReader()); }
inline Reader asReader() const { return *this; }
{{#typeStruct}}
inline size_t totalSizeInWords() { return asReader().totalSizeInWords(); }
......@@ -281,7 +281,7 @@ inline ::kj::String KJ_STRINGIFY({{typeFullName}}::Builder builder) {
{{#typeStruct}}
{{#structUnions}}
inline {{unionFullName}}::Reader {{structFullName}}::Reader::get{{unionTitleCase}}() {
inline {{unionFullName}}::Reader {{structFullName}}::Reader::get{{unionTitleCase}}() const {
return {{unionFullName}}::Reader(_reader);
}
......@@ -293,7 +293,7 @@ inline {{unionFullName}}::Builder {{structFullName}}::Builder::get{{unionTitleCa
{{#typeUnion}}
// {{unionFullName}}
inline {{unionFullName}}::Which {{unionFullName}}::Reader::which() {
inline {{unionFullName}}::Which {{unionFullName}}::Reader::which() const {
return _reader.getDataField<Which>({{unionTagOffset}} * ::capnproto::ELEMENTS);
}
......@@ -307,7 +307,7 @@ inline {{unionFullName}}::Which {{unionFullName}}::Builder::which() {
// {{typeFullName}}::{{fieldDecl}}
{{! ------------------------------------------------------------------------------------------- }}
{{#fieldIsPrimitive}}
inline {{fieldType}} {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
inline {{fieldType}} {{typeFullName}}::Reader::get{{fieldTitleCase}}() const {
{{#fieldUnion}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
......@@ -335,7 +335,7 @@ inline void {{typeFullName}}::Builder::set{{fieldTitleCase}}({{fieldType}} value
{{/fieldIsPrimitive}}
{{! ------------------------------------------------------------------------------------------- }}
{{^fieldIsPrimitive}}
inline bool {{typeFullName}}::Reader::has{{fieldTitleCase}}() {
inline bool {{typeFullName}}::Reader::has{{fieldTitleCase}}() const {
return !_reader.isPointerFieldNull({{fieldOffset}} * ::capnproto::POINTERS);
}
......@@ -344,7 +344,7 @@ inline bool {{typeFullName}}::Builder::has{{fieldTitleCase}}() {
}
{{^fieldIsGenericObject}}
inline {{fieldType}}::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
inline {{fieldType}}::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() const {
{{#fieldUnion}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
......@@ -411,7 +411,7 @@ inline {{fieldType}}::Builder {{typeFullName}}::Builder::init{{fieldTitleCase}}(
{{! ------------------------------------------------------------------------------------------- }}
{{#fieldIsGenericObject}}
template <typename T>
inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() const {
{{#fieldUnion}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
......@@ -431,7 +431,7 @@ inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}() {
}
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}}
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"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