Commit 1a515be7 authored by Kenton Varda's avatar Kenton Varda

Simplify generated code by using PointerHelpers templates.

parent e4784bc8
......@@ -36,12 +36,19 @@ class DynamicStruct; // So that it can be declared a friend.
namespace internal {
template <typename T>
struct PointerHelpers {
static inline typename T::Reader get(StructReader reader, WireReferenceCount index) {
return typename T::Reader(reader.getStructField(index, nullptr));
struct PointerHelpers<T, Kind::STRUCT> {
static inline typename T::Reader get(StructReader reader, WireReferenceCount index,
const word* defaultValue = nullptr) {
return typename T::Reader(reader.getStructField(index, defaultValue));
}
static inline typename T::Builder get(StructBuilder builder, WireReferenceCount index) {
return typename T::Builder(builder.getStructField(index, structSize<T>(), nullptr));
static inline typename T::Builder get(StructBuilder builder, WireReferenceCount index,
const word* defaultValue = nullptr) {
return typename T::Builder(builder.getStructField(index, structSize<T>(), defaultValue));
}
static inline void set(StructBuilder builder, WireReferenceCount index,
typename T::Reader value) {
// TODO(soon)
CAPNPROTO_INLINE_PRECOND(false, "Not implemented: set() for struct fields.");
}
static inline typename T::Builder init(StructBuilder builder, WireReferenceCount index) {
return typename T::Builder(builder.initStructField(index, structSize<T>()));
......@@ -49,48 +56,46 @@ struct PointerHelpers {
};
template <typename T>
struct PointerHelpers<List<T>> {
static inline typename List<T>::Reader get(StructReader reader, WireReferenceCount index) {
return typename List<T>::Reader(List<T>::getAsFieldOf(reader, index));
}
static inline typename List<T>::Builder get(StructBuilder builder, WireReferenceCount index) {
return typename List<T>::Builder(List<T>::getAsFieldOf(builder, index));
}
static inline typename List<T>::Builder init(
StructBuilder builder, WireReferenceCount index, int size) {
return typename List<T>::Builder(List<T>::initAsFieldOf(builder, index, size));
struct PointerHelpers<List<T>, Kind::LIST> {
static inline typename List<T>::Reader get(StructReader reader, WireReferenceCount index,
const word* defaultValue = nullptr) {
return typename List<T>::Reader(List<T>::getAsFieldOf(reader, index, defaultValue));
}
};
template <>
struct PointerHelpers<Text> {
static inline Text::Reader get(StructReader reader, WireReferenceCount index) {
return reader.getBlobField<Text>(index, nullptr, 0 * BYTES);
static inline typename List<T>::Builder get(StructBuilder builder, WireReferenceCount index,
const word* defaultValue = nullptr) {
return typename List<T>::Builder(List<T>::getAsFieldOf(builder, index, defaultValue));
}
static inline Text::Builder get(StructBuilder builder, WireReferenceCount index) {
return builder.getBlobField<Text>(index, nullptr, 0 * BYTES);
static inline void set(StructBuilder builder, WireReferenceCount index,
typename List<T>::Reader value) {
init(builder, index, value.size()).copyFrom(value);
}
static inline void set(StructBuilder builder, WireReferenceCount index, Text::Reader value) {
builder.setBlobField<Text>(index, value);
static inline void set(StructBuilder builder, WireReferenceCount index,
std::initializer_list<ReaderFor<T>> value) {
init(builder, index, value.size()).copyFrom(value);
}
static inline Text::Builder init(StructBuilder builder, WireReferenceCount index, int size) {
return builder.initBlobField<Text>(index, size * BYTES);
static inline typename List<T>::Builder init(
StructBuilder builder, WireReferenceCount index, uint size) {
return typename List<T>::Builder(List<T>::initAsFieldOf(builder, index, size));
}
};
template <>
struct PointerHelpers<Data> {
static inline Data::Reader get(StructReader reader, WireReferenceCount index) {
return reader.getBlobField<Data>(index, nullptr, 0 * BYTES);
template <typename T>
struct PointerHelpers<T, Kind::BLOB> {
static inline typename T::Reader get(StructReader reader, WireReferenceCount index,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return reader.getBlobField<T>(index, defaultValue, defaultBytes * BYTES);
}
static inline Data::Builder get(StructBuilder builder, WireReferenceCount index) {
return builder.getBlobField<Data>(index, nullptr, 0 * BYTES);
static inline typename T::Builder get(StructBuilder builder, WireReferenceCount index,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return builder.getBlobField<T>(index, defaultValue, defaultBytes * BYTES);
}
static inline void set(StructBuilder builder, WireReferenceCount index, Data::Reader value) {
builder.setBlobField<Data>(index, value);
static inline void set(StructBuilder builder, WireReferenceCount index, typename T::Reader value) {
builder.setBlobField<T>(index, value);
}
static inline Data::Builder init(StructBuilder builder, WireReferenceCount index, int size) {
return builder.initBlobField<Data>(index, size * BYTES);
static inline typename T::Builder init(StructBuilder builder, WireReferenceCount index, uint size) {
return builder.initBlobField<T>(index, size * BYTES);
}
};
......
......@@ -486,7 +486,7 @@ public:
: segment(nullptr), ptr(nullptr), elementCount(0 * ELEMENTS),
step(0 * BITS / ELEMENTS) {}
inline ElementCount size();
inline ElementCount size() const;
// The number of elements in the list.
Text::Builder asText();
......@@ -570,7 +570,7 @@ public:
: segment(nullptr), ptr(nullptr), elementCount(0), step(0 * BITS / ELEMENTS),
structDataSize(0), structReferenceCount(0), nestingLimit(0) {}
inline ElementCount size();
inline ElementCount size() const;
// The number of elements in the list.
Text::Reader asText();
......@@ -767,7 +767,7 @@ T StructReader::getDataField(ElementCount offset, Mask<T> mask) const {
// -------------------------------------------------------------------
inline ElementCount ListBuilder::size() { return elementCount; }
inline ElementCount ListBuilder::size() const { return elementCount; }
template <typename T>
inline T ListBuilder::getDataElement(ElementCount index) const {
......@@ -814,7 +814,7 @@ inline void ListBuilder::setDataElement<Void>(ElementCount index, Void value) co
// -------------------------------------------------------------------
inline ElementCount ListReader::size() { return elementCount; }
inline ElementCount ListReader::size() const { return elementCount; }
template <typename T>
inline T ListReader::getDataElement(ElementCount index) const {
......
This diff is collapsed.
......@@ -287,6 +287,9 @@ fieldContext parent desc = mkStrContext context where
context "fieldTitleCase" = MuVariable $ toTitleCase $ fieldName desc
context "fieldUpperCase" = MuVariable $ toUpperCaseWithUnderscores $ fieldName desc
context "fieldIsPrimitive" = MuBool $ isPrimitive $ fieldType desc
context "fieldIsListOrBlob" = MuBool $ isBlob (fieldType desc) || isList (fieldType desc)
context "fieldIsBlob" = MuBool $ isBlob $ fieldType desc
context "fieldIsInlineBlob" = MuBool $ isInlineBlob $ fieldType desc
context "fieldIsStruct" = MuBool $ isStruct $ fieldType desc
......@@ -332,6 +335,11 @@ fieldContext parent desc = mkStrContext context where
MuVariable $ cxxFieldSizeString $ fieldSize $ inlineElementType $ fieldType desc
context "fieldElementType" =
MuVariable $ cxxTypeString $ elementType $ fieldType desc
context "fieldElementReaderType" = MuVariable readerString where
readerString = if isPrimitiveList $ fieldType desc
then tString
else tString ++ "::Reader"
tString = cxxTypeString $ elementType $ fieldType desc
context "fieldInlineElementType" =
MuVariable $ cxxTypeString $ inlineElementType $ fieldType desc
context "fieldUnion" = case fieldUnion desc of
......
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