FotoSHOCK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
TypeSerializer.hpp
1 /*
2  * Copyright 2012 Lukas Jirkovsky
3  *
4  * This file is part of FotoSHOCKcore.
5  *
6  * FotoSHOCKcore is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, version 3 of the License.
9  *
10  * FotoSHOCKcore is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with FotoSHOCKcore. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TYPESERIALIZER_H
20 #define TYPESERIALIZER_H
21 
22 #include <cstdlib>
23 #include <cstdio>
24 
25 #include "Serialization_internal.hpp"
26 
27 namespace FotoSHOCKcore {
28 
29 template <typename T>
31  public:
32  static xmlNodePtr serialize(T value) {
34  }
35 
36  static const T deserialize(const xmlNodePtr value) {
37  return Serialization_internal::deserializeGeneral<T>(value);
38  }
39 };
40 
41 template <>
42 class TypeSerializer<const char*> {
43  public:
44  static xmlNodePtr serialize(const char* value) {
45  return xmlNewText(BAD_CAST value);
46  }
47 
49  static const char* deserialize(const xmlNodePtr value) {
50  std::size_t len = strlen(reinterpret_cast<const char*>(value->content));
51  char* tmp = new char[len + 1];
52  std::memcpy(tmp, value->content, len);
53  tmp[len] = '\0';
54  return tmp;
55  }
56 };
57 
58 template <>
59 class TypeSerializer<float> {
60  public:
61  static xmlNodePtr serialize(float value) {
62  char* tmp = new char[9];
63  uint32_t i = *reinterpret_cast<uint32_t*>(&value);
64  std::sprintf(tmp, "%X", htonl(i));
65  tmp[8] = '\0';
66 
67  xmlNodePtr node = xmlNewText(BAD_CAST tmp);
68  delete[] tmp;
69  return node;
70  }
71 
72  static const float deserialize(const xmlNodePtr value) {
73  uint32_t i = static_cast<uint32_t>(std::strtol(reinterpret_cast<const char*>(value->content), 0, 16));
74  i = ntohl(i);
75  float retVal = *reinterpret_cast<float*>(&i);
76  return retVal;
77  }
78 };
79 
80 template <>
81 class TypeSerializer<double> {
82  public:
83  static xmlNodePtr serialize(double value) {
84  char* tmp = new char[17];
85  uint64_t i = *reinterpret_cast<uint64_t*>(&value);
86  std::sprintf(tmp, "%lX", Serialization_internal::htonll(i));
87  tmp[16] = '\0';
88 
89  xmlNodePtr node = xmlNewText(BAD_CAST tmp);
90  delete[] tmp;
91  return node;
92  }
93 
94  static const double deserialize(const xmlNodePtr value) {
95  uint64_t i = std::strtol(reinterpret_cast<const char*>(value->content), 0, 16);
96  i = Serialization_internal::ntohll(i);
97  double retVal = *reinterpret_cast<double*>(&i);
98  return retVal;
99  }
100 };
101 
102 }
103 
104 #endif