2020-03-08 15:26:17 +00:00
|
|
|
//
|
2020-03-20 03:24:06 +00:00
|
|
|
// Struct.hpp
|
2020-03-08 15:26:17 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 06/03/2020.
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-03-20 03:24:06 +00:00
|
|
|
#ifndef Struct_hpp
|
|
|
|
#define Struct_hpp
|
2020-03-08 15:26:17 +00:00
|
|
|
|
2020-03-18 01:44:04 +00:00
|
|
|
#include <cstdarg>
|
2020-03-14 04:17:58 +00:00
|
|
|
#include <cstring>
|
2020-03-08 15:26:17 +00:00
|
|
|
#include <string>
|
2020-03-09 01:23:29 +00:00
|
|
|
#include <typeindex>
|
|
|
|
#include <typeinfo>
|
2020-03-08 15:26:17 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-03-20 03:24:06 +00:00
|
|
|
#include "Enum.hpp"
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-08 15:26:17 +00:00
|
|
|
namespace Reflection {
|
|
|
|
|
2020-03-12 03:25:29 +00:00
|
|
|
#define DeclareField(Name) declare(&Name, #Name)
|
|
|
|
|
2020-03-13 00:56:02 +00:00
|
|
|
struct Struct {
|
|
|
|
virtual std::vector<std::string> all_keys() = 0;
|
|
|
|
virtual const std::type_info *type_of(const std::string &name) = 0;
|
2020-03-14 03:38:29 +00:00
|
|
|
virtual void set(const std::string &name, const void *value) = 0;
|
|
|
|
virtual const void *get(const std::string &name) = 0;
|
2020-03-18 01:44:04 +00:00
|
|
|
virtual std::vector<std::string> values_for(const std::string &name) = 0;
|
2020-03-13 00:56:02 +00:00
|
|
|
virtual ~Struct() {}
|
2020-03-14 03:38:29 +00:00
|
|
|
};
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
/*!
|
|
|
|
Attempts to set the property @c name to @c value ; will perform limited type conversions.
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
@returns @c true if the property was successfully set; @c false otherwise.
|
2020-03-14 03:38:29 +00:00
|
|
|
*/
|
|
|
|
template <typename Type> bool set(Struct &target, const std::string &name, Type value);
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
/*!
|
|
|
|
Setting an int:
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
* to an int copies 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, int value);
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
/*!
|
|
|
|
Setting a string:
|
2020-03-14 02:42:37 +00:00
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
* 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);
|
2020-03-13 00:56:02 +00:00
|
|
|
|
2020-03-19 02:31:32 +00:00
|
|
|
/*!
|
|
|
|
Setting a bool:
|
|
|
|
|
|
|
|
* to a bool, copies the value.
|
|
|
|
*/
|
|
|
|
template <> bool set(Struct &target, const std::string &name, bool value);
|
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Fuzzy-set attempts to set any property based on a string value. This is intended to allow input provided by the user.
|
|
|
|
|
|
|
|
Amongst other steps, it might:
|
|
|
|
* if the target is a bool, map true, false, yes, no, y, n, etc;
|
|
|
|
* if the target is an integer, parse like strtrol;
|
|
|
|
* if the target is a float, parse like strtod; or
|
|
|
|
* if the target is a reflective enum, attempt to match to enum members (possibly doing so in a case insensitive fashion).
|
|
|
|
|
|
|
|
This method reserves the right to perform more or fewer attempted mappings, using any other logic it
|
|
|
|
decides is appropriate.
|
|
|
|
|
|
|
|
@returns @c true if the property was successfully set; @c false otherwise.
|
|
|
|
*/
|
|
|
|
bool fuzzy_set(Struct &target, const std::string &name, const std::string &value);
|
|
|
|
|
2020-03-16 03:48:53 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Attempts to get the property @c name to @c value ; will perform limited type conversions.
|
|
|
|
|
|
|
|
@returns @c true if the property was successfully read; @c false otherwise.
|
|
|
|
*/
|
|
|
|
template <typename Type> bool get(Struct &target, const std::string &name, Type &value);
|
|
|
|
|
|
|
|
template <> bool get(Struct &target, const std::string &name, bool &value);
|
|
|
|
|
|
|
|
|
2020-03-16 01:50:43 +00:00
|
|
|
// TODO: move this elsewhere. It's just a sketch anyway.
|
2020-03-14 00:16:36 +00:00
|
|
|
struct Serialisable {
|
2020-03-16 01:50:43 +00:00
|
|
|
/// Serialises this object, appending it to @c target.
|
|
|
|
virtual void serialise(std::vector<uint8_t> &target) = 0;
|
|
|
|
/// Deserialises this object from @c source.
|
|
|
|
/// @returns @c true if the deserialisation was successful; @c false otherwise.
|
|
|
|
virtual bool deserialise(const std::vector<uint8_t> &source) = 0;
|
2020-03-14 00:16:36 +00:00
|
|
|
};
|
|
|
|
|
2020-03-14 02:42:37 +00:00
|
|
|
template <typename Owner> class StructImpl: public Struct {
|
2020-03-08 15:26:17 +00:00
|
|
|
public:
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
|
|
|
@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.
|
|
|
|
*/
|
2020-03-14 03:38:29 +00:00
|
|
|
const void *get(const std::string &name) final {
|
2020-03-09 01:23:29 +00:00
|
|
|
const auto iterator = contents_.find(name);
|
|
|
|
if(iterator == contents_.end()) return nullptr;
|
2020-03-13 00:56:02 +00:00
|
|
|
return reinterpret_cast<uint8_t *>(this) + iterator->second.offset;
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
|
|
|
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.
|
|
|
|
*/
|
2020-03-14 03:38:29 +00:00
|
|
|
void set(const std::string &name, const void *value) final {
|
2020-03-09 01:23:29 +00:00
|
|
|
const auto iterator = contents_.find(name);
|
|
|
|
if(iterator == contents_.end()) return;
|
2020-03-13 00:56:02 +00:00
|
|
|
memcpy(reinterpret_cast<uint8_t *>(this) + iterator->second.offset, value, iterator->second.size);
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
|
|
|
@returns @c type_info for the field @c name.
|
|
|
|
*/
|
2020-03-13 00:56:02 +00:00
|
|
|
const std::type_info *type_of(const std::string &name) final {
|
2020-03-09 01:23:29 +00:00
|
|
|
const auto iterator = contents_.find(name);
|
|
|
|
if(iterator == contents_.end()) return nullptr;
|
|
|
|
return iterator->second.type;
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 01:44:04 +00:00
|
|
|
/*!
|
|
|
|
@returns a list of the valid enum value names for field @c name if it is a declared enum field of this struct;
|
|
|
|
the empty list otherwise.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> values_for(const std::string &name) final {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
// Return an empty vector if this field isn't declared.
|
|
|
|
const auto type = type_of(name);
|
|
|
|
if(!type) return result;
|
|
|
|
|
|
|
|
// Also return an empty vector if this field isn't a registered enum.
|
|
|
|
const auto all_values = Enum::all_values(*type);
|
|
|
|
if(all_values.empty()) return result;
|
|
|
|
|
|
|
|
// If no restriction is stored, return all values.
|
|
|
|
const auto permitted_values = permitted_enum_values_.find(name);
|
|
|
|
if(permitted_values == permitted_enum_values_.end()) return all_values;
|
|
|
|
|
|
|
|
// Compile a vector of only those values the stored set indicates.
|
|
|
|
auto value = all_values.begin();
|
|
|
|
auto flag = permitted_values->second.begin();
|
|
|
|
while(value != all_values.end() && flag != permitted_values->second.end()) {
|
|
|
|
if(*flag) {
|
|
|
|
result.push_back(*value);
|
|
|
|
}
|
|
|
|
++flag;
|
|
|
|
++value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
|
|
|
@returns A vector of all declared fields for this struct.
|
|
|
|
*/
|
2020-03-13 00:56:02 +00:00
|
|
|
std::vector<std::string> all_keys() final {
|
2020-03-08 15:26:17 +00:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
for(const auto &pair: contents_) {
|
|
|
|
keys.push_back(pair.first);
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-03-09 01:23:29 +00:00
|
|
|
/*
|
|
|
|
This interface requires reflective structs to declare all fields;
|
|
|
|
specifically they should call:
|
|
|
|
|
|
|
|
declare_field(&field1, "field1");
|
|
|
|
declare_field(&field2, "field2");
|
|
|
|
|
|
|
|
Fields are registered in class storage. So callers can use needs_declare()
|
|
|
|
to determine whether a class of this type has already established the
|
|
|
|
reflective fields.
|
|
|
|
*/
|
2020-03-10 02:05:31 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Exposes the field pointed to by @c t for reflection as @c name.
|
|
|
|
*/
|
2020-03-08 15:26:17 +00:00
|
|
|
template <typename Type> void declare(Type *t, const std::string &name) {
|
2020-03-09 01:23:29 +00:00
|
|
|
contents_.emplace(
|
|
|
|
std::make_pair(
|
|
|
|
name,
|
2020-03-13 00:56:02 +00:00
|
|
|
Field(typeid(Type), reinterpret_cast<uint8_t *>(t) - reinterpret_cast<uint8_t *>(this), sizeof(Type))
|
2020-03-09 01:23:29 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-03-18 01:44:04 +00:00
|
|
|
/*!
|
|
|
|
If @c t is a previously-declared field that links to a declared enum then the variable
|
|
|
|
arguments provide a list of the acceptable values for that field. The list should be terminated
|
|
|
|
with a value of -1.
|
|
|
|
*/
|
|
|
|
template <typename Type> void limit_enum(Type *t, ...) {
|
|
|
|
const auto name = name_of(t);
|
|
|
|
if(name.empty()) return;
|
|
|
|
|
|
|
|
// The default vector size of '8' isn't especially scientific,
|
|
|
|
// but I feel like it's a good choice.
|
|
|
|
std::vector<bool> permitted_values(8);
|
|
|
|
|
|
|
|
va_list list;
|
|
|
|
va_start(list, t);
|
|
|
|
while(true) {
|
|
|
|
const int next = va_arg(list, int);
|
|
|
|
if(next < 0) break;
|
|
|
|
|
2020-03-19 03:29:09 +00:00
|
|
|
if(permitted_values.size() <= size_t(next)) {
|
2020-03-18 01:44:04 +00:00
|
|
|
permitted_values.resize(permitted_values.size() << 1);
|
|
|
|
}
|
2020-03-19 03:29:09 +00:00
|
|
|
permitted_values[size_t(next)] = true;
|
2020-03-18 01:44:04 +00:00
|
|
|
}
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
permitted_enum_values_.emplace(std::make_pair(name, permitted_values));
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:05:31 +00:00
|
|
|
/*!
|
2020-03-12 02:06:16 +00:00
|
|
|
@returns @c true if this subclass of @c Struct has not yet declared any fields.
|
2020-03-09 01:23:29 +00:00
|
|
|
*/
|
|
|
|
bool needs_declare() {
|
|
|
|
return !contents_.size();
|
2020-03-08 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 01:44:04 +00:00
|
|
|
/*!
|
|
|
|
Performs a reverse lookup from field to name.
|
|
|
|
*/
|
|
|
|
std::string name_of(void *field) {
|
|
|
|
const ssize_t offset = reinterpret_cast<uint8_t *>(field) - reinterpret_cast<uint8_t *>(this);
|
|
|
|
|
|
|
|
auto iterator = contents_.begin();
|
|
|
|
while(iterator != contents_.end()) {
|
|
|
|
if(iterator->second.offset == offset) break;
|
|
|
|
++iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(iterator != contents_.end()) {
|
|
|
|
return iterator->first;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 15:26:17 +00:00
|
|
|
private:
|
2020-03-09 01:23:29 +00:00
|
|
|
struct Field {
|
|
|
|
const std::type_info *type;
|
2020-03-15 04:13:38 +00:00
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
2020-03-13 00:56:02 +00:00
|
|
|
Field(const std::type_info &type, ssize_t offset, size_t size) :
|
|
|
|
type(&type), offset(offset), size(size) {}
|
2020-03-09 01:23:29 +00:00
|
|
|
};
|
|
|
|
static inline std::unordered_map<std::string, Field> contents_;
|
2020-03-18 01:44:04 +00:00
|
|
|
static inline std::unordered_map<std::string, std::vector<bool>> permitted_enum_values_;
|
2020-03-08 15:26:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-20 03:24:06 +00:00
|
|
|
#endif /* Struct_hpp */
|