renaming source files

This commit is contained in:
fraillt
2017-02-23 15:22:31 +02:00
parent bf712bc9e5
commit 6ab4378bc2
23 changed files with 62 additions and 43 deletions

46
include/bitsery/bitsery.h Normal file
View File

@@ -0,0 +1,46 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_BITSERY_H
#define BITSERY_BITSERY_H
#define BITSERY_MAJOR_VERSION 1
#define BITSERY_MINOR_VERSION 1
#define BITSERY_PATCH_VERSION 1
#define BITSERY_QUOTE_MACRO(name) #name
#define BITSERY_BUILD_VERSION_STR(major,minor, patch) \
BITSERY_QUOTE_MACRO(major) "." \
BITSERY_QUOTE_MACRO(minor) "." \
BITSERY_QUOTE_MACRO(patch)
#define BITSERY_VERSION \
BITSERY_BUILD_VERSION_STR(BITSERY_MAJOR_VERSION, BITSERY_MINOR_VERSION, BITSERY_PATCH_VERSION)
#include "buffer_writer.h"
#include "buffer_reader.h"
#include "serializer.h"
#include "deserializer.h"
#endif //BITSERY_BITSERY_H

View File

@@ -0,0 +1,164 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_BUFFER_READER_H
#define BITSERY_BUFFER_READER_H
#include "common.h"
#include <cassert>
#include <algorithm>
namespace bitsery {
struct BufferReader {
using value_type = uint8_t;
BufferReader(const std::vector<uint8_t> &buf)
: _pos{buf.data()},
_end{buf.data() + buf.size()} {
}
BufferReader(const uint8_t* data, size_t size)
: _pos{data},
_end{data + size}
{
}
template <size_t N>
BufferReader(const uint8_t (&data)[N])
: _pos{data},
_end{data + N}
{
}
template<size_t SIZE, typename T>
bool readBytes(T &v) {
static_assert(std::is_integral<T>(), "");
static_assert(sizeof(T) == SIZE, "");
using UT = typename std::make_unsigned<T>::type;
return !m_scratch
? directRead(&v, 1)
: readBits(reinterpret_cast<UT &>(v), BITS_SIZE<T>);
}
template<size_t SIZE, typename T>
bool readBuffer(T *buf, size_t count) {
static_assert(std::is_integral<T>(), "");
static_assert(sizeof(T) == SIZE, "");
if (!m_scratchBits) {
return directRead(buf, count);
} else {
using UT = typename std::make_unsigned<T>::type;
//todo improve implementation
const auto end = buf + count;
for (auto it = buf; it != end; ++it) {
if (!readBits(reinterpret_cast<UT &>(*it), BITS_SIZE<T>))
return false;
}
}
return true;
}
template<typename T>
bool readBits(T &v, size_t bitsCount) {
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
assert(bitsCount <= BITS_SIZE<T>);
const auto bytesRequired = bitsCount > m_scratchBits
? ((bitsCount - 1 - m_scratchBits) >> 3) + 1u
: 0u;
if (static_cast<size_t>(std::distance(_pos, _end)) < bytesRequired)
return false;
readBitsInternal(v, bitsCount);
return true;
}
bool align() {
if (m_scratchBits) {
SCRATCH_TYPE tmp{};
readBitsInternal(tmp, BITS_SIZE<value_type> - m_scratchBits);
return tmp == 0;
}
return true;
}
bool isCompleted() const {
return _pos == _end;
}
private:
const value_type* _pos;
const value_type* _end;
template<typename T>
bool directRead(T *v, size_t count) {
static_assert(!std::is_const<T>::value, "");
const auto bytesCount = sizeof(T) * count;
if (static_cast<size_t>(std::distance(_pos, _end)) < bytesCount)
return false;
std::copy_n(_pos, bytesCount, reinterpret_cast<value_type *>(v));
std::advance(_pos, bytesCount);
return true;
}
template<typename T>
void readBitsInternal(T &v, size_t size) {
auto bitsLeft = size;
T res{};
while (bitsLeft > 0) {
auto bits = std::min(bitsLeft, BITS_SIZE<value_type>);
if (m_scratchBits < bits) {
value_type tmp;
std::copy_n(_pos, 1, reinterpret_cast<value_type *>(&tmp));
std::advance(_pos, 1);
m_scratch |= static_cast<SCRATCH_TYPE>(tmp) << m_scratchBits;
m_scratchBits += BITS_SIZE<value_type>;
}
auto shiftedRes =
static_cast<T>(m_scratch & ((static_cast<SCRATCH_TYPE>(1) << bits) - 1)) << (size - bitsLeft);
res |= shiftedRes;
m_scratch >>= bits;
m_scratchBits -= bits;
bitsLeft -= bits;
}
v = res;
}
using SCRATCH_TYPE = typename BIGGER_TYPE<value_type>::type;
SCRATCH_TYPE m_scratch{};
size_t m_scratchBits{}; ///< Number of bits currently in the scratch buffer. If the user wants to read more bits than this, we have to go fetch another dword from memory.
};
}
#endif //BITSERY_BUFFER_READER_H

View File

