diff options
Diffstat (limited to 'modules/codec/c++/schema.hpp')
-rw-r--r-- | modules/codec/c++/schema.hpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/modules/codec/c++/schema.hpp b/modules/codec/c++/schema.hpp index 37615c5..a992a84 100644 --- a/modules/codec/c++/schema.hpp +++ b/modules/codec/c++/schema.hpp @@ -135,6 +135,91 @@ struct Interface<Member<Function<Requests, Responses>,Names>...> { // NOLINTEND } // namespace schema + +template<typename T, typename Schema> +struct schema_has_member{ + static_assert( + always_false<T, Schema>, + "Not supported schema case"); +}; + +/** + * Checks if identical type exists in schema structure + */ +template<typename T, typename... Members> +struct schema_has_member<T, schema::Struct<Members...>> { +private: + template<size_t i> + static constexpr bool value_element() { + if constexpr ( i < sizeof...(Members) ){ + using MT = typename parameter_pack_type<i, Members...>::type; + if constexpr ( std::is_same_v<T,MT> ) { + return true; + } else { + return value_element<i+1u>(); + } + } + return false; + } +public: + static constexpr bool value = value_element<0>(); +}; + +/** + * + */ +template<string_literal Key, typename Schema> +struct schema_has_key { + static_assert( + always_false<Schema>, + "Not supported schema case"); +}; + +template<string_literal Key, typename... Members> +struct schema_has_key<Key, schema::Struct<Members...>> { +private: + template<size_t i> + static constexpr bool value_element() { + if constexpr ( i < sizeof...(Members) ){ + using MT = typename parameter_pack_type<i, Members...>::type; + + if constexpr ( Key == MT::KeyLiteral ) { + return true; + }else{ + return value_element<i+1u>(); + } + } + return false; + } +public: + static constexpr bool value = value_element<0>(); +}; + +template<string_literal Name, typename Iface> +struct schema_member_index { + static_assert( + always_false<Iface>, + "Not supported schema case"); +}; + +template<string_literal Name, typename... MemberVals, string_literal... MemberKeys > +struct schema_member_index<Name, schema::Interface<schema::Member<MemberVals, MemberKeys>...>> { + static constexpr uint64_t value = parameter_key_pack_index<Name, MemberKeys...>::value; +}; + +template<string_literal Name, typename Iface> +struct schema_member_type { + static_assert( + always_false<Iface>, + "Not supported schema case"); +}; + +template<string_literal Name, typename... MemberVals, string_literal... MemberKeys > +struct schema_member_type<Name, schema::Interface<schema::Member<MemberVals, MemberKeys>...>> { + + using type = typename parameter_pack_type<schema_member_index<Name, schema::Interface<schema::Member<MemberVals,MemberKeys>...>>::value, MemberVals...>::type; +}; + template <class T> struct is_struct { constexpr static bool value = false; }; |