Commit e4784bc8 authored by Kenton Varda's avatar Kenton Varda

Various refactoring and cleanup.

parent f017105e
......@@ -41,10 +41,10 @@ struct PointerHelpers {
return typename T::Reader(reader.getStructField(index, nullptr));
}
static inline typename T::Builder get(StructBuilder builder, WireReferenceCount index) {
return typename T::Builder(builder.getStructField(index, T::STRUCT_SIZE, nullptr));
return typename T::Builder(builder.getStructField(index, structSize<T>(), nullptr));
}
static inline typename T::Builder init(StructBuilder builder, WireReferenceCount index) {
return typename T::Builder(builder.initStructField(index, T::STRUCT_SIZE));
return typename T::Builder(builder.initStructField(index, structSize<T>()));
}
};
......@@ -65,36 +65,36 @@ struct PointerHelpers<List<T>> {
template <>
struct PointerHelpers<Text> {
static inline Text::Reader get(StructReader reader, WireReferenceCount index) {
return reader.getTextField(index, nullptr, 0 * BYTES);
return reader.getBlobField<Text>(index, nullptr, 0 * BYTES);
}
static inline Text::Builder get(StructBuilder builder, WireReferenceCount index) {
return builder.getTextField(index, nullptr, 0 * BYTES);
return builder.getBlobField<Text>(index, nullptr, 0 * BYTES);
}
static inline void set(StructBuilder builder, WireReferenceCount index, Text::Reader value) {
builder.setTextField(index, value);
builder.setBlobField<Text>(index, value);
}
static inline Text::Builder init(StructBuilder builder, WireReferenceCount index, int size) {
return builder.initTextField(index, size * BYTES);
return builder.initBlobField<Text>(index, size * BYTES);
}
};
template <>
struct PointerHelpers<Data> {
static inline Data::Reader get(StructReader reader, WireReferenceCount index) {
return reader.getDataField(index, nullptr, 0 * BYTES);
return reader.getBlobField<Data>(index, nullptr, 0 * BYTES);
}
static inline Data::Builder get(StructBuilder builder, WireReferenceCount index) {
return builder.getDataField(index, nullptr, 0 * BYTES);
return builder.getBlobField<Data>(index, nullptr, 0 * BYTES);
}
static inline void set(StructBuilder builder, WireReferenceCount index, Data::Reader value) {
builder.setDataField(index, value);
builder.setBlobField<Data>(index, value);
}
static inline Data::Builder init(StructBuilder builder, WireReferenceCount index, int size) {
return builder.initDataField(index, size * BYTES);
return builder.initBlobField<Data>(index, size * BYTES);
}
};
#ifdef CAPNPROTO_PRIVATE
#if defined(CAPNPROTO_PRIVATE) || defined(__CDT_PARSER__)
struct TrustedMessage {
typedef const word* Reader;
......@@ -116,4 +116,15 @@ struct PointerHelpers<TrustedMessage> {
} // namespace internal
} // namespace capnproto
#define CAPNPROTO_DECLARE_ENUM(type) \
template <> struct KindOf<type> { static constexpr Kind kind = Kind::ENUM; }
#define CAPNPROTO_DECLARE_STRUCT(type, dataWordSize, pointerCount, preferredElementEncoding) \
template <> struct KindOf<type> { static constexpr Kind kind = Kind::STRUCT; }; \
template <> struct StructSizeFor<type> { \
static constexpr StructSize value = StructSize( \
dataWordSize * WORDS, pointerCount * REFERENCES, FieldSize::preferredElementEncoding); \
}
#define CAPNPROTO_DECLARE_INTERFACE(type) \
template <> struct KindOf<type> { static constexpr Kind kind = Kind::INTERFACE; }
#endif // CAPNPROTO_GENERATED_HEADER_SUPPORT_H_
......@@ -1119,25 +1119,31 @@ ListBuilder StructBuilder::getListField(
references + refIndex, segment, defaultValue);
}
Text::Builder StructBuilder::initTextField(WireReferenceCount refIndex, ByteCount size) const {
template <>
Text::Builder StructBuilder::initBlobField<Text>(WireReferenceCount refIndex, ByteCount size) const {
return WireHelpers::initTextReference(references + refIndex, segment, size);
}
void StructBuilder::setTextField(WireReferenceCount refIndex, Text::Reader value) const {
template <>
void StructBuilder::setBlobField<Text>(WireReferenceCount refIndex, Text::Reader value) const {
WireHelpers::setTextReference(references + refIndex, segment, value);
}
Text::Builder StructBuilder::getTextField(
template <>
Text::Builder StructBuilder::getBlobField<Text>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
return WireHelpers::getWritableTextReference(
references + refIndex, segment, defaultValue, defaultSize);
}
Data::Builder StructBuilder::initDataField(WireReferenceCount refIndex, ByteCount size) const {
template <>
Data::Builder StructBuilder::initBlobField<Data>(WireReferenceCount refIndex, ByteCount size) const {
return WireHelpers::initDataReference(references + refIndex, segment, size);
}
void StructBuilder::setDataField(WireReferenceCount refIndex, Data::Reader value) const {
template <>
void StructBuilder::setBlobField<Data>(WireReferenceCount refIndex, Data::Reader value) const {
WireHelpers::setDataReference(references + refIndex, segment, value);
}
Data::Builder StructBuilder::getDataField(
template <>
Data::Builder StructBuilder::getBlobField<Data>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
return WireHelpers::getWritableDataReference(
references + refIndex, segment, defaultValue, defaultSize);
......@@ -1148,11 +1154,6 @@ ObjectBuilder StructBuilder::getObjectField(
return WireHelpers::getWritableObjectReference(segment, references + refIndex, defaultValue);
}
const word* StructBuilder::getTrustedPointer(WireReferenceCount refIndex) const {
PRECOND(segment == nullptr, "getTrustedPointer() only allowed on trusted messages.");
return reinterpret_cast<const word*>(references + refIndex);
}
StructReader StructBuilder::asReader() const {
return StructReader(segment, data, references,
dataSize, referenceCount, bit0Offset, std::numeric_limits<int>::max());
......@@ -1190,13 +1191,15 @@ ListReader StructReader::getListField(
segment, ref, defaultValue, expectedElementSize, nestingLimit);
}
Text::Reader StructReader::getTextField(
template <>
Text::Reader StructReader::getBlobField<Text>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
const WireReference* ref = refIndex >= referenceCount ? nullptr : references + refIndex;
return WireHelpers::readTextReference(segment, ref, defaultValue, defaultSize);
}
Data::Reader StructReader::getDataField(
template <>
Data::Reader StructReader::getBlobField<Data>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
const WireReference* ref = refIndex >= referenceCount ? nullptr : references + refIndex;
return WireHelpers::readDataReference(segment, ref, defaultValue, defaultSize);
......@@ -1208,6 +1211,11 @@ ObjectReader StructReader::getObjectField(
segment, references + refIndex, defaultValue, nestingLimit);
}
const word* StructReader::getTrustedPointer(WireReferenceCount refIndex) const {
PRECOND(segment == nullptr, "getTrustedPointer() only allowed on trusted messages.");
return reinterpret_cast<const word*>(references + refIndex);
}
// =======================================================================================
// ListBuilder
......@@ -1269,28 +1277,34 @@ ListBuilder ListBuilder::getListElement(ElementCount index) const {
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr);
}
Text::Builder ListBuilder::initTextElement(ElementCount index, ByteCount size) const {
template <>
Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) const {
return WireHelpers::initTextReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, size);
}
void ListBuilder::setTextElement(ElementCount index, Text::Reader value) const {
template <>
void ListBuilder::setBlobElement<Text>(ElementCount index, Text::Reader value) const {
WireHelpers::setTextReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, value);
}
Text::Builder ListBuilder::getTextElement(ElementCount index) const {
template <>
Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) const {
return WireHelpers::getWritableTextReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, "", 0 * BYTES);
}
Data::Builder ListBuilder::initDataElement(ElementCount index, ByteCount size) const {
template <>
Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) const {
return WireHelpers::initDataReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, size);
}
void ListBuilder::setDataElement(ElementCount index, Data::Reader value) const {
template <>
void ListBuilder::setBlobElement<Data>(ElementCount index, Data::Reader value) const {
WireHelpers::setDataReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, value);
}
Data::Builder ListBuilder::getDataElement(ElementCount index) const {
template <>
Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) const {
return WireHelpers::getWritableDataReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr,
0 * BYTES);
......@@ -1375,13 +1389,15 @@ ListReader ListReader::getListElement(
nullptr, expectedElementSize, nestingLimit);
}
Text::Reader ListReader::getTextElement(ElementCount index) const {
template <>
Text::Reader ListReader::getBlobElement<Text>(ElementCount index) const {
return WireHelpers::readTextReference(
segment, checkAlignment(ptr + index * step / BITS_PER_BYTE),
"", 0 * BYTES);
}
Data::Reader ListReader::getDataElement(ElementCount index) const {
template <>
Data::Reader ListReader::getBlobElement<Data>(ElementCount index) const {
return WireHelpers::readDataReference(
segment, checkAlignment(ptr + index * step / BITS_PER_BYTE),
nullptr, 0 * BYTES);
......
This diff is collapsed.
This diff is collapsed.
......@@ -275,12 +275,12 @@ inline typename RootType::Reader MessageReader::getRoot() {
template <typename RootType>
inline typename RootType::Builder MessageBuilder::initRoot() {
return typename RootType::Builder(initRoot(RootType::STRUCT_SIZE));
return typename RootType::Builder(initRoot(internal::structSize<RootType>()));
}
template <typename RootType>
inline typename RootType::Builder MessageBuilder::getRoot() {
return typename RootType::Builder(getRoot(RootType::STRUCT_SIZE));
return typename RootType::Builder(getRoot(internal::structSize<RootType>()));
}
template <typename RootType>
......
......@@ -81,7 +81,7 @@ T instance() noexcept;
// definition. So...
namespace internal {
struct PlacementNew {} placementNew;
struct PlacementNew {};
} // namespace internal;
} // namespace capnproto
......@@ -93,7 +93,7 @@ namespace capnproto {
template <typename T, typename... Params>
void constructAt(T* location, Params&&... params) {
new (internal::placementNew, location) T(capnproto::forward<Params>(params)...);
new (internal::PlacementNew(), location) T(capnproto::forward<Params>(params)...);
}
// =======================================================================================
......
This diff is collapsed.
......@@ -34,8 +34,6 @@ namespace {{namespaceName}} {
{{#fileTypes}}
{{#typeStructOrUnion}}
{{#typeStruct}}
constexpr ::capnproto::internal::StructSize {{structFullName}}::STRUCT_SIZE;
{{#structFields}}
{{#fieldDefaultBytes}}
const ::capnproto::internal::AlignedData<{{defaultWordCount}}>
......
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