Commit 58d8f75c authored by Kenton Varda's avatar Kenton Varda

Refactor PointerHelpers to operate on PointerReader/PointerBuilder.

parent 76bda20e
......@@ -848,55 +848,59 @@ private:
"inline typename T::Reader ", scope, "Reader::get", titleCase, "() const {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<T>::get(\n"
" _reader, ", offset, " * ::capnp::POINTERS);\n"
" _reader.getPointerField(", offset, " * ::capnp::POINTERS));\n"
"}\n"
"template <typename T>\n"
"inline typename T::Builder ", scope, "Builder::get", titleCase, "() {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<T>::get(\n"
" _builder, ", offset, " * ::capnp::POINTERS);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS));\n"
"}\n"
"template <typename T, typename Param>\n"
"inline typename T::Reader ", scope, "Reader::get", titleCase, "(Param&& param) const {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<T>::getDynamic(\n"
" _reader, ", offset, " * ::capnp::POINTERS, ::kj::fwd<Param>(param));\n"
" _reader.getPointerField(", offset, " * ::capnp::POINTERS),\n"
" ::kj::fwd<Param>(param));\n"
"}\n"
"template <typename T, typename Param>\n"
"inline typename T::Builder ", scope, "Builder::get", titleCase, "(Param&& param) {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<T>::getDynamic(\n"
" _builder, ", offset, " * ::capnp::POINTERS, ::kj::fwd<Param>(param));\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS),\n"
" ::kj::fwd<Param>(param));\n"
"}\n"
"template <typename T>\n"
"inline void ", scope, "Builder::set", titleCase, "(typename T::Reader value) {\n",
unionDiscrim.set,
" ::capnp::_::PointerHelpers<T>::set(\n"
" _builder, ", offset, " * ::capnp::POINTERS, value);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS), value);\n"
"}\n"
"template <typename T, typename U>"
"inline void ", scope, "Builder::set", titleCase, "(std::initializer_list<U> value) {\n",
unionDiscrim.set,
" ::capnp::_::PointerHelpers<T>::set(\n"
" _builder, ", offset, " * ::capnp::POINTERS, value);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS), value);\n"
"}\n"
"template <typename T, typename... Params>\n"
"inline typename T::Builder ", scope, "Builder::init", titleCase, "(Params&&... params) {\n",
unionDiscrim.set,
" return ::capnp::_::PointerHelpers<T>::init(\n"
" _builder, ", offset, " * ::capnp::POINTERS, ::kj::fwd<Params>(params)...);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS),\n"
" ::kj::fwd<Params>(params)...);\n"
"}\n"
"template <typename T>\n"
"inline void ", scope, "Builder::adopt", titleCase, "(::capnp::Orphan<T>&& value) {\n",
unionDiscrim.set,
" ::capnp::_::PointerHelpers<T>::adopt(\n"
" _builder, ", offset, " * ::capnp::POINTERS, kj::mv(value));\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS), kj::mv(value));\n"
"}\n"
"template <typename T, typename... Params>\n"
"inline ::capnp::Orphan<T> ", scope, "Builder::disown", titleCase, "(Params&&... params) {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<T>::disown(\n"
" _builder, ", offset, " * ::capnp::POINTERS, ::kj::fwd<Params>(params)...);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS),\n"
" ::kj::fwd<Params>(params)...);\n"
"}\n"
"\n")
};
......@@ -986,24 +990,24 @@ private:
"inline ", type, "::Reader ", scope, "Reader::get", titleCase, "() const {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<", type, ">::get(\n"
" _reader, ", offset, " * ::capnp::POINTERS", defaultParam, ");\n"
" _reader.getPointerField(", offset, " * ::capnp::POINTERS)", defaultParam, ");\n"
"}\n"
"inline ", type, "::Builder ", scope, "Builder::get", titleCase, "() {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<", type, ">::get(\n"
" _builder, ", offset, " * ::capnp::POINTERS", defaultParam, ");\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS)", defaultParam, ");\n"
"}\n"
"inline void ", scope, "Builder::set", titleCase, "(", type, "::Reader value) {\n",
unionDiscrim.set,
" ::capnp::_::PointerHelpers<", type, ">::set(\n"
" _builder, ", offset, " * ::capnp::POINTERS, value);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS), value);\n"
"}\n",
kind == FieldKind::LIST && !isStructList
? kj::strTree(
"inline void ", scope, "Builder::set", titleCase, "(std::initializer_list<", elementReaderType, "> value) {\n",
unionDiscrim.set,
" ::capnp::_::PointerHelpers<", type, ">::set(\n"
" _builder, ", offset, " * ::capnp::POINTERS, value);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS), value);\n"
"}\n")
: kj::strTree(),
kind == FieldKind::STRUCT
......@@ -1011,24 +1015,24 @@ private:
"inline ", type, "::Builder ", scope, "Builder::init", titleCase, "() {\n",
unionDiscrim.set,
" return ::capnp::_::PointerHelpers<", type, ">::init(\n"
" _builder, ", offset, " * ::capnp::POINTERS);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS));\n"
"}\n")
: kj::strTree(
"inline ", type, "::Builder ", scope, "Builder::init", titleCase, "(unsigned int size) {\n",
unionDiscrim.set,
" return ::capnp::_::PointerHelpers<", type, ">::init(\n"
" _builder, ", offset, " * ::capnp::POINTERS, size);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS), size);\n"
"}\n"),
"inline void ", scope, "Builder::adopt", titleCase, "(\n"
" ::capnp::Orphan<", type, ">&& value) {\n",
unionDiscrim.set,
" ::capnp::_::PointerHelpers<", type, ">::adopt(\n"
" _builder, ", offset, " * ::capnp::POINTERS, kj::mv(value));\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS), kj::mv(value));\n"
"}\n"
"inline ::capnp::Orphan<", type, "> ", scope, "Builder::disown", titleCase, "() {\n",
unionDiscrim.check,
" return ::capnp::_::PointerHelpers<", type, ">::disown(\n"
" _builder, ", offset, " * ::capnp::POINTERS);\n"
" _builder.getPointerField(", offset, " * ::capnp::POINTERS));\n"
"}\n"
"\n")
};
......
This diff is collapsed.
This diff is collapsed.
......@@ -1700,77 +1700,74 @@ void MessageBuilder::setRoot<DynamicStruct::Reader&>(DynamicStruct::Reader& valu
namespace _ { // private
DynamicStruct::Reader PointerHelpers<DynamicStruct, Kind::UNKNOWN>::getDynamic(
StructReader reader, WirePointerCount index, StructSchema schema) {
PointerReader reader, StructSchema schema) {
KJ_REQUIRE(!schema.getProto().getStruct().getIsGroup(),
"Cannot form pointer to group type.");
return DynamicStruct::Reader(schema, reader.getPointerField(index).getStruct(nullptr));
return DynamicStruct::Reader(schema, reader.getStruct(nullptr));
}
DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::getDynamic(
StructBuilder builder, WirePointerCount index, StructSchema schema) {
PointerBuilder builder, StructSchema schema) {
KJ_REQUIRE(!schema.getProto().getStruct().getIsGroup(),
"Cannot form pointer to group type.");
return DynamicStruct::Builder(schema, builder.getPointerField(index).getStruct(
return DynamicStruct::Builder(schema, builder.getStruct(
structSizeFromSchema(schema), nullptr));
}
void PointerHelpers<DynamicStruct, Kind::UNKNOWN>::set(
StructBuilder builder, WirePointerCount index, const DynamicStruct::Reader& value) {
PointerBuilder builder, const DynamicStruct::Reader& value) {
KJ_REQUIRE(!value.schema.getProto().getStruct().getIsGroup(),
"Cannot form pointer to group type.");
builder.getPointerField(index).setStruct(value.reader);
builder.setStruct(value.reader);
}
DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::init(
StructBuilder builder, WirePointerCount index, StructSchema schema) {
PointerBuilder builder, StructSchema schema) {
KJ_REQUIRE(!schema.getProto().getStruct().getIsGroup(),
"Cannot form pointer to group type.");
return DynamicStruct::Builder(schema,
builder.getPointerField(index).initStruct(structSizeFromSchema(schema)));
builder.initStruct(structSizeFromSchema(schema)));
}
DynamicList::Reader PointerHelpers<DynamicList, Kind::UNKNOWN>::getDynamic(
StructReader reader, WirePointerCount index, ListSchema schema) {
PointerReader reader, ListSchema schema) {
return DynamicList::Reader(schema,
reader.getPointerField(index).getList(elementSizeFor(schema.whichElementType()), nullptr));
reader.getList(elementSizeFor(schema.whichElementType()), nullptr));
}
DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::getDynamic(
StructBuilder builder, WirePointerCount index, ListSchema schema) {
PointerBuilder builder, ListSchema schema) {
if (schema.whichElementType() == schema::Type::STRUCT) {
return DynamicList::Builder(schema,
builder.getPointerField(index).getStructList(
builder.getStructList(
structSizeFromSchema(schema.getStructElementType()),
nullptr));
} else {
return DynamicList::Builder(schema,
builder.getPointerField(index).getList(elementSizeFor(schema.whichElementType()), nullptr));
builder.getList(elementSizeFor(schema.whichElementType()), nullptr));
}
}
void PointerHelpers<DynamicList, Kind::UNKNOWN>::set(
StructBuilder builder, WirePointerCount index, const DynamicList::Reader& value) {
builder.getPointerField(index).setList(value.reader);
PointerBuilder builder, const DynamicList::Reader& value) {
builder.setList(value.reader);
}
DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::init(
StructBuilder builder, WirePointerCount index, ListSchema schema, uint size) {
PointerBuilder builder, ListSchema schema, uint size) {
if (schema.whichElementType() == schema::Type::STRUCT) {
return DynamicList::Builder(schema,
builder.getPointerField(index).initStructList(size * ELEMENTS,
builder.initStructList(size * ELEMENTS,
structSizeFromSchema(schema.getStructElementType())));
} else {
return DynamicList::Builder(schema,
builder.getPointerField(index)
.initList(elementSizeFor(schema.whichElementType()), size * ELEMENTS));
builder.initList(elementSizeFor(schema.whichElementType()), size * ELEMENTS));
}
}
DynamicObject::Reader PointerHelpers<DynamicObject, Kind::UNKNOWN>::get(
StructReader reader, WirePointerCount index) {
return DynamicObject::Reader(reader.getPointerField(index).getObject(nullptr));
DynamicObject::Reader PointerHelpers<DynamicObject, Kind::UNKNOWN>::get(PointerReader reader) {
return DynamicObject::Reader(reader.getObject(nullptr));
}
DynamicObject::Builder PointerHelpers<DynamicObject, Kind::UNKNOWN>::get(
StructBuilder builder, WirePointerCount index) {
return DynamicObject::Builder(builder.getPointerField(index).getObject(nullptr));
DynamicObject::Builder PointerHelpers<DynamicObject, Kind::UNKNOWN>::get(PointerBuilder builder) {
return DynamicObject::Builder(builder.getObject(nullptr));
}
void PointerHelpers<DynamicObject, Kind::UNKNOWN>::set(
StructBuilder builder, WirePointerCount index, const DynamicObject::Reader& value) {
builder.getPointerField(index).setObject(value.reader);
PointerBuilder builder, const DynamicObject::Reader& value) {
builder.setObject(value.reader);
}
} // namespace _ (private)
......
......@@ -954,21 +954,15 @@ struct PointerHelpers<DynamicStruct, Kind::UNKNOWN> {
// getDynamic() is used when an Object's get() accessor is passed arguments, because for
// non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we
// don't want people to accidentally be able to provide their own default value.
static DynamicStruct::Reader getDynamic(
StructReader reader, WirePointerCount index, StructSchema schema);
static DynamicStruct::Builder getDynamic(
StructBuilder builder, WirePointerCount index, StructSchema schema);
static void set(
StructBuilder builder, WirePointerCount index, const DynamicStruct::Reader& value);
static DynamicStruct::Builder init(
StructBuilder builder, WirePointerCount index, StructSchema schema);
static inline void adopt(StructBuilder builder, WirePointerCount index,
Orphan<DynamicStruct>&& value) {
builder.getPointerField(index).adopt(kj::mv(value.builder));
static DynamicStruct::Reader getDynamic(PointerReader reader, StructSchema schema);
static DynamicStruct::Builder getDynamic(PointerBuilder builder, StructSchema schema);
static void set(PointerBuilder builder, const DynamicStruct::Reader& value);
static DynamicStruct::Builder init(PointerBuilder builder, StructSchema schema);
static inline void adopt(PointerBuilder builder, Orphan<DynamicStruct>&& value) {
builder.adopt(kj::mv(value.builder));
}
static inline Orphan<DynamicStruct> disown(StructBuilder builder, WirePointerCount index,
StructSchema schema) {
return Orphan<DynamicStruct>(schema, builder.getPointerField(index).disown());
static inline Orphan<DynamicStruct> disown(PointerBuilder builder, StructSchema schema) {
return Orphan<DynamicStruct>(schema, builder.disown());
}
};
......@@ -977,36 +971,28 @@ struct PointerHelpers<DynamicList, Kind::UNKNOWN> {
// getDynamic() is used when an Object's get() accessor is passed arguments, because for
// non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we
// don't want people to accidentally be able to provide their own default value.
static DynamicList::Reader getDynamic(
StructReader reader, WirePointerCount index, ListSchema schema);
static DynamicList::Builder getDynamic(
StructBuilder builder, WirePointerCount index, ListSchema schema);
static void set(
StructBuilder builder, WirePointerCount index, const DynamicList::Reader& value);
static DynamicList::Builder init(
StructBuilder builder, WirePointerCount index, ListSchema schema, uint size);
static inline void adopt(StructBuilder builder, WirePointerCount index,
Orphan<DynamicList>&& value) {
builder.getPointerField(index).adopt(kj::mv(value.builder));
static DynamicList::Reader getDynamic(PointerReader reader, ListSchema schema);
static DynamicList::Builder getDynamic(PointerBuilder builder, ListSchema schema);
static void set(PointerBuilder builder, const DynamicList::Reader& value);
static DynamicList::Builder init(PointerBuilder builder, ListSchema schema, uint size);
static inline void adopt(PointerBuilder builder, Orphan<DynamicList>&& value) {
builder.adopt(kj::mv(value.builder));
}
static inline Orphan<DynamicList> disown(StructBuilder builder, WirePointerCount index,
ListSchema schema) {
return Orphan<DynamicList>(schema, builder.getPointerField(index).disown());
static inline Orphan<DynamicList> disown(PointerBuilder builder, ListSchema schema) {
return Orphan<DynamicList>(schema, builder.disown());
}
};
template <>
struct PointerHelpers<DynamicObject, Kind::UNKNOWN> {
static DynamicObject::Reader get(StructReader reader, WirePointerCount index);
static DynamicObject::Builder get(StructBuilder builder, WirePointerCount index);
static void set(
StructBuilder builder, WirePointerCount index, const DynamicObject::Reader& value);
static inline void adopt(StructBuilder builder, WirePointerCount index,
Orphan<DynamicObject>&& value) {
builder.getPointerField(index).adopt(kj::mv(value.builder));
static DynamicObject::Reader get(PointerReader reader);
static DynamicObject::Builder get(PointerBuilder builder);
static void set(PointerBuilder builder, const DynamicObject::Reader& value);
static inline void adopt(PointerBuilder builder, Orphan<DynamicObject>&& value) {
builder.adopt(kj::mv(value.builder));
}
static inline Orphan<DynamicObject> disown(StructBuilder builder, WirePointerCount index) {
return Orphan<DynamicObject>(builder.getPointerField(index).disown());
static inline Orphan<DynamicObject> disown(PointerBuilder builder) {
return Orphan<DynamicObject>(builder.disown());
}
};
......
......@@ -29,6 +29,7 @@
#include "layout.h"
#include "list.h"
#include "orphan.h"
#include "pointer-helpers.h"
#include <kj/string.h>
#include <kj/string-tree.h>
......@@ -43,107 +44,6 @@ struct DynamicStruct; // So that it can be declared a friend.
namespace _ { // private
template <typename T>
struct PointerHelpers<T, Kind::STRUCT> {
static inline typename T::Reader get(StructReader reader, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename T::Reader(reader.getPointerField(index).getStruct(defaultValue));
}
static inline typename T::Builder get(StructBuilder builder, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename T::Builder(
builder.getPointerField(index).getStruct(structSize<T>(), defaultValue));
}
static inline void set(StructBuilder builder, WirePointerCount index,
typename T::Reader value) {
builder.getPointerField(index).setStruct(value._reader);
}
static inline typename T::Builder init(StructBuilder builder, WirePointerCount index) {
return typename T::Builder(builder.getPointerField(index).initStruct(structSize<T>()));
}
static inline void adopt(StructBuilder builder, WirePointerCount index, Orphan<T>&& value) {
builder.getPointerField(index).adopt(kj::mv(value.builder));
}
static inline Orphan<T> disown(StructBuilder builder, WirePointerCount index) {
return Orphan<T>(builder.getPointerField(index).disown());
}
};
template <typename T>
struct PointerHelpers<List<T>, Kind::LIST> {
static inline typename List<T>::Reader get(StructReader reader, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename List<T>::Reader(List<T>::getAsFieldOf(reader, index, defaultValue));
}
static inline typename List<T>::Builder get(StructBuilder builder, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename List<T>::Builder(List<T>::getAsFieldOf(builder, index, defaultValue));
}
static inline void set(StructBuilder builder, WirePointerCount index,
typename List<T>::Reader value) {
builder.getPointerField(index).setList(value.reader);
}
template <typename U>
static void set(StructBuilder builder, WirePointerCount index, std::initializer_list<U> value) {
auto l = init(builder, index, value.size());
uint i = 0;
for (auto& element: value) {
l.set(i++, element);
}
}
static inline typename List<T>::Builder init(
StructBuilder builder, WirePointerCount index, uint size) {
return typename List<T>::Builder(List<T>::initAsFieldOf(builder, index, size));
}
static inline void adopt(StructBuilder builder, WirePointerCount index, Orphan<List<T>>&& value) {
builder.getPointerField(index).adopt(kj::mv(value.builder));
}
static inline Orphan<List<T>> disown(StructBuilder builder, WirePointerCount index) {
return Orphan<List<T>>(builder.getPointerField(index).disown());
}
};
template <typename T>
struct PointerHelpers<T, Kind::BLOB> {
static inline typename T::Reader get(StructReader reader, WirePointerCount index,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return reader.getPointerField(index).getBlob<T>(defaultValue, defaultBytes * BYTES);
}
static inline typename T::Builder get(StructBuilder builder, WirePointerCount index,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return builder.getPointerField(index).getBlob<T>(defaultValue, defaultBytes * BYTES);
}
static inline void set(StructBuilder builder, WirePointerCount index, typename T::Reader value) {
builder.getPointerField(index).setBlob<T>(value);
}
static inline typename T::Builder init(StructBuilder builder, WirePointerCount index, uint size) {
return builder.getPointerField(index).initBlob<T>(size * BYTES);
}
static inline void adopt(StructBuilder builder, WirePointerCount index, Orphan<T>&& value) {
builder.getPointerField(index).adopt(kj::mv(value.builder));
}
static inline Orphan<T> disown(StructBuilder builder, WirePointerCount index) {
return Orphan<T>(builder.getPointerField(index).disown());
}
};
struct UncheckedMessage {
typedef const word* Reader;
};
template <>
struct PointerHelpers<UncheckedMessage> {
// Reads an Object field as an unchecked message pointer. Requires that the containing message is
// itself unchecked. This hack is currently private. It is used to locate default values within
// encoded schemas.
static inline const word* get(StructReader reader, WirePointerCount index) {
return reader.getPointerField(index).getUnchecked();
}
};
struct RawSchema {
// The generated code defines a constant RawSchema for every compiled declaration.
//
......
......@@ -168,33 +168,15 @@ struct List<T, Kind::PRIMITIVE> {
};
private:
inline static _::ListBuilder initAsElementOf(
_::ListBuilder& builder, uint index, uint size) {
return builder.getPointerElement(index * ELEMENTS)
.initList(_::elementSizeForType<T>(), size * ELEMENTS);
inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
return builder.initList(_::elementSizeForType<T>(), size * ELEMENTS);
}
inline static _::ListBuilder getAsElementOf(
_::ListBuilder& builder, uint index) {
return builder.getPointerElement(index * ELEMENTS)
.getList(_::elementSizeForType<T>(), nullptr);
inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
return builder.getList(_::elementSizeForType<T>(), defaultValue);
}
inline static _::ListReader getAsElementOf(
const _::ListReader& reader, uint index) {
return reader.getPointerElement(index * ELEMENTS)
.getList(_::elementSizeForType<T>(), nullptr);
}
inline static _::ListBuilder initAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.getPointerField(index).initList(_::elementSizeForType<T>(), size * ELEMENTS);
}
inline static _::ListBuilder getAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getPointerField(index).getList(_::elementSizeForType<T>(), defaultValue);
}
inline static _::ListReader getAsFieldOf(
const _::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getPointerField(index).getList(_::elementSizeForType<T>(), defaultValue);
inline static _::ListReader getFromPointer(
const _::PointerReader& reader, const word* defaultValue) {
return reader.getList(_::elementSizeForType<T>(), defaultValue);
}
template <typename U, Kind k>
......@@ -304,33 +286,15 @@ struct List<T, Kind::STRUCT> {
};
private:
inline static _::ListBuilder initAsElementOf(
_::ListBuilder& builder, uint index, uint size) {
return builder.getPointerElement(index * ELEMENTS)
.initStructList(size * ELEMENTS, _::structSize<T>());
}
inline static _::ListBuilder getAsElementOf(
_::ListBuilder& builder, uint index) {
return builder.getPointerElement(index * ELEMENTS)
.getStructList(_::structSize<T>(), nullptr);
}
inline static _::ListReader getAsElementOf(
const _::ListReader& reader, uint index) {
return reader.getPointerElement(index * ELEMENTS)
.getList(_::FieldSize::INLINE_COMPOSITE, nullptr);
}
inline static _::ListBuilder initAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.getPointerField(index).initStructList(size * ELEMENTS, _::structSize<T>());
inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
return builder.initStructList(size * ELEMENTS, _::structSize<T>());
}
inline static _::ListBuilder getAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getPointerField(index).getStructList(_::structSize<T>(), defaultValue);
inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
return builder.getStructList(_::structSize<T>(), defaultValue);
}
inline static _::ListReader getAsFieldOf(
const _::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getPointerField(index).getList(_::FieldSize::INLINE_COMPOSITE, defaultValue);
inline static _::ListReader getFromPointer(
const _::PointerReader& reader, const word* defaultValue) {
return reader.getList(_::FieldSize::INLINE_COMPOSITE, defaultValue);
}
template <typename U, Kind k>
......@@ -355,7 +319,8 @@ struct List<List<T>, Kind::LIST> {
inline uint size() const { return reader.size() / ELEMENTS; }
inline typename List<T>::Reader operator[](uint index) const {
KJ_IREQUIRE(index < size());
return typename List<T>::Reader(List<T>::getAsElementOf(reader, index));
return typename List<T>::Reader(
_::PointerHelpers<List<T>>::get(reader.getPointerElement(index * ELEMENTS)));
}
typedef _::IndexingIterator<const Reader, typename List<T>::Reader> Iterator;
......@@ -387,11 +352,13 @@ struct List<List<T>, Kind::LIST> {
inline uint size() const { return builder.size() / ELEMENTS; }
inline typename List<T>::Builder operator[](uint index) {
KJ_IREQUIRE(index < size());
return typename List<T>::Builder(List<T>::getAsElementOf(builder, index));
return typename List<T>::Builder(
_::PointerHelpers<List<T>>::get(builder.getPointerElement(index * ELEMENTS)));
}
inline typename List<T>::Builder init(uint index, uint size) {
KJ_IREQUIRE(index < this->size());
return typename List<T>::Builder(List<T>::initAsElementOf(builder, index, size));
return typename List<T>::Builder(
_::PointerHelpers<List<T>>::init(builder.getPointerElement(index * ELEMENTS), size));
}
inline void set(uint index, typename List<T>::Reader value) {
KJ_IREQUIRE(index < size());
......@@ -426,31 +393,15 @@ struct List<List<T>, Kind::LIST> {
};
private:
inline static _::ListBuilder initAsElementOf(
_::ListBuilder& builder, uint index, uint size) {
return builder.getPointerElement(index * ELEMENTS)
.initList(_::FieldSize::POINTER, size * ELEMENTS);
inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
return builder.initList(_::FieldSize::POINTER, size * ELEMENTS);
}
inline static _::ListBuilder getAsElementOf(
_::ListBuilder& builder, uint index) {
return builder.getPointerElement(index * ELEMENTS).getList(_::FieldSize::POINTER, nullptr);
inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
return builder.getList(_::FieldSize::POINTER, defaultValue);
}
inline static _::ListReader getAsElementOf(
const _::ListReader& reader, uint index) {
return reader.getPointerElement(index * ELEMENTS).getList(_::FieldSize::POINTER, nullptr);
}
inline static _::ListBuilder initAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.getPointerField(index).initList(_::FieldSize::POINTER, size * ELEMENTS);
}
inline static _::ListBuilder getAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getPointerField(index).getList(_::FieldSize::POINTER, defaultValue);
}
inline static _::ListReader getAsFieldOf(
const _::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getPointerField(index).getList(_::FieldSize::POINTER, defaultValue);
inline static _::ListReader getFromPointer(
const _::PointerReader& reader, const word* defaultValue) {
return reader.getList(_::FieldSize::POINTER, defaultValue);
}
template <typename U, Kind k>
......@@ -536,33 +487,15 @@ struct List<T, Kind::BLOB> {
};
private:
inline static _::ListBuilder initAsElementOf(
_::ListBuilder& builder, uint index, uint size) {
return builder.getPointerElement(index * ELEMENTS)
.initList(_::FieldSize::POINTER, size * ELEMENTS);
}
inline static _::ListBuilder getAsElementOf(
_::ListBuilder& builder, uint index) {
return builder.getPointerElement(index * ELEMENTS)
.getList(_::FieldSize::POINTER, nullptr);
}
inline static _::ListReader getAsElementOf(
const _::ListReader& reader, uint index) {
return reader.getPointerElement(index * ELEMENTS)
.getList(_::FieldSize::POINTER, nullptr);
}
inline static _::ListBuilder initAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.getPointerField(index).initList(_::FieldSize::POINTER, size * ELEMENTS);
inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
return builder.initList(_::FieldSize::POINTER, size * ELEMENTS);
}
inline static _::ListBuilder getAsFieldOf(
_::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getPointerField(index).getList(_::FieldSize::POINTER, defaultValue);
inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
return builder.getList(_::FieldSize::POINTER, defaultValue);
}
inline static _::ListReader getAsFieldOf(
const _::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getPointerField(index).getList(_::FieldSize::POINTER, defaultValue);
inline static _::ListReader getFromPointer(
const _::PointerReader& reader, const word* defaultValue) {
return reader.getList(_::FieldSize::POINTER, defaultValue);
}
template <typename U, Kind k>
......
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef CAPNP_POINTER_HELPERS_H_
#define CAPNP_POINTER_HELPERS_H_
#include "layout.h"
#include "list.h"
namespace capnp {
namespace _ { // private
// PointerHelpers is a template class that assists in wrapping/unwrapping the low-level types in
// layout.h with the high-level public API and generated types. This way, the code generator
// and other templates do not have to specialize on each kind of pointer.
template <typename T>
struct PointerHelpers<T, Kind::STRUCT> {
static inline typename T::Reader get(PointerReader reader, const word* defaultValue = nullptr) {
return typename T::Reader(reader.getStruct(defaultValue));
}
static inline typename T::Builder get(PointerBuilder builder,
const word* defaultValue = nullptr) {
return typename T::Builder(builder.getStruct(structSize<T>(), defaultValue));
}
static inline void set(PointerBuilder builder, typename T::Reader value) {
builder.setStruct(value._reader);
}
static inline typename T::Builder init(PointerBuilder builder) {
return typename T::Builder(builder.initStruct(structSize<T>()));
}
static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
builder.adopt(kj::mv(value.builder));
}
static inline Orphan<T> disown(PointerBuilder builder) {
return Orphan<T>(builder.disown());
}
};
template <typename T>
struct PointerHelpers<List<T>, Kind::LIST> {
static inline typename List<T>::Reader get(PointerReader reader,
const word* defaultValue = nullptr) {
return typename List<T>::Reader(List<T>::getFromPointer(reader, defaultValue));
}
static inline typename List<T>::Builder get(PointerBuilder builder,
const word* defaultValue = nullptr) {
return typename List<T>::Builder(List<T>::getFromPointer(builder, defaultValue));
}
static inline void set(PointerBuilder builder, typename List<T>::Reader value) {
builder.setList(value.reader);
}
template <typename U>
static void set(PointerBuilder builder, std::initializer_list<U> value) {
auto l = init(builder, value.size());
uint i = 0;
for (auto& element: value) {
l.set(i++, element);
}
}
static inline typename List<T>::Builder init(PointerBuilder builder, uint size) {
return typename List<T>::Builder(List<T>::initPointer(builder, size));
}
static inline void adopt(PointerBuilder builder, Orphan<List<T>>&& value) {
builder.adopt(kj::mv(value.builder));
}
static inline Orphan<List<T>> disown(PointerBuilder builder) {
return Orphan<List<T>>(builder.disown());
}
};
template <typename T>
struct PointerHelpers<T, Kind::BLOB> {
static inline typename T::Reader get(PointerReader reader,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return reader.getBlob<T>(defaultValue, defaultBytes * BYTES);
}
static inline typename T::Builder get(PointerBuilder builder,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return builder.getBlob<T>(defaultValue, defaultBytes * BYTES);
}
static inline void set(PointerBuilder builder, typename T::Reader value) {
builder.setBlob<T>(value);
}
static inline typename T::Builder init(PointerBuilder builder, uint size) {
return builder.initBlob<T>(size * BYTES);
}
static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
builder.adopt(kj::mv(value.builder));
}
static inline Orphan<T> disown(PointerBuilder builder) {
return Orphan<T>(builder.disown());
}
};
struct UncheckedMessage {
typedef const word* Reader;
};
template <>
struct PointerHelpers<UncheckedMessage> {
// Reads an Object field as an unchecked message pointer. Requires that the containing message is
// itself unchecked. This hack is currently private. It is used to locate default values within
// encoded schemas.
static inline const word* get(PointerReader reader) {
return reader.getUnchecked();
}
};
} // namespace _ (private)
} // namespace capnp
#endif // CAPNP_POINTER_HELPERS_H_
This diff is collapsed.
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