From 17f9cfa82fd593abf6df6bad777f173e74902f08 Mon Sep 17 00:00:00 2001 From: Volodymyr Zvarun Date: Thu, 30 May 2024 16:51:16 +0300 Subject: [PATCH] Add glm vector support --- README.md | 54 ++++++++++++++++++++++++ include/alpaca/alpaca.h | 1 + include/alpaca/detail/types/glm_vector.h | 52 +++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 include/alpaca/detail/types/glm_vector.h diff --git a/README.md b/README.md index 40de65b..ec2ce1b 100644 --- a/README.md +++ b/README.md @@ -800,6 +800,60 @@ pranav@ubuntu:~/dev/alpaca/build$ hexdump -C savefile.bin 00000025 ``` +## Add custom type serialization +Not all types are supported by this library, but you can easily define serialization for custom types from other libraries. To do this, you need to create header file, in which define: `type_info`, `to_bytes` and `from_bytes` methods, and in the end of this file include ``. After that, use your header file, instead of alpaca one. + +For example, we need to add serialization for type `MyCustomType` +```cpp +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace alpaca { + +namespace detail { + + +template struct is_my_custom_type : std::false_type {}; + +template +struct is_my_custom_type> : std::true_type {}; + +template +typename std::enable_if::value, void>::type +type_info( + std::vector &typeids, + std::unordered_map &struct_visitor_map) { + + // Run type_info for inner types + type_info(typeids, struct_visitor_map); + type_info(typeids, struct_visitor_map); +} + +template +void to_bytes(Container &bytes, std::size_t &byte_index, const MY_CUSTOM_TYPE &input) { + // implement to bytes +} + +template +bool from_bytes(MY_CUSTOM_TYPE &output, Container &bytes, std::size_t &byte_index, std::size_t &end_index, + std::error_code &error_code) { + // implement from bytes + return true; +} +} // namespace detail +} // namespace alpaca + +#include +``` + + ## Backward and Forward Compatibility * A change made to a system or technology in such a way that the existing users are unaffected is a ***backward compatible change***. The obvious advantage is that the existing users have a non-time sensitive and a graceful way of upgrading their integrations. On the other hand, a non backward-compatible change breaks the existing integrations and forces the existing users to deal with an immediate fix. diff --git a/include/alpaca/alpaca.h b/include/alpaca/alpaca.h index d9d870f..207d4d2 100644 --- a/include/alpaca/alpaca.h +++ b/include/alpaca/alpaca.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/include/alpaca/detail/types/glm_vector.h b/include/alpaca/detail/types/glm_vector.h new file mode 100644 index 0000000..6672304 --- /dev/null +++ b/include/alpaca/detail/types/glm_vector.h @@ -0,0 +1,52 @@ +#pragma once +#ifdef ALPACA_INCLUDE_SUPPORT_GLM_VECTOR +#include + +#include +#include +#include +#include + +namespace alpaca { + +namespace detail { + + +template struct is_glm_vec : std::false_type {}; + +template +struct is_glm_vec> : std::true_type {}; + +template +typename std::enable_if::value, void>::type +type_info( + std::vector &typeids, + std::unordered_map &struct_visitor_map) { + type_info>(typeids, struct_visitor_map); +} + +template +void to_bytes(Container &bytes, std::size_t &byte_index, const glm::vec &input) { + std::array data; + for (int i = 0; i < L; i++) + data[i] = input[i]; + + to_bytes(bytes, byte_index, data); +} + +template +bool from_bytes(glm::vec &output, Container &bytes, std::size_t &byte_index, std::size_t &end_index, + std::error_code &error_code) { + std::array output_array; + from_bytes(output_array, bytes, byte_index, end_index, error_code); + + for (int i = 0; i < L; i++) + output[i] = output_array[i]; + + return true; +} + +} // namespace detail + +} // namespace alpaca +#endif