1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-09 17:29:36 +00:00

Implements BSON deserialisation, other than arrays.

This commit is contained in:
Thomas Harte 2020-05-25 23:39:00 -04:00
parent c83c827484
commit 4d34d9ae2b
3 changed files with 207 additions and 9 deletions

View File

@ -72,6 +72,14 @@ class MultiStruct: public Reflection::Struct {
return nullptr;
}
void *get(const std::string &name) final {
for(auto &options: options_) {
auto value = options->get(name);
if(value) return value;
}
return nullptr;
}
void set(const std::string &name, const void *value) final {
const auto safe_type = type_of(name);
if(!safe_type) return;

View File

@ -69,22 +69,58 @@ static size_t size(const std::type_info *type) {
// MARK: - Setters
template <> bool Reflection::set(Struct &target, const std::string &name, float value) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
if(*target_type == typeid(float)) {
target.set(name, &value);
return true;
}
return set<double>(target, name, value);
}
template <> bool Reflection::set(Struct &target, const std::string &name, double value) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
if(*target_type == typeid(double)) {
target.set(name, &value);
return true;
}
if(*target_type == typeid(float)) {
const float float_value = float(value);
target.set(name, &float_value);
return true;
}
return false;
}
template <> bool Reflection::set(Struct &target, const std::string &name, int value) {
return set<int64_t>(target, name, value);
}
template <> bool Reflection::set(Struct &target, const std::string &name, int64_t value) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
// No need to convert an int or a registered enum.
if(*target_type == typeid(int) || !Reflection::Enum::name(*target_type).empty()) {
const int value32 = int(value);
target.set(name, &value32);
return true;
}
// Set an int64_t directly.
if(*target_type == typeid(int64_t)) {
target.set(name, &value);
return true;
}
// Promote to an int64_t.
if(*target_type == typeid(int64_t)) {
const auto int64 = int64_t(value);
target.set(name, &int64);
return true;
}
// TODO: other int sizes.
return false;
}
@ -93,6 +129,14 @@ template <> bool Reflection::set(Struct &target, const std::string &name, const
const auto target_type = target.type_of(name);
if(!target_type) return false;
// If the target is a string, assign.
if(*target_type == typeid(std::string)) {
auto child = reinterpret_cast<std::string *>(target.get(name));
*child = value;
return true;
}
// From here on, make an attempt to convert to a named enum.
if(Reflection::Enum::name(*target_type).empty()) {
return false;
}
@ -389,6 +433,14 @@ std::vector<uint8_t> Reflection::Struct::serialise() const {
return;
}
// Strings are written naturally.
if(*type == typeid(std::string)) {
const uint8_t *address = reinterpret_cast<const uint8_t *>(get(key));
const std::string *const text = reinterpret_cast<const std::string *>(address + offset*sizeof(std::string));
push_string(*text);
return;
}
// Store std::vector<uint_8>s as binary data.
if(*type == typeid(std::vector<uint8_t>)) {
result.push_back(0x05);
@ -442,7 +494,7 @@ std::vector<uint8_t> Reflection::Struct::serialise() const {
if(count > 1) {
// In BSON, an array is a sub-document with ASCII keys '0', '1', etc.
result.push_back(0x03); // TODO: 0x04.
result.push_back(0x04);
push_name(result, key);
std::vector<uint8_t> array;
@ -460,3 +512,112 @@ std::vector<uint8_t> Reflection::Struct::serialise() const {
wrap_object(result);
return result;
}
bool Reflection::Struct::deserialise(const std::vector<uint8_t> &bson) {
return deserialise(bson.data(), bson.size());
}
bool Reflection::Struct::deserialise(const uint8_t *bson, size_t size) {
// Validate the object's declared size.
const auto end = bson + size;
auto read_int = [&bson] (auto &target) {
constexpr auto shift = 8 * (sizeof(target) - 1);
target = 0;
for(size_t c = 0; c < sizeof(target); ++c) {
target >>= 8;
target |= decltype(target)(*bson) << shift;
++bson;
}
};
uint32_t object_size;
read_int(object_size);
if(object_size > size) return false;
while(true) {
const uint8_t next_type = *bson;
++bson;
if(!next_type) break;
std::string key;
while(*bson) {
key.push_back(char(*bson));
++bson;
}
++bson;
switch(next_type) {
default:
return false;
// TODO: arrays.
// 0x03: A subdocument; try to install the inner BSON.
// 0x05: Binary data. Seek to populate a std::vector<uint8_t>.
case 0x03:
case 0x05: {
const auto type = type_of(key);
uint32_t subobject_size;
read_int(subobject_size);
if(next_type == 0x03 && *type == typeid(Reflection::Struct)) {
auto child = reinterpret_cast<Reflection::Struct *>(get(key));
child->deserialise(bson - 4, size_t(end - bson + 4));
bson += subobject_size - 4;
}
if(next_type == 0x05 && *type == typeid(std::vector<uint8_t>)) {
auto child = reinterpret_cast<std::vector<uint8_t> *>(get(key));
*child = std::vector<uint8_t>(bson, bson + subobject_size);
bson += subobject_size;
}
} break;
// String.
case 0x02: {
uint32_t length;
read_int(length);
const std::string value(bson, bson + length - 1);
::Reflection::set<const std::string &>(*this, key, value);
bson += length;
} break;
// Boolean.
case 0x08: {
const bool value = *bson;
++bson;
::Reflection::set(*this, key, value);
} break;
// 32-bit int.
case 0x10: {
int32_t value;
read_int(value);
::Reflection::set(*this, key, value);
} break;
// 64-bit int.
case 0x12: {
int64_t value;
read_int(value);
::Reflection::set(*this, key, value);
} break;
// 64-bit double.
case 0x01: {
uint64_t value;
read_int(value);
// TODO: real conversion.
double double_value = double(value);
::Reflection::set(*this, key, double_value);
} break;
}
}
return true;
}

View File

@ -29,6 +29,7 @@ struct Struct {
virtual const std::type_info *type_of(const std::string &name) const = 0;
virtual size_t count_of(const std::string &name) const = 0;
virtual void set(const std::string &name, const void *value) = 0;
virtual void *get(const std::string &name) = 0;
virtual const void *get(const std::string &name) const = 0;
virtual std::vector<std::string> values_for(const std::string &name) const = 0;
virtual ~Struct() {}
@ -40,21 +41,38 @@ struct Struct {
std::string description() const;
/*!
@returns The BSON serialisation of this struct.
Serialises this struct in BSON format.
Supported field types:
* [u]int[8/16/32/64]_t;
* float and double;
* bool;
* std::string;
* plain C-style arrays of any of the above;
* other reflective structs;
* std::vector<uint8_t> as raw binary data.
TODO: vector of string, possibly? Or more general vector support?
@returns The BSON serialisation.
*/
std::vector<uint8_t> serialise() const;
/*!
Applies as many fields as possible from the incoming BSON.
Applies as many fields as possible from the incoming BSON. Supports the same types
as @c serialise.
*/
bool deserialise(const std::vector<uint8_t> &bson);
/*!
Called to determine whether @c key should be included in the serialisation of this struct.
*/
virtual bool should_serialise(const std::string &key) const { return true; }
private:
void append(std::ostringstream &stream, const std::string &key, const std::type_info *type, size_t offset) const;
bool deserialise(const uint8_t *bson, size_t size);
};
/*!
@ -68,9 +86,11 @@ template <typename Type> bool set(Struct &target, const std::string &name, Type
Setting an int:
* to an int copies the int;
* to a smaller type, truncates the int;
* to an int64_t promotes the int; and
* to a registered enum, copies the int.
*/
template <> bool set(Struct &target, const std::string &name, int64_t value);
template <> bool set(Struct &target, const std::string &name, int value);
/*!
@ -89,6 +109,9 @@ template <> bool set(Struct &target, const std::string &name, const char *value)
template <> bool set(Struct &target, const std::string &name, bool value);
template <> bool set(Struct &target, const std::string &name, float value);
template <> bool set(Struct &target, const std::string &name, double value);
/*!
Fuzzy-set attempts to set any property based on a string value. This is intended to allow input provided by the user.
@ -126,6 +149,12 @@ template <typename Owner> class StructImpl: public Struct {
@returns the value of type @c Type that is loaded from the offset registered for the field @c name.
It is the caller's responsibility to provide an appropriate type of data.
*/
void *get(const std::string &name) final {
const auto iterator = contents_.find(name);
if(iterator == contents_.end()) return nullptr;
return reinterpret_cast<uint8_t *>(this) + iterator->second.offset;
}
const void *get(const std::string &name) const final {
const auto iterator = contents_.find(name);
if(iterator == contents_.end()) return nullptr;