@@ -0,0 +1,183 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_BUFFER_WRITER_H
#define BITSERY_BUFFER_WRITER_H
#include "common.h"
#include <cassert>
#include <algorithm>
#include <iterator>
namespace bitsery {
struct MeasureSize {
template<size_t SIZE, typename T>
void writeBytes(const T &) {
static_assert(std::is_integral<T>(), "");
static_assert(sizeof(T) == SIZE, "");
_bitsCount += BITS_SIZE<T>;
}
template<typename T>
void writeBits(const T &, size_t bitsCount) {
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
assert(bitsCount <= BITS_SIZE<T>);
_bitsCount += bitsCount;
}
template<size_t SIZE, typename T>
void writeBuffer(const T *, size_t count) {
static_assert(std::is_integral<T>(), "");
static_assert(sizeof(T) == SIZE, "");
_bitsCount += BITS_SIZE<T> * count;
}
//get size in bytes
size_t getSize() const {
return _bitsCount / 8;
}
private:
size_t _bitsCount{};
};
struct BufferWriter {
using value_type = uint8_t;
BufferWriter(std::vector<uint8_t> &buffer) : _buf{buffer}, _outIt{std::back_inserter(buffer)} {
static_assert(std::is_unsigned<value_type>::value, "");
}
template<size_t SIZE, typename T>
void writeBytes(const T &v) {
static_assert(std::is_integral<T>(), "");
static_assert(sizeof(T) == SIZE, "");
if (!m_scratchBits) {
directWrite(&v, 1);
} else {
using UT = typename std::make_unsigned<T>::type;
writeBits(reinterpret_cast<const UT &>(v), BITS_SIZE<T>);
}
}
template<size_t SIZE, typename T>
void writeBuffer(const T *buf, size_t count) {
static_assert(std::is_integral<T>(), "");
static_assert(sizeof(T) == SIZE, "");
if (!m_scratchBits) {
directWrite(buf, count);
} else {
using UT = typename std::make_unsigned<T>::type;
//todo improve implementation
const auto end = buf + count;
for (auto it = buf; it != end; ++it)
writeBits(reinterpret_cast<const UT &>(*it), BITS_SIZE<T>);
}
}
template<typename T>
void writeBits(const T &v, size_t bitsCount) {
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
assert(bitsCount <= BITS_SIZE<T>);
assert(v <= ((1ULL << bitsCount) - 1));
writeBitsInternal(v, bitsCount);
}
void align() {
if (m_scratchBits)
writeBitsInternal(value_type{}, BITS_SIZE<value_type> - m_scratchBits);
}
void flush() {
if (m_scratchBits) {
auto tmp = static_cast<value_type>( m_scratch & bufTypeMask );
directWrite(&tmp, 1);
m_scratch >>= m_scratchBits;
m_scratchBits -= m_scratchBits;
}
}
private:
template<typename T>
void directWrite(const T *v, size_t count) {
const auto bytesSize = sizeof(T) * count;
const auto pos = _buf.size();
_buf.resize(pos + bytesSize);
std::copy_n(reinterpret_cast<const value_type *>(v), bytesSize, _buf.data() + pos);
}
template<typename T>
void writeBitsInternal(const T &v, size_t size) {
auto value = v;
auto bitsLeft = size;
while (bitsLeft > 0) {
auto bits = std::min(bitsLeft, BITS_SIZE<value_type>);
m_scratch |= static_cast<SCRATCH_TYPE>( value ) << m_scratchBits;
m_scratchBits += bits;
if (m_scratchBits >= BITS_SIZE<value_type>) {
auto tmp = static_cast<value_type>(m_scratch & bufTypeMask);
directWrite(&tmp, 1);
m_scratch >>= BITS_SIZE<value_type>;
m_scratchBits -= BITS_SIZE<value_type>;
value >>= BITS_SIZE<value_type>;
}
bitsLeft -= bits;
}
}
void writeBitsInternal(const value_type &v, size_t size) {
if (size > 0) {
m_scratch |= static_cast<SCRATCH_TYPE>( v ) << m_scratchBits;
m_scratchBits += size;
if (m_scratchBits >= BITS_SIZE<value_type>) {
auto tmp = static_cast<value_type>(m_scratch & bufTypeMask);
directWrite(&tmp, 1);
m_scratch >>= BITS_SIZE<value_type>;
m_scratchBits -= BITS_SIZE<value_type>;
}
}
}
const value_type bufTypeMask = 0xFF;
using SCRATCH_TYPE = typename BIGGER_TYPE<value_type>::type;
std::vector<value_type> &_buf;
std::back_insert_iterator<std::vector<value_type>> _outIt;
SCRATCH_TYPE m_scratch{};
size_t m_scratchBits{};
//size_t _bufSize{};
};
}
#endif //BITSERY_BUFFER_WRITER_H

238
include/bitsery/common.h Normal file
View File

