Skip to content

Commit 17f9cfa

Browse files
committed
Add glm vector support
1 parent bcef014 commit 17f9cfa

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,60 @@ pranav@ubuntu:~/dev/alpaca/build$ hexdump -C savefile.bin
800800
00000025
801801
```
802802

803+
## Add custom type serialization
804+
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 `<alpaca/alpaca.h>`. After that, use your header file, instead of alpaca one.
805+
806+
For example, we need to add serialization for type `MyCustomType<typename T, typename U, int L>`
807+
```cpp
808+
#pragma once
809+
810+
#include <alpaca/detail/field_type.h>
811+
#include <alpaca/detail/options.h>
812+
#include <alpaca/detail/aggregate_arity.h>
813+
814+
#include <system_error>
815+
#include <unordered_map>
816+
#include <vector>
817+
818+
namespace alpaca {
819+
820+
namespace detail {
821+
822+
823+
template <typename T> struct is_my_custom_type : std::false_type {};
824+
825+
template <typename T, typename U, int L>
826+
struct is_my_custom_type<MY_CUSTOM_TYPE<T, U, L>> : std::true_type {};
827+
828+
template <typename T>
829+
typename std::enable_if<is_my_custom_type<T>::value, void>::type
830+
type_info(
831+
std::vector<uint8_t> &typeids,
832+
std::unordered_map<std::string_view, std::size_t> &struct_visitor_map) {
833+
834+
// Run type_info for inner types
835+
type_info<typename T::T>(typeids, struct_visitor_map);
836+
type_info<typename T::U>(typeids, struct_visitor_map);
837+
}
838+
839+
template <options O, typename Container, typename T, typename U, int L>
840+
void to_bytes(Container &bytes, std::size_t &byte_index, const MY_CUSTOM_TYPE<T, U, L> &input) {
841+
// implement to bytes
842+
}
843+
844+
template <options O, typename T, typename U, int L, typename Container>
845+
bool from_bytes(MY_CUSTOM_TYPE<T, U, L> &output, Container &bytes, std::size_t &byte_index, std::size_t &end_index,
846+
std::error_code &error_code) {
847+
// implement from bytes
848+
return true;
849+
}
850+
} // namespace detail
851+
} // namespace alpaca
852+
853+
#include <alpaca/alpaca.h>
854+
```
855+
856+
803857
## Backward and Forward Compatibility
804858
805859
* 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.

include/alpaca/alpaca.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <alpaca/detail/types/unique_ptr.h>
2525
#include <alpaca/detail/types/variant.h>
2626
#include <alpaca/detail/types/vector.h>
27+
#include <alpaca/detail/types/glm_vector.h>
2728
#include <alpaca/detail/variable_length_encoding.h>
2829
#include <cassert>
2930
#include <system_error>
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
#ifdef ALPACA_INCLUDE_SUPPORT_GLM_VECTOR
3+
#include <alpaca/detail/types/array.h>
4+
5+
#include <glm/ext/vector_float3.hpp>
6+
#include <system_error>
7+
#include <unordered_map>
8+
#include <vector>
9+
10+
namespace alpaca {
11+
12+
namespace detail {
13+
14+
15+
template <typename T> struct is_glm_vec : std::false_type {};
16+
17+
template <int L, typename T, glm::qualifier Q>
18+
struct is_glm_vec<glm::vec<L, T, Q>> : std::true_type {};
19+
20+
template <typename T>
21+
typename std::enable_if<is_glm_vec<T>::value, void>::type
22+
type_info(
23+
std::vector<uint8_t> &typeids,
24+
std::unordered_map<std::string_view, std::size_t> &struct_visitor_map) {
25+
type_info<std::array<typename T::T, T::L>>(typeids, struct_visitor_map);
26+
}
27+
28+
template <options O, typename Container, int L, typename T, glm::qualifier Q>
29+
void to_bytes(Container &bytes, std::size_t &byte_index, const glm::vec<L, T, Q> &input) {
30+
std::array<T, L> data;
31+
for (int i = 0; i < L; i++)
32+
data[i] = input[i];
33+
34+
to_bytes<O, Container, T, L>(bytes, byte_index, data);
35+
}
36+
37+
template <options O, int L, typename T, glm::qualifier Q, typename Container>
38+
bool from_bytes(glm::vec<L, T, Q> &output, Container &bytes, std::size_t &byte_index, std::size_t &end_index,
39+
std::error_code &error_code) {
40+
std::array<T, L> output_array;
41+
from_bytes<O, T, Container, L>(output_array, bytes, byte_index, end_index, error_code);
42+
43+
for (int i = 0; i < L; i++)
44+
output[i] = output_array[i];
45+
46+
return true;
47+
}
48+
49+
} // namespace detail
50+
51+
} // namespace alpaca
52+
#endif

0 commit comments

Comments
 (0)