Commit 80722b03 authored by Kenton Varda's avatar Kenton Varda

Revamp dynamic struct getters/setters to not distinguish unions. Add…

Revamp dynamic struct getters/setters to not distinguish unions.  Add converience versions that take string names.
parent 449f4b23
......@@ -39,7 +39,7 @@ TEST(Blob, Text) {
EXPECT_STREQ("foo", text.data());
EXPECT_EQ(3u, text.size());
std::string str2 = text;
std::string str2 = text.as<std::string>();
EXPECT_EQ("foo", str2);
Text::Reader text2 = "bar";
......@@ -62,7 +62,7 @@ TEST(Blob, Data) {
EXPECT_EQ("foo", data);
EXPECT_EQ(3u, data.size());
std::string str2 = data;
std::string str2 = data.as<std::string>();
EXPECT_EQ("foo", str2);
Data::Reader data2 = "bar";
......
......@@ -66,10 +66,6 @@ public:
inline Reader(const T& other): bytes(other.data()), size_(other.size()) {}
// Primarily intended for converting from std::string.
template <typename T>
inline operator T() const { return T(bytes, size_); }
// Primarily intended for converting to std::string.
template <typename T>
inline T as() const { return T(bytes, size_); }
// Explicitly converts to the desired type, which must have a (const char*, size) constructor.
......
This diff is collapsed.
This diff is collapsed.
......@@ -70,6 +70,12 @@ template <typename T>
using FromBuilder = typename RemoveReference<T>::Builds;
// FromBuilder<MyType::Builder> = MyType (for any Cap'n Proto type).
template <typename T, Kind k = kind<T>()> struct TypeIfEnum_;
template <typename T> struct TypeIfEnum_<T, Kind::ENUM> { typedef T Type; };
template <typename T>
using TypeIfEnum = typename TypeIfEnum_<RemoveReference<T>>::Type;
namespace internal {
template <typename T, Kind k> struct KindOf<List<T, k>> { static constexpr Kind kind = Kind::LIST; };
......@@ -229,11 +235,11 @@ struct List<T, Kind::PRIMITIVE> {
set(pos, *i);
}
CAPNPROTO_INLINE_DPRECOND(pos == size() && i == end,
"List::copyFrom() argument had different size.");
"List::copyFrom() argument had different size.");
}
void copyFrom(std::initializer_list<T> other) {
CAPNPROTO_INLINE_DPRECOND(other.size() == size(),
"List::copyFrom() argument had different size.");
"List::copyFrom() argument had different size.");
for (uint i = 0; i < other.size(); i++) {
set(i, other.begin()[i]);
}
......
......@@ -198,6 +198,11 @@ struct StructNode {
ordinal @1 :UInt16;
index @7 :UInt16;
# The index of this member within the containing struct or union's member list. This is
# redundant information, but it can be useful for the dynamic API which has methods that take
# a Member pointer to specify on which member to act.
codeOrder @2 :UInt16;
# Indicates where this member appeared in the code, relative to other members.
# Code ordering may have semantic relevance -- programmers tend to place related fields
......@@ -219,11 +224,6 @@ struct StructNode {
}
struct Field {
index @3 :UInt16;
# The index of this field within the containing struct or union's member list. This is
# redundant information, but it can be useful for the dynamic API which uses Field pointers as
# identifiers.
offset @0 :UInt32;
# Offset, in units of the field's size, from the beginning of the section in which the field
# resides. E.g. for a UInt32 field, multiply this by 4 to get the byte offset from the
......
......@@ -617,15 +617,15 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
dataValues2 = [ (0, encUInt16 $ fieldNumber field)
, (16, encUInt16 codeOrder)
, (32, encUInt16 (0::Word16)) -- discriminant
, (48, encUInt16 index)
]
ptrValues2 = [ (0, encText $ fieldName field)
, (1, encAnnotationList $ fieldAnnotations field)
, (2, encStruct (DataSectionWords 1, 2) (dataValues3, ptrValues3))
, (2, encStruct (DataSection32, 2) (dataValues3, ptrValues3))
]
-- StructNode.Field
dataValues3 = [ (0, encUInt32 $ offsetToInt $ fieldOffset field)
, (32, encUInt16 index) ]
dataValues3 = [ (0, encUInt32 $ offsetToInt $ fieldOffset field) ]
ptrValues3 = [ (0, encStruct typeSize $ encType $ fieldType field)
, (1, encStruct valueSize $ encValue (fieldType field) $
fieldDefaultValue field)
......@@ -637,10 +637,11 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
offsetToInt (InlineCompositeOffset {}) =
error "Inline types not currently supported by codegen plugins."
encMember _ (codeOrder, (_, DescUnion union)) = (dataValues2, ptrValues2) where
encMember index (codeOrder, (_, DescUnion union)) = (dataValues2, ptrValues2) where
dataValues2 = [ (0, encUInt16 $ unionNumber union)
, (16, encUInt16 codeOrder)
, (32, encUInt16 (1::Word16)) -- discriminant
, (48, encUInt16 index)
]
ptrValues2 = [ (0, encText $ unionName union)
, (1, encAnnotationList $ unionAnnotations union)
......
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