@@ -0,0 +1,238 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_COMMON_H
#define BITSERY_COMMON_H
#include <cstdint>
namespace bitsery {
template<typename T>
constexpr size_t BITS_SIZE = sizeof(T) << 3;
template<typename T>
struct BIGGER_TYPE {
};
template<>
struct BIGGER_TYPE<uint8_t> {
typedef uint16_t type;
};
template<>
struct BIGGER_TYPE<uint16_t> {
typedef uint32_t type;
};
template<>
struct BIGGER_TYPE<uint32_t> {
typedef uint64_t type;
};
template<>
struct BIGGER_TYPE<int8_t> {
typedef int16_t type;
};
template<>
struct BIGGER_TYPE<int16_t> {
typedef int32_t type;
};
template<>
struct BIGGER_TYPE<int32_t> {
typedef int64_t type;
};
template<>
struct BIGGER_TYPE<char> {
typedef int16_t type;
};
template<typename T>
constexpr size_t ARITHMETIC_OR_ENUM_SIZE = std::is_arithmetic<T>::value || std::is_enum<T>::value ? sizeof(T) : 0;
template<typename T, typename Enable = void>
struct SAME_SIZE_UNSIGNED_TYPE {
typedef std::make_unsigned_t<T> type;
};
template<typename T>
struct SAME_SIZE_UNSIGNED_TYPE<T, typename std::enable_if<std::is_enum<T>::value>::type> {
typedef std::make_unsigned_t<std::underlying_type_t<T>> type;
};
template<typename T>
struct SAME_SIZE_UNSIGNED_TYPE<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
typedef std::conditional_t<std::is_same<T, float>::value, uint32_t, uint64_t> type;
};
template<typename T>
using SAME_SIZE_UNSIGNED = typename SAME_SIZE_UNSIGNED_TYPE<T>::type;
template<size_t SIZE>
struct ProcessAnyType {
template<typename S, typename T>
static void serialize(S &s, T &&v) {
s.template value<SIZE>(std::forward<T>(v));
}
};
template<>
struct ProcessAnyType<0> {
template<typename S, typename T>
static void serialize(S &s, T &&v) {
s.object(std::forward<T>(v));
}
};
#define SERIALIZE(ObjectType) \
template <typename S, typename T, typename std::enable_if<std::is_same<T, ObjectType>::value || std::is_same<T, const ObjectType>::value>::type* = nullptr> \
S& serialize(S& s, T& o)
/*
* range functions
*/
template<typename T>
constexpr size_t calcRequiredBits(T min, T max) {
size_t res{};
for (auto diff = max - min; diff > 0; diff >>= 1)
++res;
return res;
}
template<typename T, typename Enable = void>
struct RangeSpec {
constexpr RangeSpec(T minValue, T maxValue)
: min{minValue},
max{maxValue},
bitsRequired{calcRequiredBits(min, max)} {
}
const T min;
const T max;
const size_t bitsRequired;
};
template<typename T>
struct RangeSpec<T, typename std::enable_if<std::is_enum<T>::value>::type> {
constexpr RangeSpec(T minValue, T maxValue) :
min{minValue},
max{maxValue},
bitsRequired{calcRequiredBits(
static_cast<std::underlying_type_t<T>>(min),
static_cast<std::underlying_type_t<T>>(max))} {
}
const T min;
const T max;
const size_t bitsRequired;
};
//this class is used to make default RangeSpec float specialization always prefer constructor with precision
struct BitsConstraint {
explicit constexpr BitsConstraint(size_t bits) : value{bits} {}
const size_t value;
};
template<typename T>
struct RangeSpec<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
constexpr RangeSpec(T minValue, T maxValue, BitsConstraint bits) :
min{minValue},
max{maxValue},
bitsRequired{bits.value} {
}
constexpr RangeSpec(T minValue, T maxValue, T precision) :
min{minValue},
max{maxValue},
bitsRequired{calcRequiredBits<SAME_SIZE_UNSIGNED<T>>({}, ((max - min) / precision))} {
}
const T min;
const T max;
const size_t bitsRequired;
};
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
bool isRangeValid(const T &v, const RangeSpec<T> &r) {
return !(r.min > v || v > r.max);
}
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type * = nullptr>
bool isRangeValid(const T &v, const RangeSpec<T> &r) {
using VT = std::underlying_type_t<T>;
return !(static_cast<VT>(r.min) > static_cast<VT>(v)
|| static_cast<VT>(v) > static_cast<VT>(r.max));
}
/*
* delta functions
*/
class ObjectMemoryPosition {
public:
template<typename T>
ObjectMemoryPosition(const T &oldObj, const T &newObj)
:ObjectMemoryPosition{reinterpret_cast<const char *>(&oldObj), reinterpret_cast<const char *>(&newObj),
sizeof(T)} {
}
template<typename T>
bool isFieldsEquals(const T &newObjField) {
return *getOldObjectField(newObjField) == newObjField;
}
template<typename T>
const T *getOldObjectField(const T &field) {
auto offset = reinterpret_cast<const char *>(&field) - newObj;
return reinterpret_cast<const T *>(oldObj + offset);
}
private:
ObjectMemoryPosition(const char *objOld, const char *objNew, size_t)
: oldObj{objOld},
newObj{objNew} {
}
const char *oldObj;
const char *newObj;
};
}
#endif //BITSERY_COMMON_H

View File

