Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
container.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_CONTAINER_H_
20 #define OMNI_SERIALIZATION_CONTAINER_H_
21 
22 #include <array>
23 #include <vector>
24 #include <deque>
25 #include <list>
26 #include <set>
27 #include <map>
28 #include "traits.h"
29 
30 namespace omni {
31  namespace serialization {
32  namespace traits {
33  /// Deserialize elements in array
34  template<typename T, size_t N>
35  struct Read<std::array<T, N>, false>{
36  template<typename STREAM, typename OBJ>
37  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
38  for (size_t i = 0; i < N; ++i) {
39  _obj[i] = deserializeReturn<T>(_stream);
40  }
41  }
42  };
43 
44  /// Deserialize elements in vector
45  template<typename T, typename ALLOCATOR>
46  struct Read<std::vector<T, ALLOCATOR>, false>{
47  template<typename STREAM, typename OBJ>
48  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
49  uint32_t _size = deserializeReturn<uint32_t>(_stream, 0);
50 
51  _obj.clear();
52 
53  for (uint32_t i = 0; i < _size; ++i) {
54  _obj.push_back(deserializeReturn<T>(_stream));
55  }
56  return _stream;
57  }
58  };
59 
60  /// Deserialize elements in deque
61  template<typename T, typename ALLOCATOR>
62  struct Read<std::deque<T, ALLOCATOR>, false>:
63  Read<std::vector<T, ALLOCATOR> >{};
64 
65  /// Handle classes that are derived from serialization::Interface
66  template<typename T, typename ALLOCATOR>
67  struct Read<std::list<T, ALLOCATOR>, false>:
68  Read<std::vector<T, ALLOCATOR> >{};
69 
70  /// Deserialize elements in set
71  template<typename T, typename SORT, typename ALLOCATOR>
72  struct Read<std::set<T, SORT, ALLOCATOR>, false>{
73  template<typename STREAM, typename OBJ>
74  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
75  uint32_t _size = deserializeReturn<uint32_t>(_stream, 0);
76 
77  _obj.clear();
78 
79  for (uint32_t i = 0; i < _size; ++i) {
80  _obj.insert(deserializeReturn<T>(_stream));
81  }
82  return _stream;
83  }
84  };
85 
86  /// Deserialize elements in map
87  template<typename KEY, typename T, typename COMPARE, typename ALLOCATOR>
88  struct Read<std::map<KEY, T, COMPARE, ALLOCATOR>, false>{
89  template<typename STREAM, typename OBJ>
90  STREAM& operator()(STREAM& _stream, OBJ& _obj) {
91  uint32_t _size = deserializeReturn<uint32_t>(_stream, 0);
92 
93  _obj.clear();
94 
95  for (uint32_t i = 0; i < _size; ++i) {
96  KEY && _key = deserializeReturn<KEY>(_stream);
97  T && _value = deserializeReturn<T>(_stream);
98  _obj[_key] = _value;
99  }
100  return _stream;
101  }
102  };
103 
104  /// Serialize elements in array
105  template<typename T, size_t N>
106  struct Write<std::array<T, N>, false>{
107  template<typename STREAM, typename OBJ>
108  STREAM& operator()(STREAM& _stream, OBJ const& _obj) {
109  for (size_t i = 0; i < N; ++i) {
110  serialize(_stream, _obj);
111  }
112  return _stream;
113  }
114  };
115 
116  /// Serialize elements in vector
117  template<typename T, typename ALLOCATOR>
118  struct Write<std::vector<T, ALLOCATOR>, false>{
119  template<typename STREAM, typename OBJ>
120  STREAM& operator()(STREAM& _stream, OBJ const& _obj) {
121  serialize(_stream, uint32_t(_obj.size()));
122 
123  for (auto& _element : _obj) {
124  serialize(_stream, _element);
125  }
126  return _stream;
127  }
128  };
129 
130  /// Serialize elements in deque
131  template<typename T, typename ALLOCATOR>
132  struct Write<std::deque<T, ALLOCATOR>, false>:
133  Write<std::vector<T, ALLOCATOR>, false>{};
134 
135  /// Serialize elements in list
136  template<typename T, typename ALLOCATOR>
137  struct Write<std::list<T, ALLOCATOR>, false>:
138  Write<std::vector<T, ALLOCATOR>, false>{};
139 
140  /// Serialize elements in set
141  template<typename T, typename SORT, typename ALLOCATOR>
142  struct Write<std::set<T, SORT, ALLOCATOR>, false>:
143  Write<std::vector<T, ALLOCATOR>, false>{};
144 
145  /// Serialize elements in map
146  template<typename KEY, typename T, typename COMPARE, typename ALLOCATOR>
147  struct Write<std::map<KEY, T, COMPARE, ALLOCATOR>, false>{
148  template<typename STREAM, typename OBJ>
149  STREAM& operator()(STREAM& _stream, OBJ const& _obj) {
150  serialize(_stream, uint32_t(_obj.size()));
151 
152  for (auto& _keyValue : _obj) {
153  serialize(_keyValue.first, _obj);
154  serialize(_keyValue.second, _obj);
155  }
156  return _stream;
157  }
158  };
159  }
160  }
161 }
162 
163 #endif /* OMNI_SERIALIZATION_VECTOR_H_ */
Definition: traits.h:82
STREAM & operator()(STREAM &_stream, OBJ const &_obj)
Definition: container.h:149
STREAM & operator()(STREAM &_stream, OBJ const &_obj)
Definition: container.h:120
STREAM & operator()(STREAM &_stream, OBJ const &_obj)
Definition: container.h:108
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: container.h:90
STREAM & serialize(STREAM &_stream, T const &_t)
Serialize object to stream.
Definition: traits.h:140
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: container.h:48
Traits for reading a value from stream.
Definition: traits.h:34
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: container.h:37
STREAM & operator()(STREAM &_stream, OBJ &_obj)
Definition: container.h:74