From 3428e9887d69e33e75fb118a3fc79d7a9647ed9f Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 8 Mar 2020 11:26:17 -0400 Subject: [PATCH] Starts experimenting with declared reflection. --- Reflection/Struct.h | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Reflection/Struct.h diff --git a/Reflection/Struct.h b/Reflection/Struct.h new file mode 100644 index 000000000..250396b76 --- /dev/null +++ b/Reflection/Struct.h @@ -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 +#include +#include +#include + +namespace Reflection { + +class Struct { + public: + template const Type &get(const std::string &name) { + return *std::any_cast(contents_[name]); + } + + template void set(const std::string &name, const Type &value) { + *std::any_cast(contents_[name]) = value; + } + + const std::type_info &type_of(const std::string &name) { + return contents_[name].type(); + } + + std::vector all_keys() { + std::vector keys; + for(const auto &pair: contents_) { + keys.push_back(pair.first); + } + return keys; + } + + protected: + template void declare(Type *t, const std::string &name) { + contents_[name] = t; + } + + private: + std::unordered_map contents_; +}; + +} + +#endif /* Struct_h */