@@ -0,0 +1,220 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_DELTADESERIALIZER_H
#define BITSERY_DELTADESERIALIZER_H
#include <array>
#include <stack>
#include <algorithm>
#include "deserializer.h"
namespace bitsery {
template<typename Reader, typename TObj>
class DeltaDeserializer {
public:
DeltaDeserializer(Reader &r, const TObj &oldObj, const TObj &newObj)
: _deserializer{r},
_reader{r},
_oldObj{oldObj},
_newObj{newObj},
_objMemPos(std::deque<ObjectMemoryPosition>(1, ObjectMemoryPosition{oldObj, newObj})),
_isNewElement{false} {
};
template<size_t SIZE = 0, typename T, typename std::enable_if<std::is_integral<T>::value>::type * = nullptr>
DeltaDeserializer &value(T &v) {
if (getChangedState(v)) {
constexpr size_t ValueSize = SIZE == 0 ? sizeof(T) : SIZE;
_reader.template readBytes<ValueSize>(v);
}
return *this;
}
template<typename T>
DeltaDeserializer &object(T &&obj) {
if (getChangedState(obj))
return serialize(*this, std::forward<T>(obj));
return *this;
}
template<size_t VSIZE = 1, typename T>
DeltaDeserializer &text(std::basic_string<T> &str, size_t maxSize) {
if (getChangedState(str)) {
_deserializer.template text<VSIZE>(str, maxSize);
}
return *this;
}
template<size_t VSIZE = 1, typename T, size_t N>
DeltaDeserializer &text(T (&str)[N]) {
if (getChangedState(str)) {
_deserializer.template text<VSIZE>(str);
}
return *this;
}
template<typename T, size_t N, typename Fnc>
DeltaDeserializer &array(std::array<T, N> &arr, Fnc &&fnc) {
if (getChangedState(arr)) {
if (!_isNewElement) {
const auto old = *_objMemPos.top().getOldObjectField(arr);
processContainer(std::begin(old), std::end(old), std::begin(arr), std::end(arr), fnc);
} else {
for (auto &v:arr)
fnc(v);
}
}
return *this;
}
template<typename T, size_t N, typename Fnc>
DeltaDeserializer &array(T (&arr)[N], Fnc &&fnc) {
if (getChangedState(arr)) {
if (!_isNewElement) {
const auto old = *_objMemPos.top().getOldObjectField(arr);
T *tmp = arr;
processContainer(old, old + N, tmp, tmp + N, fnc);
} else {
T *tmp = arr;
for (auto i = 0u; i < N; ++i, ++tmp)
fnc(*tmp);
}
}
return *this;
}
template<typename T, typename Fnc>
DeltaDeserializer &container(T &obj, Fnc &&fnc, size_t maxSize) {
if (getChangedState(obj)) {
size_t newSize{};
_reader.readBits(newSize, 32);
if (!_isNewElement) {
auto old = *_objMemPos.top().getOldObjectField(obj);
if (old.size() != newSize)
obj.resize(newSize);
processContainer(std::begin(old), std::end(old), std::begin(obj), std::end(obj),
std::forward<Fnc>(fnc));
} else {
obj.resize(newSize);
for (auto &v:obj)
fnc(v);
}
}
return *this;
}
private:
Deserializer <Reader> _deserializer;
Reader &_reader;
const TObj &_oldObj;
const TObj &_newObj;
std::stack<ObjectMemoryPosition> _objMemPos;
bool _isNewElement;
template<typename T>
bool getChangedState(T &obj) {
if (!_isNewElement) {
if (!readChangedState()) {
obj = *_objMemPos.top().getOldObjectField(obj);
return false;
}
}
return true;
}
template<typename T, size_t N>
bool getChangedState(T (&arr)[N]) {
if (!_isNewElement) {
if (!readChangedState()) {
auto old = *_objMemPos.top().getOldObjectField(arr);
auto end = arr + N;
auto pOld = old;
for (auto p = arr; p != end; ++p, ++pOld)
*p = *pOld;
return false;
}
}
return true;
}
template<typename TConstIt, typename TIt, typename Fnc>
bool processContainer(TConstIt oldBegin, TConstIt oldEnd, TIt begin, TIt end, Fnc &&fnc) {
auto offset = readIndexOffset();
auto p = begin;
auto pOld = oldBegin;
for (; p != end && pOld != oldEnd; ++p, ++pOld) {
if (offset) {
*p = *pOld;
--offset;
} else {
_objMemPos.emplace(ObjectMemoryPosition{*pOld, *p});
fnc(*p);
_objMemPos.pop();
offset = readIndexOffset();
}
}
if (offset != 0 && pOld != oldEnd)
return false;
_isNewElement = true;
for (; p != end; ++p, --offset)
fnc(*p);
_isNewElement = false;
return offset == 0;
}
bool readChangedState() {
unsigned char res{};
_reader.readBits(res, 1);
return res;
}
size_t readIndexOffset() {
//special case, if items are updated sequentialy
unsigned char tmp{};
_reader.readBits(tmp, 1);
if (tmp) {
return 0u;
} else {
size_t res{};
_reader.readBits(tmp, 1);
if (tmp > 0)
_reader.readBits(res, 4);
else
_reader.readBits(res, 32);
return res;
}
}
};
}
#endif //BITSERY_DELTADESERIALIZER_H

View File

