/*
* This file is part of the DXX-Rebirth project .
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
#pragma once
#include
#include
#include
#include
#include
#include
#include "dxxsconf.h"
#include "compiler-addressof.h"
#include "compiler-array.h"
#include "compiler-integer_sequence.h"
#include "compiler-range_for.h"
#include "compiler-static_assert.h"
#include "compiler-type_traits.h"
namespace serial {
template
class message;
/* Classifiers to identify whether a type is a message<...> */
template
class is_message : public tt::false_type
{
};
template
class is_message> : public tt::true_type
{
};
template
class integral_type
{
static_assert(tt::is_integral::value, "integral_type used on non-integral type");
public:
static const std::size_t maximum_size = sizeof(T);
};
template
class enum_type
{
static_assert(tt::is_enum::value, "enum_type used on non-enum type");
public:
static const std::size_t maximum_size = sizeof(T);
};
template
class is_cxx_array : public tt::false_type
{
};
template
class is_cxx_array> : public tt::true_type
{
};
template
class is_cxx_array : public is_cxx_array
{
};
template
class is_generic_class : public tt::conditional::value, tt::false_type, tt::is_class>::type
{
};
template
static inline typename tt::enable_if::value, void>::type process_buffer(Accessor &, A1 &);
template
static inline typename tt::enable_if::value, void>::type process_buffer(Accessor &, A1 &);
template
static inline typename tt::enable_if::value, void>::type process_buffer(Accessor &, A1 &);
template
typename tt::enable_if::value, void>::type process_buffer(Accessor &, A1 &);
template
static void process_buffer(Accessor &, const message &);
template
class class_type;
template
class array_type;
template
class unhandled_type;
/* Implementation details - avoid namespace pollution */
namespace detail {
template
class pad_type
{
};
template
message> udt_to_message(const pad_type &);
/*
* This can never be instantiated, but will be requested if a UDT
* specialization is missing.
*/
template
struct missing_udt_specialization
{
#ifndef DXX_HAVE_CXX11_EXPLICIT_DELETE
protected:
#endif
missing_udt_specialization() DXX_CXX11_EXPLICIT_DELETE;
};
template
void udt_to_message(T &, missing_udt_specialization = missing_udt_specialization());
template
void preprocess_udt(Accessor &, UDT &) {}
template
void postprocess_udt(Accessor &, UDT &) {}
template
static inline void process_udt(Accessor &accessor, UDT &udt)
{
process_buffer(accessor, udt_to_message(udt));
}
template
class class_type_indirection
{
public:
typedef typename tt::enable_if::value, decltype(udt_to_message(*(const T*)0))>::type type;
};
template
void check_enum(Accessor &, E) {}
template
class base_bytebuffer_t : public std::iterator
{
public:
typedef typename std::iterator::pointer pointer;
typedef typename std::iterator::difference_type difference_type;
base_bytebuffer_t(pointer u) : p(u) {}
operator pointer() const { return p; }
D &operator+=(difference_type d)
{
p += d;
return *static_cast(this);
}
protected:
pointer p;
};
template
struct pad_storage
{
#define SERIAL_UDT_ROUND_UP(X,M) (((X) + (M) - 1) & ~((M) - 1))
static const std::size_t SERIAL_UDT_ROUND_MULTIPLIER = sizeof(void *);
static const std::size_t SERIAL_UDT_ROUND_UP_AMOUNT = SERIAL_UDT_ROUND_UP(amount, SERIAL_UDT_ROUND_MULTIPLIER);
static_assert(amount % SERIAL_UDT_ROUND_MULTIPLIER ? SERIAL_UDT_ROUND_UP_AMOUNT > amount && SERIAL_UDT_ROUND_UP_AMOUNT < amount + SERIAL_UDT_ROUND_MULTIPLIER : SERIAL_UDT_ROUND_UP_AMOUNT == amount, "round up error");
static_assert(SERIAL_UDT_ROUND_UP_AMOUNT % SERIAL_UDT_ROUND_MULTIPLIER == 0, "round modulus error");
union {
array f;
array p;
};
#undef SERIAL_UDT_ROUND_UP
};
template
struct in_pad_storage : public pad_storage
{
};
template
struct out_pad_storage : public pad_storage
{
out_pad_storage()
{
this->f.fill(value);
}
};
template
static inline void process_udt(Accessor &accessor, const pad_type &udt)
{
typename tt::conditional::type>::value, in_pad_storage, out_pad_storage>::type s;
for (std::size_t count = amount; count; count -= s.f.size())
{
if (count < s.f.size())
{
assert(count == s.p.size());
process_buffer(accessor, s.p);
break;
}
process_buffer(accessor, s.f);
}
}
static inline void sequence(std::initializer_list) {}
}
template
static inline const detail::pad_type &pad()
{
static const detail::pad_type p{};
return p;
}
#define DEFINE_SERIAL_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \
DEFINE_SERIAL_CONST_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \
DEFINE_SERIAL_MUTABLE_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \
#define _DEFINE_SERIAL_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \
static inline auto udt_to_message(TYPE &NAME) -> decltype(serial::make_message MEMBERLIST) { \
return serial::make_message MEMBERLIST; \
}
#define DEFINE_SERIAL_CONST_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \
_DEFINE_SERIAL_UDT_TO_MESSAGE(const TYPE, NAME, MEMBERLIST)
#define DEFINE_SERIAL_MUTABLE_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \
_DEFINE_SERIAL_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST)
#define ASSERT_SERIAL_UDT_MESSAGE_SIZE(T, SIZE) \
assert_equal(serial::class_type::maximum_size, SIZE, "sizeof(" #T ") is not " #SIZE)
template
class udt_message_compatible_same_type : public tt::integral_constant::type, T1>::value>
{
static_assert(tt::is_same::type, T1>::value, "parameter type mismatch");
};
template
class assert_udt_message_compatible2;
template
class assert_udt_message_compatible2 : public tt::integral_constant
{
};
template
class assert_udt_message_compatible2, std::tuple> : public udt_message_compatible_same_type
{
};
template
class assert_udt_message_compatible2, std::tuple> :
public tt::integral_constant::value, message, std::tuple>::value>
{
};
template
class assert_udt_message_compatible1;
template
class assert_udt_message_compatible1, std::tuple> : public tt::integral_constant, std::tuple>::value>
{
static_assert(sizeof...(Mn) <= sizeof...(Tn), "too few types in tuple");
static_assert(sizeof...(Mn) >= sizeof...(Tn), "too few types in message");
};
template
class assert_udt_message_compatible
{
};
template
class assert_udt_message_compatible> : public tt::integral_constant::message, std::tuple>::value>
{
};
#define _SERIAL_UDT_UNWRAP_LIST(A1,...) A1, ## __VA_ARGS__
#define ASSERT_SERIAL_UDT_MESSAGE_TYPE(T, TYPELIST) \
ASSERT_SERIAL_UDT_MESSAGE_CONST_TYPE(T, TYPELIST); \
ASSERT_SERIAL_UDT_MESSAGE_MUTABLE_TYPE(T, TYPELIST); \
#define _ASSERT_SERIAL_UDT_MESSAGE_TYPE(T, TYPELIST) \
static_assert(serial::assert_udt_message_compatible>::value, "udt/message mismatch")
#define ASSERT_SERIAL_UDT_MESSAGE_CONST_TYPE(T, TYPELIST) \
_ASSERT_SERIAL_UDT_MESSAGE_TYPE(const T, TYPELIST)
#define ASSERT_SERIAL_UDT_MESSAGE_MUTABLE_TYPE(T, TYPELIST) \
_ASSERT_SERIAL_UDT_MESSAGE_TYPE(T, TYPELIST)
/*
* Copy bytes from src to dst. If running on a little endian system,
* behave like memcpy. If running on a big endian system, copy
* backwards so that the destination is endian-swapped from the source.
*/
static inline void little_endian_copy(const uint8_t *src, uint8_t *dst, std::size_t len)
{
const uint8_t *srcend = src + len;
union {
uint8_t c;
uint16_t s;
};
s = 1;
if (c)
std::copy(src, srcend, dst);
else
std::reverse_copy(src, srcend, dst);
}
template
class message_type
{
typedef
typename tt::conditional::value, integral_type,
typename tt::conditional::value, enum_type,
typename tt::conditional::value, array_type,
typename tt::conditional::value, class_type,
unhandled_type
>::type
>::type
>::type
>::type effective_type;
public:
static const std::size_t maximum_size = effective_type::maximum_size;
};
template
class class_type : public message_type::type>
{
public:
typedef typename detail::class_type_indirection::type message;
};
template
class array_type>
{
public:
static const std::size_t maximum_size = message_type::maximum_size * N;
};
template
class array_type> : public array_type>
{
};
template
class message_type>
{
public:
static const std::size_t maximum_size = message_type::maximum_size;
};
template
class message_type>
{
public:
static const std::size_t maximum_size = message_type::maximum_size + message_type>::maximum_size;
};
template
class message
{
typedef std::tuple::type, typename tt::add_pointer::type...> tuple_type;
template
static void check_type()
{
static_assert(message_type::maximum_size > 0, "empty field in message");
}
static void check_types()
{
check_type();
detail::sequence({(check_type(), static_cast(0))...});
}
tuple_type t;
public:
typedef A1 head_type;
message(A1 &a1, Args&... args) :
t(addressof(a1), addressof(args)...)
{
check_types();
}
const tuple_type &get_tuple() const
{
return t;
}
};
template
static inline message make_message(A1 &a1, Args&... args)
{
return message(a1, std::forward(args)...);
}
namespace reader {
class bytebuffer_t : public detail::base_bytebuffer_t
{
public:
bytebuffer_t(pointer u) : base_bytebuffer_t(u) {}
};
template
static inline void process_integer(Accessor &buffer, A1 &a1)
{
union {
A1 a;
uint8_t u[message_type::maximum_size];
};
assert_equal(sizeof(a), sizeof(u), "message_type::maximum_size is wrong");
little_endian_copy(buffer, u, sizeof(u));
std::advance(buffer, sizeof(u));
a1 = a;
}
}
namespace writer {
class bytebuffer_t : public detail::base_bytebuffer_t
{
public:
bytebuffer_t(pointer u) : base_bytebuffer_t(u) {}
};
template
static inline void process_integer(Accessor &buffer, const A1 &a1)
{
union {
A1 a;
uint8_t u[message_type::maximum_size];
};
assert_equal(sizeof(a), sizeof(u), "message_type::maximum_size is wrong");
a = a1;
little_endian_copy(u, buffer, sizeof(u));
std::advance(buffer, sizeof(u));
}
}
template
static inline typename tt::enable_if::value, void>::type process_buffer(Accessor &accessor, A1 &a1)
{
process_integer(accessor, a1);
}
template
static inline typename tt::enable_if::value, void>::type process_buffer(Accessor &accessor, A1 &a1)
{
using detail::check_enum;
process_integer(accessor, a1);
/* Hook for enum types to check that the given value is legal */
check_enum(accessor, a1);
}
template
static inline typename tt::enable_if::value, void>::type process_buffer(Accessor &accessor, A1 &a1)
{
using detail::preprocess_udt;
using detail::process_udt;
using detail::postprocess_udt;
preprocess_udt(accessor, a1);
process_udt(accessor, a1);
postprocess_udt(accessor, a1);
}
template
typename tt::enable_if::value, void>::type process_buffer(Accessor &accessor, A1 &a1)
{
range_for (auto &i, a1)
process_buffer(accessor, i);
}
template
static inline void process_message_tuple(Accessor &accessor, const std::tuple &t, index_sequence)
{
detail::sequence({(process_buffer(accessor, *std::get(t)), static_cast(0))...});
}
template
static void process_buffer(Accessor &accessor, const message &m)
{
process_message_tuple(accessor, m.get_tuple(), make_tree_index_sequence<1 + sizeof...(Args)>());
}
}