1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-26 08:49:37 +00:00

Starts experimenting with declared reflection.

This commit is contained in:
Thomas Harte 2020-03-08 11:26:17 -04:00
parent 5a8fcac4dc
commit 3428e9887d

52
Reflection/Struct.h Normal file
View File

@ -0,0 +1,52 @@
//
// Struct.h
// Clock Signal
//
// Created by Thomas Harte on 06/03/2020.
// Copyright © 2020 Thomas Harte. All rights reserved.
//
#ifndef Struct_h
#define Struct_h
#include <any>
#include <string>
#include <unordered_map>
#include <vector>
namespace Reflection {
class Struct {
public:
template <typename Type> const Type &get(const std::string &name) {
return *std::any_cast<Type *>(contents_[name]);
}
template <typename Type> void set(const std::string &name, const Type &value) {
*std::any_cast<Type *>(contents_[name]) = value;
}
const std::type_info &type_of(const std::string &name) {
return contents_[name].type();
}
std::vector<std::string> all_keys() {
std::vector<std::string> keys;
for(const auto &pair: contents_) {
keys.push_back(pair.first);
}
return keys;
}
protected:
template <typename Type> void declare(Type *t, const std::string &name) {
contents_[name] = t;
}
private:
std::unordered_map<std::string, std::any> contents_;
};
}
#endif /* Struct_h */