1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-28 22:30:30 +00:00

Attempts to add array support to ::set and BSON deserialisation.

This commit is contained in:
Thomas Harte 2020-05-26 22:55:55 -04:00
parent 023d76a3e7
commit 51d684820f
3 changed files with 74 additions and 36 deletions

View File

@ -80,7 +80,7 @@ class MultiStruct: public Reflection::Struct {
return nullptr;
}
void set(const std::string &name, const void *value) final {
void set(const std::string &name, const void *value, size_t offset) final {
const auto safe_type = type_of(name);
if(!safe_type) return;
@ -91,7 +91,7 @@ class MultiStruct: public Reflection::Struct {
if(!type) continue;
if(*type == *safe_type) {
options->set(name, value);
options->set(name, value, offset);
}
}
}

View File

@ -69,65 +69,65 @@ static size_t size(const std::type_info *type) {
// MARK: - Setters
template <> bool Reflection::set(Struct &target, const std::string &name, float value) {
template <> bool Reflection::set(Struct &target, const std::string &name, float value, size_t offset) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
if(*target_type == typeid(float)) {
target.set(name, &value);
target.set(name, &value, offset);
return true;
}
return set<double>(target, name, value);
}
template <> bool Reflection::set(Struct &target, const std::string &name, double value) {
template <> bool Reflection::set(Struct &target, const std::string &name, double value, size_t offset) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
if(*target_type == typeid(double)) {
target.set(name, &value);
target.set(name, &value, offset);
return true;
}
if(*target_type == typeid(float)) {
const float float_value = float(value);
target.set(name, &float_value);
target.set(name, &float_value, offset);
return true;
}
return false;
}
template <> bool Reflection::set(Struct &target, const std::string &name, int value) {
template <> bool Reflection::set(Struct &target, const std::string &name, int value, size_t offset) {
return set<int64_t>(target, name, value);
}
template <> bool Reflection::set(Struct &target, const std::string &name, int64_t value) {
template <> bool Reflection::set(Struct &target, const std::string &name, int64_t value, size_t offset) {
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);
target.set(name, &value32, offset);
return true;
}
// Set an int64_t directly.
if(*target_type == typeid(int64_t)) {
target.set(name, &value);
target.set(name, &value, offset);
return true;
}
#define SetInt(x) if(*target_type == typeid(x)) { x truncated_value = x(value); target.set(name, &truncated_value); }
#define SetInt(x) if(*target_type == typeid(x)) { x truncated_value = x(value); target.set(name, &truncated_value, offset); }
ForAllInts(SetInt);
#undef SetInt
return false;
}
template <> bool Reflection::set(Struct &target, const std::string &name, const std::string &value) {
template <> bool Reflection::set(Struct &target, const std::string &name, const std::string &value, size_t offset) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
@ -147,22 +147,22 @@ template <> bool Reflection::set(Struct &target, const std::string &name, const
if(enum_value < 0) {
return false;
}
target.set(name, &enum_value);
target.set(name, &enum_value, offset);
return true;
}
template <> bool Reflection::set(Struct &target, const std::string &name, const char *value) {
template <> bool Reflection::set(Struct &target, const std::string &name, const char *value, size_t offset) {
const std::string string(value);
return set<const std::string &>(target, name, string);
}
template <> bool Reflection::set(Struct &target, const std::string &name, bool value) {
template <> bool Reflection::set(Struct &target, const std::string &name, bool value, size_t offset) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
if(*target_type == typeid(bool)) {
target.set(name, &value);;
target.set(name, &value, offset);;
}
return false;
@ -519,6 +519,45 @@ bool Reflection::Struct::deserialise(const std::vector<uint8_t> &bson) {
return deserialise(bson.data(), bson.size());
}
namespace {
/*!
Provides a proxy struct that redirects calls to set to another object and property, picking
an offset based on the propety name specified here.
*/
struct ArrayReceiver: public Reflection::Struct {
ArrayReceiver(Reflection::Struct *target, const std::type_info *type, const std::string &key, size_t count) :
target_(target), type_(type), key_(key), count_(count) {}
std::vector<std::string> all_keys() const final { return {}; }
const std::type_info *type_of(const std::string &name) const final { return type_; }
size_t count_of(const std::string &name) const final { return 0; }
void set(const std::string &name, const void *value, size_t offset) final {
const auto index = size_t(std::stoi(name));
if(index >= count_) {
return;
}
target_->set(key_, value, index);
}
virtual std::vector<std::string> values_for(const std::string &name) const final {
return {};
}
void *get(const std::string &name) final {
return nullptr;
}
private:
Reflection::Struct *target_;
const std::type_info *type_;
std::string key_;
size_t count_;
};
}
bool Reflection::Struct::deserialise(const uint8_t *bson, size_t size) {
// Validate the object's declared size.
const auto end = bson + size;
@ -580,10 +619,12 @@ bool Reflection::Struct::deserialise(const uint8_t *bson, size_t size) {
// but given the string keys "0", "1", etc. So: validate the keys, decode
// the objects.
case 0x04: {
ArrayReceiver receiver(this, type_of(key), key, count_of(key));
uint32_t subobject_size;
read_int(subobject_size);
// TODO: parse objects.
receiver.deserialise(bson - 4, size_t(end - bson + 4));
bson += subobject_size - 4;
} break;

View File

@ -28,9 +28,11 @@ struct Struct {
virtual std::vector<std::string> all_keys() const = 0;
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 set(const std::string &name, const void *value, size_t offset = 0) = 0;
virtual void *get(const std::string &name) = 0;
virtual const void *get(const std::string &name) const = 0;
virtual const void *get(const std::string &name) const {
return const_cast<Struct *>(this)->get(name);
}
virtual std::vector<std::string> values_for(const std::string &name) const = 0;
virtual ~Struct() {}
@ -80,7 +82,7 @@ struct Struct {
@returns @c true if the property was successfully set; @c false otherwise.
*/
template <typename Type> bool set(Struct &target, const std::string &name, Type value);
template <typename Type> bool set(Struct &target, const std::string &name, Type value, size_t offset = 0);
/*!
Setting an int:
@ -90,27 +92,27 @@ template <typename Type> bool set(Struct &target, const std::string &name, Type
* 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);
template <> bool set(Struct &target, const std::string &name, int64_t value, size_t offset);
template <> bool set(Struct &target, const std::string &name, int value, size_t offset);
/*!
Setting a string:
* to an enum, if the string names a member of the enum, sets the value.
*/
template <> bool set(Struct &target, const std::string &name, const std::string &value);
template <> bool set(Struct &target, const std::string &name, const char *value);
template <> bool set(Struct &target, const std::string &name, const std::string &value, size_t offset);
template <> bool set(Struct &target, const std::string &name, const char *value, size_t offset);
/*!
Setting a bool:
* to a bool, copies the value.
*/
template <> bool set(Struct &target, const std::string &name, bool value);
template <> bool set(Struct &target, const std::string &name, bool value, size_t offset);
template <> bool set(Struct &target, const std::string &name, float value);
template <> bool set(Struct &target, const std::string &name, double value);
template <> bool set(Struct &target, const std::string &name, float value, size_t offset);
template <> bool set(Struct &target, const std::string &name, double value, size_t offset);
/*!
Fuzzy-set attempts to set any property based on a string value. This is intended to allow input provided by the user.
@ -155,21 +157,16 @@ template <typename Owner> class StructImpl: public Struct {
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;
return reinterpret_cast<const uint8_t *>(this) + iterator->second.offset;
}
/*!
Stores the @c value of type @c Type to the offset registered for the field @c name.
It is the caller's responsibility to provide an appropriate type of data.
*/
void set(const std::string &name, const void *value) final {
void set(const std::string &name, const void *value, size_t offset) final {
const auto iterator = contents_.find(name);
if(iterator == contents_.end()) return;
memcpy(reinterpret_cast<uint8_t *>(this) + iterator->second.offset, value, iterator->second.size);
assert(offset < iterator->second.count);
memcpy(reinterpret_cast<uint8_t *>(this) + iterator->second.offset + offset * iterator->second.size, value, iterator->second.size);
}
/*!