@@ -0,0 +1,220 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_DELTASERIALIZER_H
#define BITSERY_DELTASERIALIZER_H
#include <array>
#include <stack>
#include <algorithm>
#include "serializer.h"
namespace bitsery {
template<typename Writter, typename TObj>
class DeltaSerializer {
public:
DeltaSerializer(Writter &w, const TObj &oldObj, const TObj &newObj)
: _serializer{w},
_writter{w},
_oldObj{oldObj},
_newObj{newObj},
_objMemPos(std::deque<ObjectMemoryPosition>(1, ObjectMemoryPosition{oldObj, newObj})),
_isNewElement{false} {
};
template<size_t SIZE = 0, typename T, typename std::enable_if<std::is_integral<T>::value>::type * = nullptr>
DeltaSerializer &value(const T &v) {
if (setChangedState(v)) {
constexpr size_t ValueSize = SIZE == 0 ? sizeof(T) : SIZE;
_writter.template writeBytes<ValueSize>(v);
}
return *this;
}
template<typename T>
DeltaSerializer &object(T &&obj) {
if (setChangedState(obj)) {
serialize(*this, std::forward<T>(obj));
}
return *this;
}
template<size_t VSIZE = 1, typename T>
DeltaSerializer &text(const std::basic_string<T> &str, size_t maxSize) {
if (setChangedState(str)) {
_serializer.template text<VSIZE>(str, maxSize);
}
return *this;
}
template<size_t VSIZE = 1, typename T, size_t N>
DeltaSerializer &text(const T (&str)[N]) {
if (setChangedState(str)) {
_serializer.template text<VSIZE>(str);
}
return *this;
}
template<typename T, size_t N, typename Fnc>
DeltaSerializer &array(const std::array<T, N> &arr, Fnc &&fnc) {
if (setChangedState(arr)) {
if (!_isNewElement) {
const auto &old = *_objMemPos.top().getOldObjectField(arr);
processContainer(std::begin(old), std::end(old), std::begin(arr), std::end(arr), fnc);
} else {
for (auto &v:arr)
fnc(v);
}
}
return *this;
}
template<typename T, size_t N, typename Fnc>
DeltaSerializer &array(const T (&arr)[N], Fnc &&fnc) {
if (setChangedState(arr)) {
if (!_isNewElement) {
auto old = *_objMemPos.top().getOldObjectField(arr);
const T *tmp = arr;
processContainer(old, old + N, tmp, tmp + N, fnc);
} else {
const T *tmp = arr;
for (auto i = 0u; i < N; ++i, ++tmp)
fnc(*tmp);
}
}
return *this;
}
template<typename T, typename Fnc>
DeltaSerializer &container(T &&obj, Fnc &&fnc, size_t maxSize) {
if (setChangedState(obj)) {
_writter.writeBits(obj.size(), 32);
if (!_isNewElement) {
auto old = *_objMemPos.top().getOldObjectField(obj);
processContainer(std::begin(old), std::end(old), std::begin(obj), std::end(obj),
std::forward<Fnc>(fnc));
} else {
for (auto &v:obj)
fnc(v);
}
}
return *this;
}
private:
Serializer <Writter> _serializer;
Writter &_writter;
const TObj &_oldObj;
const TObj &_newObj;
std::stack<ObjectMemoryPosition> _objMemPos;
bool _isNewElement;
template<typename T>
bool setChangedState(const T &obj) {
if (!_isNewElement) {
auto res = !_objMemPos.top().isFieldsEquals(obj);
writeChangedState(res);
return res;
}
return true;
}
template<typename T, size_t N>
bool setChangedState(const T (&arr)[N]) {
if (!_isNewElement) {
auto old = *_objMemPos.top().getOldObjectField(arr);
auto end = arr + N;
bool changed{};
for (auto p = arr, pOld = old; p != end; ++p, ++pOld) {
if (!(*p == *pOld)) {
changed = true;
break;
}
}
writeChangedState(changed);
return changed;
}
return true;
}
template<typename T, typename Fnc>
void processContainer(const T oldBegin, const T oldEnd, const T begin, const T end, Fnc &&fnc) {
auto misMatch = std::mismatch(oldBegin, oldEnd, begin, end);
auto lastChanged = begin;
while (misMatch.first != oldEnd && misMatch.second != end) {
writeIndexOffset(std::distance(lastChanged, misMatch.second));
_objMemPos.emplace(ObjectMemoryPosition{*misMatch.first, *misMatch.second});
fnc(*misMatch.second);
_objMemPos.pop();
++misMatch.first;
++misMatch.second;
lastChanged = misMatch.second;
misMatch = std::mismatch(misMatch.first, oldEnd, misMatch.second, end);
}
auto p = misMatch.second;
//write items left
writeIndexOffset(std::distance(lastChanged, end));
//write old elements
for (auto pOld = misMatch.first; p != end && pOld != oldEnd; ++p, ++pOld) {
_objMemPos.emplace(ObjectMemoryPosition{*pOld, *p});
fnc(*p);
_objMemPos.pop();
}
//write new elements
_isNewElement = true;
for (; p != end; ++p)
fnc(*p);
_isNewElement = false;
}
void writeChangedState(bool state) {
_writter.writeBits(state ? 1u : 0u, 1);
}
void writeIndexOffset(const size_t offset) {
//special case, if items are updated sequentialy
if (offset == 0) {
_writter.writeBits(1u, 1);
} else {
_writter.writeBits(0u, 1);
auto smallOffset = offset < 16;
_writter.writeBits(smallOffset ? 1u : 0u, 1);
if (smallOffset)
_writter.writeBits(offset, 4);
else
_writter.writeBits(offset, 32);
}
}
};
}
#endif //BITSERY_DELTASERIALIZER_H

View File

