Skip to content

Commit

Permalink
Add glm vector support
Browse files Browse the repository at this point in the history
  • Loading branch information
lolpie244 committed Jun 3, 2024
1 parent bcef014 commit 17f9cfa
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<alpaca/alpaca.h>`. After that, use your header file, instead of alpaca one.

For example, we need to add serialization for type `MyCustomType<typename T, typename U, int L>`
```cpp
#pragma once

#include <alpaca/detail/field_type.h>
#include <alpaca/detail/options.h>
#include <alpaca/detail/aggregate_arity.h>

#include <system_error>
#include <unordered_map>
#include <vector>

namespace alpaca {

namespace detail {


template <typename T> struct is_my_custom_type : std::false_type {};

template <typename T, typename U, int L>
struct is_my_custom_type<MY_CUSTOM_TYPE<T, U, L>> : std::true_type {};

template <typename T>
typename std::enable_if<is_my_custom_type<T>::value, void>::type
type_info(
std::vector<uint8_t> &typeids,
std::unordered_map<std::string_view, std::size_t> &struct_visitor_map) {

// Run type_info for inner types
type_info<typename T::T>(typeids, struct_visitor_map);
type_info<typename T::U>(typeids, struct_visitor_map);
}

template <options O, typename Container, typename T, typename U, int L>
void to_bytes(Container &bytes, std::size_t &byte_index, const MY_CUSTOM_TYPE<T, U, L> &input) {
// implement to bytes
}

template <options O, typename T, typename U, int L, typename Container>
bool from_bytes(MY_CUSTOM_TYPE<T, U, L> &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 <alpaca/alpaca.h>
```
## 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.
Expand Down
1 change: 1 addition & 0 deletions include/alpaca/alpaca.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <alpaca/detail/types/unique_ptr.h>
#include <alpaca/detail/types/variant.h>
#include <alpaca/detail/types/vector.h>
#include <alpaca/detail/types/glm_vector.h>
#include <alpaca/detail/variable_length_encoding.h>
#include <cassert>
#include <system_error>
Expand Down
52 changes: 52 additions & 0 deletions include/alpaca/detail/types/glm_vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once
#ifdef ALPACA_INCLUDE_SUPPORT_GLM_VECTOR
#include <alpaca/detail/types/array.h>

#include <glm/ext/vector_float3.hpp>
#include <system_error>
#include <unordered_map>
#include <vector>

namespace alpaca {

namespace detail {


template <typename T> struct is_glm_vec : std::false_type {};

template <int L, typename T, glm::qualifier Q>
struct is_glm_vec<glm::vec<L, T, Q>> : std::true_type {};

template <typename T>
typename std::enable_if<is_glm_vec<T>::value, void>::type
type_info(
std::vector<uint8_t> &typeids,
std::unordered_map<std::string_view, std::size_t> &struct_visitor_map) {
type_info<std::array<typename T::T, T::L>>(typeids, struct_visitor_map);
}

template <options O, typename Container, int L, typename T, glm::qualifier Q>
void to_bytes(Container &bytes, std::size_t &byte_index, const glm::vec<L, T, Q> &input) {
std::array<T, L> data;
for (int i = 0; i < L; i++)
data[i] = input[i];

to_bytes<O, Container, T, L>(bytes, byte_index, data);
}

template <options O, int L, typename T, glm::qualifier Q, typename Container>
bool from_bytes(glm::vec<L, T, Q> &output, Container &bytes, std::size_t &byte_index, std::size_t &end_index,
std::error_code &error_code) {
std::array<T, L> output_array;
from_bytes<O, T, Container, L>(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

0 comments on commit 17f9cfa

Please sign in to comment.