Add the ability to derive message types from one another.
`DAP_IMPLEMENT_STRUCT_TYPEINFO_EXT` and `DAP_STRUCT_TYPEINFO_EXT` are two new flavors of `DAP_IMPLEMENT_STRUCT_TYPEINFO` and `DAP_STRUCT_TYPEINFO` that allow you to derive message types. This involved a bit of reworking on the serializer interfaces. Added test. Issue: #32
This commit is contained in:
@@ -78,10 +78,6 @@ class Deserializer {
|
||||
template <typename T0, typename... Types>
|
||||
inline bool deserialize(dap::variant<T0, Types...>*) const;
|
||||
|
||||
// deserialize() decodes a list of fields and stores them into the object.
|
||||
inline bool deserialize(void* object,
|
||||
const std::initializer_list<Field>&) const;
|
||||
|
||||
// deserialize() decodes the struct field f with the given name.
|
||||
template <typename T>
|
||||
inline bool field(const std::string& name, T* f) const;
|
||||
@@ -117,20 +113,6 @@ bool Deserializer::deserialize(dap::variant<T0, Types...>* var) const {
|
||||
return deserialize(&var->value);
|
||||
}
|
||||
|
||||
bool Deserializer::deserialize(
|
||||
void* object,
|
||||
const std::initializer_list<Field>& fields) const {
|
||||
for (auto const& f : fields) {
|
||||
if (!field(f.name, [&](Deserializer* d) {
|
||||
auto ptr = reinterpret_cast<uint8_t*>(object) + f.offset;
|
||||
return f.type->deserialize(d, ptr);
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Deserializer::field(const std::string& name, T* v) const {
|
||||
return this->field(name,
|
||||
@@ -140,6 +122,7 @@ bool Deserializer::field(const std::string& name, T* v) const {
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Serializer
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class FieldSerializer;
|
||||
|
||||
// Serializer is the interface used to encode data to structured storage.
|
||||
// A Serializer is associated with a single storage object, whos type and value
|
||||
@@ -149,16 +132,12 @@ bool Deserializer::field(const std::string& name, T* v) const {
|
||||
// Methods that return a bool use this to indicate success.
|
||||
class Serializer {
|
||||
public:
|
||||
using FieldSerializer = std::function<bool(Serializer*)>;
|
||||
template <typename T>
|
||||
using IsFieldSerializer = std::is_convertible<T, FieldSerializer>;
|
||||
|
||||
// serialization methods for simple data types.
|
||||
virtual bool serialize(boolean) = 0;
|
||||
virtual bool serialize(integer) = 0;
|
||||
virtual bool serialize(number) = 0;
|
||||
virtual bool serialize(const string&) = 0;
|
||||
virtual bool serialize(const object&) = 0;
|
||||
virtual bool serialize(const dap::object&) = 0;
|
||||
virtual bool serialize(const any&) = 0;
|
||||
|
||||
// array() encodes count array elements to the array object referenced by this
|
||||
@@ -166,14 +145,10 @@ class Serializer {
|
||||
// Serializer that should be used to encode the n'th array element's data.
|
||||
virtual bool array(size_t count, const std::function<bool(Serializer*)>&) = 0;
|
||||
|
||||
// fields() encodes all the provided fields of the given object.
|
||||
virtual bool fields(const void* object,
|
||||
const std::initializer_list<Field>&) = 0;
|
||||
|
||||
// field() encodes a field to the struct object referenced by this Serializer.
|
||||
// The FieldSerializer will be called with a Serializer used to encode the
|
||||
// field's data.
|
||||
virtual bool field(const std::string& name, const FieldSerializer&) = 0;
|
||||
// object() begins encoding the object referenced by this Serializer.
|
||||
// The std::function will be called with a FieldSerializer to serialize the
|
||||
// object's fields.
|
||||
virtual bool object(const std::function<bool(dap::FieldSerializer*)>&) = 0;
|
||||
|
||||
// remove() deletes the object referenced by this Serializer.
|
||||
// remove() can be used to serialize optionals with no value assigned.
|
||||
@@ -198,12 +173,6 @@ class Serializer {
|
||||
|
||||
// deserialize() encodes the given string.
|
||||
inline bool serialize(const char* v);
|
||||
|
||||
// field() encodes the field with the given name and value.
|
||||
template <
|
||||
typename T,
|
||||
typename = typename std::enable_if<!IsFieldSerializer<T>::value>::type>
|
||||
inline bool field(const std::string& name, const T& v);
|
||||
};
|
||||
|
||||
template <typename T, typename>
|
||||
@@ -235,8 +204,31 @@ bool Serializer::serialize(const char* v) {
|
||||
return serialize(std::string(v));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// FieldSerializer
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// FieldSerializer is the interface used to serialize fields of an object.
|
||||
class FieldSerializer {
|
||||
public:
|
||||
using SerializeFunc = std::function<bool(Serializer*)>;
|
||||
template <typename T>
|
||||
using IsSerializeFunc = std::is_convertible<T, SerializeFunc>;
|
||||
|
||||
// field() encodes a field to the struct object referenced by this Serializer.
|
||||
// The SerializeFunc will be called with a Serializer used to encode the
|
||||
// field's data.
|
||||
virtual bool field(const std::string& name, const SerializeFunc&) = 0;
|
||||
|
||||
// field() encodes the field with the given name and value.
|
||||
template <
|
||||
typename T,
|
||||
typename = typename std::enable_if<!IsSerializeFunc<T>::value>::type>
|
||||
inline bool field(const std::string& name, const T& v);
|
||||
};
|
||||
|
||||
template <typename T, typename>
|
||||
bool Serializer::field(const std::string& name, const T& v) {
|
||||
bool FieldSerializer::field(const std::string& name, const T& v) {
|
||||
return this->field(name, [&](Serializer* s) { return s->serialize(v); });
|
||||
}
|
||||
|
||||
|
||||
@@ -136,46 +136,127 @@ M member_type(M T::*);
|
||||
// NAME is the serialized name of the field, as described by the DAP
|
||||
// specification.
|
||||
#define DAP_FIELD(FIELD, NAME) \
|
||||
dap::Field { \
|
||||
::dap::Field { \
|
||||
NAME, DAP_OFFSETOF(StructTy, FIELD), \
|
||||
TypeOf<DAP_TYPEOF(StructTy, FIELD)>::type(), \
|
||||
}
|
||||
|
||||
// DAP_DECLARE_STRUCT_TYPEINFO() declares a TypeOf<> specialization for STRUCT.
|
||||
#define DAP_DECLARE_STRUCT_TYPEINFO(STRUCT) \
|
||||
template <> \
|
||||
struct TypeOf<STRUCT> { \
|
||||
static constexpr bool has_custom_serialization = true; \
|
||||
static const TypeInfo* type(); \
|
||||
// Must be used within the 'dap' namespace.
|
||||
#define DAP_DECLARE_STRUCT_TYPEINFO(STRUCT) \
|
||||
template <> \
|
||||
struct TypeOf<STRUCT> { \
|
||||
static constexpr bool has_custom_serialization = true; \
|
||||
static const TypeInfo* type(); \
|
||||
static bool deserializeFields(const Deserializer*, void* obj); \
|
||||
static bool serializeFields(FieldSerializer*, const void* obj); \
|
||||
}
|
||||
|
||||
// DAP_DECLARE_STRUCT_TYPEINFO() implements the type() member function for the
|
||||
// DAP_IMPLEMENT_STRUCT_FIELD_SERIALIZATION() implements the deserializeFields()
|
||||
// and serializeFields() static methods of a TypeOf<> specialization. Used
|
||||
// internally by DAP_IMPLEMENT_STRUCT_TYPEINFO() and
|
||||
// DAP_IMPLEMENT_STRUCT_TYPEINFO_EXT().
|
||||
// You probably do not want to use this directly.
|
||||
#define DAP_IMPLEMENT_STRUCT_FIELD_SERIALIZATION(STRUCT, NAME, ...) \
|
||||
bool TypeOf<STRUCT>::deserializeFields(const Deserializer* d, void* obj) { \
|
||||
using StructTy = STRUCT; \
|
||||
(void)sizeof(StructTy); /* avoid unused 'using' warning */ \
|
||||
for (auto field : std::initializer_list<Field>{__VA_ARGS__}) { \
|
||||
if (!d->field(field.name, [&](Deserializer* d) { \
|
||||
auto ptr = reinterpret_cast<uint8_t*>(obj) + field.offset; \
|
||||
return field.type->deserialize(d, ptr); \
|
||||
})) { \
|
||||
return false; \
|
||||
} \
|
||||
} \
|
||||
return true; \
|
||||
} \
|
||||
bool TypeOf<STRUCT>::serializeFields(FieldSerializer* s, const void* obj) { \
|
||||
using StructTy = STRUCT; \
|
||||
(void)sizeof(StructTy); /* avoid unused 'using' warning */ \
|
||||
for (auto field : std::initializer_list<Field>{__VA_ARGS__}) { \
|
||||
if (!s->field(field.name, [&](Serializer* s) { \
|
||||
auto ptr = reinterpret_cast<const uint8_t*>(obj) + field.offset; \
|
||||
return field.type->serialize(s, ptr); \
|
||||
})) { \
|
||||
return false; \
|
||||
} \
|
||||
} \
|
||||
return true; \
|
||||
}
|
||||
|
||||
// DAP_IMPLEMENT_STRUCT_TYPEINFO() implements the type() member function for the
|
||||
// TypeOf<> specialization for STRUCT.
|
||||
// STRUCT is the structure typename.
|
||||
// NAME is the serialized name of the structure, as described by the DAP
|
||||
// specification. The variadic (...) parameters should be a repeated list of
|
||||
// DAP_FIELD()s, one for each field of the struct.
|
||||
#define DAP_IMPLEMENT_STRUCT_TYPEINFO(STRUCT, NAME, ...) \
|
||||
const TypeInfo* TypeOf<STRUCT>::type() { \
|
||||
using StructTy = STRUCT; \
|
||||
struct TI : BasicTypeInfo<StructTy> { \
|
||||
TI() : BasicTypeInfo<StructTy>(NAME) {} \
|
||||
bool deserialize(const Deserializer* d, void* ptr) const override { \
|
||||
return d->deserialize(ptr, {__VA_ARGS__}); \
|
||||
// Must be used within the 'dap' namespace.
|
||||
#define DAP_IMPLEMENT_STRUCT_TYPEINFO(STRUCT, NAME, ...) \
|
||||
DAP_IMPLEMENT_STRUCT_FIELD_SERIALIZATION(STRUCT, NAME, __VA_ARGS__) \
|
||||
const ::dap::TypeInfo* TypeOf<STRUCT>::type() { \
|
||||
struct TI : BasicTypeInfo<STRUCT> { \
|
||||
TI() : BasicTypeInfo<STRUCT>(NAME) {} \
|
||||
bool deserialize(const Deserializer* d, void* obj) const override { \
|
||||
return deserializeFields(d, obj); \
|
||||
} \
|
||||
bool serialize(Serializer* s, const void* obj) const override { \
|
||||
return s->object( \
|
||||
[&](FieldSerializer* fs) { return serializeFields(fs, obj); }); \
|
||||
} \
|
||||
}; \
|
||||
static TI typeinfo; \
|
||||
return &typeinfo; \
|
||||
}
|
||||
|
||||
// DAP_STRUCT_TYPEINFO() is a helper for declaring and implementing a TypeOf<>
|
||||
// specialization for STRUCT in a single statement.
|
||||
// Must be used within the 'dap' namespace.
|
||||
#define DAP_STRUCT_TYPEINFO(STRUCT, NAME, ...) \
|
||||
DAP_DECLARE_STRUCT_TYPEINFO(STRUCT); \
|
||||
DAP_IMPLEMENT_STRUCT_TYPEINFO(STRUCT, NAME, __VA_ARGS__)
|
||||
|
||||
// DAP_IMPLEMENT_STRUCT_TYPEINFO_EXT() implements the type() member function for
|
||||
// the TypeOf<> specialization for STRUCT that derives from BASE.
|
||||
// STRUCT is the structure typename.
|
||||
// BASE is the base structure typename.
|
||||
// NAME is the serialized name of the structure, as described by the DAP
|
||||
// specification. The variadic (...) parameters should be a repeated list of
|
||||
// DAP_FIELD()s, one for each field of the struct.
|
||||
// Must be used within the 'dap' namespace.
|
||||
#define DAP_IMPLEMENT_STRUCT_TYPEINFO_EXT(STRUCT, BASE, NAME, ...) \
|
||||
static_assert(std::is_base_of<BASE, STRUCT>::value, \
|
||||
#STRUCT " does not derive from " #BASE); \
|
||||
DAP_IMPLEMENT_STRUCT_FIELD_SERIALIZATION(STRUCT, NAME, __VA_ARGS__) \
|
||||
const ::dap::TypeInfo* TypeOf<STRUCT>::type() { \
|
||||
struct TI : BasicTypeInfo<STRUCT> { \
|
||||
TI() : BasicTypeInfo<STRUCT>(NAME) {} \
|
||||
bool deserialize(const Deserializer* d, void* obj) const override { \
|
||||
auto derived = static_cast<STRUCT*>(obj); \
|
||||
auto base = static_cast<BASE*>(obj); \
|
||||
return TypeOf<BASE>::deserializeFields(d, base) && \
|
||||
deserializeFields(d, derived); \
|
||||
} \
|
||||
bool serialize(Serializer* s, const void* ptr) const override { \
|
||||
return s->fields(ptr, {__VA_ARGS__}); \
|
||||
bool serialize(Serializer* s, const void* obj) const override { \
|
||||
return s->object([&](FieldSerializer* fs) { \
|
||||
auto derived = static_cast<const STRUCT*>(obj); \
|
||||
auto base = static_cast<const BASE*>(obj); \
|
||||
return TypeOf<BASE>::serializeFields(fs, base) && \
|
||||
serializeFields(fs, derived); \
|
||||
}); \
|
||||
} \
|
||||
}; \
|
||||
static TI typeinfo; \
|
||||
return &typeinfo; \
|
||||
}
|
||||
|
||||
// DAP_STRUCT_TYPEINFO() is a helper for declaring and implementing a TypeOf<>
|
||||
// specialization for STRUCT in a single statement.
|
||||
#define DAP_STRUCT_TYPEINFO(STRUCT, NAME, ...) \
|
||||
DAP_DECLARE_STRUCT_TYPEINFO(STRUCT); \
|
||||
DAP_IMPLEMENT_STRUCT_TYPEINFO(STRUCT, NAME, __VA_ARGS__)
|
||||
// DAP_STRUCT_TYPEINFO_EXT() is a helper for declaring and implementing a
|
||||
// TypeOf<> specialization for STRUCT that derives from BASE in a single
|
||||
// statement.
|
||||
// Must be used within the 'dap' namespace.
|
||||
#define DAP_STRUCT_TYPEINFO_EXT(STRUCT, BASE, NAME, ...) \
|
||||
DAP_DECLARE_STRUCT_TYPEINFO(STRUCT); \
|
||||
DAP_IMPLEMENT_STRUCT_TYPEINFO_EXT(STRUCT, BASE, NAME, __VA_ARGS__)
|
||||
|
||||
} // namespace dap
|
||||
|
||||
|
||||
Reference in New Issue
Block a user