mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-21 06:39:07 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef278277e3 |
1
CHANGELOG.md
Normal file
1
CHANGELOG.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#
|
||||||
@@ -1,23 +1,12 @@
|
|||||||
cmake_minimum_required(VERSION 3.2)
|
cmake_minimum_required(VERSION 3.2)
|
||||||
|
|
||||||
set(PROJECT_NAME bitsery)
|
project(bitsery)
|
||||||
set(TEST_PROJECT_NAME ${PROJECT_NAME}_tests)
|
|
||||||
project(${PROJECT_NAME})
|
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O0 -fprofile-arcs -ftest-coverage")
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
|
||||||
|
|
||||||
|
|
||||||
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/ext)
|
|
||||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
|
||||||
#include(CodeCoverage)
|
|
||||||
#setup_target_for_coverage(${PROJECT_NAME}_coverage ${TEST_PROJECT_NAME} coverage)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_subdirectory(examples)
|
add_subdirectory(examples)
|
||||||
|
|
||||||
#add tests
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
|
|||||||
@@ -25,14 +25,11 @@ cmake_minimum_required(VERSION 3.2)
|
|||||||
|
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||||
|
|
||||||
file(GLOB EXAMPLE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
file(GLOB ExampleFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||||
|
|
||||||
FOREACH(EXAMPLE ${EXAMPLE_FILES})
|
FOREACH(ExampleFile ${ExampleFiles})
|
||||||
get_filename_component(EXAMPLE_NAME ${EXAMPLE} NAME_WE)
|
get_filename_component(ExampleName ${ExampleFile} NAME_WE)
|
||||||
add_executable(${EXAMPLE_NAME} ${EXAMPLE})
|
add_executable(${ExampleName} ${ExampleFile})
|
||||||
|
|
||||||
set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 14)
|
|
||||||
set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
|
||||||
|
|
||||||
ENDFOREACH()
|
ENDFOREACH()
|
||||||
|
|
||||||
|
|||||||
@@ -146,7 +146,8 @@ FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
|
|||||||
|
|
||||||
# Capturing lcov counters and generating report
|
# Capturing lcov counters and generating report
|
||||||
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${coverage_info}
|
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${coverage_info}
|
||||||
COMMAND ${LCOV_PATH} --remove ${coverage_info} 'tests/*' '/usr/*' --output-file ${coverage_cleaned}
|
#extract only /include/bitsery directory
|
||||||
|
COMMAND ${LCOV_PATH} --extract ${coverage_info} '*include/bitsery*' --output-file ${coverage_cleaned}
|
||||||
COMMAND ${GENHTML_PATH} -o ${_outputname} ${coverage_cleaned}
|
COMMAND ${GENHTML_PATH} -o ${_outputname} ${coverage_cleaned}
|
||||||
COMMAND ${CMAKE_COMMAND} -E remove ${coverage_info} ${coverage_cleaned}
|
COMMAND ${CMAKE_COMMAND} -E remove ${coverage_info} ${coverage_cleaned}
|
||||||
|
|
||||||
|
|||||||
18
ext/LinkTestLIb.cmake
Normal file
18
ext/LinkTestLIb.cmake
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
function(LinkTestLib TargetName)
|
||||||
|
|
||||||
|
add_dependencies(${TargetName} googletest)
|
||||||
|
|
||||||
|
if(NOT WIN32 OR MINGW)
|
||||||
|
FOREACH(LibName ${GTestLinkLibNames})
|
||||||
|
target_link_libraries(${TargetName} ${GTestLibsDir}/lib${LibName}.a )
|
||||||
|
ENDFOREACH()
|
||||||
|
else()
|
||||||
|
FOREACH(LibName ${GTestLinkLibNames})
|
||||||
|
target_link_libraries(${TargetName}
|
||||||
|
debug ${GTestLibsDir}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LibName}${CMAKE_FIND_LIBRARY_SUFFIXES}
|
||||||
|
optimized ${GTestLibsDir}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LibName}${CMAKE_FIND_LIBRARY_SUFFIXES})
|
||||||
|
ENDFOREACH()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(${TargetName} ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
endfunction(LinkTestLib)
|
||||||
@@ -24,32 +24,32 @@ cmake_minimum_required(VERSION 3.2)
|
|||||||
project(gtest_builder C CXX)
|
project(gtest_builder C CXX)
|
||||||
include(ExternalProject)
|
include(ExternalProject)
|
||||||
|
|
||||||
set(GTEST_FORCE_SHARED_CRT ON)
|
set(ForceSharedCrt ON)
|
||||||
set(GTEST_DISABLE_PTHREADS OFF)
|
set(DisablePThreads OFF)
|
||||||
|
|
||||||
if(MINGW)
|
if(MINGW)
|
||||||
set(GTEST_DISABLE_PTHREADS ON)
|
set(DisablePThreads ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (${USE_GMOCK})
|
if (${UseGMock})
|
||||||
message("use gmock")
|
message("use gmock")
|
||||||
set(BUILD_ARGS -DBUILD_GTEST=OFF -DBUILD_GMOCK=ON)
|
set(BuildArgs -DBUILD_GTEST=OFF -DBUILD_GMOCK=ON)
|
||||||
else ()
|
else ()
|
||||||
message("use gtest only")
|
message("use gtest only")
|
||||||
set(BUILD_ARGS -DBUILD_GTEST=ON -DBUILD_GMOCK=OFF)
|
set(BuildArgs -DBUILD_GTEST=ON -DBUILD_GMOCK=OFF)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (WIN32 AND NOT MINGW)
|
if (WIN32 AND NOT MINGW)
|
||||||
set(BUILD_ARGS ${BUILD_ARGS}
|
set(BuildArgs ${BuildArgs}
|
||||||
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
|
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
|
||||||
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs)
|
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
ExternalProject_Add(googletest
|
ExternalProject_Add(googletest
|
||||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||||
CMAKE_ARGS ${BUILD_ARGS}
|
CMAKE_ARGS ${BuildArgs}
|
||||||
-Dgtest_force_shared_crt=${GTEST_FORCE_SHARED_CRT}
|
-Dgtest_force_shared_crt=${ForceSharedCrt}
|
||||||
-Dgtest_disable_pthreads=${GTEST_DISABLE_PTHREADS}
|
-Dgtest_disable_pthreads=${DisablePThreads}
|
||||||
PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
|
PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
|
||||||
# disable update command
|
# disable update command
|
||||||
UPDATE_COMMAND ""
|
UPDATE_COMMAND ""
|
||||||
@@ -60,20 +60,20 @@ ExternalProject_Add(googletest
|
|||||||
#export variables
|
#export variables
|
||||||
ExternalProject_Get_Property(googletest source_dir)
|
ExternalProject_Get_Property(googletest source_dir)
|
||||||
ExternalProject_Get_Property(googletest binary_dir)
|
ExternalProject_Get_Property(googletest binary_dir)
|
||||||
if (${USE_GMOCK})
|
if (${UseGMock})
|
||||||
# need to include both googletest and googlemock
|
# need to include both googletest and googlemock
|
||||||
set(GTEST_INCLUDE_DIRS ${source_dir}/googlemock/include ${source_dir}/googletest/include PARENT_SCOPE)
|
set(GTestIncludeDirs ${source_dir}/googlemock/include ${source_dir}/googletest/include PARENT_SCOPE)
|
||||||
set(GTEST_LIBS_DIR ${binary_dir}/googlemock PARENT_SCOPE)
|
set(GTestLibsDir ${binary_dir}/googlemock PARENT_SCOPE)
|
||||||
set(GTEST_LIBNAME gmock PARENT_SCOPE)
|
set(GTestLibName gmock PARENT_SCOPE)
|
||||||
set(GTEST_MAIN_LIBNAME gmock_main PARENT_SCOPE)
|
set(GTestMainLibName gmock_main PARENT_SCOPE)
|
||||||
set(GTEST_LINK_LIBNAMES gmock_main PARENT_SCOPE)
|
set(GTestLinkLibNames gmock_main PARENT_SCOPE)
|
||||||
else()
|
else()
|
||||||
set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include PARENT_SCOPE)
|
set(GTestIncludeDirs ${source_dir}/googletest/include PARENT_SCOPE)
|
||||||
set(GTEST_LIBS_DIR ${binary_dir}/googletest PARENT_SCOPE)
|
set(GTestLibsDir ${binary_dir}/googletest PARENT_SCOPE)
|
||||||
set(GTEST_LIBNAME gtest PARENT_SCOPE)
|
set(GTestLibName gtest PARENT_SCOPE)
|
||||||
set(GTEST_MAIN_LIBNAME gtest_main PARENT_SCOPE)
|
set(GTestMainLibName gtest_main PARENT_SCOPE)
|
||||||
# need to include both libs gtest and gtest_main
|
# need to include both libs gtest and gtest_main
|
||||||
set(GTEST_LINK_LIBNAMES gtest gtest_main PARENT_SCOPE)
|
set(GTestLinkLibNames gtest gtest_main PARENT_SCOPE)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -32,27 +32,32 @@
|
|||||||
|
|
||||||
namespace bitsery {
|
namespace bitsery {
|
||||||
|
|
||||||
struct BufferReader {
|
template <typename Config>
|
||||||
|
struct BasicBufferReader {
|
||||||
|
using ValueType = typename Config::BufferValueType;
|
||||||
|
using ScratchType = typename Config::BufferScrathType;
|
||||||
|
|
||||||
using value_type = uint8_t;
|
BasicBufferReader(const ValueType* data, size_t size) : _pos{data}, _end{data + size}
|
||||||
|
|
||||||
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}
|
|
||||||
{
|
{
|
||||||
|
static_assert(std::is_unsigned<ValueType>(), "Config::BufferValueType must be unsigned");
|
||||||
|
static_assert(std::is_unsigned<ScratchType>(), "Config::BufferScrathType must be unsigned");
|
||||||
|
static_assert(sizeof(ValueType)*2 == sizeof(ScratchType), "ScratchType must be 2x bigger than value type");
|
||||||
|
static_assert(sizeof(ValueType) == 1, "currently only supported BufferValueType is 1 byte");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
explicit BasicBufferReader(const std::vector<ValueType> &buf) : BasicBufferReader(buf.data(), buf.size()) {
|
||||||
|
}
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
BufferReader(const uint8_t (&data)[N]): _pos{data}, _end{data + N}
|
explicit BasicBufferReader(const ValueType (&data)[N]): BasicBufferReader(data, N)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferReader(const BufferReader&) = delete;
|
BasicBufferReader(const BasicBufferReader&) = delete;
|
||||||
BufferReader& operator=(const BufferReader& ) = delete;
|
BasicBufferReader& operator=(const BasicBufferReader& ) = delete;
|
||||||
BufferReader(BufferReader&&) noexcept = default;
|
BasicBufferReader(BasicBufferReader&&) noexcept = default;
|
||||||
BufferReader& operator=(BufferReader&&) noexcept = default;
|
BasicBufferReader& operator=(BasicBufferReader&&) noexcept = default;
|
||||||
~BufferReader() noexcept = default;
|
~BasicBufferReader() noexcept = default;
|
||||||
|
|
||||||
|
|
||||||
template<size_t SIZE, typename T>
|
template<size_t SIZE, typename T>
|
||||||
@@ -62,7 +67,7 @@ namespace bitsery {
|
|||||||
using UT = typename std::make_unsigned<T>::type;
|
using UT = typename std::make_unsigned<T>::type;
|
||||||
return !m_scratch
|
return !m_scratch
|
||||||
? directRead(&v, 1)
|
? directRead(&v, 1)
|
||||||
: readBits(reinterpret_cast<UT &>(v), BITS_SIZE<T>);
|
: readBits(reinterpret_cast<UT &>(v), details::BITS_SIZE<T>);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t SIZE, typename T>
|
template<size_t SIZE, typename T>
|
||||||
@@ -70,16 +75,15 @@ namespace bitsery {
|
|||||||
static_assert(std::is_integral<T>(), "");
|
static_assert(std::is_integral<T>(), "");
|
||||||
static_assert(sizeof(T) == SIZE, "");
|
static_assert(sizeof(T) == SIZE, "");
|
||||||
|
|
||||||
if (!m_scratchBits) {
|
if (!m_scratchBits)
|
||||||
return directRead(buf, count);
|
return directRead(buf, count);
|
||||||
} else {
|
|
||||||
using UT = typename std::make_unsigned<T>::type;
|
using UT = typename std::make_unsigned<T>::type;
|
||||||
//todo improve implementation
|
//todo improve implementation
|
||||||
const auto end = buf + count;
|
const auto end = buf + count;
|
||||||
for (auto it = buf; it != end; ++it) {
|
for (auto it = buf; it != end; ++it) {
|
||||||
if (!readBits(reinterpret_cast<UT &>(*it), BITS_SIZE<T>))
|
if (!readBits(reinterpret_cast<UT &>(*it), details::BITS_SIZE<T>))
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -88,7 +92,7 @@ namespace bitsery {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
bool readBits(T &v, size_t bitsCount) {
|
bool readBits(T &v, size_t bitsCount) {
|
||||||
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
|
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
|
||||||
assert(bitsCount <= BITS_SIZE<T>);
|
assert(bitsCount <= details::BITS_SIZE<T>);
|
||||||
|
|
||||||
const auto bytesRequired = bitsCount > m_scratchBits
|
const auto bytesRequired = bitsCount > m_scratchBits
|
||||||
? ((bitsCount - 1 - m_scratchBits) >> 3) + 1u
|
? ((bitsCount - 1 - m_scratchBits) >> 3) + 1u
|
||||||
@@ -101,8 +105,8 @@ namespace bitsery {
|
|||||||
|
|
||||||
bool align() {
|
bool align() {
|
||||||
if (m_scratchBits) {
|
if (m_scratchBits) {
|
||||||
SCRATCH_TYPE tmp{};
|
ScratchType tmp{};
|
||||||
readBitsInternal(tmp, BITS_SIZE<value_type> - m_scratchBits);
|
readBitsInternal(tmp, m_scratchBits);
|
||||||
return tmp == 0;
|
return tmp == 0;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -113,8 +117,8 @@ namespace bitsery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const value_type* _pos;
|
const ValueType* _pos;
|
||||||
const value_type* _end;
|
const ValueType* _end;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool directRead(T *v, size_t count) {
|
bool directRead(T *v, size_t count) {
|
||||||
@@ -122,28 +126,39 @@ namespace bitsery {
|
|||||||
const auto bytesCount = sizeof(T) * count;
|
const auto bytesCount = sizeof(T) * count;
|
||||||
if (static_cast<size_t>(std::distance(_pos, _end)) < bytesCount)
|
if (static_cast<size_t>(std::distance(_pos, _end)) < bytesCount)
|
||||||
return false;
|
return false;
|
||||||
std::copy_n(_pos, bytesCount, reinterpret_cast<value_type *>(v));
|
//read from buffer, to data ptr,
|
||||||
|
std::copy_n(_pos, bytesCount, reinterpret_cast<ValueType *>(v));
|
||||||
std::advance(_pos, bytesCount);
|
std::advance(_pos, bytesCount);
|
||||||
|
//swap each byte if nessesarry
|
||||||
|
_swapDataBits(v, count, std::integral_constant<bool,
|
||||||
|
Config::NetworkEndianness != details::getSystemEndianness()>{});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void _swapDataBits(T *v, size_t count, std::true_type) {
|
||||||
|
std::for_each(v, std::next(v, count), [this](T& v) { v = details::swap(v); });
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void _swapDataBits(T *v, size_t count, std::false_type) {
|
||||||
|
//empty function because no swap is required
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void readBitsInternal(T &v, size_t size) {
|
void readBitsInternal(T &v, size_t size) {
|
||||||
auto bitsLeft = size;
|
auto bitsLeft = size;
|
||||||
T res{};
|
T res{};
|
||||||
while (bitsLeft > 0) {
|
while (bitsLeft > 0) {
|
||||||
auto bits = std::min(bitsLeft, BITS_SIZE<value_type>);
|
auto bits = std::min(bitsLeft, details::BITS_SIZE<ValueType>);
|
||||||
if (m_scratchBits < bits) {
|
if (m_scratchBits < bits) {
|
||||||
value_type tmp;
|
ValueType tmp;
|
||||||
|
directRead(&tmp, 1);
|
||||||
std::copy_n(_pos, 1, reinterpret_cast<value_type *>(&tmp));
|
m_scratch |= static_cast<ScratchType>(tmp) << m_scratchBits;
|
||||||
std::advance(_pos, 1);
|
m_scratchBits += details::BITS_SIZE<ValueType>;
|
||||||
|
|
||||||
m_scratch |= static_cast<SCRATCH_TYPE>(tmp) << m_scratchBits;
|
|
||||||
m_scratchBits += BITS_SIZE<value_type>;
|
|
||||||
}
|
}
|
||||||
auto shiftedRes =
|
auto shiftedRes =
|
||||||
static_cast<T>(m_scratch & ((static_cast<SCRATCH_TYPE>(1) << bits) - 1)) << (size - bitsLeft);
|
static_cast<T>(m_scratch & ((static_cast<ScratchType>(1) << bits) - 1)) << (size - bitsLeft);
|
||||||
res |= shiftedRes;
|
res |= shiftedRes;
|
||||||
m_scratch >>= bits;
|
m_scratch >>= bits;
|
||||||
m_scratchBits -= bits;
|
m_scratchBits -= bits;
|
||||||
@@ -152,13 +167,12 @@ namespace bitsery {
|
|||||||
v = res;
|
v = res;
|
||||||
}
|
}
|
||||||
|
|
||||||
using SCRATCH_TYPE = typename BIGGER_TYPE<value_type>::type;
|
ScratchType m_scratch{};
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
};
|
};
|
||||||
|
//helper type
|
||||||
|
using BufferReader = BasicBufferReader<DefaultConfig>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BITSERY_BUFFER_READER_H
|
#endif //BITSERY_BUFFER_READER_H
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace bitsery {
|
namespace bitsery {
|
||||||
|
|
||||||
@@ -38,13 +39,13 @@ namespace bitsery {
|
|||||||
void writeBytes(const T &) {
|
void writeBytes(const T &) {
|
||||||
static_assert(std::is_integral<T>(), "");
|
static_assert(std::is_integral<T>(), "");
|
||||||
static_assert(sizeof(T) == SIZE, "");
|
static_assert(sizeof(T) == SIZE, "");
|
||||||
_bitsCount += BITS_SIZE<T>;
|
_bitsCount += details::BITS_SIZE<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void writeBits(const T &, size_t bitsCount) {
|
void writeBits(const T &, size_t bitsCount) {
|
||||||
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
|
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
|
||||||
assert(bitsCount <= BITS_SIZE<T>);
|
assert(bitsCount <= details::BITS_SIZE<T>);
|
||||||
_bitsCount += bitsCount;
|
_bitsCount += bitsCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ namespace bitsery {
|
|||||||
void writeBuffer(const T *, size_t count) {
|
void writeBuffer(const T *, size_t count) {
|
||||||
static_assert(std::is_integral<T>(), "");
|
static_assert(std::is_integral<T>(), "");
|
||||||
static_assert(sizeof(T) == SIZE, "");
|
static_assert(sizeof(T) == SIZE, "");
|
||||||
_bitsCount += BITS_SIZE<T> * count;
|
_bitsCount += details::BITS_SIZE<T> * count;
|
||||||
}
|
}
|
||||||
|
|
||||||
//get size in bytes
|
//get size in bytes
|
||||||
@@ -65,30 +66,34 @@ namespace bitsery {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Config>
|
||||||
|
struct BasicBufferWriter {
|
||||||
|
using ValueType = typename Config::BufferValueType;
|
||||||
|
using ScratchType = typename Config::BufferScrathType;
|
||||||
|
|
||||||
struct BufferWriter {
|
explicit BasicBufferWriter(std::vector<ValueType> &buffer) : _outIt{std::back_inserter(buffer)} {
|
||||||
using value_type = uint8_t;
|
static_assert(std::is_unsigned<ValueType>(), "Config::BufferValueType must be unsigned");
|
||||||
|
static_assert(std::is_unsigned<ScratchType>(), "Config::BufferScrathType must be unsigned");
|
||||||
explicit BufferWriter(std::vector<uint8_t> &buffer) : _buf{buffer}, _outIt{std::back_inserter(buffer)} {
|
static_assert(sizeof(ValueType)*2 == sizeof(ScratchType), "ScratchType must be 2x bigger than value type");
|
||||||
static_assert(std::is_unsigned<value_type>::value, "");
|
static_assert(sizeof(ValueType) == 1, "currently only supported BufferValueType is 1 byte");
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferWriter(const BufferWriter&) = delete;
|
BasicBufferWriter(const BasicBufferWriter&) = delete;
|
||||||
BufferWriter& operator=(const BufferWriter& ) = delete;
|
BasicBufferWriter& operator=(const BasicBufferWriter& ) = delete;
|
||||||
BufferWriter(BufferWriter&&) noexcept = default;
|
BasicBufferWriter(BasicBufferWriter&&) noexcept = default;
|
||||||
BufferWriter& operator=(BufferWriter&&) noexcept = default;
|
BasicBufferWriter& operator=(BasicBufferWriter&&) noexcept = default;
|
||||||
~BufferWriter() noexcept = default;
|
~BasicBufferWriter() noexcept = default;
|
||||||
|
|
||||||
template<size_t SIZE, typename T>
|
template<size_t SIZE, typename T>
|
||||||
void writeBytes(const T &v) {
|
void writeBytes(const T &v) {
|
||||||
static_assert(std::is_integral<T>(), "");
|
static_assert(std::is_integral<T>(), "");
|
||||||
static_assert(sizeof(T) == SIZE, "");
|
static_assert(sizeof(T) == SIZE, "");
|
||||||
|
|
||||||
if (!m_scratchBits) {
|
if (!_scratchBits) {
|
||||||
directWrite(&v, 1);
|
directWrite(&v, 1);
|
||||||
} else {
|
} else {
|
||||||
using UT = typename std::make_unsigned<T>::type;
|
using UT = typename std::make_unsigned<T>::type;
|
||||||
writeBits(reinterpret_cast<const UT &>(v), BITS_SIZE<T>);
|
writeBits(reinterpret_cast<const UT &>(v), details::BITS_SIZE<T>);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,36 +101,36 @@ namespace bitsery {
|
|||||||
void writeBuffer(const T *buf, size_t count) {
|
void writeBuffer(const T *buf, size_t count) {
|
||||||
static_assert(std::is_integral<T>(), "");
|
static_assert(std::is_integral<T>(), "");
|
||||||
static_assert(sizeof(T) == SIZE, "");
|
static_assert(sizeof(T) == SIZE, "");
|
||||||
if (!m_scratchBits) {
|
if (!_scratchBits) {
|
||||||
directWrite(buf, count);
|
directWrite(buf, count);
|
||||||
} else {
|
} else {
|
||||||
using UT = typename std::make_unsigned<T>::type;
|
using UT = typename std::make_unsigned<T>::type;
|
||||||
//todo improve implementation
|
//todo improve implementation
|
||||||
const auto end = buf + count;
|
const auto end = buf + count;
|
||||||
for (auto it = buf; it != end; ++it)
|
for (auto it = buf; it != end; ++it)
|
||||||
writeBits(reinterpret_cast<const UT &>(*it), BITS_SIZE<T>);
|
writeBits(reinterpret_cast<const UT &>(*it), details::BITS_SIZE<T>);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void writeBits(const T &v, size_t bitsCount) {
|
void writeBits(const T &v, size_t bitsCount) {
|
||||||
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
|
static_assert(std::is_integral<T>() && std::is_unsigned<T>(), "");
|
||||||
assert(bitsCount <= BITS_SIZE<T>);
|
assert(0 < bitsCount && bitsCount <= details::BITS_SIZE<T>);
|
||||||
assert(v <= ((1ULL << bitsCount) - 1));
|
assert(v <= ((1ULL << bitsCount) - 1));
|
||||||
writeBitsInternal(v, bitsCount);
|
writeBitsInternal(v, bitsCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
void align() {
|
void align() {
|
||||||
if (m_scratchBits)
|
if (_scratchBits)
|
||||||
writeBitsInternal(value_type{}, BITS_SIZE<value_type> - m_scratchBits);
|
writeBitsInternal(ValueType{}, details::BITS_SIZE<ValueType> - _scratchBits);
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush() {
|
void flush() {
|
||||||
if (m_scratchBits) {
|
if (_scratchBits) {
|
||||||
auto tmp = static_cast<value_type>( m_scratch & bufTypeMask );
|
auto tmp = static_cast<ValueType>( _scratch & _MASK );
|
||||||
directWrite(&tmp, 1);
|
directWrite(&tmp, 1);
|
||||||
m_scratch >>= m_scratchBits;
|
_scratch >>= _scratchBits;
|
||||||
m_scratchBits -= m_scratchBits;
|
_scratchBits -= _scratchBits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,57 +138,68 @@ namespace bitsery {
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void directWrite(const T *v, size_t count) {
|
void directWrite(T&& v, size_t count) {
|
||||||
const auto bytesSize = sizeof(T) * count;
|
_directWriteSwapTag(std::forward<T>(v), count, std::integral_constant<bool,
|
||||||
const auto pos = _buf.size();
|
Config::NetworkEndianness != details::getSystemEndianness()>{});
|
||||||
_buf.resize(pos + bytesSize);
|
}
|
||||||
std::copy_n(reinterpret_cast<const value_type *>(v), bytesSize, _buf.data() + pos);
|
|
||||||
|
template<typename T>
|
||||||
|
void _directWriteSwapTag(const T *v, size_t count, std::true_type) {
|
||||||
|
std::for_each(v, std::next(v, count), [this](const T& v) {
|
||||||
|
const auto res = details::swap(v);
|
||||||
|
std::copy_n(reinterpret_cast<const ValueType*>(&res), sizeof(T), _outIt);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void _directWriteSwapTag(const T *v, size_t count, std::false_type) {
|
||||||
|
std::copy_n(reinterpret_cast<const ValueType*>(v), count * sizeof(T), _outIt);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void writeBitsInternal(const T &v, size_t size) {
|
void writeBitsInternal(const T &v, size_t size) {
|
||||||
|
constexpr size_t valueSize = details::BITS_SIZE<ValueType>;
|
||||||
auto value = v;
|
auto value = v;
|
||||||
auto bitsLeft = size;
|
auto bitsLeft = size;
|
||||||
while (bitsLeft > 0) {
|
while (bitsLeft > 0) {
|
||||||
auto bits = std::min(bitsLeft, BITS_SIZE<value_type>);
|
auto bits = std::min(bitsLeft, valueSize);
|
||||||
m_scratch |= static_cast<SCRATCH_TYPE>( value ) << m_scratchBits;
|
_scratch |= static_cast<ScratchType>( value ) << _scratchBits;
|
||||||
m_scratchBits += bits;
|
_scratchBits += bits;
|
||||||
if (m_scratchBits >= BITS_SIZE<value_type>) {
|
if (_scratchBits >= valueSize) {
|
||||||
auto tmp = static_cast<value_type>(m_scratch & bufTypeMask);
|
auto tmp = static_cast<ValueType>(_scratch & _MASK);
|
||||||
directWrite(&tmp, 1);
|
directWrite(&tmp, 1);
|
||||||
m_scratch >>= BITS_SIZE<value_type>;
|
_scratch >>= valueSize;
|
||||||
m_scratchBits -= BITS_SIZE<value_type>;
|
_scratchBits -= valueSize;
|
||||||
|
|
||||||
value >>= BITS_SIZE<value_type>;
|
value >>= valueSize;
|
||||||
}
|
}
|
||||||
bitsLeft -= bits;
|
bitsLeft -= bits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void writeBitsInternal(const value_type &v, size_t size) {
|
|
||||||
|
//overload for ValueType, for better performance
|
||||||
|
void writeBitsInternal(const ValueType &v, size_t size) {
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
m_scratch |= static_cast<SCRATCH_TYPE>( v ) << m_scratchBits;
|
_scratch |= static_cast<ScratchType>( v ) << _scratchBits;
|
||||||
m_scratchBits += size;
|
_scratchBits += size;
|
||||||
if (m_scratchBits >= BITS_SIZE<value_type>) {
|
if (_scratchBits >= details::BITS_SIZE<ValueType>) {
|
||||||
auto tmp = static_cast<value_type>(m_scratch & bufTypeMask);
|
auto tmp = static_cast<ValueType>(_scratch & _MASK);
|
||||||
directWrite(&tmp, 1);
|
directWrite(&tmp, 1);
|
||||||
m_scratch >>= BITS_SIZE<value_type>;
|
_scratch >>= details::BITS_SIZE<ValueType>;
|
||||||
m_scratchBits -= BITS_SIZE<value_type>;
|
_scratchBits -= details::BITS_SIZE<ValueType>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const value_type bufTypeMask = 0xFF;
|
const ValueType _MASK = std::numeric_limits<ValueType>::max();
|
||||||
using SCRATCH_TYPE = typename BIGGER_TYPE<value_type>::type;
|
std::back_insert_iterator<std::vector<ValueType>> _outIt;
|
||||||
std::vector<value_type> &_buf;
|
ScratchType _scratch{};
|
||||||
std::back_insert_iterator<std::vector<value_type>> _outIt;
|
size_t _scratchBits{};
|
||||||
SCRATCH_TYPE m_scratch{};
|
|
||||||
size_t m_scratchBits{};
|
|
||||||
|
|
||||||
|
|
||||||
//size_t _bufSize{};
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//helper type
|
||||||
|
using BufferWriter = BasicBufferWriter<DefaultConfig>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BITSERY_BUFFER_WRITER_H
|
#endif //BITSERY_BUFFER_WRITER_H
|
||||||
|
|||||||
@@ -21,215 +21,27 @@
|
|||||||
//SOFTWARE.
|
//SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef BITSERY_COMMON_H
|
#ifndef BITSERY_COMMON_H
|
||||||
#define BITSERY_COMMON_H
|
#define BITSERY_COMMON_H
|
||||||
|
|
||||||
#include <cstdint>
|
#include "bitsery/details/buffer_common.h"
|
||||||
|
|
||||||
namespace bitsery {
|
namespace bitsery {
|
||||||
|
|
||||||
template<typename T>
|
struct DefaultConfig {
|
||||||
constexpr size_t BITS_SIZE = sizeof(T) << 3;
|
static constexpr EndiannessType NetworkEndianness = EndiannessType::LittleEndian;
|
||||||
|
using BufferValueType = uint8_t;
|
||||||
template<typename T>
|
using BufferScrathType = uint16_t;
|
||||||
struct BIGGER_TYPE {
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
/*
|
||||||
struct BIGGER_TYPE<uint8_t> {
|
* serializer macro, serialize function specialization that accepts T& and const 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, 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) \
|
#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> \
|
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)
|
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
|
#endif //BITSERY_COMMON_H
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ namespace bitsery {
|
|||||||
_reader{r},
|
_reader{r},
|
||||||
_oldObj{oldObj},
|
_oldObj{oldObj},
|
||||||
_newObj{newObj},
|
_newObj{newObj},
|
||||||
_objMemPos(std::deque<ObjectMemoryPosition>(1, ObjectMemoryPosition{oldObj, newObj})),
|
_objMemPos(std::deque<details::ObjectMemoryPosition>(1, details::ObjectMemoryPosition{oldObj, newObj})),
|
||||||
_isNewElement{false} {
|
_isNewElement{false} {
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ namespace bitsery {
|
|||||||
|
|
||||||
const TObj &_oldObj;
|
const TObj &_oldObj;
|
||||||
const TObj &_newObj;
|
const TObj &_newObj;
|
||||||
std::stack<ObjectMemoryPosition> _objMemPos;
|
std::stack<details::ObjectMemoryPosition> _objMemPos;
|
||||||
bool _isNewElement;
|
bool _isNewElement;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -173,7 +173,7 @@ namespace bitsery {
|
|||||||
*p = *pOld;
|
*p = *pOld;
|
||||||
--offset;
|
--offset;
|
||||||
} else {
|
} else {
|
||||||
_objMemPos.emplace(ObjectMemoryPosition{*pOld, *p});
|
_objMemPos.emplace(details::ObjectMemoryPosition{*pOld, *p});
|
||||||
fnc(*this, *p);
|
fnc(*this, *p);
|
||||||
_objMemPos.pop();
|
_objMemPos.pop();
|
||||||
offset = readIndexOffset();
|
offset = readIndexOffset();
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ namespace bitsery {
|
|||||||
_writter{w},
|
_writter{w},
|
||||||
_oldObj{oldObj},
|
_oldObj{oldObj},
|
||||||
_newObj{newObj},
|
_newObj{newObj},
|
||||||
_objMemPos(std::deque<ObjectMemoryPosition>(1, ObjectMemoryPosition{oldObj, newObj})),
|
_objMemPos(std::deque<details::ObjectMemoryPosition>(1, details::ObjectMemoryPosition{oldObj, newObj})),
|
||||||
_isNewElement{false} {
|
_isNewElement{false} {
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -132,7 +132,7 @@ namespace bitsery {
|
|||||||
Writter &_writter;
|
Writter &_writter;
|
||||||
const TObj &_oldObj;
|
const TObj &_oldObj;
|
||||||
const TObj &_newObj;
|
const TObj &_newObj;
|
||||||
std::stack<ObjectMemoryPosition> _objMemPos;
|
std::stack<details::ObjectMemoryPosition> _objMemPos;
|
||||||
bool _isNewElement;
|
bool _isNewElement;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -170,7 +170,7 @@ namespace bitsery {
|
|||||||
auto lastChanged = begin;
|
auto lastChanged = begin;
|
||||||
while (misMatch.first != oldEnd && misMatch.second != end) {
|
while (misMatch.first != oldEnd && misMatch.second != end) {
|
||||||
writeIndexOffset(std::distance(lastChanged, misMatch.second));
|
writeIndexOffset(std::distance(lastChanged, misMatch.second));
|
||||||
_objMemPos.emplace(ObjectMemoryPosition{*misMatch.first, *misMatch.second});
|
_objMemPos.emplace(details::ObjectMemoryPosition{*misMatch.first, *misMatch.second});
|
||||||
fnc(*this, *misMatch.second);
|
fnc(*this, *misMatch.second);
|
||||||
_objMemPos.pop();
|
_objMemPos.pop();
|
||||||
++misMatch.first;
|
++misMatch.first;
|
||||||
@@ -183,7 +183,7 @@ namespace bitsery {
|
|||||||
writeIndexOffset(std::distance(lastChanged, end));
|
writeIndexOffset(std::distance(lastChanged, end));
|
||||||
//write old elements
|
//write old elements
|
||||||
for (auto pOld = misMatch.first; p != end && pOld != oldEnd; ++p, ++pOld) {
|
for (auto pOld = misMatch.first; p != end && pOld != oldEnd; ++p, ++pOld) {
|
||||||
_objMemPos.emplace(ObjectMemoryPosition{*pOld, *p});
|
_objMemPos.emplace(details::ObjectMemoryPosition{*pOld, *p});
|
||||||
fnc(*this, *p);
|
fnc(*this, *p);
|
||||||
_objMemPos.pop();
|
_objMemPos.pop();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,33 +25,11 @@
|
|||||||
#define BITSERY_DESERIALIZER_H
|
#define BITSERY_DESERIALIZER_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <array>
|
#include "details/serialization_common.h"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace bitsery {
|
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>
|
template<typename Reader>
|
||||||
class Deserializer {
|
class Deserializer {
|
||||||
@@ -63,6 +41,7 @@ namespace bitsery {
|
|||||||
return serialize(*this, std::forward<T>(obj));
|
return serialize(*this, std::forward<T>(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//in c++17 change "class" to typename
|
||||||
template <template <typename> class Extension, typename TValue, typename Fnc>
|
template <template <typename> class Extension, typename TValue, typename Fnc>
|
||||||
Deserializer& ext(TValue& v, Fnc&& fnc) {
|
Deserializer& ext(TValue& v, Fnc&& fnc) {
|
||||||
static_assert(!std::is_const<TValue>(), "");
|
static_assert(!std::is_const<TValue>(), "");
|
||||||
@@ -79,7 +58,7 @@ namespace bitsery {
|
|||||||
Deserializer& value(T& v) {
|
Deserializer& value(T& v) {
|
||||||
static_assert(std::numeric_limits<T>::is_iec559, "");
|
static_assert(std::numeric_limits<T>::is_iec559, "");
|
||||||
if (_isValid) {
|
if (_isValid) {
|
||||||
_isValid = _reader.template readBytes<VSIZE>(reinterpret_cast<SAME_SIZE_UNSIGNED<T>&>(v));
|
_isValid = _reader.template readBytes<VSIZE>(reinterpret_cast<details::SAME_SIZE_UNSIGNED<T>&>(v));
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -132,10 +111,10 @@ namespace bitsery {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
Deserializer& range(T& v, const RangeSpec<T>& range) {
|
Deserializer& range(T& v, const RangeSpec<T>& range) {
|
||||||
if (_isValid) {
|
if (_isValid) {
|
||||||
_isValid = _reader.template readBits(reinterpret_cast<SAME_SIZE_UNSIGNED<T>&>(v), range.bitsRequired);
|
_isValid = _reader.readBits(reinterpret_cast<details::SAME_SIZE_UNSIGNED<T>&>(v), range.bitsRequired);
|
||||||
setRangeValue(v, range);
|
details::setRangeValue(v, range);
|
||||||
if (_isValid)
|
if (_isValid)
|
||||||
_isValid = isRangeValid(v, range);
|
_isValid = details::isRangeValid(v, range);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -192,15 +171,7 @@ namespace bitsery {
|
|||||||
readSize(size, maxSize);
|
readSize(size, maxSize);
|
||||||
if (_isValid) {
|
if (_isValid) {
|
||||||
str.resize(size);
|
str.resize(size);
|
||||||
if (size) {
|
procContainer<VSIZE>(std::begin(str), std::end(str), std::true_type{});
|
||||||
//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;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -210,7 +181,9 @@ namespace bitsery {
|
|||||||
size_t size;
|
size_t size;
|
||||||
readSize(size, N-1);
|
readSize(size, N-1);
|
||||||
if (_isValid) {
|
if (_isValid) {
|
||||||
_isValid = _reader.template readBuffer<VSIZE>(str, size);
|
auto first = std::begin(str);
|
||||||
|
procContainer<VSIZE>(first, std::next(first, size), std::true_type{});
|
||||||
|
//null-terminated string
|
||||||
str[size] = {};
|
str[size] = {};
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
@@ -226,11 +199,7 @@ namespace bitsery {
|
|||||||
readSize(size, maxSize);
|
readSize(size, maxSize);
|
||||||
if (_isValid) {
|
if (_isValid) {
|
||||||
obj.resize(size);
|
obj.resize(size);
|
||||||
for (auto& v:obj) {
|
procContainer(std::begin(obj), std::end(obj), std::forward<Fnc>(fnc));
|
||||||
if (_isValid)
|
|
||||||
fnc(*this, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -241,10 +210,7 @@ namespace bitsery {
|
|||||||
readSize(size, maxSize);
|
readSize(size, maxSize);
|
||||||
if (_isValid) {
|
if (_isValid) {
|
||||||
obj.resize(size);
|
obj.resize(size);
|
||||||
for (auto& v: obj) {
|
procContainer<VSIZE>(std::begin(obj), std::end(obj), std::false_type{});
|
||||||
if (_isValid)
|
|
||||||
value<VSIZE>(v);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -255,10 +221,7 @@ namespace bitsery {
|
|||||||
readSize(size, maxSize);
|
readSize(size, maxSize);
|
||||||
if (_isValid) {
|
if (_isValid) {
|
||||||
obj.resize(size);
|
obj.resize(size);
|
||||||
for (auto& v: obj) {
|
procContainer(std::begin(obj), std::end(obj));
|
||||||
if (_isValid)
|
|
||||||
object(v);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -271,27 +234,19 @@ namespace bitsery {
|
|||||||
|
|
||||||
template<typename T, size_t N, typename Fnc>
|
template<typename T, size_t N, typename Fnc>
|
||||||
Deserializer& array(std::array<T,N> &arr, Fnc && fnc) {
|
Deserializer& array(std::array<T,N> &arr, Fnc && fnc) {
|
||||||
for (auto& v: arr)
|
procContainer(std::begin(arr), std::end(arr), std::forward<Fnc>(fnc));
|
||||||
if (_isValid)
|
|
||||||
fnc(*this, v);
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t VSIZE, typename T, size_t N>
|
template<size_t VSIZE, typename T, size_t N>
|
||||||
Deserializer& array(std::array<T,N> &arr) {
|
Deserializer& array(std::array<T,N> &arr) {
|
||||||
for (auto& v: arr) {
|
procContainer<VSIZE>(std::begin(arr), std::end(arr), std::true_type{});
|
||||||
if (_isValid)
|
|
||||||
value<VSIZE>(v);
|
|
||||||
}
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
Deserializer& array(std::array<T,N> &arr) {
|
Deserializer& array(std::array<T,N> &arr) {
|
||||||
for (auto& v: arr) {
|
procContainer(std::begin(arr), std::end(arr));
|
||||||
if (_isValid)
|
|
||||||
object(v);
|
|
||||||
}
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,23 +254,19 @@ namespace bitsery {
|
|||||||
|
|
||||||
template<typename T, size_t N, typename Fnc>
|
template<typename T, size_t N, typename Fnc>
|
||||||
Deserializer& array(T (&arr)[N], Fnc&& fnc) {
|
Deserializer& array(T (&arr)[N], Fnc&& fnc) {
|
||||||
T* end = arr + N;
|
procContainer(std::begin(arr), std::end(arr), std::forward<Fnc>(fnc));
|
||||||
for (T* it= arr; it != end; ++it) {
|
|
||||||
if (_isValid)
|
|
||||||
fnc(*this, *it);
|
|
||||||
}
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t VSIZE, typename T, size_t N>
|
template<size_t VSIZE, typename T, size_t N>
|
||||||
Deserializer& array(T (&arr)[N]) {
|
Deserializer& array(T (&arr)[N]) {
|
||||||
procCArray<VSIZE>(arr);
|
procContainer<VSIZE>(std::begin(arr), std::end(arr), std::true_type{});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
Deserializer& array(T (&arr)[N]) {
|
Deserializer& array(T (&arr)[N]) {
|
||||||
procCArray<0>(arr);
|
procContainer(std::begin(arr), std::end(arr));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
bool isValid() const {
|
bool isValid() const {
|
||||||
@@ -390,45 +341,34 @@ namespace bitsery {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template< size_t VSIZE, typename TIterator>
|
//process value types
|
||||||
void procContainerValues(TIterator begin, TIterator end) {
|
//false_type means that we must process all elements individually
|
||||||
for (auto it = begin; it != end; ++it) {
|
template<size_t VSIZE, typename It>
|
||||||
if (_isValid)
|
void procContainer(It first, It last, std::false_type) {
|
||||||
value<VSIZE>(*it);
|
for (;_isValid && first != last; ++first)
|
||||||
}
|
value<VSIZE>(*first);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TIterator>
|
//process value types
|
||||||
void procContainerValues(TIterator begin, TIterator end) {
|
//true_type means, that we can copy whole buffer
|
||||||
for (auto it = begin; it != end; ++it) {
|
template<size_t VSIZE, typename It>
|
||||||
if (_isValid)
|
void procContainer(It first, It last, std::true_type) {
|
||||||
object(*it);
|
if (_isValid && first != last)
|
||||||
}
|
_isValid = _reader.template readBuffer<VSIZE>(&(*first), std::distance(first, last));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//process by calling functions
|
||||||
template <size_t VSIZE, typename T>
|
template<typename It, typename Fnc>
|
||||||
void procContainer(T&& obj) {
|
void procContainer(It first, It last, Fnc fnc) {
|
||||||
//todo could be improved for arithmetic types in contiguous containers (std::vector, std::array) (keep in mind std::vector<bool> specialization)
|
for (;_isValid && first != last; ++first)
|
||||||
for (auto& v: obj)
|
fnc(*this, *first);
|
||||||
if (_isValid)
|
|
||||||
ProcessAnyType<VSIZE>::serialize(*this, v);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <size_t VSIZE, typename T, size_t N>
|
//process object types
|
||||||
void procCArray(T (&arr)[N]) {
|
template<typename It>
|
||||||
//todo could be improved for arithmetic types
|
void procContainer(It first, It last) {
|
||||||
T* end = arr + N;
|
for (;_isValid && first != last; ++first)
|
||||||
for (T* it = arr; it != end; ++it)
|
object(*first);
|
||||||
if (_isValid)
|
|
||||||
ProcessAnyType<VSIZE>::serialize(*this, *it);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Iterator, typename Fnc>
|
|
||||||
void procCont(Iterator begin, Iterator end, Fnc&& fnc) {
|
|
||||||
for (Iterator it = begin; it != end; ++it)
|
|
||||||
if (_isValid)
|
|
||||||
fnc(*it);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
96
include/bitsery/details/buffer_common.h
Normal file
96
include/bitsery/details/buffer_common.h
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
//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_COMMON_H
|
||||||
|
#define BITSERY_BUFFER_COMMON_H
|
||||||
|
|
||||||
|
namespace bitsery {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* endianess
|
||||||
|
*/
|
||||||
|
enum class EndiannessType {
|
||||||
|
LittleEndian,
|
||||||
|
BigEndian
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
template<typename T>
|
||||||
|
constexpr size_t BITS_SIZE = sizeof(T) << 3;
|
||||||
|
|
||||||
|
//add swap functions to class, to avoid compilation warning about unused functions
|
||||||
|
struct swapImpl {
|
||||||
|
static uint64_t exec(uint64_t value) {
|
||||||
|
#ifdef __GNUC__
|
||||||
|
return __builtin_bswap64( value );
|
||||||
|
#else
|
||||||
|
value = ( value & 0x00000000FFFFFFFF ) << 32 | ( value & 0xFFFFFFFF00000000 ) >> 32;
|
||||||
|
value = ( value & 0x0000FFFF0000FFFF ) << 16 | ( value & 0xFFFF0000FFFF0000 ) >> 16;
|
||||||
|
value = ( value & 0x00FF00FF00FF00FF ) << 8 | ( value & 0xFF00FF00FF00FF00 ) >> 8;
|
||||||
|
return value;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t exec(uint32_t value)
|
||||||
|
{
|
||||||
|
#ifdef __GNUC__
|
||||||
|
return __builtin_bswap32( value );
|
||||||
|
#else
|
||||||
|
return ( value & 0x000000ff ) << 24 | ( value & 0x0000ff00 ) << 8 | ( value & 0x00ff0000 ) >> 8 | ( value & 0xff000000 ) >> 24;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t exec(uint16_t value)
|
||||||
|
{
|
||||||
|
return ( value & 0x00ff ) << 8 | ( value & 0xff00 ) >> 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t exec(uint8_t value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TValue>
|
||||||
|
TValue swap(TValue value) {
|
||||||
|
constexpr size_t TSize = sizeof(TValue);
|
||||||
|
using UT = typename std::conditional<TSize == 1, uint8_t,
|
||||||
|
typename std::conditional<TSize == 2, uint16_t,
|
||||||
|
typename std::conditional<TSize == 4, uint32_t , uint64_t>::type>::type>::type;
|
||||||
|
return swapImpl::exec(static_cast<UT>(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
//add test data in separate struct, because some compilers only support constexpr functions with return-only body
|
||||||
|
struct EndiannessTestData {
|
||||||
|
static constexpr uint32_t _sample4Bytes = 0x01020304;
|
||||||
|
static constexpr uint8_t _sample1stByte = (const uint8_t&)_sample4Bytes;
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr EndiannessType getSystemEndianness() {
|
||||||
|
static_assert(EndiannessTestData::_sample1stByte == 0x04 || EndiannessTestData::_sample1stByte == 0x01, "system must be either little or big endian");
|
||||||
|
return EndiannessTestData::_sample1stByte == 0x04 ? EndiannessType::LittleEndian : EndiannessType::BigEndian;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BITSERY_BUFFER_COMMON_H
|
||||||
239
include/bitsery/details/serialization_common.h
Normal file
239
include/bitsery/details/serialization_common.h
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
//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_SERIALIZATION_COMMON_H
|
||||||
|
#define BITSERY_SERIALIZATION_COMMON_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
namespace bitsery {
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
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 <typename T>
|
||||||
|
constexpr size_t getSize(T v, size_t s) {
|
||||||
|
return v > 0 ? getSize(v / 2, s + 1) : s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr size_t calcRequiredBits(T min, T max) {
|
||||||
|
//call recursive function, because some compilers only support constexpr functions with return-only body
|
||||||
|
return getSize(max - min, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* range functions in bitsery namespace because these are used by user
|
||||||
|
*/
|
||||||
|
|
||||||
|
template<typename T, typename Enable = void>
|
||||||
|
struct RangeSpec {
|
||||||
|
|
||||||
|
constexpr RangeSpec(T minValue, T maxValue)
|
||||||
|
: min{minValue},
|
||||||
|
max{maxValue},
|
||||||
|
bitsRequired{details::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{details::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{details::calcRequiredBits<details::SAME_SIZE_UNSIGNED<T>>({}, ((max - min) / precision))} {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const T min;
|
||||||
|
const T max;
|
||||||
|
const size_t bitsRequired;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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);
|
||||||
|
};
|
||||||
|
|
||||||
|
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 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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_SERIALIZATION_COMMON_H
|
||||||
@@ -27,49 +27,10 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include "details/serialization_common.h"
|
||||||
|
|
||||||
namespace bitsery {
|
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>
|
template<typename Writter>
|
||||||
class Serializer {
|
class Serializer {
|
||||||
public:
|
public:
|
||||||
@@ -80,6 +41,7 @@ namespace bitsery {
|
|||||||
return serialize(*this, obj);
|
return serialize(*this, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//in c++17 change "class" to typename
|
||||||
template <template <typename> class Extension, typename TValue, typename Fnc>
|
template <template <typename> class Extension, typename TValue, typename Fnc>
|
||||||
Serializer& ext(const TValue& v, Fnc&& fnc ) {
|
Serializer& ext(const TValue& v, Fnc&& fnc ) {
|
||||||
Extension<const TValue> ext{v};
|
Extension<const TValue> ext{v};
|
||||||
@@ -95,7 +57,7 @@ namespace bitsery {
|
|||||||
template<size_t VSIZE, typename T, typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
|
template<size_t VSIZE, typename T, typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
|
||||||
Serializer& value(const T &v) {
|
Serializer& value(const T &v) {
|
||||||
static_assert(std::numeric_limits<T>::is_iec559, "");
|
static_assert(std::numeric_limits<T>::is_iec559, "");
|
||||||
_writter.template writeBytes<VSIZE>(reinterpret_cast<const SAME_SIZE_UNSIGNED<T> &>(v));
|
_writter.template writeBytes<VSIZE>(reinterpret_cast<const details::SAME_SIZE_UNSIGNED<T> &>(v));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +78,7 @@ namespace bitsery {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Serializer& boolBit(bool v) {
|
Serializer& boolBit(bool v) {
|
||||||
_writter.template writeBits(static_cast<unsigned char>(v ? 1 : 0), 1);
|
_writter.writeBits(static_cast<unsigned char>(v ? 1 : 0), 1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,8 +93,8 @@ namespace bitsery {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Serializer& range(const T &v, const RangeSpec<T> &range) {
|
Serializer& range(const T &v, const RangeSpec<T> &range) {
|
||||||
assert(isRangeValid(v, range));
|
assert(details::isRangeValid(v, range));
|
||||||
_writter.template writeBits(getRangeValue(v, range), range.bitsRequired);
|
_writter.template writeBits<decltype(details::getRangeValue(v, range))>(details::getRangeValue(v, range), range.bitsRequired);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +103,7 @@ namespace bitsery {
|
|||||||
*/
|
*/
|
||||||
template<typename T, size_t N, typename Fnc>
|
template<typename T, size_t N, typename Fnc>
|
||||||
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues, Fnc &&fnc) {
|
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues, Fnc &&fnc) {
|
||||||
auto index = findSubstitutionIndex(v, expectedValues);
|
auto index = details::findSubstitutionIndex(v, expectedValues);
|
||||||
range(index, {{}, N +1});
|
range(index, {{}, N +1});
|
||||||
if (!index)
|
if (!index)
|
||||||
fnc(*this, v);
|
fnc(*this, v);
|
||||||
@@ -150,7 +112,7 @@ namespace bitsery {
|
|||||||
|
|
||||||
template<size_t VSIZE, typename T, size_t N>
|
template<size_t VSIZE, typename T, size_t N>
|
||||||
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues) {
|
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues) {
|
||||||
auto index = findSubstitutionIndex(v, expectedValues);
|
auto index = details::findSubstitutionIndex(v, expectedValues);
|
||||||
range(index, {{}, N +1});
|
range(index, {{}, N +1});
|
||||||
if (!index)
|
if (!index)
|
||||||
value<VSIZE>(v);
|
value<VSIZE>(v);
|
||||||
@@ -159,7 +121,7 @@ namespace bitsery {
|
|||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues) {
|
Serializer& substitution(const T &v, const std::array<T, N> &expectedValues) {
|
||||||
auto index = findSubstitutionIndex(v, expectedValues);
|
auto index = details::findSubstitutionIndex(v, expectedValues);
|
||||||
range(index, {{}, N +1});
|
range(index, {{}, N +1});
|
||||||
if (!index)
|
if (!index)
|
||||||
object(v);
|
object(v);
|
||||||
@@ -173,13 +135,19 @@ namespace bitsery {
|
|||||||
template<size_t VSIZE, typename T>
|
template<size_t VSIZE, typename T>
|
||||||
Serializer& text(const std::basic_string<T> &str, size_t maxSize) {
|
Serializer& text(const std::basic_string<T> &str, size_t maxSize) {
|
||||||
assert(str.size() <= maxSize);
|
assert(str.size() <= maxSize);
|
||||||
procText<VSIZE>(str.data(), str.size());
|
auto first = std::begin(str);
|
||||||
|
auto last = std::end(str);
|
||||||
|
writeSize(std::distance(first, last));
|
||||||
|
procContainer<VSIZE>(first, last, std::true_type{});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t VSIZE, typename T, size_t N>
|
template<size_t VSIZE, typename T, size_t N>
|
||||||
Serializer& text(const T (&str)[N]) {
|
Serializer& text(const T (&str)[N]) {
|
||||||
procText<VSIZE>(str, std::min(std::char_traits<T>::length(str), N - 1));
|
auto first = std::begin(str);
|
||||||
|
auto last = std::next(first, std::min(std::char_traits<T>::length(str), N - 1));
|
||||||
|
writeSize(std::distance(first, last));
|
||||||
|
procContainer<VSIZE>(first, last, std::true_type{});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,8 +159,7 @@ namespace bitsery {
|
|||||||
Serializer& container(const T &obj, size_t maxSize, Fnc &&fnc) {
|
Serializer& container(const T &obj, size_t maxSize, Fnc &&fnc) {
|
||||||
assert(obj.size() <= maxSize);
|
assert(obj.size() <= maxSize);
|
||||||
writeSize(obj.size());
|
writeSize(obj.size());
|
||||||
for (auto &v: obj)
|
procContainer(std::begin(obj), std::end(obj), std::forward<Fnc>(fnc));
|
||||||
fnc(*this, v);
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +168,8 @@ namespace bitsery {
|
|||||||
static_assert(VSIZE > 0, "");
|
static_assert(VSIZE > 0, "");
|
||||||
assert(obj.size() <= maxSize);
|
assert(obj.size() <= maxSize);
|
||||||
writeSize(obj.size());
|
writeSize(obj.size());
|
||||||
procContainer<VSIZE>(obj);
|
//todo optimisation is possible for contigous containers, but currently there is no compile-time check for this
|
||||||
|
procContainer<VSIZE>(std::begin(obj), std::end(obj), std::false_type{});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +177,7 @@ namespace bitsery {
|
|||||||
Serializer& container(const T &obj, size_t maxSize) {
|
Serializer& container(const T &obj, size_t maxSize) {
|
||||||
assert(obj.size() <= maxSize);
|
assert(obj.size() <= maxSize);
|
||||||
writeSize(obj.size());
|
writeSize(obj.size());
|
||||||
procContainer<0>(obj);
|
procContainer(std::begin(obj), std::end(obj));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,21 +189,20 @@ namespace bitsery {
|
|||||||
|
|
||||||
template<typename T, size_t N, typename Fnc>
|
template<typename T, size_t N, typename Fnc>
|
||||||
Serializer& array(const std::array<T, N> &arr, Fnc &&fnc) {
|
Serializer& array(const std::array<T, N> &arr, Fnc &&fnc) {
|
||||||
for (auto &v: arr)
|
procContainer(std::begin(arr), std::end(arr), std::forward<Fnc>(fnc));
|
||||||
fnc(*this, v);
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t VSIZE, typename T, size_t N>
|
template<size_t VSIZE, typename T, size_t N>
|
||||||
Serializer& array(const std::array<T, N> &arr) {
|
Serializer& array(const std::array<T, N> &arr) {
|
||||||
static_assert(VSIZE > 0, "");
|
static_assert(VSIZE > 0, "");
|
||||||
procContainer<VSIZE>(arr);
|
procContainer<VSIZE>(std::begin(arr), std::end(arr), std::true_type{});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
Serializer& array(const std::array<T, N> &arr) {
|
Serializer& array(const std::array<T, N> &arr) {
|
||||||
procContainer<0>(arr);
|
procContainer(std::begin(arr), std::end(arr));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,22 +210,20 @@ namespace bitsery {
|
|||||||
|
|
||||||
template<typename T, size_t N, typename Fnc>
|
template<typename T, size_t N, typename Fnc>
|
||||||
Serializer& array(const T (&arr)[N], Fnc &&fnc) {
|
Serializer& array(const T (&arr)[N], Fnc &&fnc) {
|
||||||
const T *end = arr + N;
|
procContainer(std::begin(arr), std::end(arr), std::forward<Fnc>(fnc));
|
||||||
for (const T *tmp = arr; tmp != end; ++tmp)
|
|
||||||
fnc(*this, *tmp);
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t VSIZE, typename T, size_t N>
|
template<size_t VSIZE, typename T, size_t N>
|
||||||
Serializer& array(const T (&arr)[N]) {
|
Serializer& array(const T (&arr)[N]) {
|
||||||
static_assert(VSIZE > 0, "");
|
static_assert(VSIZE > 0, "");
|
||||||
procCArray<VSIZE>(arr);
|
procContainer<VSIZE>(std::begin(arr), std::end(arr), std::true_type{});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, size_t N>
|
template<typename T, size_t N>
|
||||||
Serializer& array(const T (&arr)[N]) {
|
Serializer& array(const T (&arr)[N]) {
|
||||||
procCArray<0>(arr);
|
procContainer(std::begin(arr), std::end(arr));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,27 +294,36 @@ namespace bitsery {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t VSIZE, typename T>
|
//process value types
|
||||||
void procContainer(T &&obj) {
|
//false_type means that we must process all elements individually
|
||||||
//todo could be improved for arithmetic types in contiguous containers (std::vector, std::array) (keep in mind std::vector<bool> specialization)
|
template<size_t VSIZE, typename It>
|
||||||
for (auto &v: obj)
|
void procContainer(It first, It last, std::false_type) {
|
||||||
ProcessAnyType<VSIZE>::serialize(*this, v);
|
for (;first != last; ++first)
|
||||||
|
value<VSIZE>(*first);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<size_t VSIZE, typename T, size_t N>
|
//process value types
|
||||||
void procCArray(T (&arr)[N]) {
|
//true_type means, that we can copy whole buffer
|
||||||
//todo could be improved for arithmetic types
|
template<size_t VSIZE, typename It>
|
||||||
const T *end = arr + N;
|
void procContainer(It first, It last, std::true_type) {
|
||||||
for (const T *it = arr; it != end; ++it)
|
if (first != last)
|
||||||
ProcessAnyType<VSIZE>::serialize(*this, *it);
|
_writter.template writeBuffer<VSIZE>(&(*first), std::distance(first, last));
|
||||||
|
};
|
||||||
|
|
||||||
|
//process by calling functions
|
||||||
|
template<typename It, typename Fnc>
|
||||||
|
void procContainer(It first, It last, Fnc fnc) {
|
||||||
|
for (;first != last; ++first)
|
||||||
|
fnc(*this, *first);
|
||||||
|
};
|
||||||
|
|
||||||
|
//process object types
|
||||||
|
template<typename It>
|
||||||
|
void procContainer(It first, It last) {
|
||||||
|
for (;first != last; ++first)
|
||||||
|
object(*first);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<size_t VSIZE, typename T>
|
|
||||||
void procText(const T *str, size_t size) {
|
|
||||||
writeSize(size);
|
|
||||||
if (size)
|
|
||||||
_writter.template writeBuffer<VSIZE>(str, size);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,77 +21,54 @@
|
|||||||
#SOFTWARE.
|
#SOFTWARE.
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.2)
|
cmake_minimum_required(VERSION 3.2)
|
||||||
project(${TEST_PROJECT_NAME} C CXX)
|
set(TestProjectName bitsery_tests)
|
||||||
|
project(${TestProjectName} C CXX)
|
||||||
|
|
||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
#add googletest external project
|
#add googletest external project
|
||||||
#USE_GMOCK enable gmock
|
#USE_GMOCK enable gmock
|
||||||
#exports variables GTEST_INCLUDE_DIRS, GTEST_LIBS_DIR, GTEST_LIBNAME, GTEST_MAIN_LIBNAME
|
#exports variables GTEST_INCLUDE_DIRS, GTEST_LIBS_DIR, GTEST_LIBNAME, GTEST_MAIN_LIBNAME
|
||||||
set(EXT_PROJECTS_DIR ${CMAKE_SOURCE_DIR}/ext)
|
set(ExtCMakeFilesDir ${CMAKE_SOURCE_DIR}/ext)
|
||||||
set(USE_GMOCK ON)
|
set(UseGMock ON)
|
||||||
add_subdirectory(${EXT_PROJECTS_DIR}/gtest ${CMAKE_BINARY_DIR}/gtest)
|
add_subdirectory(${ExtCMakeFilesDir}/gtest ${CMAKE_BINARY_DIR}/gtest)
|
||||||
|
|
||||||
|
#this helps idea to know which files are actually used
|
||||||
|
file(GLOB_RECURSE IncludeHeaders ${CMAKE_SOURCE_DIR}/include/bitsery/*.h)
|
||||||
# set common include folder for module
|
# set common include folder for module
|
||||||
include_directories(SYSTEM ${GTEST_INCLUDE_DIRS})
|
include_directories(SYSTEM ${GTestIncludeDirs})
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||||
|
|
||||||
file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
file(GLOB TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||||
|
|
||||||
FOREACH(TEST_PROJECT_FILE ${TEST_SRC_FILES})
|
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
||||||
get_filename_component(SEPARATE_TEST_NAME ${TEST_PROJECT_FILE} NAME_WE)
|
message(WARNING "extension tests for optional is disable for VS, because VS currenty doesn't have <optional>")
|
||||||
set(SEPARATE_TEST_NAME TEST_${SEPARATE_TEST_NAME})
|
list(REMOVE_ITEM TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/serialization_ext_optional.cpp)
|
||||||
add_executable(${SEPARATE_TEST_NAME} ${TEST_PROJECT_FILE})
|
endif()
|
||||||
add_dependencies(${SEPARATE_TEST_NAME} googletest)
|
|
||||||
|
|
||||||
set_property(TARGET ${SEPARATE_TEST_NAME} PROPERTY CXX_STANDARD 14)
|
include(${ExtCMakeFilesDir}/LinkTestLib.cmake)
|
||||||
set_property(TARGET ${SEPARATE_TEST_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
|
||||||
|
|
||||||
|
FOREACH(TestFile ${TestSourceFiles})
|
||||||
|
get_filename_component(TestName ${TestFile} NAME_WE)
|
||||||
|
set(TestName TEST_${TestName})
|
||||||
|
add_executable(${TestName} ${TestFile} ${IncludeHeaders})
|
||||||
|
LinkTestLib(${TestName})
|
||||||
|
|
||||||
if(NOT WIN32 OR MINGW)
|
add_test(NAME ${TestName} COMMAND $<TARGET_FILE:${TestName}>)
|
||||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
|
||||||
target_link_libraries(${SEPARATE_TEST_NAME} ${GTEST_LIBS_DIR}/lib${LIBNAME}.a )
|
|
||||||
ENDFOREACH()
|
|
||||||
else()
|
|
||||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
|
||||||
target_link_libraries(${SEPARATE_TEST_NAME}
|
|
||||||
debug ${GTEST_LIBS_DIR}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
|
|
||||||
optimized ${GTEST_LIBS_DIR}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES})
|
|
||||||
ENDFOREACH()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_link_libraries(${SEPARATE_TEST_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
|
||||||
|
|
||||||
add_test(NAME ${SEPARATE_TEST_NAME} COMMAND $<TARGET_FILE:${SEPARATE_TEST_NAME}>)
|
|
||||||
|
|
||||||
ENDFOREACH()
|
ENDFOREACH()
|
||||||
|
|
||||||
#all in one tests for code coverage
|
#all in one tests for code coverage
|
||||||
add_executable(${TEST_PROJECT_NAME} ${TEST_SRC_FILES})
|
add_executable(${TestProjectName} ${TestSourceFiles})
|
||||||
add_dependencies(${TEST_PROJECT_NAME} googletest)
|
|
||||||
|
|
||||||
set_property(TARGET ${TEST_PROJECT_NAME} PROPERTY CXX_STANDARD 14)
|
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||||
set_property(TARGET ${TEST_PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
include(${ExtCMakeFilesDir}/CodeCoverage.cmake)
|
||||||
|
target_compile_options(${TestProjectName} PUBLIC -O0 -fprofile-arcs -ftest-coverage)
|
||||||
|
target_link_libraries(${TestProjectName} -O0 -fprofile-arcs -ftest-coverage)
|
||||||
if(NOT WIN32 OR MINGW)
|
setup_target_for_coverage(tests_coverage ${TestProjectName} coverage)
|
||||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
|
||||||
target_link_libraries(${TEST_PROJECT_NAME} ${GTEST_LIBS_DIR}/lib${LIBNAME}.a )
|
|
||||||
ENDFOREACH()
|
|
||||||
else()
|
|
||||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
|
||||||
target_link_libraries(${TEST_PROJECT_NAME}
|
|
||||||
debug ${GTEST_LIBS_DIR}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
|
|
||||||
optimized ${GTEST_LIBS_DIR}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES})
|
|
||||||
ENDFOREACH()
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(${TEST_PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
LinkTestLib(${TestProjectName})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,13 +24,13 @@
|
|||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
#include <bitsery/buffer_writer.h>
|
#include <bitsery/buffer_writer.h>
|
||||||
#include <bitsery/buffer_reader.h>
|
#include <bitsery/buffer_reader.h>
|
||||||
#include <list>
|
#include <bitsery/details/serialization_common.h>
|
||||||
#include <bitset>
|
|
||||||
|
|
||||||
using testing::Eq;
|
using testing::Eq;
|
||||||
using testing::ContainerEq;
|
using testing::ContainerEq;
|
||||||
using bitsery::BufferWriter;
|
using bitsery::BufferWriter;
|
||||||
using bitsery::BufferReader;
|
using bitsery::BufferReader;
|
||||||
|
using Buffer = std::vector<bitsery::DefaultConfig::BufferValueType>;
|
||||||
|
|
||||||
struct IntegralUnsignedTypes {
|
struct IntegralUnsignedTypes {
|
||||||
uint32_t a;
|
uint32_t a;
|
||||||
@@ -40,23 +40,29 @@ struct IntegralUnsignedTypes {
|
|||||||
uint64_t e;
|
uint64_t e;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr size_t getBits(T v) {
|
||||||
|
return bitsery::details::calcRequiredBits<T>({}, v);
|
||||||
|
};
|
||||||
|
|
||||||
TEST(BufferBitsOperations, WriteAndReadBits) {
|
TEST(BufferBitsOperations, WriteAndReadBits) {
|
||||||
//setup data
|
//setup data
|
||||||
IntegralUnsignedTypes data;
|
constexpr IntegralUnsignedTypes data{
|
||||||
data.a = 485454;//bits 19
|
485454,//bits 19
|
||||||
data.b = 45978;//bits 16
|
45978,//bits 16
|
||||||
data.c = 0;//bits 1
|
0,//bits 1
|
||||||
data.d = 36;//bits 6
|
36,//bits 6
|
||||||
data.e = 479845648946;//bits 39
|
479845648946//bits 39
|
||||||
|
};
|
||||||
|
|
||||||
constexpr size_t aBITS = 21;
|
constexpr size_t aBITS = getBits(data.a) + 2;
|
||||||
constexpr size_t bBITS = 16;
|
constexpr size_t bBITS = getBits(data.b) + 0;
|
||||||
constexpr size_t cBITS = 5;
|
constexpr size_t cBITS = getBits(data.c) + 2;
|
||||||
constexpr size_t dBITS = 7;
|
constexpr size_t dBITS = getBits(data.d) + 1;
|
||||||
constexpr size_t eBITS = 40;
|
constexpr size_t eBITS = getBits(data.e) + 8;
|
||||||
|
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf;
|
Buffer buf;
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
bw.writeBits(data.a, aBITS);
|
bw.writeBits(data.a, aBITS);
|
||||||
@@ -87,7 +93,7 @@ TEST(BufferBitsOperations, WriteAndReadBits) {
|
|||||||
|
|
||||||
TEST(BufferBitsOperations, WhenFinishedFlushWriter) {
|
TEST(BufferBitsOperations, WhenFinishedFlushWriter) {
|
||||||
|
|
||||||
std::vector<uint8_t> buf;
|
Buffer buf;
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
bw.writeBits(3u, 2);
|
bw.writeBits(3u, 2);
|
||||||
@@ -101,7 +107,7 @@ TEST(BufferBitsOperations, BufferSizeIsCountedPerByteNotPerBit) {
|
|||||||
//setup data
|
//setup data
|
||||||
|
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf;
|
Buffer buf;
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
bw.writeBits(7u,3);
|
bw.writeBits(7u,3);
|
||||||
@@ -110,7 +116,7 @@ TEST(BufferBitsOperations, BufferSizeIsCountedPerByteNotPerBit) {
|
|||||||
|
|
||||||
//read from buffer
|
//read from buffer
|
||||||
BufferReader br{buf};
|
BufferReader br{buf};
|
||||||
unsigned tmp;
|
uint16_t tmp;
|
||||||
EXPECT_THAT(br.readBits(tmp,4), Eq(true));
|
EXPECT_THAT(br.readBits(tmp,4), Eq(true));
|
||||||
EXPECT_THAT(br.readBits(tmp,2), Eq(true));
|
EXPECT_THAT(br.readBits(tmp,2), Eq(true));
|
||||||
EXPECT_THAT(br.readBits(tmp,2), Eq(true));
|
EXPECT_THAT(br.readBits(tmp,2), Eq(true));
|
||||||
@@ -126,11 +132,44 @@ TEST(BufferBitsOperations, BufferSizeIsCountedPerByteNotPerBit) {
|
|||||||
EXPECT_THAT(br2.readBits(tmp,9), Eq(false));
|
EXPECT_THAT(br2.readBits(tmp,9), Eq(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(BufferBitsOperations, ConsecutiveCallsToAlignHasNoEffect) {
|
||||||
|
Buffer buf;
|
||||||
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
|
bw.writeBits(3u, 2);
|
||||||
|
//3 calls to align after 1st data
|
||||||
|
bw.align();
|
||||||
|
bw.align();
|
||||||
|
bw.align();
|
||||||
|
bw.writeBits(7u, 3);
|
||||||
|
//1 call to align after 2nd data
|
||||||
|
bw.align();
|
||||||
|
bw.writeBits(15u, 4);
|
||||||
|
bw.flush();
|
||||||
|
|
||||||
|
unsigned char tmp;
|
||||||
|
BufferReader br{buf};
|
||||||
|
EXPECT_THAT(br.readBits(tmp,2), Eq(true));
|
||||||
|
EXPECT_THAT(tmp, Eq(3u));
|
||||||
|
EXPECT_THAT(br.align(), Eq(true));
|
||||||
|
|
||||||
|
EXPECT_THAT(br.readBits(tmp,3), Eq(true));
|
||||||
|
EXPECT_THAT(tmp, Eq(7u));
|
||||||
|
EXPECT_THAT(br.align(), Eq(true));
|
||||||
|
EXPECT_THAT(br.align(), Eq(true));
|
||||||
|
EXPECT_THAT(br.align(), Eq(true));
|
||||||
|
|
||||||
|
EXPECT_THAT(br.readBits(tmp,4), Eq(true));
|
||||||
|
EXPECT_THAT(tmp, Eq(15u));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST(BufferBitsOperations, WhenAlignedFlushHasNoEffect) {
|
TEST(BufferBitsOperations, WhenAlignedFlushHasNoEffect) {
|
||||||
//setup data
|
//setup data
|
||||||
|
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf;
|
Buffer buf;
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
bw.writeBits(3u, 2);
|
bw.writeBits(3u, 2);
|
||||||
@@ -140,11 +179,12 @@ TEST(BufferBitsOperations, WhenAlignedFlushHasNoEffect) {
|
|||||||
EXPECT_THAT(std::distance(buf.begin(), buf.end()), Eq(1));
|
EXPECT_THAT(std::distance(buf.begin(), buf.end()), Eq(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST(BufferBitsOperations, AlignMustWriteZerosBits) {
|
TEST(BufferBitsOperations, AlignMustWriteZerosBits) {
|
||||||
//setup data
|
//setup data
|
||||||
|
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf;
|
Buffer buf;
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
//write 2 bits and align
|
//write 2 bits and align
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ using testing::Eq;
|
|||||||
using testing::ContainerEq;
|
using testing::ContainerEq;
|
||||||
using bitsery::BufferWriter;
|
using bitsery::BufferWriter;
|
||||||
using bitsery::BufferReader;
|
using bitsery::BufferReader;
|
||||||
|
using Buffer = std::vector<bitsery::DefaultConfig::BufferValueType>;
|
||||||
|
|
||||||
struct IntegralTypes {
|
struct IntegralTypes {
|
||||||
int64_t a;
|
int64_t a;
|
||||||
@@ -68,7 +69,7 @@ TEST(BufferBytesOperations, WriteAndReadBytes) {
|
|||||||
//setup data
|
//setup data
|
||||||
auto data =getInitializedIntegralTypes();
|
auto data =getInitializedIntegralTypes();
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf{};
|
Buffer buf{};
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
writeIntegralTypesToBuffer(bw, data);
|
writeIntegralTypesToBuffer(bw, data);
|
||||||
|
|
||||||
@@ -98,7 +99,7 @@ TEST(BufferBytesOperations, BufferReaderUsingDataPlusSizeCtor) {
|
|||||||
//setup data
|
//setup data
|
||||||
auto data =getInitializedIntegralTypes();
|
auto data =getInitializedIntegralTypes();
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf{};
|
Buffer buf{};
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
writeIntegralTypesToBuffer(bw, data);
|
writeIntegralTypesToBuffer(bw, data);
|
||||||
|
|
||||||
@@ -127,7 +128,7 @@ TEST(BufferBytesOperations, BufferReaderUsingCArrayCtor) {
|
|||||||
//setup data
|
//setup data
|
||||||
auto data =getInitializedIntegralTypes();
|
auto data =getInitializedIntegralTypes();
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf{};
|
Buffer buf{};
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
writeIntegralTypesToBuffer(bw, data);
|
writeIntegralTypesToBuffer(bw, data);
|
||||||
|
|
||||||
@@ -161,7 +162,7 @@ TEST(BufferBytesOperations, ReadReturnsFalseIfNotEnoughBufferSize) {
|
|||||||
uint8_t a = 111;
|
uint8_t a = 111;
|
||||||
|
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf{};
|
Buffer buf{};
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
bw.writeBytes<1>(a);
|
bw.writeBytes<1>(a);
|
||||||
@@ -190,7 +191,7 @@ TEST(BufferBytesOperations, ReadIsCompletedWhenAllBytesAreRead) {
|
|||||||
data.d = 200;
|
data.d = 200;
|
||||||
|
|
||||||
//create and write to buffer
|
//create and write to buffer
|
||||||
std::vector<uint8_t> buf{};
|
Buffer buf{};
|
||||||
BufferWriter bw{buf};
|
BufferWriter bw{buf};
|
||||||
|
|
||||||
bw.writeBytes<4>(data.b);
|
bw.writeBytes<4>(data.b);
|
||||||
@@ -217,3 +218,57 @@ TEST(BufferBytesOperations, ReadIsCompletedWhenAllBytesAreRead) {
|
|||||||
EXPECT_THAT(br1.isCompleted(), Eq(true));
|
EXPECT_THAT(br1.isCompleted(), Eq(true));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(BufferBytesOperations, ReadWriteBufferFncCanAcceptSignedData) {
|
||||||
|
//setup data
|
||||||
|
constexpr size_t DATA_SIZE = 3;
|
||||||
|
int16_t src[DATA_SIZE] {54,-4877,30067};
|
||||||
|
//create and write to buffer
|
||||||
|
Buffer buf{};
|
||||||
|
BufferWriter bw{buf};
|
||||||
|
bw.writeBuffer<2>(src, DATA_SIZE);
|
||||||
|
bw.flush();
|
||||||
|
//read from buffer
|
||||||
|
BufferReader br1{buf};
|
||||||
|
int16_t dst[DATA_SIZE]{};
|
||||||
|
EXPECT_THAT(br1.readBuffer<2>(dst, DATA_SIZE), Eq(true));
|
||||||
|
EXPECT_THAT(dst, ContainerEq(src));
|
||||||
|
|
||||||
|
//read more than available
|
||||||
|
BufferReader br2{buf};
|
||||||
|
int16_t dstMore[DATA_SIZE+1]{};
|
||||||
|
EXPECT_THAT(br2.readBuffer<2>(dstMore, DATA_SIZE+1), Eq(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferBytesOperations, ReadWriteBufferCanWorkOnUnalignedData) {
|
||||||
|
//setup data
|
||||||
|
constexpr size_t DATA_SIZE = 3;
|
||||||
|
int16_t src[DATA_SIZE] {54,-4877,30067};
|
||||||
|
//create and write to buffer
|
||||||
|
Buffer buf{};
|
||||||
|
BufferWriter bw{buf};
|
||||||
|
bw.writeBits(15u, 4);
|
||||||
|
bw.writeBuffer<2>(src, DATA_SIZE);
|
||||||
|
bw.writeBits(12u, 4);
|
||||||
|
bw.flush();
|
||||||
|
EXPECT_THAT(buf.size(), Eq(sizeof(src) + 1));
|
||||||
|
|
||||||
|
//read from buffer
|
||||||
|
BufferReader br1{buf};
|
||||||
|
int16_t dst[DATA_SIZE]{};
|
||||||
|
uint8_t tmp{};
|
||||||
|
br1.readBits(tmp, 4);
|
||||||
|
EXPECT_THAT(tmp, Eq(15));
|
||||||
|
EXPECT_THAT(br1.readBuffer<2>(dst, DATA_SIZE), Eq(true));
|
||||||
|
EXPECT_THAT(dst, ContainerEq(src));
|
||||||
|
br1.readBits(tmp, 4);
|
||||||
|
EXPECT_THAT(tmp, Eq(12));
|
||||||
|
|
||||||
|
//read more than available
|
||||||
|
BufferReader br2{buf};
|
||||||
|
br2.readBits(tmp, 4);
|
||||||
|
int16_t dstMore[DATA_SIZE+1]{};
|
||||||
|
EXPECT_THAT(tmp, Eq(15));
|
||||||
|
EXPECT_THAT(br2.readBuffer<2>(dstMore, DATA_SIZE+1), Eq(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
183
tests/buffer_endianness.cpp
Normal file
183
tests/buffer_endianness.cpp
Normal 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.
|
||||||
|
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <bitsery/buffer_writer.h>
|
||||||
|
#include <bitsery/buffer_reader.h>
|
||||||
|
#include <bitsery/details/serialization_common.h>
|
||||||
|
|
||||||
|
using testing::Eq;
|
||||||
|
using testing::ContainerEq;
|
||||||
|
using bitsery::EndiannessType;
|
||||||
|
using bitsery::DefaultConfig;
|
||||||
|
using Buffer = std::vector<bitsery::DefaultConfig::BufferValueType>;
|
||||||
|
|
||||||
|
constexpr EndiannessType getInverseEndianness(EndiannessType e) {
|
||||||
|
return e == EndiannessType::LittleEndian
|
||||||
|
? EndiannessType::BigEndian
|
||||||
|
: EndiannessType::LittleEndian;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct InverseEndiannessConfig {
|
||||||
|
static constexpr bitsery::EndiannessType NetworkEndianness = getInverseEndianness(DefaultConfig::NetworkEndianness);
|
||||||
|
using BufferValueType = DefaultConfig::BufferValueType;
|
||||||
|
using BufferScrathType = DefaultConfig::BufferScrathType;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IntegralTypes {
|
||||||
|
int64_t a;
|
||||||
|
uint32_t b;
|
||||||
|
int16_t c;
|
||||||
|
uint8_t d;
|
||||||
|
int8_t e;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
TEST(BufferEndianness, WhenWriteBytesThenBytesAreSwapped) {
|
||||||
|
//fill initial values
|
||||||
|
IntegralTypes src{};
|
||||||
|
src.a = 0x1122334455667788;
|
||||||
|
src.b = 0xBBCCDDEE;
|
||||||
|
src.c = 0xCCDD;
|
||||||
|
src.d = 0xDD;
|
||||||
|
src.e = 0xEE;
|
||||||
|
|
||||||
|
//fill expected result after swap
|
||||||
|
IntegralTypes resInv{};
|
||||||
|
resInv.a = 0x8877665544332211;
|
||||||
|
resInv.b = 0xEEDDCCBB;
|
||||||
|
resInv.c = 0xDDCC;
|
||||||
|
resInv.d = 0xDD;
|
||||||
|
resInv.e = 0xEE;
|
||||||
|
|
||||||
|
//create and write to buffer
|
||||||
|
Buffer buf{};
|
||||||
|
bitsery::BasicBufferWriter<DefaultConfig> bw{buf};
|
||||||
|
bw.writeBytes<8>(src.a);
|
||||||
|
bw.writeBytes<4>(src.b);
|
||||||
|
bw.writeBytes<2>(src.c);
|
||||||
|
bw.writeBytes<1>(src.d);
|
||||||
|
bw.writeBytes<1>(src.e);
|
||||||
|
bw.flush();
|
||||||
|
//read from buffer using inverse endianness config
|
||||||
|
bitsery::BasicBufferReader<InverseEndiannessConfig> br{buf};
|
||||||
|
IntegralTypes res{};
|
||||||
|
br.readBytes<8>(res.a);
|
||||||
|
br.readBytes<4>(res.b);
|
||||||
|
br.readBytes<2>(res.c);
|
||||||
|
br.readBytes<1>(res.d);
|
||||||
|
br.readBytes<1>(res.e);
|
||||||
|
//check results
|
||||||
|
EXPECT_THAT(res.a, Eq(resInv.a));
|
||||||
|
EXPECT_THAT(res.b, Eq(resInv.b));
|
||||||
|
EXPECT_THAT(res.c, Eq(resInv.c));
|
||||||
|
EXPECT_THAT(res.d, Eq(resInv.d));
|
||||||
|
EXPECT_THAT(res.e, Eq(resInv.e));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferEndianness, WhenWriteBuffer1ByteValuesThenEndiannessIsIgnored) {
|
||||||
|
//fill initial values
|
||||||
|
constexpr size_t SIZE = 4;
|
||||||
|
uint8_t src[SIZE] = {0xAA, 0xBB, 0xCC, 0xDD};
|
||||||
|
uint8_t res[SIZE] = {};
|
||||||
|
//create and write to buffer
|
||||||
|
Buffer buf{};
|
||||||
|
bitsery::BasicBufferWriter<DefaultConfig> bw{buf};
|
||||||
|
bw.writeBuffer<1>(src, SIZE);
|
||||||
|
bw.flush();
|
||||||
|
//read from buffer using inverse endianness config
|
||||||
|
bitsery::BasicBufferReader<InverseEndiannessConfig> br{buf};
|
||||||
|
br.readBuffer<1>(res, SIZE);
|
||||||
|
//result is identical, because we write separate values, of size 1byte, that requires no swapping
|
||||||
|
//check results
|
||||||
|
EXPECT_THAT(res, ContainerEq(src));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferEndianness, WhenWriteBufferMoreThan1ByteValuesThenValuesAreSwapped) {
|
||||||
|
//fill initial values
|
||||||
|
constexpr size_t SIZE = 4;
|
||||||
|
uint16_t src[SIZE] = {0xAA00, 0xBB11, 0xCC22, 0xDD33};
|
||||||
|
uint16_t resInv[SIZE] = {0x00AA, 0x11BB, 0x22CC, 0x33DD};
|
||||||
|
uint16_t res[SIZE] = {};
|
||||||
|
//create and write to buffer
|
||||||
|
Buffer buf{};
|
||||||
|
bitsery::BasicBufferWriter<DefaultConfig> bw{buf};
|
||||||
|
bw.writeBuffer<2>(src, SIZE);
|
||||||
|
bw.flush();
|
||||||
|
//read from buffer using inverse endianness config
|
||||||
|
bitsery::BasicBufferReader<InverseEndiannessConfig> br{buf};
|
||||||
|
br.readBuffer<2>(res, SIZE);
|
||||||
|
//result is identical, because we write separate values, of size 1byte, that requires no swapping
|
||||||
|
//check results
|
||||||
|
EXPECT_THAT(res, ContainerEq(resInv));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr size_t getBits(T v) {
|
||||||
|
return bitsery::details::calcRequiredBits<T>({}, v);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IntegralUnsignedTypes {
|
||||||
|
uint64_t a;
|
||||||
|
uint32_t b;
|
||||||
|
uint16_t c;
|
||||||
|
uint8_t d;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(BufferEndianness, WhenBufferValueTypeIs1ByteThenBitOperationsIsNotAffectedByEndianness) {
|
||||||
|
//fill initial values
|
||||||
|
static_assert(sizeof(DefaultConfig::BufferValueType) == 1, "currently only 1 byte size, value size is supported");
|
||||||
|
//fill initial values
|
||||||
|
constexpr IntegralUnsignedTypes src {
|
||||||
|
0x0000334455667788,//bits 19
|
||||||
|
0x00CCDDEE,//bits 16
|
||||||
|
0x00DD,//bits 1
|
||||||
|
0x0F,//bits 6
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr size_t aBITS = getBits(src.a) + 8;
|
||||||
|
constexpr size_t bBITS = getBits(src.b) + 0;
|
||||||
|
constexpr size_t cBITS = getBits(src.c) + 5;
|
||||||
|
constexpr size_t dBITS = getBits(src.d) + 2;
|
||||||
|
//create and write to buffer
|
||||||
|
Buffer buf{};
|
||||||
|
bitsery::BasicBufferWriter<DefaultConfig> bw{buf};
|
||||||
|
bw.writeBits(src.a, aBITS);
|
||||||
|
bw.writeBits(src.b, bBITS);
|
||||||
|
bw.writeBits(src.c, cBITS);
|
||||||
|
bw.writeBits(src.d, dBITS);
|
||||||
|
bw.flush();
|
||||||
|
//read from buffer using inverse endianness config
|
||||||
|
bitsery::BasicBufferReader<InverseEndiannessConfig> br{buf};
|
||||||
|
IntegralUnsignedTypes res{};
|
||||||
|
br.readBits(res.a, aBITS);
|
||||||
|
br.readBits(res.b, bBITS);
|
||||||
|
br.readBits(res.c, cBITS);
|
||||||
|
br.readBits(res.d, dBITS);
|
||||||
|
//check results
|
||||||
|
EXPECT_THAT(res.a, Eq(src.a));
|
||||||
|
EXPECT_THAT(res.b, Eq(src.b));
|
||||||
|
EXPECT_THAT(res.c, Eq(src.c));
|
||||||
|
EXPECT_THAT(res.d, Eq(src.d));
|
||||||
|
}
|
||||||
@@ -31,11 +31,41 @@
|
|||||||
using testing::ContainerEq;
|
using testing::ContainerEq;
|
||||||
using testing::Eq;
|
using testing::Eq;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* overload to get container of types
|
||||||
|
*/
|
||||||
|
|
||||||
template <typename Container>
|
template <typename Container>
|
||||||
Container getFilledContainer() {
|
Container getFilledContainer() {
|
||||||
return {1,2,3,4,5,78,456,8,54};
|
return {1,2,3,4,5,78,456,8,54};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
std::vector<MyStruct1> getFilledContainer<std::vector<MyStruct1>>() {
|
||||||
|
return {
|
||||||
|
{0,1},
|
||||||
|
{2,3},
|
||||||
|
{4,5},
|
||||||
|
{6,7},
|
||||||
|
{8,9},
|
||||||
|
{11,34},
|
||||||
|
{5134,1532}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
std::list<MyStruct2> getFilledContainer<std::list<MyStruct2>>() {
|
||||||
|
return {
|
||||||
|
{MyStruct2::V1, {0,1}} ,
|
||||||
|
{MyStruct2::V3, {-45,45}}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* start testing session
|
||||||
|
*/
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class SerializeContainerArthmeticTypes:public testing::Test {
|
class SerializeContainerArthmeticTypes:public testing::Test {
|
||||||
public:
|
public:
|
||||||
@@ -107,26 +137,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
|
||||||
std::vector<MyStruct1> getFilledContainer<std::vector<MyStruct1>>() {
|
|
||||||
return {
|
|
||||||
{0,1},
|
|
||||||
{2,3},
|
|
||||||
{4,5},
|
|
||||||
{6,7},
|
|
||||||
{8,9},
|
|
||||||
{11,34},
|
|
||||||
{5134,1532}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
std::list<MyStruct2> getFilledContainer<std::list<MyStruct2>>() {
|
|
||||||
return {
|
|
||||||
{MyStruct2::V1, {0,1}} ,
|
|
||||||
{MyStruct2::V3, {-45,45}}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
using SequenceContainersWithCompositeTypes = ::testing::Types<
|
using SequenceContainersWithCompositeTypes = ::testing::Types<
|
||||||
std::vector<MyStruct1>,
|
std::vector<MyStruct1>,
|
||||||
|
|||||||
@@ -23,12 +23,15 @@
|
|||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
#include "serialization_test_utils.h"
|
#include "serialization_test_utils.h"
|
||||||
|
|
||||||
#include <experimental/optional>
|
#if __cplusplus > 201402L
|
||||||
|
# include<optional>
|
||||||
namespace std {
|
#else
|
||||||
template <typename T>
|
# include <experimental/optional>
|
||||||
using optional = experimental::optional<T>;
|
namespace std {
|
||||||
};
|
template <typename T>
|
||||||
|
using optional = experimental::optional<T>;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <bitsery/ext/optional.h>
|
#include <bitsery/ext/optional.h>
|
||||||
|
|
||||||
@@ -81,5 +84,3 @@ TEST(SerializeExtensionOptional, OptionalHasValue) {
|
|||||||
EXPECT_THAT(t1.value(), Eq(r1.value()));
|
EXPECT_THAT(t1.value(), Eq(r1.value()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ TEST(DeltaSerializer, GeneralConceptTest) {
|
|||||||
yNew.vx[1].s = "bla";
|
yNew.vx[1].s = "bla";
|
||||||
yNew.vx.push_back(X{ 3 });
|
yNew.vx.push_back(X{ 3 });
|
||||||
|
|
||||||
std::vector<uint8_t> buf;
|
std::vector<bitsery::DefaultConfig::BufferValueType> buf;
|
||||||
bitsery::BufferWriter bw{ buf };
|
bitsery::BufferWriter bw{ buf };
|
||||||
bitsery::DeltaSerializer<bitsery::BufferWriter, Y> ser(bw, y, yNew);
|
bitsery::DeltaSerializer<bitsery::BufferWriter, Y> ser(bw, y, yNew);
|
||||||
serialize(ser, yNew);
|
serialize(ser, yNew);
|
||||||
|
|||||||
@@ -29,17 +29,17 @@ using bitsery::BitsConstraint;
|
|||||||
|
|
||||||
TEST(SerializeRange, RequiredBitsIsConstexpr) {
|
TEST(SerializeRange, RequiredBitsIsConstexpr) {
|
||||||
constexpr RangeSpec<int> r1{0, 31};
|
constexpr RangeSpec<int> r1{0, 31};
|
||||||
static_assert(r1.bitsRequired == 5);
|
static_assert(r1.bitsRequired == 5, "r1.bitsRequired == 5");
|
||||||
|
|
||||||
constexpr RangeSpec<MyEnumClass> r2{MyEnumClass::E1, MyEnumClass::E4};
|
constexpr RangeSpec<MyEnumClass> r2{MyEnumClass::E1, MyEnumClass::E4};
|
||||||
static_assert(r2.bitsRequired == 2);
|
static_assert(r2.bitsRequired == 2, "r2.bitsRequired == 2");
|
||||||
|
|
||||||
constexpr RangeSpec<double> r3{-1.0,1.0, BitsConstraint{5u}};
|
constexpr RangeSpec<double> r3{-1.0,1.0, BitsConstraint{5u}};
|
||||||
//EXPECT_THAT(r1.bitsRequired, Eq(5));
|
//EXPECT_THAT(r1.bitsRequired, Eq(5));
|
||||||
static_assert(r3.bitsRequired == 5);
|
static_assert(r3.bitsRequired == 5, "r3.bitsRequired == 5");
|
||||||
|
|
||||||
constexpr RangeSpec<float> r4{-1.0f,1.0f, 0.01f};
|
constexpr RangeSpec<float> r4{-1.0f,1.0f, 0.01f};
|
||||||
static_assert(r4.bitsRequired == 8);
|
static_assert(r4.bitsRequired == 8, "r4.bitsRequired == 8");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ TEST(SerializeRange, FloatUsingBitsSizeConstraint1) {
|
|||||||
ctx.createDeserializer().range(res1, r1);
|
ctx.createDeserializer().range(res1, r1);
|
||||||
|
|
||||||
EXPECT_THAT(ctx.getBufferSize(), Eq(1));
|
EXPECT_THAT(ctx.getBufferSize(), Eq(1));
|
||||||
EXPECT_THAT(res1, ::testing::FloatNear(t1, (max - min) / (static_cast<bitsery::SAME_SIZE_UNSIGNED<float>>(1) << bits)));
|
EXPECT_THAT(res1, ::testing::FloatNear(t1, (max - min) / (static_cast<bitsery::details::SAME_SIZE_UNSIGNED<float>>(1) << bits)));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SerializeRange, DoubleUsingBitsSizeConstraint2) {
|
TEST(SerializeRange, DoubleUsingBitsSizeConstraint2) {
|
||||||
@@ -150,5 +150,5 @@ TEST(SerializeRange, DoubleUsingBitsSizeConstraint2) {
|
|||||||
ctx.createDeserializer().range(res1, r1);
|
ctx.createDeserializer().range(res1, r1);
|
||||||
|
|
||||||
EXPECT_THAT(ctx.getBufferSize(), Eq(7));
|
EXPECT_THAT(ctx.getBufferSize(), Eq(7));
|
||||||
EXPECT_THAT(res1, ::testing::DoubleNear(t1, (max - min) / (static_cast<bitsery::SAME_SIZE_UNSIGNED<double>>(1) << bits)));
|
EXPECT_THAT(res1, ::testing::DoubleNear(t1, (max - min) / (static_cast<bitsery::details::SAME_SIZE_UNSIGNED<double>>(1) << bits)));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,11 @@
|
|||||||
#include <bitsery/bitsery.h>
|
#include <bitsery/bitsery.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* define some types for testing
|
||||||
|
*/
|
||||||
|
|
||||||
struct MyStruct1 {
|
struct MyStruct1 {
|
||||||
MyStruct1(int v1, int v2):i1{v1}, i2{v2} {}
|
MyStruct1(int v1, int v2):i1{v1}, i2{v2} {}
|
||||||
MyStruct1():MyStruct1{0,0} {}
|
MyStruct1():MyStruct1{0,0} {}
|
||||||
@@ -70,8 +75,9 @@ SERIALIZE(MyStruct2) {
|
|||||||
object(o.s1);
|
object(o.s1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class SerializationContext {
|
class SerializationContext {
|
||||||
std::vector<uint8_t> buf{};
|
std::vector<bitsery::DefaultConfig::BufferValueType> buf{};
|
||||||
std::unique_ptr<bitsery::BufferWriter> bw;
|
std::unique_ptr<bitsery::BufferWriter> bw;
|
||||||
std::unique_ptr<bitsery::BufferReader> br;
|
std::unique_ptr<bitsery::BufferReader> br;
|
||||||
public:
|
public:
|
||||||
@@ -83,7 +89,7 @@ public:
|
|||||||
size_t getBufferSize() const {
|
size_t getBufferSize() const {
|
||||||
return buf.size();
|
return buf.size();
|
||||||
}
|
}
|
||||||
//since all containers .size() method returns size_t, it cannot be dirrectly serialized, because size_t is platform dependant
|
//since all containers .size() method returns size_t, it cannot be directly serialized, because size_t is platform dependant
|
||||||
//this function returns number of bytes writen to buffer, when reading/writing size of container
|
//this function returns number of bytes writen to buffer, when reading/writing size of container
|
||||||
static size_t containerSizeSerializedBytesCount(size_t elemsCount) {
|
static size_t containerSizeSerializedBytesCount(size_t elemsCount) {
|
||||||
if (elemsCount < 0x80u)
|
if (elemsCount < 0x80u)
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ TEST(SerializeValues, ValueSizeOverload2Byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(SerializeValues, ValueSizeOverload4Byte) {
|
TEST(SerializeValues, ValueSizeOverload4Byte) {
|
||||||
float v{54.498};
|
float v{54.498f};
|
||||||
float res;
|
float res;
|
||||||
constexpr size_t TSIZE = sizeof(v);
|
constexpr size_t TSIZE = sizeof(v);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user