Allolib  1.0
C++ Components For Interactive Multimedia
al_Serialize.hpp
1 #ifndef INCLUDE_AL_SERIALIZE_HPP
2 #define INCLUDE_AL_SERIALIZE_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  This is a C++ wrapper around the C serialization structs/functions.
40 
41  File author(s):
42  Lance Putnam, 2010, putnam.lance@gmail.com
43 */
44 
45 #include <string>
46 #include <vector>
47 #include "al_Serialize.h"
48 
49 namespace al {
50 
51 namespace ser {
52 
53 template <class T>
54 inline uint32_t encode(char* b, const T* v, uint32_t n) {
55  return 0;
56 }
57 
58 template <>
59 inline uint32_t encode(char* b, const float* v, uint32_t n) {
60  return serEncodeFloat32(b, v, n);
61 }
62 template <>
63 inline uint32_t encode(char* b, const double* v, uint32_t n) {
64  return serEncodeFloat64(b, v, n);
65 }
66 template <>
67 inline uint32_t encode(char* b, const char* v, uint32_t n) {
68  return serEncodeInt8(b, (const int8_t*)v, n);
69 }
70 template <>
71 inline uint32_t encode(char* b, const int8_t* v, uint32_t n) {
72  return serEncodeInt8(b, v, n);
73 }
74 template <>
75 inline uint32_t encode(char* b, const int16_t* v, uint32_t n) {
76  return serEncodeInt16(b, v, n);
77 }
78 template <>
79 inline uint32_t encode(char* b, const int32_t* v, uint32_t n) {
80  return serEncodeInt32(b, v, n);
81 }
82 template <>
83 inline uint32_t encode(char* b, const int64_t* v, uint32_t n) {
84  return serEncodeInt64(b, v, n);
85 }
86 template <>
87 inline uint32_t encode(char* b, const bool* v, uint32_t n) {
88  return serEncodeUInt8(b, (const uint8_t*)v, n);
89 }
90 template <>
91 inline uint32_t encode(char* b, const uint8_t* v, uint32_t n) {
92  return serEncodeUInt8(b, v, n);
93 }
94 template <>
95 inline uint32_t encode(char* b, const uint16_t* v, uint32_t n) {
96  return serEncodeUInt16(b, v, n);
97 }
98 template <>
99 inline uint32_t encode(char* b, const uint32_t* v, uint32_t n) {
100  return serEncodeUInt32(b, v, n);
101 }
102 template <>
103 inline uint32_t encode(char* b, const uint64_t* v, uint32_t n) {
104  return serEncodeUInt64(b, v, n);
105 }
106 
107 template <class T>
108 uint8_t getType();
109 template <>
110 inline uint8_t getType<float>() {
111  return 'f';
112 }
113 template <>
114 inline uint8_t getType<double>() {
115  return 'd';
116 }
117 template <>
118 inline uint8_t getType<bool>() {
119  return 't';
120 }
121 template <>
122 inline uint8_t getType<uint8_t>() {
123  return 't';
124 }
125 template <>
126 inline uint8_t getType<uint16_t>() {
127  return 'T';
128 }
129 template <>
130 inline uint8_t getType<uint32_t>() {
131  return 'u';
132 }
133 template <>
134 inline uint8_t getType<uint64_t>() {
135  return 'U';
136 }
137 template <>
138 inline uint8_t getType<int8_t>() {
139  return 'h';
140 }
141 template <>
142 inline uint8_t getType<int16_t>() {
143  return 'H';
144 }
145 template <>
146 inline uint8_t getType<int32_t>() {
147  return 'i';
148 }
149 template <>
150 inline uint8_t getType<int64_t>() {
151  return 'I';
152 }
153 
154 } // namespace ser
155 
160 struct Serializer {
161  template <class T>
162  Serializer& operator<<(T v);
163 
164  Serializer& operator<<(const char* v);
165  Serializer& operator<<(const std::string& v);
166 
167  template <class T>
168  Serializer& add(const T* v, uint32_t num);
169 
170  const std::vector<char>& buf() const;
171 
172  private:
173  std::vector<char> mBuf, mTemp;
174 
175  template <class T>
176  void checkSize(uint32_t n = 1);
177 };
178 
183 struct Deserializer {
184  Deserializer(const std::vector<char>& b);
185 
186  Deserializer(const char* b, uint32_t n);
187 
188  template <class T>
189  Deserializer& operator>>(T& v);
190  Deserializer& operator>>(char* v);
191  Deserializer& operator>>(std::string& v);
192 
193  const std::vector<char>& buf() const;
194 
195  private:
196  int mStart;
197  std::vector<char> mBuf;
198  char* bufDec();
199 };
200 
201 // =============================================================================
202 // Implementation
203 // =============================================================================
204 
205 template <class T>
206 Serializer& Serializer::operator<<(T v) {
207  return add(&v, 1);
208 }
209 
210 template <class T>
211 Serializer& Serializer::add(const T* v, uint32_t num) {
212  checkSize<T>(num);
213  uint32_t n = ser::encode(&mTemp[0], v, num);
214  mBuf.insert(mBuf.end(), mTemp.begin(), mTemp.begin() + n);
215  return *this;
216 }
217 
218 template <class T>
219 void Serializer::checkSize(uint32_t n) {
220  uint32_t need = n * sizeof(T) + serHeaderSize();
221  if (mTemp.size() < need) mTemp.resize(need);
222 }
223 
224 template <class T>
225 Deserializer& Deserializer::operator>>(T& v) {
226  uint32_t n = serDecode(bufDec(), &v);
227  mStart += n;
228  return *this;
229 }
230 
231 // template <class T>
232 // Deserializer& decode(const T * v, uint32_t num){
233 // uint32_t n = decode(bufDec(), v);
234 // return *this;
235 //}
236 
237 } // namespace al
238 
239 #endif
Definition: al_App.hpp:23
The Deserializer struct.
The Serializer struct.