2010-08-09 13:28:56 +00:00
|
|
|
#ifndef NALL_SERIALIZER_HPP
|
|
|
|
#define NALL_SERIALIZER_HPP
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
//serializer: a class designed to save and restore the state of classes.
|
|
|
|
//
|
|
|
|
//benefits:
|
|
|
|
//- data() will be portable in size (it is not necessary to specify type sizes.)
|
|
|
|
//- data() will be portable in endianness (always stored internally as little-endian.)
|
|
|
|
//- one serialize function can both save and restore class states.
|
|
|
|
//
|
|
|
|
//caveats:
|
|
|
|
//- only plain-old-data can be stored. complex classes must provide serialize(serializer&);
|
2013-07-29 09:42:45 +00:00
|
|
|
//- floating-point usage is not portable across different implementations
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
#include <nall/stdint.hpp>
|
|
|
|
#include <nall/utility.hpp>
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
struct serializer;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct has_serialize {
|
|
|
|
template<typename C> static char test(decltype(std::declval<C>().serialize(std::declval<serializer&>()))*);
|
|
|
|
template<typename C> static long test(...);
|
|
|
|
static const bool value = sizeof(test<T>(0)) == sizeof(char);
|
|
|
|
};
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
struct serializer {
|
|
|
|
enum mode_t { Load, Save, Size };
|
|
|
|
|
|
|
|
mode_t mode() const {
|
2013-07-29 09:42:45 +00:00
|
|
|
return _mode;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* data() const {
|
2013-07-29 09:42:45 +00:00
|
|
|
return _data;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned size() const {
|
2013-07-29 09:42:45 +00:00
|
|
|
return _size;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned capacity() const {
|
2013-07-29 09:42:45 +00:00
|
|
|
return _capacity;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
template<typename T> serializer& floatingpoint(T& value) {
|
2013-05-02 11:25:45 +00:00
|
|
|
enum { size = sizeof(T) };
|
|
|
|
//this is rather dangerous, and not cross-platform safe;
|
|
|
|
//but there is no standardized way to export FP-values
|
|
|
|
uint8_t* p = (uint8_t*)&value;
|
2013-07-29 09:42:45 +00:00
|
|
|
if(_mode == Save) {
|
|
|
|
for(unsigned n = 0; n < size; n++) _data[_size++] = p[n];
|
|
|
|
} else if(_mode == Load) {
|
|
|
|
for(unsigned n = 0; n < size; n++) p[n] = _data[_size++];
|
2013-05-02 11:25:45 +00:00
|
|
|
} else {
|
2013-07-29 09:42:45 +00:00
|
|
|
_size += size;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
2013-07-29 09:42:45 +00:00
|
|
|
return *this;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
template<typename T> serializer& integer(T& value) {
|
2013-05-02 11:25:45 +00:00
|
|
|
enum { size = std::is_same<bool, T>::value ? 1 : sizeof(T) };
|
2013-07-29 09:42:45 +00:00
|
|
|
if(_mode == Save) {
|
|
|
|
for(unsigned n = 0; n < size; n++) _data[_size++] = (uintmax_t)value >> (n << 3);
|
|
|
|
} else if(_mode == Load) {
|
2013-05-02 11:25:45 +00:00
|
|
|
value = 0;
|
2013-07-29 09:42:45 +00:00
|
|
|
for(unsigned n = 0; n < size; n++) value |= (uintmax_t)_data[_size++] << (n << 3);
|
|
|
|
} else if(_mode == Size) {
|
|
|
|
_size += size;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
2013-07-29 09:42:45 +00:00
|
|
|
return *this;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
template<typename T, int N> serializer& array(T (&array)[N]) {
|
|
|
|
for(unsigned n = 0; n < N; n++) operator()(array[n]);
|
|
|
|
return *this;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
template<typename T> serializer& array(T array, unsigned size) {
|
|
|
|
for(unsigned n = 0; n < size; n++) operator()(array[n]);
|
|
|
|
return *this;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
template<typename T> serializer& operator()(T& value, typename std::enable_if<has_serialize<T>::value>::type* = 0) { value.serialize(*this); return *this; }
|
|
|
|
template<typename T> serializer& operator()(T& value, typename std::enable_if<std::is_integral<T>::value>::type* = 0) { return integer(value); }
|
|
|
|
template<typename T> serializer& operator()(T& value, typename std::enable_if<std::is_floating_point<T>::value>::type* = 0) { return floatingpoint(value); }
|
|
|
|
template<typename T> serializer& operator()(T& value, typename std::enable_if<std::is_array<T>::value>::type* = 0) { return array(value); }
|
|
|
|
template<typename T> serializer& operator()(T& value, unsigned size, typename std::enable_if<std::is_pointer<T>::value>::type* = 0) { return array(value, size); }
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
serializer& operator=(const serializer& s) {
|
2013-07-29 09:42:45 +00:00
|
|
|
if(_data) delete[] _data;
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
_mode = s._mode;
|
|
|
|
_data = new uint8_t[s._capacity];
|
|
|
|
_size = s._size;
|
|
|
|
_capacity = s._capacity;
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
memcpy(_data, s._data, s._capacity);
|
2013-05-02 11:25:45 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer& operator=(serializer&& s) {
|
2013-07-29 09:42:45 +00:00
|
|
|
if(_data) delete[] _data;
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
_mode = s._mode;
|
|
|
|
_data = s._data;
|
|
|
|
_size = s._size;
|
|
|
|
_capacity = s._capacity;
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
s._data = nullptr;
|
2013-05-02 11:25:45 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer() = default;
|
2013-07-29 09:42:45 +00:00
|
|
|
serializer(const serializer& s) { operator=(s); }
|
|
|
|
serializer(serializer&& s) { operator=(std::move(s)); }
|
2013-05-02 11:25:45 +00:00
|
|
|
|
|
|
|
serializer(unsigned capacity) {
|
2013-07-29 09:42:45 +00:00
|
|
|
_mode = Save;
|
|
|
|
_data = new uint8_t[capacity]();
|
|
|
|
_size = 0;
|
|
|
|
_capacity = capacity;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serializer(const uint8_t* data, unsigned capacity) {
|
2013-07-29 09:42:45 +00:00
|
|
|
_mode = Load;
|
|
|
|
_data = new uint8_t[capacity];
|
|
|
|
_size = 0;
|
|
|
|
_capacity = capacity;
|
|
|
|
memcpy(_data, data, capacity);
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~serializer() {
|
2013-07-29 09:42:45 +00:00
|
|
|
if(_data) delete[] _data;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-07-29 09:42:45 +00:00
|
|
|
mode_t _mode = Size;
|
|
|
|
uint8_t* _data = nullptr;
|
|
|
|
unsigned _size = 0;
|
|
|
|
unsigned _capacity = 0;
|
2013-05-02 11:25:45 +00:00
|
|
|
};
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|