mirror of
https://github.com/TomHarte/CLK.git
synced 2024-11-19 08:31:11 +00:00
Switches to class storage.
This commit is contained in:
parent
5248475e73
commit
6a8c6f5a06
@ -9,25 +9,32 @@
|
||||
#ifndef Struct_h
|
||||
#define Struct_h
|
||||
|
||||
#include <any>
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Reflection {
|
||||
|
||||
class Struct {
|
||||
template <typename Owner> class Struct {
|
||||
public:
|
||||
template <typename Type> const Type &get(const std::string &name) {
|
||||
return *std::any_cast<Type *>(contents_[name]);
|
||||
template <typename Type> const Type *get(const std::string &name) {
|
||||
const auto iterator = contents_.find(name);
|
||||
if(iterator == contents_.end()) return nullptr;
|
||||
return reinterpret_cast<Type *>(reinterpret_cast<uint8_t *>(this) + iterator->second.offset);
|
||||
}
|
||||
|
||||
template <typename Type> void set(const std::string &name, const Type &value) {
|
||||
*std::any_cast<Type *>(contents_[name]) = value;
|
||||
const auto iterator = contents_.find(name);
|
||||
if(iterator == contents_.end()) return;
|
||||
*reinterpret_cast<Type *>(reinterpret_cast<uint8_t *>(this) + iterator->second.offset) = value;
|
||||
}
|
||||
|
||||
const std::type_info &type_of(const std::string &name) {
|
||||
return contents_[name].type();
|
||||
const std::type_info *type_of(const std::string &name) {
|
||||
const auto iterator = contents_.find(name);
|
||||
if(iterator == contents_.end()) return nullptr;
|
||||
return iterator->second.type;
|
||||
}
|
||||
|
||||
std::vector<std::string> all_keys() {
|
||||
@ -39,12 +46,40 @@ class Struct {
|
||||
}
|
||||
|
||||
protected:
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
template <typename Type> void declare(Type *t, const std::string &name) {
|
||||
contents_[name] = t;
|
||||
contents_.emplace(
|
||||
std::make_pair(
|
||||
name,
|
||||
Field(typeid(Type), reinterpret_cast<uint8_t *>(t) - reinterpret_cast<uint8_t *>(this))
|
||||
));
|
||||
}
|
||||
|
||||
/*!
|
||||
@returns @c true if this
|
||||
*/
|
||||
bool needs_declare() {
|
||||
return !contents_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, std::any> contents_;
|
||||
struct Field {
|
||||
const std::type_info *type;
|
||||
ssize_t offset;
|
||||
Field(const std::type_info &type, ssize_t offset) :
|
||||
type(&type), offset(offset) {}
|
||||
};
|
||||
static inline std::unordered_map<std::string, Field> contents_;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user