Created
February 11, 2018 20:15
-
-
Save veryjos/506c0933b9d8ce6fc0bb1f210d4fda88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
#include <functional> | |
#include <unordered_map> | |
template <typename T> | |
struct AutoPropMeta { | |
static AutoPropMeta<T>* GetInstance() { | |
static bool first = true; | |
static AutoPropMeta<T> instance; | |
if (first) { | |
first = false; | |
T::InitAutoPropChain(); | |
} | |
return &instance; | |
}; | |
std::unordered_map<std::string, std::function<void(T*, int)>> setters; | |
}; | |
template <int i> | |
struct AutoPropIndex { | |
static constexpr const int index = i; | |
}; | |
template <typename T, int i, typename R> | |
static constexpr int NextValue() { | |
if constexpr (decltype(T::__meta_counter(AutoPropIndex<i>{}))::index == -1) { | |
return i; | |
} else { | |
return NextValue<T, i + 1, R>(); | |
} | |
}; | |
#define AUTOPROP_ENABLE(mytyyype) \ | |
using MYTYPE = mytyyype;\ | |
\ | |
template <int i> \ | |
static constexpr AutoPropIndex<-1> __meta_counter(AutoPropIndex<i>); \ | |
\ | |
static inline void InitAutoPropChain() { \ | |
InitAutoPropChainRecurse<0>(); \ | |
} \ | |
\ | |
template <int currentIndex> \ | |
static inline void InitAutoPropChainRecurse() { \ | |
if constexpr (HasProp(AutoPropIndex<currentIndex>{})) { \ | |
InitProp(AutoPropIndex<currentIndex>{}); \ | |
\ | |
InitAutoPropChainRecurse<currentIndex + 1>(); \ | |
} \ | |
} \ | |
\ | |
static inline constexpr bool HasProp(...) { \ | |
return false; \ | |
} \ | |
\ | |
static inline void Set(MYTYPE& obj, const std::string& propertyName, int value) { \ | |
auto meta = AutoPropMeta<MYTYPE>::GetInstance(); \ | |
\ | |
meta->setters[propertyName](&obj, value); \ | |
} | |
#define CONCAT_(x,y) x##y | |
#define CONCAT(x,y) CONCAT_(x,y) | |
#define AUTOPROP(name) __AUTOPROP_IMPL(name, CONCAT(__meta_index_, name), __LINE__) | |
#define __AUTOPROP_IMPL(name, ident, line) \ | |
static constexpr const int ident = NextValue<MYTYPE, 0, AutoPropIndex<line>>(); \ | |
static constexpr AutoPropIndex<1> __meta_counter(AutoPropIndex<ident>); \ | |
\ | |
static inline constexpr bool HasProp(AutoPropIndex<ident>) { \ | |
return true; \ | |
}; \ | |
\ | |
static inline void InitProp(AutoPropIndex<ident>) { \ | |
auto meta = AutoPropMeta<MYTYPE>::GetInstance(); \ | |
\ | |
meta->setters[#name] = [](MYTYPE* ptr, int value) { \ | |
ptr->name = value; \ | |
}; \ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "AutoProp.hpp" | |
struct LayoutMeta { | |
AUTOPROP_ENABLE(LayoutMeta) | |
AUTOPROP(x) | |
int x; | |
AUTOPROP(y) | |
int y; | |
AUTOPROP(z) | |
int z; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "LayoutMeta.hpp" | |
int main(int argc, char** argv) { | |
printf("Size of LayoutMeta: %lu\n", sizeof(LayoutMeta)); | |
LayoutMeta test; | |
test.x = 33; | |
printf("Before reflection: test.x = %d\n", test.x); | |
LayoutMeta::Set(test, "x", 44); | |
printf("After reflection: test.x = %d\n", test.x); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment