Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
traits.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2016 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  **/
19 #ifndef OMNI_SERIALIZATION_TRAITS_H_
20 #define OMNI_SERIALIZATION_TRAITS_H_
21 
22 #include <type_traits>
23 
24 #include <QString>
25 #include <QByteArray>
27 
28 namespace omni {
29  namespace serialization {
30  namespace traits {
31  /// Traits for reading a value from stream
32  template<typename T, bool BASE_OF_SERIALIZATION_INTERFACE =
33  std::is_base_of<serialization::Interface, T>::value>
34  struct Read {
35  template<typename STREAM, typename OBJ>
36  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
37  _stream >> _obj;
38  return _stream;
39  }
40  };
41 
42  /// Handle classes that are derived from serialization::Interface
43  template<typename T>
44  struct Read<T, true>{
45  template<typename STREAM, typename OBJ>
46  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
47  _obj.fromStream(_stream);
48  return _stream;
49  }
50  };
51 
52 
53  /// Template specialization for QByteArray
54  template<>
55  struct Read<QByteArray, false>{
56  template<typename STREAM, typename OBJ>
57  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
58  uint32_t _length = 0;
59 
60  _stream >> _length;
61  _obj.resize(_length);
62  _stream.readRawData(_obj.data(), _length);
63  return _stream;
64  }
65  };
66 
67  /// Template specialization for QString
68  template<>
69  struct Read<QString, false>{
70  template<typename STREAM, typename OBJ>
71  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
72  QByteArray _bytes;
73 
74  Read<QByteArray>()(_stream, _bytes);
75  _obj = QString::fromUtf8(_bytes);
76  return _stream;
77  }
78  };
79 
80  template<typename T, bool BASE_OF_SERIALIZATION_INTERFACE =
81  std::is_base_of<serialization::Interface, T>::value>
82  struct Write {
83  template<typename STREAM, typename OBJ>
84  STREAM& operator()(STREAM& _stream, OBJ const& _obj) {
85  _stream << _obj;
86  return _stream;
87  }
88  };
89 
90  template<typename T>
91  struct Write<T, true>{
92  template<typename STREAM, typename OBJ>
93  STREAM& operator()(STREAM& _stream, OBJ const& _obj) {
94  _obj.toStream(_stream);
95  return _stream;
96  }
97  };
98 
99  /// Template specialization for QByteArray
100  template<>
101  struct Write<QByteArray, false>{
102  template<typename STREAM, typename OBJ>
103  STREAM& operator()(STREAM& _stream, OBJ const& _obj) {
104  _stream << uint32_t(_obj.size());
105  _stream.writeRawData(_obj.data(), _obj.size());
106  return _stream;
107  }
108  };
109 
110  /// Template specialization for QString
111  template<>
112  struct Write<QString, false>{
113  template<typename STREAM, typename OBJ>
114  STREAM& operator()(STREAM& _stream, OBJ const& _obj) {
115  Write<QByteArray>()(_stream, _obj.toUtf8());
116  return _stream;
117  }
118  };
119  }
120 
121  /**@brief Deserialize object of type T from stream
122  @detail with optional additional arguments
123  **/
124  template<typename STREAM, typename T, typename ... ARGS>
125  STREAM& deserialize(STREAM& _stream, T& _t, ARGS&& ... _args) {
126  return traits::Read<T>(_args ...)(_stream, _t);
127  }
128 
129  /// Deserialize object of type T and return its value
130  template<typename T, typename STREAM>
131  T deserializeReturn(STREAM& _stream, T const _default = T()) {
132  T _t = _default;
133 
134  deserialize(_stream, _t);
135  return _t;
136  }
137 
138  /// Serialize object to stream
139  template<typename STREAM, typename T>
140  STREAM& serialize(STREAM& _stream, T const& _t) {
141  return traits::Write<T>()(_stream, _t);
142  }
143  }
144 
148 }
149 
150 #endif /* OMNI_SERIALIZATION_TRAITS_H_ */
Definition: traits.h:82
STREAM & operator()(STREAM &_stream, OBJ const &_obj)
Definition: traits.h:103
STREAM & serialize(STREAM &_stream, T const &_t)
Serialize object to stream.
Definition: traits.h:140
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: traits.h:46
Traits for reading a value from stream.
Definition: traits.h:34
T deserializeReturn(STREAM &_stream, T const _default=T())
Deserialize object of type T and return its value.
Definition: traits.h:131
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: traits.h:71
STREAM & deserialize(STREAM &_stream, T &_t, ARGS &&..._args)
Deserialize object of type T from stream with optional additional arguments.
Definition: traits.h:125
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: traits.h:36
STREAM & operator()(STREAM &_stream, OBJ const &_obj)
Definition: traits.h:84
STREAM & operator()(STREAM &_stream, OBJ const &_obj)
Definition: traits.h:114
STREAM & operator()(STREAM &_stream, OBJ const &_obj)
Definition: traits.h:93
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: traits.h:57