Commit 1cf9ca9b authored by ufownl's avatar ufownl

Support array members in `announce`

parent f4af07aa
......@@ -86,6 +86,7 @@ using list_impl = std::integral_constant<int, 1>;
using map_impl = std::integral_constant<int, 2>;
using pair_impl = std::integral_constant<int, 3>;
using opt_impl = std::integral_constant<int, 4>;
using array_impl = std::integral_constant<int, 5>;
using recursive_impl = std::integral_constant<int, 9>;
template <class T>
......@@ -100,7 +101,9 @@ constexpr int impl_id() {
? 3
: (detail::is_optional<T>::value
? 4
: 9))));
: (std::is_array<T>::value
? 5
: 9)))));
}
template <class T>
......@@ -166,6 +169,13 @@ private:
}
}
template <class T, size_t N>
void simpl(const T (&val)[N], serializer* s, array_impl) const {
for (size_t i = 0; i < N; ++i) {
(*this)(val[i], s);
}
}
template <class T>
void simpl(const T& val, serializer* s, recursive_impl) const {
uniform_typeid<T>()->serialize(&val, s);
......@@ -219,6 +229,13 @@ private:
}
}
template <class T, size_t N>
void dimpl(T (&val)[N], deserializer* d, array_impl) const {
for (size_t i = 0; i < N; ++i) {
(*this)(val[i], d);
}
}
template <class T>
void dimpl(T& storage, deserializer* d, recursive_impl) const {
uniform_typeid<T>()->deserialize(&storage, d);
......
......@@ -71,6 +71,48 @@ private:
T value_;
};
template <class T, size_t N>
class uniform_value_impl<T[N]> : public uniform_value_t {
public:
uniform_value_impl(const uniform_type_info* ptr)
: uniform_value_t(ptr, &value_) {
// nop
}
uniform_value_impl(const uniform_type_info* ptr, const T (&val)[N])
: uniform_value_t(ptr, &value_) {
array_copy(value_, val);
}
uniform_value copy() override {
return uniform_value{new uniform_value_impl(ti, value_)};
}
private:
template <class U, size_t Len>
static void array_copy_impl(U (&lhs)[Len], const U (&rhs)[Len],
std::true_type) {
for (size_t i = 0; i < Len; ++i) {
array_copy(lhs[i], rhs[i]);
}
}
template <class U, size_t Len>
static void array_copy_impl(U (&lhs)[Len], const U (&rhs)[Len],
std::false_type) {
std::copy(rhs, rhs + Len, lhs);
}
template <class U, size_t Len>
static void array_copy(U (&lhs)[Len], const U (&rhs)[Len]) {
array_copy_impl(lhs, rhs,
std::integral_constant<bool,
std::is_array<U>::value>{});
}
T value_[N];
};
/// Creates a uniform value of type `T`.
template <class T, class... Ts>
uniform_value make_uniform_value(const uniform_type_info* uti, Ts&&... xs) {
......
......@@ -106,16 +106,29 @@ enum class test_enum {
c
};
struct test_array {
int value[4];
int value2[2][4];
};
struct fixture {
int32_t i32 = -345;
test_enum te = test_enum::b;
string str = "Lorem ipsum dolor sit amet.";
raw_struct rs;
test_array ta {
{0, 1, 2, 3},
{
{0, 1, 2, 3},
{4, 5, 6, 7}
},
};
message msg;
fixture() {
announce<test_enum>("test_enum");
announce(typeid(raw_struct), uniform_type_info_ptr{new raw_struct_type_info});
announce<test_array>("test_array", &test_array::value, &test_array::value2);
rs.str.assign(string(str.rbegin(), str.rend()));
msg = make_message(i32, te, str, rs);
}
......@@ -259,6 +272,20 @@ CAF_TEST(test_raw_struct) {
CAF_CHECK(rs == x);
}
CAF_TEST(test_array_serialization) {
auto buf = binary_util::serialize(ta);
test_array x;
binary_util::deserialize(buf, &x);
for (auto i = 0; i < 4; ++i) {
CAF_CHECK(ta.value[i] == x.value[i]);
}
for (auto i = 0; i < 2; ++i) {
for (auto j = 0; j < 4; ++j) {
CAF_CHECK(ta.value2[i][j] == x.value2[i][j]);
}
}
}
CAF_TEST(test_single_message) {
auto buf = binary_util::serialize(msg);
message x;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment