Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ANTICHEAT_H_INCLUDED
- #define ANTICHEAT_H_INCLUDED
- #include <tuple>
- template<typename T, typename... Types>
- class AntiCheat
- {
- T * value;
- std::tuple<Types...> checks;
- // check if elements of tuple return valid
- template<int index, typename... Ts>
- struct check_tuple {
- bool operator() (std::tuple<Ts...>& t, T & x) {
- return std::get<index>(t)(x)
- && check_tuple<index-1, Ts...>{}(t, x);
- }
- };
- template<typename... Ts>
- struct check_tuple<0, Ts...> {
- bool operator() (std::tuple<Ts...>& t, T & x) {
- return std::get<0>(t)(x);
- }
- };
- // update elements of the tuple
- template<int index, typename... Ts>
- struct update_tuple {
- void operator() (std::tuple<Ts...>& t, T & x) {
- std::get<index>(t) = x;
- update_tuple<index-1, Ts...>{}(t, x);
- }
- };
- template<typename... Ts>
- struct update_tuple<0, Ts...> {
- void operator() (std::tuple<Ts...>& t, T & x) {
- std::get<0>(t) = x;
- }
- };
- public:
- AntiCheat():
- value(new T),
- checks(std::forward<Types>(*value)...)
- {
- }
- AntiCheat(T t):
- value(new T(t)),
- checks(std::forward<Types>(t)...)
- {
- }
- AntiCheat(const AntiCheat & o):
- value(new T(*o.value)),
- checks(std::forward<Types>(*o.value)...)
- {
- }
- ~AntiCheat()
- {
- delete value;
- }
- AntiCheat & operator=(const AntiCheat & o)
- {
- *value = *o.value;
- const auto size = std::tuple_size<std::tuple<Types...>>::value;
- update_tuple<size-1, Types...>{}(checks, *value);
- return *this;
- }
- AntiCheat & operator=(T t)
- {
- *value = t;
- const auto size = std::tuple_size<std::tuple<Types...>>::value;
- update_tuple<size-1, Types...>{}(checks, t);
- return *this;
- }
- bool valid()
- {
- const auto size = std::tuple_size<std::tuple<Types...>>::value;
- return check_tuple<size-1, Types...>{}(checks, *value);
- }
- operator T()
- {
- return *value;
- }
- };
- #endif // ANTICHEAT_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment