diff options
Diffstat (limited to 'modules/codec/c++/schema.hpp')
-rw-r--r-- | modules/codec/c++/schema.hpp | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/modules/codec/c++/schema.hpp b/modules/codec/c++/schema.hpp new file mode 100644 index 0000000..4549916 --- /dev/null +++ b/modules/codec/c++/schema.hpp @@ -0,0 +1,172 @@ +#pragma once + +#include <forstio/common.h> +#include <forstio/string_literal.h> + +namespace saw { +namespace schema { +// NOLINTBEGIN +template <typename T, string_literal Literal> struct Member { + static constexpr string_literal name = "Member"; +}; + +template <typename... T> struct Struct { + static_assert( + always_false<T...>, + "This schema template doesn't support this type of template argument"); +}; + +template <typename... V, string_literal... K> +struct Struct<Member<V, K>...> { + static constexpr string_literal name = "Struct"; +}; + +template <typename... T> struct Union { + static_assert( + always_false<T...>, + "This schema template doesn't support this type of template argument"); +}; + +template <typename... V, string_literal... K> +struct Union<Member<V, K>...> { + static constexpr string_literal name = "Union"; +}; + +template <typename T, size_t Dim = 1> struct Array { + static constexpr string_literal name = "Array"; +}; + +template<typename T, size_t... S> struct FixedArray { + static constexpr string_literal name = "FixedArray"; +}; + +template <typename... T> struct Tuple { + static constexpr string_literal name = "Tuple"; +}; + +/** + * This acts as a separator of different encodings being mashed together + * For example we can transport any base64 encodings in JSON + * + * using WrappedExample = schema::Tuple< + * schema::Wrapper<schema::String, encode::Base64> + * >; + * + * data<WrappedExample, encode::Json> ex_data; + */ +template <typename T, typename Enc> +class Wrapper { + static constexpr string_literal name = "Wrapper"; +}; + +struct String { + static constexpr string_literal name = "String"; +}; + + +struct SignedInteger { + static constexpr string_literal name = "SignedInteger"; +}; +struct UnsignedInteger { + static constexpr string_literal name = "UnsignedInteger"; +}; +struct FloatingPoint { + static constexpr string_literal name = "FloatingPoint"; +}; + +template <class T, size_t N> struct Primitive { + static_assert(((std::is_same_v<T, SignedInteger> || + std::is_same_v<T, UnsignedInteger>)&&(N == 1 || N == 2 || + N == 4 || N == 8)) || + (std::is_same_v<T, FloatingPoint> && (N == 4 || N == 8)), + "Primitive Type is not supported"); + static constexpr string_literal name = "Primitive"; +}; + +using Int8 = Primitive<SignedInteger, 1>; +using Int16 = Primitive<SignedInteger, 2>; +using Int32 = Primitive<SignedInteger, 4>; +using Int64 = Primitive<SignedInteger, 8>; + +using UInt8 = Primitive<UnsignedInteger, 1>; +using UInt16 = Primitive<UnsignedInteger, 2>; +using UInt32 = Primitive<UnsignedInteger, 4>; +using UInt64 = Primitive<UnsignedInteger, 8>; + +using Float32 = Primitive<FloatingPoint, 4>; +using Float64 = Primitive<FloatingPoint, 8>; + +/** + * Classes allowing to distinguish Ints from VarInts + */ +template<typename T, std::size_t MaxLen> +struct VariableLengthPrimitive { + static constexpr string_literal name = "VariableLengthPrimitive"; +}; + +using VarInt = VariableLengthPrimitive<SignedInteger, 5>; +using VarLong = VariableLengthPrimitive<SignedInteger, 10>; + +/** + * Classes enabling Rpc calls + */ +template <class Request, class Response> +struct Function { + static constexpr string_literal name = "Function"; +}; + +template <class... T> struct Interface { + static_assert( + always_false<T...>, + "This schema template doesn't support this type of template argument"); + + static constexpr string_literal name = "Interface"; +}; + +template <class... Requests, class... Responses, string_literal... Names> +struct Interface<Member<Function<Requests, Responses>,Names>...> { + static constexpr string_literal name = "Interface"; +}; + +// NOLINTEND +} // namespace schema +template <class T> struct is_struct { + constexpr static bool value = false; +}; + +template <class... V, string_literal... K> struct is_struct<schema::Struct<schema::Member<V,K>...>> { + constexpr static bool value = true; +}; + +template <class T> struct is_string { + constexpr static bool value = false; +}; + +template <> struct is_string<schema::String> { + constexpr static bool value = true; +}; + +template <class T> struct is_tuple { + constexpr static bool value = false; +}; + +template <class... T> struct is_tuple<schema::Tuple<T...>> { + constexpr static bool value = true; +}; + +template <class T> struct is_array { + constexpr static bool value = false; +}; + +template <class T, size_t Dim> struct is_array<schema::Array<T,Dim>> { + constexpr static bool value = true; +}; + +template <class T> struct is_primitive { + constexpr static bool value = false; +}; + +template <typename T, size_t N> struct is_primitive<schema::Primitive<T,N>> { + constexpr static bool value = true; +}; +} // namespace saw |