OSVR-Core
Serialization.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_Serialization_h_GUID_339B4851_CA90_41A6_E1CC_4F94A18614AC
26 #define INCLUDED_Serialization_h_GUID_339B4851_CA90_41A6_E1CC_4F94A18614AC
27 
28 // Internal Includes
31 
32 // Library/third-party includes
33 #include <boost/call_traits.hpp>
34 #include <boost/noncopyable.hpp>
35 
36 // Standard includes
37 #include <string>
38 
39 namespace osvr {
40 namespace common {
41 
42  namespace serialization {
43 
47  template <typename BufferType>
48  class SerializeFunctor : boost::noncopyable {
49  public:
51  SerializeFunctor(BufferType &buf) : m_buf(buf) {}
52 
60  template <typename T> void operator()(T const &v) {
61  apply<T, DefaultSerializationTag<T> >(v);
62  }
63 
72  template <typename Tag, typename T>
73  void operator()(T const &v, Tag const &tag = Tag()) {
74  apply<T, Tag>(v, tag);
75  }
76 
77  std::true_type isSerialize() const { return std::true_type(); }
78 
79  std::false_type isDeserialize() const { return std::false_type(); }
80 
81  private:
84  template <typename T, typename Tag>
85  void apply(typename boost::call_traits<T>::param_type v,
86  Tag const &tag = Tag()) {
87  serializeRaw(m_buf, v, tag);
88  }
89  BufferType &m_buf;
90  };
91 
95  template <typename BufferReaderType>
96  class DeserializeFunctor : boost::noncopyable {
97  public:
99  DeserializeFunctor(BufferReaderType &reader) : m_reader(reader) {}
100 
105  template <typename T> void operator()(T &v) {
106  apply<T, DefaultSerializationTag<T> >(v);
107  }
108 
114  template <typename Tag, typename T>
115  void operator()(T &v, Tag const &tag = Tag()) {
116  apply<T, Tag>(v, tag);
117  }
118 
122  template <typename Tag, typename T>
123  void operator()(T *v, Tag const &tag = Tag()) {
124  apply<T *, Tag>(v, tag);
125  }
126 
127  std::false_type isSerialize() const { return std::false_type(); }
128  std::true_type isDeserialize() const { return std::true_type(); }
129 
130  private:
133  template <typename T, typename Tag>
134  void apply(typename boost::call_traits<T>::reference v,
135  Tag const &tag = Tag()) {
136  deserializeRaw(m_reader, v, tag);
137  }
138  BufferReaderType &m_reader;
139  };
140 
141  } // namespace serialization
142 
151  template <typename BufferType, typename MessageClass>
152  void serialize(BufferType &buf, MessageClass &msg) {
153  static_assert(is_buffer<BufferType>::value,
154  "First argument must be a buffer object");
158  msg.processMessage(functor);
159  }
168  template <typename BufferReaderType, typename MessageClass>
169  void deserialize(BufferReaderType &reader, MessageClass &msg) {
171  "First argument must be a buffer reader object");
173  msg.processMessage(functor);
174  }
175 } // namespace common
176 } // namespace osvr
177 
178 #endif // INCLUDED_Serialization_h_GUID_339B4851_CA90_41A6_E1CC_4F94A18614AC
void operator()(T &v, Tag const &tag=Tag())
Main function call operator method, taking a "tag type" to specify non-default serialization-related ...
Definition: Serialization.h:115
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
void deserialize(BufferReaderType &reader, MessageClass &msg)
Deserializes a message from a buffer, using a MessageClass
Definition: Serialization.h:169
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
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
void operator()(T *v, Tag const &tag=Tag())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: Serialization.h:123
Functor class used by osvr::common::deserialize to deserialize a message (passed as the "process" arg...
Definition: Serialization.h:96
void operator()(T &v)
Main function call operator method.
Definition: Serialization.h:105
SerializeFunctor(BufferType &buf)
Constructor, taking the buffer.
Definition: Serialization.h:51
void operator()(T const &v, Tag const &tag=Tag())
Main function call operator method, taking a "tag type" to specify non-default serialization-related ...
Definition: Serialization.h:73
Type trait: is the given type a buffer?
Definition: BufferTraits.h:59
DeserializeFunctor(BufferReaderType &reader)
Constructor, taking the buffer reader.
Definition: Serialization.h:99
Functor class used by osvr::common::serialize to serialize a message (passed as the "process" argumen...
Definition: Serialization.h:48
void operator()(T const &v)
Main function call operator method.
Definition: Serialization.h:60
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
Type trait: is the given type a buffer reader?
Definition: BufferTraits.h:63