25 #ifndef INCLUDED_SerializationTraits_h_GUID_DF0CDFE0_097F_41B2_2DAE_7D4033D28D43 26 #define INCLUDED_SerializationTraits_h_GUID_DF0CDFE0_097F_41B2_2DAE_7D4033D28D43 38 #include <boost/call_traits.hpp> 39 #include <boost/noncopyable.hpp> 44 #include <type_traits> 49 namespace serialization {
53 template <
typename Tag>
55 std::integral_constant<bool, std::is_same<Tag, Tag>::value>;
85 template <
typename Tag,
typename Dummy =
void>
100 "You must implement a SerializationTraits " 101 "specialization for this type/tag, or at least a " 102 "SimpleStructSerialization!");
107 template <
typename T,
typename BufferType,
110 Tag
const &tag = Tag()) {
116 template <
typename T,
typename BufferReaderType,
119 Tag
const &tag = Tag()) {
125 template <
typename T,
typename Tag = DefaultSerializationTag<T> >
128 Tag
const &tag = Tag()) {
136 typedef typename boost::call_traits<type>::param_type param_type;
137 typedef typename boost::call_traits<type>::value_type value_type;
138 typedef typename boost::call_traits<type>::reference reference_type;
159 typedef void is_specialized;
173 template <
typename T>
186 template <
typename BufferType>
188 typename Base::param_type val,
190 StructSerializeFunctor<BufferType> f(buf);
194 template <
typename BufferReaderType>
196 typename Base::reference_type val,
198 StructDeserializeFunctor<BufferReaderType> f(buf);
202 static size_t spaceRequired(
size_t existingBytes,
203 typename Base::param_type val,
205 StructSpaceRequirementFunctor f(existingBytes);
213 template <
typename BufferType>
214 class StructSerializeFunctor : boost::noncopyable {
217 StructSerializeFunctor(BufferType &buf) : m_buf(buf) {}
221 template <
typename U>
void operator()(U
const &val) {
227 template <
typename U,
typename Tag>
228 void operator()(U
const &val, Tag &t) {
238 template <
typename BufferReaderType>
239 class StructDeserializeFunctor : boost::noncopyable {
242 StructDeserializeFunctor(BufferReaderType &buf) : m_buf(buf) {}
246 template <
typename U>
void operator()(U &val) {
252 template <
typename U,
typename Tag>
253 void operator()(U &val, Tag &t) {
258 BufferReaderType &m_buf;
264 class StructSpaceRequirementFunctor : boost::noncopyable {
267 StructSpaceRequirementFunctor(
size_t existingBytes)
268 : m_initialBytes(existingBytes), m_bytes(existingBytes) {}
272 template <
typename U>
void operator()(U
const &val) {
278 template <
typename U,
typename Tag>
279 void operator()(U
const &val, Tag &t) {
284 size_t get()
const {
return m_bytes - m_initialBytes; }
287 size_t m_initialBytes;
295 template <
typename T,
size_t Alignment>
300 template <
typename BufferType,
typename Tag>
302 typename Base::param_type val, Tag
const &) {
303 buf.appendAligned(
hton(val), Alignment);
307 template <
typename BufferReaderType,
typename Tag>
309 typename Base::reference_type val,
311 buf.readAligned(val, Alignment);
318 template <
typename Tag>
320 typename Base::param_type,
329 template <
typename T>
332 typename
std::enable_if<std::is_arithmetic<T>::value &&
333 !std::is_same<bool, T>::value>
::type>
336 static_assert(std::alignment_of<T>::value ==
sizeof(
T),
337 "Arithmetic types are assumed to have an alignment " 338 "equal to their size");
353 template <
typename BufferType>
354 static void serialize(BufferType &buf, Base::param_type val,
359 template <
typename BufferReaderType>
361 Base::reference_type val,
368 template <
typename EnumType,
typename IntegerType>
374 template <
typename BufferType>
376 typename Base::param_type val,
381 template <
typename BufferReaderType>
383 typename Base::reference_type val,
387 val =
static_cast<EnumType
>(intVal);
399 typedef uint32_t length_type;
401 template <
typename BufferType>
402 static void serialize(BufferType &buf, Base::param_type val,
404 length_type len = val.length();
406 buf.append(val.c_str(), len);
410 template <
typename BufferReaderType>
412 Base::reference_type val,
416 auto iter = reader.readBytes(len);
417 val.assign(iter, iter + len);
424 Base::param_type val,
440 template <
typename BufferType>
441 static void serialize(BufferType &buf, Base::param_type val,
443 buf.append(val.c_str(), val.length());
446 template <
typename BufferReaderType>
448 Base::reference_type val,
450 auto len = reader.bytesRemaining();
451 auto iter = reader.readBytes(len);
452 val.assign(iter, iter + len);
469 template <
typename BufferType,
typename DataType>
470 static void serialize(BufferType &buf, DataType
const *val,
471 tag_type
const &tag) {
473 reinterpret_cast<typename BufferType::ElementType
const *
>(
475 buf.appendAligned(dataPtr, tag.length(), tag.alignment());
478 template <
typename BufferReaderType,
typename DataType>
479 static void deserialize(BufferReaderType &reader, DataType *val,
480 tag_type
const &tag) {
481 auto len = tag.length();
482 auto iter = reader.readBytesAligned(len, tag.alignment());
483 std::copy(iter, iter + len, val);
489 template <
typename DataType>
491 tag_type
const &tag) {
497 template <
typename ValueType>
501 using value_type = std::vector<ValueType>;
502 using param_type = value_type
const &;
503 using reference_type = value_type &;
507 template <
typename BufferType>
508 static void serialize(BufferType &buf, param_type val,
511 for (
auto &elt : val) {
516 template <
typename BufferReaderType>
517 static void deserialize(BufferReaderType &buf, reference_type val,
523 for (uint32_t i = 0; i < n; ++i) {
530 static size_t spaceRequired(
size_t existingBytes, param_type val,
532 size_t bytes = existingBytes;
534 bytes, static_cast<uint32_t>(val.size()));
535 for (
auto &elt : val) {
538 return bytes - existingBytes;
545 template <
typename F,
typename T>
static void apply(F &f,
T &val) {
554 template <
typename F,
typename T>
static void apply(F &f,
T &val) {
561 template <
typename Tag>
564 template <
typename F,
typename T>
static void apply(F &f,
T &val) {
573 #endif // INCLUDED_SerializationTraits_h_GUID_DF0CDFE0_097F_41B2_2DAE_7D4033D28D43 static void serialize(BufferType &buf, typename Base::param_type val, Tag const &)
Buffers an object of this type.
Definition: SerializationTraits.h:301
Base of serialization traits, containing useful typedefs.
Definition: SerializationTraits.h:134
uint8_t OSVR_CBool
A pre-C99-safe bool type.
Definition: BoolC.h:50
Definition: RunLoopManager.h:42
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
An alternate, simpler method of serializing things that are effectively structs is to explicitly spec...
Definition: SerializationTraits.h:153
#define OSVR_FALSE
Canonical "false" value for OSVR_CBool.
Definition: BoolC.h:54
A tag for use for raw data (bytestream), optionally aligned.
Definition: SerializationTags.h:56
const Scalar & value() const
Definition: SparseUtil.h:164
void deserialize(BufferReaderType &reader, MessageClass &msg)
Deserializes a message from a buffer, using a MessageClass
Definition: Serialization.h:169
Header providing a C-safe "bool" type, because we can't depend on Visual Studio providing proper C99 ...
A tag for use when the only field in a message is a string so the length prefix is unnecessary...
Definition: SerializationTags.h:75
A structure defining a 3D vector, often a position/translation.
Definition: Vec3C.h:48
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
void serialize(BufferType &buf, MessageClass &msg)
Serializes a message into a buffer, using a MessageClass
Definition: Serialization.h:152
std::integral_constant< bool, std::is_same< Tag, Tag >::value > MissingSerializationTraitsForTagOrType
Dummy template for better error messages - inspired by http://stackoverflow.com/a/17917624/265522.
Definition: SerializationTraits.h:55
static size_t spaceRequired(size_t, Base::param_type val, tag_type const &)
Returns the number of bytes required for this type (and alignment padding if applicable) to be append...
Definition: SerializationTraits.h:458
static void deserialize(BufferReaderType &reader, Base::reference_type val, tag_type const &)
Reads an object of this type from a buffer.
Definition: SerializationTraits.h:411
Definition: TypeSafeIdHash.h:44
#define OSVR_TRUE
Canonical "true" value for OSVR_CBool.
Definition: BoolC.h:52
static size_t spaceRequired(size_t existingBytes, typename Base::param_type, Tag const &)
Returns the number of bytes required for this type (and alignment padding if applicable) to be append...
Definition: SerializationTraits.h:319
size_t computeAlignmentPadding(size_t alignment, size_t currentSize)
Given an alignment in bytes, and a current size of a buffer, return the number of bytes of padding re...
Definition: AlignmentPadding.h:49
T ntoh(T const v)
Convert network byte order (big endian) to host byte order.
Definition: Endianness.h:177
typename F::template apply< Args... > apply
Apply an alias class.
Definition: Apply.h:44
Serialization traits for a given arithmetic type (that is, a number type that has a network byte orde...
Definition: SerializationTraits.h:296
Used to indicate the kind of integer that should back the serialization of the enum provided...
Definition: SerializationTags.h:71
static size_t spaceRequired(size_t existingBytes, DataType *, tag_type const &tag)
Returns the number of bytes required for this type (and alignment padding if applicable) to be append...
Definition: SerializationTraits.h:490
static void deserialize(BufferReaderType &buf, typename Base::reference_type val, Tag const &)
Reads an object of this type from a buffer.
Definition: SerializationTraits.h:308
void deserializeRaw(BufferReaderType &reader, T &v, Tag const &tag=Tag())
Deserialize a value from a buffer, with optional tag to specify non-default traits.
Definition: SerializationTraits.h:118
SimpleStructSerialization< T > SimpleSerialization
the SimpleStructSerialization specialization that will take each of our functors and apply them to ev...
Definition: SerializationTraits.h:184
T hton(T const v)
Convert host byte order to network byte order (big endian)
Definition: Endianness.h:182
A structure defining a 2D vector, which represents position.
Definition: Vec2C.h:48
The default "type tag" for specifying serialization behavior.
Definition: SerializationTags.h:48
static size_t spaceRequired(size_t existingBytes, Base::param_type val, tag_type const &)
Returns the number of bytes required for this type (and alignment padding if applicable) to be append...
Definition: SerializationTraits.h:423
size_t getBufferSpaceRequiredRaw(size_t existingBufferSize, T const &v, Tag const &tag=Tag())
Get the size a value from a buffer, with optional tag to specify non-default traits.
Definition: SerializationTraits.h:126
Mandatory base class for SimpleStructSerialization specializations: allows detection of specializatio...
Definition: SerializationTraits.h:158
Traits class indicating how to serialize a type with a given tag.
Definition: SerializationTraits.h:86
void serializeRaw(BufferType &buf, T const &v, Tag const &tag=Tag())
Serialize a value to a buffer, with optional tag to specify non-default traits.
Definition: SerializationTraits.h:109