1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
//Fundamentals.
/**
* \fn void operator(T& data, D...)
* \brief read a fundamental type from the archive.
* \param T& the data to read.
* \param D... used for SFINAE.
*/
template <typename T,
class... D,
class = typename std::enable_if<std::is_fundamental<T>::value>::type>
void operator() (T& data, D...) {
buffer>>data;
char space;
buffer.get(space);
}
/**
* \fn void operator(T& data, D...)
* \brief read a char from the archive. (we need to read unformatted input here to also read special chars like \n, spaces, etc...)
* \param T& the data to read.
* \param D... used for SFINAE.
*/
void operator() (char& data) {
buffer.get(data);
char space;
buffer.get(space);
}
/**
* \fn void operator(T& data, D...)
* \brief read an unsigned char from the archive. (we need to read unformatted input here to also read special chars like \n, spaces, etc...)
* \param T& the data to read.
* \param D... used for SFINAE.
*/
void operator() (unsigned char& data) {
buffer.get((char&) data);
char space;
buffer.get(space);
}
/**
* \fn void operator(T& data, D...)
* \brief read a pointer to a fundamental type from the archive.
* \param T& the data to read.
* \param D... used for SFINAE.
*/
template <typename T,
class... D,
class = typename std::enable_if<std::is_fundamental<T>::value>::type>
void operator() (T*& data, D...) {
unsigned long long int id;
buffer>>id;
char space;
buffer.get(space);
if (id != -1) {
std::map<unsigned long long int, unsigned long long int>::iterator it = adresses.find(id);
if (it != adresses.end()) {
data = reinterpret_cast<T*> (it->second);
} else {
data = new T();
(*this)(*data);
std::pair<unsigned long long int, unsigned long long int> newAddress (id, reinterpret_cast<unsigned long long int>(data));
adresses.insert(newAddress);
nbDeserialized++;
}
}
}
template <typename E,
class... D,
class = typename std::enable_if<!std::is_fundamental<E>::value>::type,
class = typename std::enable_if<std::is_enum<E>::value>::type>
void operator()(E& data, D...) {
int eVal;
buffer>>eVal;
data = static_cast<E>(eVal);
char space;
buffer.get(space);
}
template <typename E,
class... D,
class = typename std::enable_if<!std::is_fundamental<E>::value>::type,
class = typename std::enable_if<std::is_enum<E>::value>::type>
void operator() (E*& data, D...) {
unsigned long long int id;
buffer>>id;
char space;
buffer.get(space);
if (id != -1) {
std::map<unsigned long long int, unsigned long long int>::iterator it = adresses.find(id);
if (it != adresses.end()) {
data = reinterpret_cast<E*> (it->second);
} else {
data = new E();
(*this)(*data);
std::pair<unsigned long long int, unsigned long long int> newAddress (id, reinterpret_cast<unsigned long long int>(data));
adresses.insert(newAddress);
nbDeserialized++;
}
}
}
//std::string.
/**
* \fn void operator(T& data, D...)
* \brief read an std::string from the archive.
* \param T& the data to read.
* \param D... used for SFINAE.
*/
template <typename T,
class... D,
class = typename std::enable_if<!std::is_fundamental<T>::value>::type,
class = typename std::enable_if<std::is_same<T, std::string>::value>::type,
class = typename std::enable_if<!std::is_enum<T>::value>::type>
void operator() (T& data, D...) {
std::vector<char> vDatas;
(*this)(vDatas);
const char* datas = &vDatas[0];
std::size_t str_size = vDatas.size();
data = std::string(datas, str_size);
}
/**
* \fn void operator(T& data, D...)
* \brief read a pointer to an std::string from the archive.
* \param T* the data to read.
* \param D... used for SFINAE.
*/
template <typename T,
class... D,
class = typename std::enable_if<!std::is_fundamental<T>::value>::type,
class = typename std::enable_if<std::is_same<T, std::string>::value>::type,
class = typename std::enable_if<!std::is_enum<T>::value>::type>
void operator() (T*& data, D...) {
unsigned long long int id;
buffer>>id;
char space;
buffer.get(space);
if (id != -1) {
std::map<unsigned long long int, unsigned long long int>::iterator it = adresses.find(id);
if (it != adresses.end()) {
data = reinterpret_cast<T*> (it->second);
} else {
data = new T();
(*this)(*data);
std::pair<unsigned long long int, unsigned long long int> newAddress (id, reinterpret_cast<unsigned long long int>(data));
adresses.insert(newAddress);
nbDeserialized++;
}
}
}
//Static objects.
/**
* \fn void operator(O& data, D...)
* \brief read a static object from the archive.
* \param O& the data to read.
* \param D... used for SFINAE.
*/
template <class O,
class... D,
class = typename std::enable_if<!std::is_fundamental<O>::value>::type,
class = typename std::enable_if<!std::is_same<O, std::string>::value && !std::is_pointer<O>::value>::type,
class = typename std::enable_if<!has_typedef_key<O>::value>::type,
class = typename std::enable_if<!std::is_enum<O>::value>::type>
void operator() (O& object, D...) {
object.serialize(*this);
}
/**
* \fn void operator(O& data, D...)
* \brief read a pointer to a static object from the archive.
* \param O* the data to read.
* \param D... used for SFINAE.
*/
template <class O,
class... D,
class = typename std::enable_if<!std::is_fundamental<O>::value>::type,
class = typename std::enable_if<!std::is_same<O, std::string>::value>::type,
class = typename std::enable_if<!has_typedef_key<O>::value>::type,
class = typename std::enable_if<!std::is_enum<O>::value>::type>
void operator() (O*& object, D...) {
unsigned long long int id;
buffer>>id;
char space;
buffer.get(space);
if (id != -1) {
std::map<unsigned long long int, unsigned long long int>::iterator it = adresses.find(id);
if (it != adresses.end()) {
object = reinterpret_cast<O*>(it->second);
} else {
object = new O();
object->serialize(*this);
std::pair<unsigned long long int, unsigned long long int> newAddress (id, reinterpret_cast<unsigned long long int>(object));
adresses.insert(newAddress);
nbDeserialized++;
}
}
}
//Dynamic objects.
/**
* \fn void operator(O* data, D...)
* \brief read a dynamic object from the archive.
* \param O& the data to read.
* \param D... used for SFINAE.
*/
template <class O,
class... D,
class = typename std::enable_if<!std::is_fundamental<O>::value>::type,
class = typename std::enable_if<!std::is_same<O, std::string>::value>::type,
class = typename std::enable_if<has_typedef_key<O>::value>::type,
class = typename std::enable_if<!std::is_enum<O>::value>::type,
class = typename std::enable_if<!sizeof...(D)>::type>
void operator() (O& object, D...) {
if (typeid(decltype(object)) == typeid(object)) {
object.vtserialize(*this);
} else {
object.key.register_object(&object);
object.key.serialize_object("serialize", "ITextArchive", *this);
}
}
/**
* \fn void operator(O* data, D...)
* \brief read a pointer to a non abstract dynamic object from the archive.
* \param O* the data to read.
* \param D... used for SFINAE.
*/
template <class O,
class... D,
class = typename std::enable_if<!std::is_fundamental<O>::value>::type,
class = typename std::enable_if<!std::is_same<O, std::string>::value>::type,
class = typename std::enable_if<!std::is_enum<O>::value>::type,
class = typename std::enable_if<has_typedef_key<O>::value && !std::is_abstract<O>::value>::type,
class = typename std::enable_if<!sizeof...(D)>::type>
void operator() (O*& object, D...) {
unsigned long long int id;
std::string typeName;
buffer>>id;
char space;
buffer.get(space);
if (id != -1) {
getline(buffer, typeName);
std::map<unsigned long long int, unsigned long long int>::iterator it = adresses.find(id);
if (it != adresses.end()) {
object = reinterpret_cast<O*>(it->second);
} else {
if (typeName == "BaseType") {
object = new O();
object->vtserialize(*this);
std::pair<unsigned long long int, unsigned long long int> newAddress (id, reinterpret_cast<unsigned long long int>(object));
adresses.insert(newAddress);
} else {
object = O::allocate(typeName);
object->key.register_object(object);
object->key.serialize_object("serialize", "ITextArchive", *this);
std::pair<unsigned long long int, unsigned long long int> newAddress (id, reinterpret_cast<unsigned long long int>(object));
adresses.insert(newAddress);
}
nbDeserialized++;
}
}
}
/**
* \fn void operator(O* data, D...)
* \brief read a pointer to an abstract dynamic object from the archive.
* \param O* the data to read.
* \param D... used for SFINAE.
*/
template <class O,
class... D,
class = typename std::enable_if<!std::is_fundamental<O>::value>::type,
class = typename std::enable_if<!std::is_same<O, std::string>::value>::type,
class = typename std::enable_if<!std::is_enum<O>::value>::type,
class = typename std::enable_if<has_typedef_key<O>::value>::type,
class = typename std::enable_if<std::is_abstract<O>::value>::type,
class = typename std::enable_if<!sizeof...(D)>::type>
void operator() (O*& object, D...) {
std::string typeName;
unsigned long long int id;
buffer>>id;
if (id != -1) {
char space;
buffer.get(space);
getline(buffer, typeName);
std::map<unsigned long long int, unsigned long long int>::iterator it = adresses.find(id);
if (it != adresses.end()) {
object = reinterpret_cast<O*>(it->second);
} else {
object = O::allocate(typeName);
object->key.register_object(object);
object->key.serialize_object("serialize", "ITextArchive", *this);
std::pair<unsigned long long int, unsigned long long int> newAddress (id, reinterpret_cast<unsigned long long int>(object));
adresses.insert(newAddress);
nbDeserialized++;
}
}
}
/**
* \fn void operator(O* data, D...)
* \brief read a list of objects from the archive.
* \param O* the data to read.
* \param D... used for SFINAE.
*/
template <class O>
void operator() (std::vector<O>& objects) {
std::size_t size;
buffer>>size;
char space;
buffer.get(space);
for (unsigned int i = 0; i < size; i++) {
O object;
(*this)(object);
objects.push_back(object);
}
}
template <class T>
void operator() (std::unique_ptr<T>& ptr) {
(*this)(ptr.get());
}
template <class T>
void operator() (std::shared_ptr<T>& ptr) {
(*this)(ptr.get());
} |
Partager