@@ -0,0 +1,357 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_DESERIALIZER_H
#define BITSERY_DESERIALIZER_H
#include "common.h"
#include <array>
#include <utility>
namespace bitsery {
/*
* functions for range
*/
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
void setRangeValue(T& v, const RangeSpec<T>& r) {
v += r.min;
};
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
void setRangeValue(T& v, const RangeSpec<T>& r) {
using VT = std::underlying_type_t<T>;
reinterpret_cast<VT&>(v) += static_cast<VT>(r.min);
};
template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
void setRangeValue(T& v, const RangeSpec<T>& r) {
using UIT = SAME_SIZE_UNSIGNED<T>;
const auto intRep = reinterpret_cast<UIT&>(v);
const UIT maxUint = (static_cast<UIT>(1) << r.bitsRequired) - 1;
v = r.min + (static_cast<T>(intRep) / maxUint) * (r.max - r.min);
};
template<typename Reader>
class Deserializer {
public:
Deserializer(Reader& r):_reader{r}, _isValid{true} {};
template <typename T>
Deserializer& object(T&& obj) {
return serialize(*this, std::forward<T>(obj));
}
/*
* value overloads
*/
template<size_t VSIZE = 0, typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
Deserializer& value(T& v) {
static_assert(std::numeric_limits<float>::is_iec559, "");
static_assert(std::numeric_limits<double>::is_iec559, "");
if (_isValid) {
constexpr size_t ValueSize = VSIZE == 0 ? sizeof(T) : VSIZE;
_isValid = _reader.template readBytes<ValueSize>(reinterpret_cast<SAME_SIZE_UNSIGNED<T>&>(v));
}
return *this;
}
template<size_t VSIZE = 0, typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
Deserializer& value(T& v) {
constexpr size_t ValueSize = VSIZE == 0 ? sizeof(T) : VSIZE;
using UT = std::underlying_type_t<T>;
if (_isValid) {
_isValid = _reader.template readBytes<ValueSize>(reinterpret_cast<UT&>(v));
}
return *this;
}
template<size_t VSIZE = 0, typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
Deserializer& value(T& v) {
constexpr size_t ValueSize = VSIZE == 0 ? sizeof(T) : VSIZE;
if (_isValid) {
_isValid = _reader.template readBytes<ValueSize>(v);
}
return *this;
}
/*
* bool
*/
Deserializer& boolBit(bool& v) {
if (_isValid) {
unsigned char tmp;
_isValid = _reader.readBits(tmp, 1);
v = tmp == 1;
}
return *this;
}
Deserializer& boolByte(bool& v) {
if (_isValid) {
unsigned char tmp;
_isValid = _reader.template readBytes<1>(tmp);
if (_isValid)
_isValid = tmp < 2;
v = tmp == 1;
}
return *this;
}
/*
* range
*/
template <typename T>
Deserializer& range(T& v, const RangeSpec<T>& range) {
if (_isValid) {
_isValid = _reader.template readBits(reinterpret_cast<SAME_SIZE_UNSIGNED<T>&>(v), range.bitsRequired);
setRangeValue(v, range);
if (_isValid)
_isValid = isRangeValid(v, range);
}
return *this;
}
/*
* substitution overloads
*/
template<typename T, size_t N, typename Fnc>
Deserializer& substitution(T& v, const std::array<T,N>& expectedValues, Fnc&& fnc) {
size_t index;
range(index, {{}, N + 1});
if (_isValid) {
if (index)
v = expectedValues[index-1];
else
fnc(v);
}
return *this;
};
template<size_t VSIZE, typename T, size_t N>
Deserializer& substitution(T& v, const std::array<T,N>& expectedValues) {
size_t index;
range(index, {{}, N + 1});
if (_isValid) {
if (index)
v = expectedValues[index-1];
else
ProcessAnyType<VSIZE>::serialize(*this, v);
}
return *this;
};
template<typename T, size_t N>
Deserializer& substitution(T& v, const std::array<T,N>& expectedValues) {
size_t index;
range(index, {{}, N + 1});
if (_isValid) {
if (index)
v = expectedValues[index-1];
else
ProcessAnyType<ARITHMETIC_OR_ENUM_SIZE<T>>::serialize(*this, v);
}
return *this;
};
/*
* text overloads
*/
template <size_t VSIZE = 1, typename T>
Deserializer& text(std::basic_string<T>& str, size_t maxSize) {
size_t size;
readSize(size, maxSize);
if (_isValid) {
str.resize(size);
if (size) {
//if (std::is_const<decltype(std::declval<std::basic_string<T>>().data())>::value) {
std::vector<T> buf(size);
_isValid = _reader.template readBuffer<VSIZE>(buf.data(), size);
str.assign(buf.data(), size);
//} else {
//_isValid = _reader.template readBuffer<VSIZE>(str.data(), size);
//}
}
}
return *this;
}
template<size_t VSIZE=1, typename T, size_t N>
Deserializer& text(T (&str)[N]) {
size_t size;
readSize(size, N-1);
if (_isValid) {
_isValid = _reader.template readBuffer<VSIZE>(str, size);
str[size] = {};
}
return *this;
}
/*
* container overloads
*/
template <typename T, typename Fnc>
Deserializer& container(T&& obj, Fnc&& fnc, size_t maxSize) {
decltype(obj.size()) size{};
readSize(size, maxSize);
if (_isValid) {
obj.resize(size);
for (auto& v:obj) {
if (_isValid)
fnc(v);
}
}
return *this;
}
template <size_t VSIZE, typename T>
Deserializer& container(T& obj, size_t maxSize) {
decltype(obj.size()) size{};
readSize(size, maxSize);
if (_isValid) {
obj.resize(size);
procContainer<VSIZE>(obj);
}
return *this;
}
template <typename T>
Deserializer& container(T& obj, size_t maxSize) {
decltype(obj.size()) size{};
readSize(size, maxSize);
if (_isValid) {
obj.resize(size);
procContainer<ARITHMETIC_OR_ENUM_SIZE<typename T::value_type>>(obj);
}
return *this;
}
/*
* array overloads (fixed size array (std::array, and c-style array))
*/
//std::array overloads
template<typename T, size_t N, typename Fnc>
Deserializer& array(std::array<T,N> &arr, Fnc && fnc) {
for (auto& v: arr)
if (_isValid)
fnc(v);
return *this;
}
template<size_t VSIZE, typename T, size_t N>
Deserializer& array(std::array<T,N> &arr) {
procContainer<VSIZE>(arr);
return *this;
}
template<typename T, size_t N>
Deserializer& array(std::array<T,N> &arr) {
procContainer<ARITHMETIC_OR_ENUM_SIZE<T>>(arr);
return *this;
}
//c-style array overloads
template<typename T, size_t N, typename Fnc>
Deserializer& array(T (&arr)[N], Fnc&& fnc) {
T* tmp = arr;
for (auto i = 0u; i < N; ++i, ++tmp)
if (_isValid)
fnc(*tmp);
return *this;
}
template<size_t VSIZE, typename T, size_t N>
Deserializer& array(T (&arr)[N]) {
procCArray<VSIZE>(arr);
return *this;
}
template<typename T, size_t N>
Deserializer& array(T (&arr)[N]) {
procCArray<ARITHMETIC_OR_ENUM_SIZE<T>>(arr);
return *this;
}
bool isValid() const {
return _isValid;
}
private:
Reader& _reader;
bool _isValid;
void readSize(size_t &size, size_t maxSize) {
size = {};
if (_isValid) {
unsigned char firstBit;
_isValid = _reader.readBits(firstBit, 1);
if (_isValid) {
if (firstBit) {
_isValid = _reader.readBits(size, 7);
} else {
unsigned char secondBit;
_isValid = _reader.readBits(secondBit, 1);
if (_isValid) {
if (secondBit) {
_isValid = _reader.readBits(size,14);
} else {
_isValid = _reader.readBits(size,30);
}
}
}
}
if (_isValid)
_isValid = size <= maxSize;
}
}
template <size_t VSIZE, typename T>
void procContainer(T&& obj) {
//todo could be improved for arithmetic types in contiguous containers (std::vector, std::array) (keep in mind std::vector<bool> specialization)
for (auto& v: obj)
if (_isValid)
ProcessAnyType<VSIZE>::serialize(*this, v);
};
template <size_t VSIZE, typename T, size_t N>
void procCArray(T (&arr)[N]) {
//todo could be improved for arithmetic types
T* end = arr + N;
for (T* it = arr; it != end; ++it)
if (_isValid)
ProcessAnyType<VSIZE>::serialize(*this, *it);
};
};
}
#endif //BITSERY_DESERIALIZER_H

