Fix any, embedded object, array, and struct serialization (#94)

* add embedded object serialization tests (failing)

* Add empty object tests (failing)

* test for deserializing null field object (failing)

* Fix embedded object deserialization

* Fix serializing empty objects on nlohmann

* Fix nullptr_t handling in `any`

* Add test for de/serializing struct embedded in `object`

* Remove extraneous `get()` from `any`

* fix compiler errors and warnings on gcc

* add `any = dap::null` assignment test

* Remove extraneous template
This commit is contained in:
nikitalita
2023-02-16 15:09:45 -08:00
committed by GitHub
parent d9041149a8
commit 315ffff9e7
6 changed files with 273 additions and 13 deletions

View File

@@ -23,6 +23,8 @@ namespace dap {
template <typename T>
struct TypeOf;
class Deserializer;
class Serializer;
// any provides a type-safe container for values of any of dap type (boolean,
// integer, number, array, variant, any, null, dap-structs).
@@ -47,6 +49,7 @@ class any {
inline any& operator=(any&& rhs) noexcept;
template <typename T>
inline any& operator=(const T& val);
inline any& operator=(const std::nullptr_t& val);
// get() returns the contained value of the type T.
// If the any does not contain a value of type T, then get() will assert.
@@ -58,6 +61,9 @@ class any {
inline bool is() const;
private:
friend class Deserializer;
friend class Serializer;
static inline void* alignUp(void* val, size_t alignment);
inline void alloc(size_t size, size_t align);
inline void free();
@@ -142,8 +148,15 @@ any& any::operator=(const T& val) {
return *this;
}
any& any::operator=(const std::nullptr_t&) {
reset();
return *this;
}
template <typename T>
T& any::get() const {
static_assert(!std::is_same<T, std::nullptr_t>(),
"Cannot get nullptr from 'any'.");
assert(is<T>());
return *reinterpret_cast<T*>(value);
}

View File

@@ -177,8 +177,18 @@ class Serializer {
// deserialize() encodes the given string.
inline bool serialize(const char* v);
protected:
static inline const TypeInfo* get_any_type(const any&);
static inline const void* get_any_val(const any&);
};
inline const TypeInfo* Serializer::get_any_type(const any& a){
return a.type;
}
const void* Serializer::get_any_val(const any& a) {
return a.value;
}
template <typename T, typename>
bool Serializer::serialize(const T& object) {
return TypeOf<T>::type()->serialize(this, &object);