|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "tmp_util/control/if.hpp" |
| 4 | +#include "tmp_util/functional/lift.hpp" |
| 5 | + |
| 6 | +namespace JSON |
| 7 | +{ |
| 8 | + template <typename... Tags> |
| 9 | + struct Tag |
| 10 | + { |
| 11 | + using json_tags = Tag<Tags...>; |
| 12 | + }; |
| 13 | + |
| 14 | + struct RootObject; |
| 15 | + |
| 16 | + template <typename...> |
| 17 | + struct always_false |
| 18 | + { |
| 19 | + static constexpr const bool value = false; |
| 20 | + using type = false_; |
| 21 | + }; |
| 22 | + |
| 23 | + template <typename...> |
| 24 | + struct always_true |
| 25 | + { |
| 26 | + static constexpr const bool value = true; |
| 27 | + using type = true_; |
| 28 | + }; |
| 29 | + |
| 30 | + template<typename T> |
| 31 | + struct void_ { typedef void type; }; |
| 32 | + |
| 33 | + template <typename Obj, typename Tag, typename = void> |
| 34 | + struct contains_any_tag |
| 35 | + { |
| 36 | + static constexpr const bool value = false; |
| 37 | + using vt = false_; |
| 38 | + using type = void; |
| 39 | + }; |
| 40 | + |
| 41 | + template <typename Obj, typename Tag> |
| 42 | + struct contains_any_tag <Obj, Tag, typename void_<typename Obj::json_tags>::type> |
| 43 | + { |
| 44 | + static constexpr const bool value = true; |
| 45 | + using vt = true_; |
| 46 | + using type = typename Obj::json_tags; |
| 47 | + }; |
| 48 | + |
| 49 | + template <typename T> |
| 50 | + struct identity : public T {}; |
| 51 | + |
| 52 | + template <typename What, typename T, typename... List> |
| 53 | + struct find |
| 54 | + { |
| 55 | + using lifted = lift <find>; |
| 56 | + using true_lift = lift <always_true>; |
| 57 | + using type = lazy_if_t< |
| 58 | + bool_ <std::is_same <What, T>::value>, |
| 59 | + then_ <true_lift>, |
| 60 | + else_ <lifted, What, List...> |
| 61 | + >; |
| 62 | + }; |
| 63 | + |
| 64 | + template <typename What, typename T> |
| 65 | + struct find <What, T> |
| 66 | + { |
| 67 | + using true_lift = lift <always_true>; |
| 68 | + using false_lift = lift <always_false>; |
| 69 | + using type = lazy_if_t < |
| 70 | + bool_ <std::is_same <What, T>::value>, |
| 71 | + then_ <true_lift>, |
| 72 | + else_ <false_lift> |
| 73 | + >; |
| 74 | + }; |
| 75 | + |
| 76 | + template <typename What, typename Obj> |
| 77 | + struct contains_tag_impl |
| 78 | + { |
| 79 | + using type = lift <always_false>; |
| 80 | + }; |
| 81 | + |
| 82 | + template <typename What, typename... List> |
| 83 | + struct contains_tag_impl <What, Tag <List...>> |
| 84 | + { |
| 85 | + using type = typename find <What, List...>::type; |
| 86 | + }; |
| 87 | + |
| 88 | + template <typename Obj, typename What> |
| 89 | + struct contains_tag |
| 90 | + { |
| 91 | + using tag_check = contains_any_tag <Obj, What>; |
| 92 | + |
| 93 | + using impl_lift = lift <contains_tag_impl>; |
| 94 | + using false_lift = lift <always_false>; |
| 95 | + |
| 96 | + using type = lazy_if_t < |
| 97 | + typename tag_check::vt, |
| 98 | + then_ <impl_lift, What, typename tag_check::type>, |
| 99 | + else_ <false_lift> |
| 100 | + >; |
| 101 | + |
| 102 | + constexpr static const bool value = type::template apply<>::value; |
| 103 | + }; |
| 104 | +} |
0 commit comments