View File

@@ -0,0 +1,300 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_SERIALIZER_H
#define BITSERY_SERIALIZER_H
#include "common.h"
#include <array>
namespace bitsery {
/*
* functions for range
*/
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type * = nullptr>
auto getRangeValue(const T &v, const RangeSpec<T> &r) {
return static_cast<SAME_SIZE_UNSIGNED<T>>(v - r.min);
};
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type * = nullptr>
auto getRangeValue(const T &v, const RangeSpec<T> &r) {
return static_cast<SAME_SIZE_UNSIGNED<T>>(v) - static_cast<SAME_SIZE_UNSIGNED<T>>(r.min);
};
template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
auto getRangeValue(const T &v, const RangeSpec<T> &r) {
using VT = SAME_SIZE_UNSIGNED<T>;
const VT maxUint = (static_cast<VT>(1) << r.bitsRequired) - 1;
const auto ratio = (v - r.min) / (r.max - r.min);
return static_cast<VT>(ratio * maxUint);
};
/*
* functions for substitution
*/
template<typename T, size_t N>
size_t findSubstitutionIndex(const T &v, const std::array<T, N> &defValues) {
auto index{1u};
for (auto &d:defValues) {
if (d == v)
return index;
++index;
}
return 0u;
};
template<typename Writter>
class Serializer {
public:
Serializer(Writter &w) : _writter{w} {};
template<typename T>
Serializer& object(const T &obj) {
return serialize(*this, obj);
}
/*
* value overloads
*/
template<size_t VSIZE = 0, typename T, typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
Serializer& value(const T &v) {
static_assert(std::numeric_limits<float>::is_iec559, "");
static_assert(std::numeric_limits<double>::is_iec559, "");
constexpr size_t ValueSize = VSIZE == 0 ? sizeof(T) : VSIZE;
_writter.template writeBytes<ValueSize>(reinterpret_cast<const SAME_SIZE_UNSIGNED<T> &>(v));
return *this;
}
template<size_t VSIZE = 0, typename T, typename std::enable_if<std::is_enum<T>::value>::type * = nullptr>
Serializer& value(const T &v) {
constexpr size_t ValueSize = VSIZE == 0 ? sizeof(T) : VSIZE;
_writter.template writeBytes<ValueSize>(reinterpret_cast<const std::underlying_type_t<T> &>(v));
return *this;
}
template<size_t VSIZE = 0, typename T, typename std::enable_if<std::is_integral<T>::value>::type * = nullptr>
Serializer& value(const T &v) {
constexpr size_t ValueSize = VSIZE == 0 ? sizeof(T) : VSIZE;
_writter.template writeBytes<ValueSize>(v);
return *this;
}
/*
* bool
*/
Serializer& boolBit(bool v) {
_writter.template writeBits(static_cast<unsigned char>(v ? 1 : 0), 1);
return *this;
}
Serializer& boolByte(bool v) {
_writter.template writeBytes<1>(static_cast<unsigned char>(v ? 1 : 0));
return *this;
}
/*
* range
*/
template<typename T>
Serializer& range(const T &v, const RangeSpec<T> &range) {
assert(isRangeValid(v, range));
_writter.template writeBits(getRangeValue(v, range), range.bitsRequired);
return *this;
}
/*
* substitution overloads
*/
template<typename T, size_t N, typename Fnc>
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues, Fnc &&fnc) {
auto index = findSubstitutionIndex(v, expectedValues);
range(index, {{}, N +1});
if (!index)
fnc(v);
return *this;
};
template<size_t VSIZE, typename T, size_t N>
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues) {
auto index = findSubstitutionIndex(v, expectedValues);
range(index, {{}, N +1});
if (!index)
ProcessAnyType<VSIZE>::serialize(*this, v);
return *this;
};
template<typename T, size_t N>
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues) {
auto index = findSubstitutionIndex(v, expectedValues);
range(index, {{}, N +1});
if (!index)
ProcessAnyType<ARITHMETIC_OR_ENUM_SIZE<T>>::serialize(*this, v);
return *this;
};
/*
* text overloads
*/
template<size_t VSIZE = 1, typename T>
Serializer& text(const std::basic_string<T> &str, size_t maxSize) {
assert(str.size() <= maxSize);
procText<VSIZE>(str.data(), str.size());
return *this;
}
template<size_t VSIZE = 1, typename T, size_t N>
Serializer& text(const T (&str)[N]) {
procText<VSIZE>(str, std::min(std::char_traits<T>::length(str), N - 1));
return *this;
}
/*
* container overloads
*/
template<typename T, typename Fnc>
Serializer& container(const T &obj, Fnc &&fnc, size_t maxSize) {
assert(obj.size() <= maxSize);
writeSize(obj.size());
for (auto &v: obj)
fnc(v);
return *this;
}
template<size_t VSIZE, typename T>
Serializer& container(const T &obj, size_t maxSize) {
assert(obj.size() <= maxSize);
writeSize(obj.size());
procContainer<VSIZE>(obj);
return *this;
}
template<typename T>
Serializer& container(const T &obj, size_t maxSize) {
assert(obj.size() <= maxSize);
writeSize(obj.size());
procContainer<ARITHMETIC_OR_ENUM_SIZE<typename T::value_type>>(obj);
return *this;
}
/*
* array overloads (fixed size array (std::array, and c-style array))
*/
//std::array overloads
template<typename T, size_t N, typename Fnc>
Serializer& array(const std::array<T, N> &arr, Fnc &&fnc) {
for (auto &v: arr)
fnc(v);
return *this;
}
template<size_t VSIZE, typename T, size_t N>
Serializer& array(const std::array<T, N> &arr) {
procContainer<VSIZE>(arr);
return *this;
}
template<typename T, size_t N>
Serializer& array(const std::array<T, N> &arr) {
procContainer<ARITHMETIC_OR_ENUM_SIZE<T>>(arr);
return *this;
}
//c-style array overloads
template<typename T, size_t N, typename Fnc>
Serializer& array(const T (&arr)[N], Fnc &&fnc) {
const T *end = arr + N;
for (const T *tmp = arr; tmp != end; ++tmp)
fnc(*tmp);
return *this;
}
template<size_t VSIZE, typename T, size_t N>
Serializer& array(const T (&arr)[N]) {
procCArray<VSIZE>(arr);
return *this;
}
template<typename T, size_t N>
Serializer& array(const T (&arr)[N]) {
procCArray<ARITHMETIC_OR_ENUM_SIZE<T>>(arr);
return *this;
}
private:
Writter &_writter;
void writeSize(const size_t size) {
if (size < 0x80u) {
_writter.writeBits(1u, 1);
_writter.writeBits(size, 7);
} else if (size < 0x4000u) {
_writter.writeBits(2u,2);
_writter.writeBits(size, 14);
} else {
assert(size < 0x40000000u);
_writter.writeBits(0u,2);
_writter.writeBits(size, 30);
}
}
template<size_t VSIZE, typename T>
void procContainer(T &&obj) {
//todo could be improved for arithmetic types in contiguous containers (std::vector, std::array) (keep in mind std::vector<bool> specialization)
for (auto &v: obj)
ProcessAnyType<VSIZE>::serialize(*this, v);
};
template<size_t VSIZE, typename T, size_t N>
void procCArray(T (&arr)[N]) {
//todo could be improved for arithmetic types
const T *end = arr + N;
for (const T *it = arr; it != end; ++it)
ProcessAnyType<VSIZE>::serialize(*this, *it);
};
template<size_t VSIZE, typename T>
void procText(const T *str, size_t size) {
writeSize(size);
if (size)
_writter.template writeBuffer<VSIZE>(str, size);
}
};
}
#endif //BITSERY_SERIALIZER_H