.. _program_listing_file__tmp_ws_src_data_tamer_data_tamer_cpp_include_data_tamer_types.hpp: Program Listing for File types.hpp ================================== |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/data_tamer/data_tamer_cpp/include/data_tamer/types.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include #include #include namespace DataTamer { constexpr int SCHEMA_VERSION = 4; // clang-format off enum class BasicType { BOOL, CHAR, INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT32, FLOAT64, OTHER }; constexpr size_t TypesCount = 13; using VarNumber = std::variant< bool, char, int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, double >; // clang-format on [[nodiscard]] VarNumber DeserializeAsVarType(const BasicType& type, const void* data); [[nodiscard]] size_t SizeOf(const BasicType& type); [[nodiscard]] const std::string& ToStr(const BasicType& type); [[nodiscard]] BasicType FromStr(const std::string& str); template inline constexpr BasicType GetBasicType() { if constexpr(std::is_enum_v) { return GetBasicType>(); } // clang-format off if constexpr (std::is_same_v) return BasicType::BOOL; if constexpr (std::is_same_v) return BasicType::CHAR; if constexpr (std::is_same_v) return BasicType::INT8; if constexpr (std::is_same_v) return BasicType::UINT8; if constexpr (std::is_same_v) return BasicType::INT16; if constexpr (std::is_same_v) return BasicType::UINT16; if constexpr (std::is_same_v) return BasicType::INT32; if constexpr (std::is_same_v) return BasicType::UINT32; if constexpr (std::is_same_v) return BasicType::UINT64; if constexpr (std::is_same_v) return BasicType::INT64; if constexpr (std::is_same_v) return BasicType::FLOAT32; if constexpr (std::is_same_v) return BasicType::FLOAT64; // clang-format on return BasicType::OTHER; } template inline constexpr bool IsNumericType() { return std::is_arithmetic_v || std::is_same_v || std::is_same_v || std::is_enum_v; } struct RegistrationID { size_t first_index = 0; size_t fields_count = 0; // syntactic sugar to be used to concatenate contiguous RegistrationID. void operator+=(const RegistrationID& other) { fields_count += other.fields_count; } }; //--------------------------------------------------------- struct TypeField { std::string field_name; BasicType type = BasicType::OTHER; std::string type_name; bool is_vector = 0; uint32_t array_size = 0; bool operator==(const TypeField& other) const; bool operator!=(const TypeField& other) const; friend std::ostream& operator<<(std::ostream& os, const TypeField& field); }; using FieldsVector = std::vector; class CustomSerializer; struct CustomSchema { std::string encoding; std::string schema; }; struct Schema { uint64_t hash = 0; FieldsVector fields; std::string channel_name; std::unordered_map custom_types; std::unordered_map custom_schemas; friend std::ostream& operator<<(std::ostream& os, const Schema& schema); }; std::string ToStr(const Schema& schema); [[nodiscard]] uint64_t AddFieldToHash(const TypeField& field, uint64_t hash); } // namespace DataTamer template <> struct std::hash { std::size_t operator()( const DataTamer::RegistrationID& id ) const { // Compute individual hash values for first, second and third // http://stackoverflow.com/a/1646913/126995 std::size_t res = 17; res = res * 31 + hash()( id.first_index ); res = res * 31 + hash()( id.fields_count ); return res; } };