mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-18 13:19:11 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ca45aab79 | ||
|
|
952635ff70 | ||
|
|
507b5ae01d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
||||
.vs/
|
||||
build/
|
||||
cmake-build-debug/
|
||||
CTestConfig.cmake
|
||||
|
||||
13
.travis.yml
13
.travis.yml
@@ -9,15 +9,22 @@ before_install:
|
||||
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
|
||||
- sudo apt-get update -qq
|
||||
|
||||
install:
|
||||
install:
|
||||
- sudo apt-get install -qq g++-5 lcov
|
||||
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 90
|
||||
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 90
|
||||
- wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz
|
||||
- tar xf release-1.8.0.tar.gz
|
||||
- cd googletest-release-1.8.0
|
||||
- cmake -DBUILD_SHARED_LIBS=ON .
|
||||
- make
|
||||
- sudo make install
|
||||
- cd ..
|
||||
|
||||
before_script:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake ..
|
||||
- cmake -DBITSERY_BUILD_TESTS=ON ..
|
||||
|
||||
script: make && make test
|
||||
script: make && (cd tests && ctest)
|
||||
|
||||
|
||||
12
CHANGELOG.md
12
CHANGELOG.md
@@ -1,3 +1,15 @@
|
||||
# [4.2.1](https://github.com/fraillt/bitsery/compare/v4.2.0...v4.2.1) (2018-03-09)
|
||||
|
||||
### Improvements
|
||||
* changed CMake structure, to follow **Modern CMake** principles.
|
||||
* bitsery now has *install* target and **find_package(Bitsery)** exports **Bitsery::bitsery** target.
|
||||
* *GTest* no longer downloads as external application, but tries to find via *find_package*.
|
||||
* removed *ext* folder, and instead added *scripts* folder that contains few helper scripts for development, currently tested on Ubuntu.
|
||||
* fixed/added few tests cases.
|
||||
|
||||
### Other notes
|
||||
* some work was done on polymorphism support, but current solution, although working, but has many design and performance issues, and interfaces for extensibility might change drastically.
|
||||
|
||||
# [4.2.0](https://github.com/fraillt/bitsery/compare/v4.1.0...v4.2.0) (2017-11-12)
|
||||
|
||||
### Features
|
||||
|
||||
@@ -1,18 +1,64 @@
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
project(bitsery
|
||||
LANGUAGES CXX
|
||||
VERSION 4.2.1)
|
||||
|
||||
project(bitsery)
|
||||
#======== build options ===================================
|
||||
option(BITSERY_BUILD_EXAMPLES "Build examples" OFF)
|
||||
option(BITSERY_BUILD_TESTS "Build tests" OFF)
|
||||
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
||||
set(COMPILE_FLAGS "")
|
||||
#============= setup target ======================
|
||||
add_library(bitsery INTERFACE)
|
||||
# create alias, so that user could always write target_link_libraries(... Bitsery::bitsery)
|
||||
# despite of bitsery target is imported or not
|
||||
add_library(Bitsery::bitsery ALIAS bitsery)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
target_include_directories(bitsery INTERFACE
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
|
||||
target_compile_features(bitsery INTERFACE
|
||||
cxx_auto_type
|
||||
cxx_constexpr
|
||||
cxx_lambdas
|
||||
cxx_nullptr
|
||||
cxx_variadic_templates)
|
||||
|
||||
#=============== setup installation =======================
|
||||
include(CMakePackageConfigHelpers)
|
||||
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/BitseryConfigVersion.cmake
|
||||
COMPATIBILITY SameMajorVersion)
|
||||
install(TARGETS bitsery
|
||||
EXPORT bitseryTargets
|
||||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
install(EXPORT bitseryTargets
|
||||
FILE "BitseryConfig.cmake"
|
||||
NAMESPACE Bitsery::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bitsery)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/BitseryConfigVersion.cmake
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bitsery)
|
||||
install(DIRECTORY include/bitsery
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
|
||||
#================ handle sub-projects =====================
|
||||
# examples and tests are independend directories, that can be built separatelly.
|
||||
# make find_package to no-op, when searching for "Bitsery", because it is created here.
|
||||
macro(find_package)
|
||||
if (NOT ${ARGV0} STREQUAL Bitsery)
|
||||
_find_package(${ARGV})
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
if (BITSERY_BUILD_EXAMPLES)
|
||||
message("build bitsery examples")
|
||||
add_subdirectory(examples)
|
||||
else()
|
||||
set(COMPILE_FLAGS "-Wall -Wextra -Wno-missing-braces -Wpedantic -Weffc++")
|
||||
message("skip bitsery examples")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}")
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_subdirectory(examples)
|
||||
|
||||
enable_testing()
|
||||
add_subdirectory(tests)
|
||||
if (BITSERY_BUILD_TESTS)
|
||||
message("build bitsery tests")
|
||||
add_subdirectory(tests)
|
||||
else()
|
||||
message("skip bitsery tests")
|
||||
endif()
|
||||
|
||||
@@ -8,15 +8,19 @@ you contribute:
|
||||
1. Fork the repository.
|
||||
2. Create new branch based on the *master* branch (`git checkout -b your_branch master`). If your contribution is a bug fix, you should name your branch `bugfix/xxx`; for a feature, it should be `feature/xxx`. Otherwise, just use your good judgment. Consistent naming of branches is appreciated since it makes the output of `git branch` easier to understand with a single glance.
|
||||
3. Do your modifications on that branch. Except for special cases, your contribution should include proper unit tests and documentation.
|
||||
4. Make sure your modifications did not break anything by building, running tests and checking code coverage (test coverage should not be less than 100%):
|
||||
4. Make sure your modifications did not break anything by building, running tests:
|
||||
```shell
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake -DBITSERY_BUILD_TESTS=ON ..
|
||||
make
|
||||
ctest
|
||||
make tests_coverage
|
||||
x-www-browser ./coverage/index.html
|
||||
(cd tests; ctest)
|
||||
```
|
||||
or run CTest scripts and view code coverage (scripts tested on ubuntu, requires lcov for coverage):
|
||||
```shell
|
||||
cd scripts
|
||||
ctest -S build.bitsery.cmake
|
||||
./show_coverage.sh build
|
||||
```
|
||||
5. Commit your changes, and push to your fork (`git push origin your_branch`). Commit message should be one line short description. When applicable, please squash adjacent *wip* commits into a single *logical* commit.
|
||||
6. Open a pull request against Bitsery *master* branch. Currently ongoing development is on *master*. At some point an integration branch will be set-up, and pull-requests should target that, but for now its all against master. You may see feature branches come and go, too.
|
||||
|
||||
@@ -20,10 +20,12 @@
|
||||
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
#SOFTWARE.
|
||||
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
project(bitsery_examples CXX)
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
if (NOT TARGET Bitsery::bitsery)
|
||||
message(FATAL_ERROR "Bitsery::bitsery alias not set. Please generate CMake from bitsery root directory.")
|
||||
endif()
|
||||
|
||||
file(GLOB ExampleFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
|
||||
@@ -32,10 +34,9 @@ if (WIN32)
|
||||
list(REMOVE_ITEM ExampleFiles ${CMAKE_CURRENT_SOURCE_DIR}/flexible_assert_linux_x64.cpp)
|
||||
endif()
|
||||
|
||||
|
||||
FOREACH(ExampleFile ${ExampleFiles})
|
||||
foreach(ExampleFile ${ExampleFiles})
|
||||
get_filename_component(ExampleName ${ExampleFile} NAME_WE)
|
||||
add_executable(${ExampleName} ${ExampleFile})
|
||||
|
||||
ENDFOREACH()
|
||||
add_executable(bitsery.example.${ExampleName} ${ExampleFile})
|
||||
target_link_libraries(bitsery.example.${ExampleName} PRIVATE Bitsery::bitsery)
|
||||
endforeach()
|
||||
|
||||
|
||||
@@ -1,198 +0,0 @@
|
||||
# Copyright (c) 2012 - 2015, Lars Bilke
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this
|
||||
# list of conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
# may be used to endorse or promote products derived from this software without
|
||||
# specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
#
|
||||
#
|
||||
# 2012-01-31, Lars Bilke
|
||||
# - Enable Code Coverage
|
||||
#
|
||||
# 2013-09-17, Joakim Söderberg
|
||||
# - Added support for Clang.
|
||||
# - Some additional usage instructions.
|
||||
#
|
||||
# USAGE:
|
||||
|
||||
# 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here:
|
||||
# http://stackoverflow.com/a/22404544/80480
|
||||
#
|
||||
# 1. Copy this file into your cmake modules path.
|
||||
#
|
||||
# 2. Add the following line to your CMakeLists.txt:
|
||||
# INCLUDE(CodeCoverage)
|
||||
#
|
||||
# 3. Set compiler flags to turn off optimization and enable coverage:
|
||||
# SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
|
||||
# SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
|
||||
#
|
||||
# 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
|
||||
# which runs your test executable and produces a lcov code coverage report:
|
||||
# Example:
|
||||
# SETUP_TARGET_FOR_COVERAGE(
|
||||
# my_coverage_target # Name for custom target.
|
||||
# test_driver # Name of the test driver executable that runs the tests.
|
||||
# # NOTE! This should always have a ZERO as exit code
|
||||
# # otherwise the coverage generation will not complete.
|
||||
# coverage # Name of output directory.
|
||||
# )
|
||||
#
|
||||
# 4. Build a Debug build:
|
||||
# cmake -DCMAKE_BUILD_TYPE=Debug ..
|
||||
# make
|
||||
# make my_coverage_target
|
||||
#
|
||||
#
|
||||
|
||||
# Check prereqs
|
||||
FIND_PROGRAM( GCOV_PATH gcov )
|
||||
FIND_PROGRAM( LCOV_PATH lcov )
|
||||
FIND_PROGRAM( GENHTML_PATH genhtml )
|
||||
FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
|
||||
|
||||
IF(NOT GCOV_PATH)
|
||||
MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
|
||||
ENDIF() # NOT GCOV_PATH
|
||||
|
||||
IF("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
|
||||
IF("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
|
||||
MESSAGE(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
|
||||
ENDIF()
|
||||
ELSEIF(NOT CMAKE_COMPILER_IS_GNUCXX)
|
||||
MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
|
||||
ENDIF() # CHECK VALID COMPILER
|
||||
|
||||
SET(CMAKE_CXX_FLAGS_COVERAGE
|
||||
"-g -O0 --coverage -fprofile-arcs -ftest-coverage"
|
||||
CACHE STRING "Flags used by the C++ compiler during coverage builds."
|
||||
FORCE )
|
||||
SET(CMAKE_C_FLAGS_COVERAGE
|
||||
"-g -O0 --coverage -fprofile-arcs -ftest-coverage"
|
||||
CACHE STRING "Flags used by the C compiler during coverage builds."
|
||||
FORCE )
|
||||
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
|
||||
""
|
||||
CACHE STRING "Flags used for linking binaries during coverage builds."
|
||||
FORCE )
|
||||
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
|
||||
""
|
||||
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
|
||||
FORCE )
|
||||
MARK_AS_ADVANCED(
|
||||
CMAKE_CXX_FLAGS_COVERAGE
|
||||
CMAKE_C_FLAGS_COVERAGE
|
||||
CMAKE_EXE_LINKER_FLAGS_COVERAGE
|
||||
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
|
||||
|
||||
IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage"))
|
||||
MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
|
||||
ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
|
||||
|
||||
|
||||
# Param _targetname The name of new the custom make target
|
||||
# Param _testrunner The name of the target which runs the tests.
|
||||
# MUST return ZERO always, even on errors.
|
||||
# If not, no coverage report will be created!
|
||||
# Param _outputname lcov output is generated as _outputname.info
|
||||
# HTML report is generated in _outputname/index.html
|
||||
# Optional fourth parameter is passed as arguments to _testrunner
|
||||
# Pass them in list form, e.g.: "-j;2" for -j 2
|
||||
FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
|
||||
|
||||
IF(NOT LCOV_PATH)
|
||||
MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
|
||||
ENDIF() # NOT LCOV_PATH
|
||||
|
||||
IF(NOT GENHTML_PATH)
|
||||
MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
|
||||
ENDIF() # NOT GENHTML_PATH
|
||||
|
||||
SET(coverage_info "${CMAKE_BINARY_DIR}/${_outputname}.info")
|
||||
SET(coverage_cleaned "${coverage_info}.cleaned")
|
||||
|
||||
SEPARATE_ARGUMENTS(test_command UNIX_COMMAND "${_testrunner}")
|
||||
|
||||
# Setup target
|
||||
ADD_CUSTOM_TARGET(${_targetname}
|
||||
|
||||
# Cleanup lcov
|
||||
${LCOV_PATH} --directory . --zerocounters
|
||||
|
||||
# Run tests
|
||||
COMMAND ${test_command} ${ARGV3}
|
||||
|
||||
# Capturing lcov counters and generating report
|
||||
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${coverage_info}
|
||||
#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 ${CMAKE_COMMAND} -E remove ${coverage_info} ${coverage_cleaned}
|
||||
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
|
||||
)
|
||||
|
||||
# Show info where to find the report
|
||||
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
|
||||
COMMAND ;
|
||||
COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
|
||||
)
|
||||
|
||||
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
|
||||
|
||||
# Param _targetname The name of new the custom make target
|
||||
# Param _testrunner The name of the target which runs the tests
|
||||
# Param _outputname cobertura output is generated as _outputname.xml
|
||||
# Optional fourth parameter is passed as arguments to _testrunner
|
||||
# Pass them in list form, e.g.: "-j;2" for -j 2
|
||||
FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
|
||||
|
||||
IF(NOT PYTHON_EXECUTABLE)
|
||||
MESSAGE(FATAL_ERROR "Python not found! Aborting...")
|
||||
ENDIF() # NOT PYTHON_EXECUTABLE
|
||||
|
||||
IF(NOT GCOVR_PATH)
|
||||
MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
|
||||
ENDIF() # NOT GCOVR_PATH
|
||||
|
||||
ADD_CUSTOM_TARGET(${_targetname}
|
||||
|
||||
# Run tests
|
||||
${_testrunner} ${ARGV3}
|
||||
|
||||
# Running gcovr
|
||||
COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
COMMENT "Running gcovr to produce Cobertura code coverage report."
|
||||
)
|
||||
|
||||
# Show info where to find the report
|
||||
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
|
||||
COMMAND ;
|
||||
COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
|
||||
)
|
||||
|
||||
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA
|
||||
@@ -1,17 +0,0 @@
|
||||
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}d${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)
|
||||
@@ -1,79 +0,0 @@
|
||||
#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.
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
project(gtest_builder C CXX)
|
||||
include(ExternalProject)
|
||||
|
||||
set(ForceSharedCrt ON)
|
||||
set(DisablePThreads OFF)
|
||||
|
||||
if(MINGW)
|
||||
set(DisablePThreads ON)
|
||||
endif()
|
||||
|
||||
if (${UseGMock})
|
||||
message("use gmock")
|
||||
set(BuildArgs -DBUILD_GTEST=OFF -DBUILD_GMOCK=ON)
|
||||
else ()
|
||||
message("use gtest only")
|
||||
set(BuildArgs -DBUILD_GTEST=ON -DBUILD_GMOCK=OFF)
|
||||
endif()
|
||||
|
||||
if (WIN32 AND NOT MINGW)
|
||||
set(BuildArgs ${BuildArgs}
|
||||
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
|
||||
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs)
|
||||
endif()
|
||||
|
||||
ExternalProject_Add(googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
CMAKE_ARGS ${BuildArgs}
|
||||
-Dgtest_force_shared_crt=${ForceSharedCrt}
|
||||
-Dgtest_disable_pthreads=${DisablePThreads}
|
||||
PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
|
||||
# disable update command
|
||||
UPDATE_COMMAND ""
|
||||
# disable install step
|
||||
INSTALL_COMMAND ""
|
||||
)
|
||||
|
||||
#export variables
|
||||
ExternalProject_Get_Property(googletest source_dir)
|
||||
ExternalProject_Get_Property(googletest binary_dir)
|
||||
if (${UseGMock})
|
||||
# need to include both googletest and googlemock
|
||||
set(GTestIncludeDirs ${source_dir}/googlemock/include ${source_dir}/googletest/include PARENT_SCOPE)
|
||||
set(GTestLibsDir ${binary_dir}/googlemock PARENT_SCOPE)
|
||||
set(GTestLibName gmock PARENT_SCOPE)
|
||||
set(GTestMainLibName gmock_main PARENT_SCOPE)
|
||||
set(GTestLinkLibNames gmock_main PARENT_SCOPE)
|
||||
else()
|
||||
set(GTestIncludeDirs ${source_dir}/googletest/include PARENT_SCOPE)
|
||||
set(GTestLibsDir ${binary_dir}/googletest PARENT_SCOPE)
|
||||
set(GTestLibName gtest PARENT_SCOPE)
|
||||
set(GTestMainLibName gtest_main PARENT_SCOPE)
|
||||
# need to include both libs gtest and gtest_main
|
||||
set(GTestLinkLibNames gtest gtest_main PARENT_SCOPE)
|
||||
endif()
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
#define BITSERY_MAJOR_VERSION 4
|
||||
#define BITSERY_MINOR_VERSION 2
|
||||
#define BITSERY_PATCH_VERSION 0
|
||||
#define BITSERY_PATCH_VERSION 1
|
||||
|
||||
#define BITSERY_QUOTE_MACRO(name) #name
|
||||
#define BITSERY_BUILD_VERSION_STR(major,minor, patch) \
|
||||
|
||||
@@ -24,309 +24,78 @@
|
||||
#define BITSERY_EXT_POINTER_H
|
||||
|
||||
#include "../traits/core/traits.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "utils/pointer_utils.h"
|
||||
#include "utils/rtti_utils.h"
|
||||
|
||||
namespace bitsery {
|
||||
|
||||
namespace ext {
|
||||
|
||||
//forward declare
|
||||
class PointerLinkingContext;
|
||||
|
||||
enum class PointerType {
|
||||
Nullable,
|
||||
NotNull
|
||||
};
|
||||
|
||||
enum class PointerOwnershipType:uint8_t {
|
||||
//is not responsible for pointer lifetime management.
|
||||
Observer,
|
||||
//only ONE owner is responsible for this pointers creation/destruction
|
||||
Owner,
|
||||
//MANY shared owners is responsible for pointer creation/destruction
|
||||
//requires additional context to manage shared owners themselves.
|
||||
Shared
|
||||
};
|
||||
|
||||
namespace details_pointer {
|
||||
|
||||
//helper functions that creates or destroys pointers
|
||||
//useful, because can be specialized
|
||||
template <typename T>
|
||||
void destroyObject(T &ptr) {
|
||||
if (ptr) {
|
||||
delete ptr;
|
||||
}
|
||||
ptr = nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void createObject(T &ptr) {
|
||||
using TNonPtr = typename std::remove_pointer<T>::type;
|
||||
if (ptr == nullptr)
|
||||
ptr = new TNonPtr{};
|
||||
}
|
||||
|
||||
|
||||
template <typename S, typename TPtr>
|
||||
void serializeObject(S &s, const TPtr &obj) {
|
||||
using TNonPtr = typename std::remove_pointer<TPtr>::type;
|
||||
static_assert(!std::is_polymorphic<TNonPtr>::value, "Polymorphic types are not supported");
|
||||
serializeImpl(s, *obj, details::IsFundamentalType<TNonPtr>{});
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void serializeImpl(S& s, const T& obj, std::true_type) {
|
||||
s.template value<sizeof(T)>(obj);
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void serializeImpl(S& s, const T& obj, std::false_type) {
|
||||
s.object(obj);
|
||||
}
|
||||
|
||||
template <typename S, typename TPtr>
|
||||
void deserializeObject(S &s, TPtr &obj) {
|
||||
using TNonPtr = typename std::remove_pointer<TPtr>::type;
|
||||
static_assert(!std::is_polymorphic<TNonPtr>::value, "Polymorphic types are not supported");
|
||||
deserializeImpl(s, *obj, details::IsFundamentalType<TNonPtr>{});
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void deserializeImpl(S& s, T& obj, std::true_type) {
|
||||
s.template value<sizeof(T)>(obj);
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void deserializeImpl(S& s, T& obj, std::false_type) {
|
||||
s.object(obj);
|
||||
}
|
||||
|
||||
class RawPointerManager {
|
||||
public:
|
||||
template <typename Writer, typename Ser, typename T>
|
||||
static void serializeObject(Writer& , Ser& ser, const T& obj) {
|
||||
|
||||
details_pointer::serializeObject(ser, obj);
|
||||
}
|
||||
|
||||
template <typename Reader, typename Des, typename T>
|
||||
static void deserializeObject(Reader& , Des& des, T& obj) {
|
||||
details_pointer::createObject(obj);
|
||||
details_pointer::deserializeObject(des, obj);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void destroyObject(T& obj) {
|
||||
details_pointer::destroyObject(obj);
|
||||
}
|
||||
};
|
||||
|
||||
class PointerLinkingContextSerialization {
|
||||
public:
|
||||
explicit PointerLinkingContextSerialization()
|
||||
:_currId{0},
|
||||
_ptrMap{}
|
||||
{}
|
||||
|
||||
PointerLinkingContextSerialization(const PointerLinkingContextSerialization&) = delete;
|
||||
PointerLinkingContextSerialization& operator = (const PointerLinkingContextSerialization&) = delete;
|
||||
|
||||
PointerLinkingContextSerialization(PointerLinkingContextSerialization&&) = default;
|
||||
PointerLinkingContextSerialization& operator = (PointerLinkingContextSerialization&&) = default;
|
||||
~PointerLinkingContextSerialization() = default;
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, PointerOwnershipType ownershipType_)
|
||||
:id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
sharedCount{0}
|
||||
{};
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
size_t sharedCount;
|
||||
};
|
||||
|
||||
const PointerInfo& getInfoByPtr(const void *ptr, PointerOwnershipType ptrType) {
|
||||
auto res = _ptrMap.emplace(ptr, PointerInfo{_currId + 1u, ptrType});
|
||||
auto& ptrInfo = res.first->second;
|
||||
if (res.second) {
|
||||
++_currId;
|
||||
return ptrInfo;
|
||||
}
|
||||
//ptr already exists
|
||||
//for observer return success
|
||||
if (ptrType == PointerOwnershipType::Observer)
|
||||
return ptrInfo;
|
||||
//set owner and return success
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer) {
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
return ptrInfo;
|
||||
}
|
||||
//only shared ownership can get here multiple times
|
||||
assert(ptrType == PointerOwnershipType::Shared);
|
||||
ptrInfo.sharedCount++;
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers have owners.
|
||||
//we cannot serialize pointers, if we haven't serialized objects themselves
|
||||
bool isPointerSerializationValid() const {
|
||||
return std::all_of(_ptrMap.begin(), _ptrMap.end(), [](const std::pair<const void*, PointerInfo>& p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _currId;
|
||||
std::unordered_map<const void*, PointerInfo> _ptrMap;
|
||||
|
||||
};
|
||||
|
||||
//this class is used to store context for shared ptr owners
|
||||
struct SharedContextBase {
|
||||
virtual ~SharedContextBase() = default;
|
||||
};
|
||||
|
||||
class PointerLinkingContextDeserialization {
|
||||
public:
|
||||
explicit PointerLinkingContextDeserialization()
|
||||
: _idMap{}
|
||||
{}
|
||||
|
||||
PointerLinkingContextDeserialization(const PointerLinkingContextDeserialization&) = delete;
|
||||
PointerLinkingContextDeserialization& operator = (const PointerLinkingContextDeserialization&) = delete;
|
||||
|
||||
PointerLinkingContextDeserialization(PointerLinkingContextDeserialization&&) = default;
|
||||
PointerLinkingContextDeserialization& operator = (PointerLinkingContextDeserialization&&) = default;
|
||||
~PointerLinkingContextDeserialization() = default;
|
||||
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, void* ptr, PointerOwnershipType ownershipType_)
|
||||
:id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
ownerPtr{ptr},
|
||||
observersList{},
|
||||
sharedContext{}
|
||||
{};
|
||||
PointerInfo(const PointerInfo&) = delete;
|
||||
PointerInfo& operator = (const PointerInfo&) = delete;
|
||||
PointerInfo(PointerInfo&&) = default;
|
||||
PointerInfo&operator = (PointerInfo&&) = default;
|
||||
~PointerInfo() = default;
|
||||
|
||||
void processOwner(void* ptr) {
|
||||
ownerPtr = ptr;
|
||||
assert(ownershipType != PointerOwnershipType::Observer);
|
||||
for (auto& o:observersList)
|
||||
o.get() = ptr;
|
||||
observersList.clear();
|
||||
observersList.shrink_to_fit();
|
||||
}
|
||||
void processObserver(void* (&ptr)) {
|
||||
if (ownerPtr) {
|
||||
ptr = ownerPtr;
|
||||
} else {
|
||||
observersList.push_back(ptr);
|
||||
}
|
||||
}
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
void* ownerPtr;
|
||||
std::vector<std::reference_wrapper<void*>> observersList;
|
||||
std::unique_ptr<SharedContextBase> sharedContext;
|
||||
};
|
||||
|
||||
PointerInfo& getInfoById(size_t id, PointerOwnershipType ptrType) {
|
||||
auto res = _idMap.emplace(id, PointerInfo{id, nullptr, ptrType});
|
||||
auto& ptrInfo = res.first->second;
|
||||
if (!res.second) {
|
||||
assert(ptrType != PointerOwnershipType::Owner || ptrInfo.ownershipType == PointerOwnershipType::Observer);
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer)
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
}
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers has owners
|
||||
bool isPointerDeserializationValid() const {
|
||||
return std::all_of(_idMap.begin(), _idMap.end(), [](const std::pair<const size_t, PointerInfo>& p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, PointerInfo> _idMap;
|
||||
};
|
||||
|
||||
template <typename S>
|
||||
PointerLinkingContext& getLinkingContext(S& s) {
|
||||
template<typename S>
|
||||
PointerLinkingContext &getLinkingContext(S &s) {
|
||||
auto res = s.template context<PointerLinkingContext>();
|
||||
assert(res != nullptr);
|
||||
return *res;
|
||||
}
|
||||
|
||||
|
||||
template<typename TObject>
|
||||
struct RawPointerObjectHandler {
|
||||
|
||||
using TPointer = TObject;
|
||||
|
||||
template<typename T>
|
||||
void create(TObject &obj) const {
|
||||
obj = new T{};
|
||||
}
|
||||
|
||||
void destroy(TObject &obj) const {
|
||||
delete obj;
|
||||
obj = nullptr;
|
||||
}
|
||||
|
||||
const TPointer getPtr(const TObject &obj) const {
|
||||
return obj;
|
||||
}
|
||||
|
||||
TPointer getPtr(TObject &obj) const {
|
||||
return obj;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <typename TObject>
|
||||
struct RawPointerManagerConfig {
|
||||
using RTTI = bitsery::ext::utils::StandardRTTI;
|
||||
static constexpr PointerOwnershipType OwnershipType = PointerOwnershipType::Owner;
|
||||
|
||||
using Handler = RawPointerObjectHandler<TObject>;
|
||||
|
||||
static std::unique_ptr<utils::PointerSharedContextBase> createSharedContext(TObject &) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static void restoreFromSharedContext(TObject &, utils::PointerSharedContextBase *) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//this class is for convenience
|
||||
class PointerLinkingContext:
|
||||
public details_pointer::PointerLinkingContextSerialization,
|
||||
public details_pointer::PointerLinkingContextDeserialization {
|
||||
class PointerOwner : public utils::PointerOwnerManager<details_pointer::RawPointerManagerConfig> {
|
||||
public:
|
||||
bool isValid() {
|
||||
return isPointerSerializationValid() && isPointerDeserializationValid();
|
||||
}
|
||||
};
|
||||
|
||||
class PointerOwner {
|
||||
public:
|
||||
explicit PointerOwner(PointerType ptrType = PointerType::Nullable):_ptrType{ptrType} {}
|
||||
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&) const {
|
||||
auto& ctx = details_pointer::getLinkingContext(ser);
|
||||
if (obj) {
|
||||
auto& ptrInfo = ctx.getInfoByPtr(obj, PointerOwnershipType::Owner);
|
||||
details::writeSize(w, ptrInfo.id);
|
||||
details_pointer::RawPointerManager::serializeObject(w, ser, obj);
|
||||
} else {
|
||||
assert(_ptrType == PointerType::Nullable);
|
||||
details::writeSize(w, 0);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Des, typename Reader, typename T, typename Fnc>
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc &&) const {
|
||||
details_pointer::getLinkingContext(des);
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
if (id) {
|
||||
auto& ctx = details_pointer::getLinkingContext(des);
|
||||
auto& ptrInfo = ctx.getInfoById(id, PointerOwnershipType::Owner);
|
||||
details_pointer::RawPointerManager::deserializeObject(r, des, obj);
|
||||
ptrInfo.processOwner(obj);
|
||||
} else {
|
||||
if (_ptrType == PointerType::Nullable)
|
||||
details_pointer::RawPointerManager::destroyObject(obj);
|
||||
else
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
private:
|
||||
PointerType _ptrType;
|
||||
explicit PointerOwner(PointerType ptrType = PointerType::Nullable) : PointerOwnerManager(ptrType) {}
|
||||
};
|
||||
|
||||
class PointerObserver {
|
||||
public:
|
||||
|
||||
explicit PointerObserver(PointerType ptrType = PointerType::Nullable):_ptrType{ptrType} {}
|
||||
explicit PointerObserver(PointerType ptrType = PointerType::Nullable) : _ptrType{ptrType} {}
|
||||
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&) const {
|
||||
auto& ctx = details_pointer::getLinkingContext(ser);
|
||||
auto &ctx = details_pointer::getLinkingContext(ser);
|
||||
if (obj) {
|
||||
details::writeSize(w, ctx.getInfoByPtr(obj, PointerOwnershipType::Observer).id);
|
||||
} else {
|
||||
@@ -340,8 +109,8 @@ namespace bitsery {
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
if (id) {
|
||||
auto& ctx = details_pointer::getLinkingContext(des);
|
||||
ctx.getInfoById(id, PointerOwnershipType::Observer).processObserver(reinterpret_cast<void*&>(obj));
|
||||
auto &ctx = details_pointer::getLinkingContext(des);
|
||||
ctx.getInfoById(id, PointerOwnershipType::Observer).processObserver(reinterpret_cast<void *&>(obj));
|
||||
} else {
|
||||
if (_ptrType == PointerType::Nullable)
|
||||
obj = nullptr;
|
||||
@@ -349,6 +118,7 @@ namespace bitsery {
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
PointerType _ptrType;
|
||||
};
|
||||
@@ -356,18 +126,18 @@ namespace bitsery {
|
||||
class ReferencedByPointer {
|
||||
public:
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc && fnc) const {
|
||||
auto& ctx = details_pointer::getLinkingContext(ser);
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&fnc) const {
|
||||
auto &ctx = details_pointer::getLinkingContext(ser);
|
||||
details::writeSize(w, ctx.getInfoByPtr(&obj, PointerOwnershipType::Owner).id);
|
||||
fnc(const_cast<T&>(obj));
|
||||
fnc(const_cast<T &>(obj));
|
||||
}
|
||||
|
||||
template<typename Des, typename Reader, typename T, typename Fnc>
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc && fnc) const {
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc &&fnc) const {
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
if (id) {
|
||||
auto& ctx = details_pointer::getLinkingContext(des);
|
||||
auto &ctx = details_pointer::getLinkingContext(des);
|
||||
fnc(obj);
|
||||
ctx.getInfoById(id, PointerOwnershipType::Owner).processOwner(&obj);
|
||||
} else {
|
||||
@@ -382,7 +152,7 @@ namespace bitsery {
|
||||
namespace traits {
|
||||
|
||||
template<typename T>
|
||||
struct ExtensionTraits<ext::PointerOwner, T*> {
|
||||
struct ExtensionTraits<ext::PointerOwner, T *> {
|
||||
using TValue = T;
|
||||
static constexpr bool SupportValueOverload = true;
|
||||
static constexpr bool SupportObjectOverload = true;
|
||||
@@ -391,7 +161,7 @@ namespace bitsery {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ExtensionTraits<ext::PointerObserver, T*> {
|
||||
struct ExtensionTraits<ext::PointerObserver, T *> {
|
||||
//although pointer observer doesn't serialize anything, but we still add value overload support to be consistent with pointer owners
|
||||
using TValue = T;
|
||||
static constexpr bool SupportValueOverload = true;
|
||||
|
||||
319
include/bitsery/ext/utils/pointer_utils.h
Normal file
319
include/bitsery/ext/utils/pointer_utils.h
Normal file
@@ -0,0 +1,319 @@
|
||||
//
|
||||
// Created by fraillt on 17.11.30.
|
||||
//
|
||||
|
||||
#ifndef BITSERY_POINTER_UTILS_H
|
||||
#define BITSERY_POINTER_UTILS_H
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "polymorphism_utils.h"
|
||||
|
||||
namespace bitsery {
|
||||
namespace ext {
|
||||
|
||||
enum class PointerType {
|
||||
Nullable,
|
||||
NotNull
|
||||
};
|
||||
|
||||
enum class PointerOwnershipType : uint8_t {
|
||||
//is not responsible for pointer lifetime management.
|
||||
Observer,
|
||||
//only ONE owner is responsible for this pointers creation/destruction
|
||||
Owner,
|
||||
//MANY shared owners is responsible for pointer creation/destruction
|
||||
//requires additional context to manage shared owners themselves.
|
||||
Shared
|
||||
};
|
||||
|
||||
//forward declaration
|
||||
class PointerLinkingContext;
|
||||
|
||||
namespace utils {
|
||||
class PointerLinkingContextSerialization {
|
||||
public:
|
||||
explicit PointerLinkingContextSerialization()
|
||||
: _currId{0},
|
||||
_ptrMap{} {}
|
||||
|
||||
PointerLinkingContextSerialization(const PointerLinkingContextSerialization &) = delete;
|
||||
|
||||
PointerLinkingContextSerialization &operator=(const PointerLinkingContextSerialization &) = delete;
|
||||
|
||||
PointerLinkingContextSerialization(PointerLinkingContextSerialization &&) = default;
|
||||
|
||||
PointerLinkingContextSerialization &operator=(PointerLinkingContextSerialization &&) = default;
|
||||
|
||||
~PointerLinkingContextSerialization() = default;
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, PointerOwnershipType ownershipType_)
|
||||
: id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
sharedCount{0} {};
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
size_t sharedCount;
|
||||
};
|
||||
|
||||
const PointerInfo &getInfoByPtr(const void *ptr, PointerOwnershipType ptrType) {
|
||||
auto res = _ptrMap.emplace(ptr, PointerInfo{_currId + 1u, ptrType});
|
||||
auto &ptrInfo = res.first->second;
|
||||
if (res.second) {
|
||||
++_currId;
|
||||
return ptrInfo;
|
||||
}
|
||||
//ptr already exists
|
||||
//for observer return success
|
||||
if (ptrType == PointerOwnershipType::Observer)
|
||||
return ptrInfo;
|
||||
//set owner and return success
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer) {
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
return ptrInfo;
|
||||
}
|
||||
//only shared ownership can get here multiple times
|
||||
assert(ptrType == PointerOwnershipType::Shared);
|
||||
ptrInfo.sharedCount++;
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers have owners.
|
||||
//we cannot serialize pointers, if we haven't serialized objects themselves
|
||||
bool isPointerSerializationValid() const {
|
||||
return std::all_of(_ptrMap.begin(), _ptrMap.end(),
|
||||
[](const std::pair<const void *, PointerInfo> &p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _currId;
|
||||
std::unordered_map<const void *, PointerInfo> _ptrMap;
|
||||
|
||||
};
|
||||
|
||||
//this class is used to store context for shared ptr owners
|
||||
struct PointerSharedContextBase {
|
||||
virtual ~PointerSharedContextBase() = default;
|
||||
};
|
||||
|
||||
class PointerLinkingContextDeserialization {
|
||||
public:
|
||||
explicit PointerLinkingContextDeserialization()
|
||||
: _idMap{} {}
|
||||
|
||||
PointerLinkingContextDeserialization(const PointerLinkingContextDeserialization &) = delete;
|
||||
|
||||
PointerLinkingContextDeserialization &operator=(const PointerLinkingContextDeserialization &) = delete;
|
||||
|
||||
PointerLinkingContextDeserialization(PointerLinkingContextDeserialization &&) = default;
|
||||
|
||||
PointerLinkingContextDeserialization &operator=(PointerLinkingContextDeserialization &&) = default;
|
||||
|
||||
~PointerLinkingContextDeserialization() = default;
|
||||
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, void *ptr, PointerOwnershipType ownershipType_)
|
||||
: id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
ownerPtr{ptr},
|
||||
observersList{},
|
||||
sharedContext{} {};
|
||||
|
||||
PointerInfo(const PointerInfo &) = delete;
|
||||
|
||||
PointerInfo &operator=(const PointerInfo &) = delete;
|
||||
|
||||
PointerInfo(PointerInfo &&) = default;
|
||||
|
||||
PointerInfo &operator=(PointerInfo &&) = default;
|
||||
|
||||
~PointerInfo() = default;
|
||||
|
||||
void processOwner(void *ptr) {
|
||||
ownerPtr = ptr;
|
||||
assert(ownershipType != PointerOwnershipType::Observer);
|
||||
for (auto &o:observersList)
|
||||
o.get() = ptr;
|
||||
observersList.clear();
|
||||
observersList.shrink_to_fit();
|
||||
}
|
||||
|
||||
void processObserver(void *(&ptr)) {
|
||||
if (ownerPtr) {
|
||||
ptr = ownerPtr;
|
||||
} else {
|
||||
observersList.push_back(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
void *ownerPtr;
|
||||
std::vector<std::reference_wrapper<void *>> observersList;
|
||||
std::unique_ptr<PointerSharedContextBase> sharedContext;
|
||||
};
|
||||
|
||||
PointerInfo &getInfoById(size_t id, PointerOwnershipType ptrType) {
|
||||
auto res = _idMap.emplace(id, PointerInfo{id, nullptr, ptrType});
|
||||
auto &ptrInfo = res.first->second;
|
||||
if (!res.second) {
|
||||
assert(ptrType != PointerOwnershipType::Owner ||
|
||||
ptrInfo.ownershipType == PointerOwnershipType::Observer);
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer)
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
}
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers has owners
|
||||
bool isPointerDeserializationValid() const {
|
||||
return std::all_of(_idMap.begin(), _idMap.end(), [](const std::pair<const size_t, PointerInfo> &p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, PointerInfo> _idMap;
|
||||
};
|
||||
|
||||
template<template<typename> class Config>
|
||||
class PointerOwnerManager {
|
||||
|
||||
template <typename TObject>
|
||||
using Handler = typename Config<TObject>::Handler;
|
||||
|
||||
template<typename TObject>
|
||||
struct HelperTypes {
|
||||
using RTTI = typename Config<TObject>::RTTI;
|
||||
using THandler = Handler<TObject>;
|
||||
using TValue = typename std::remove_pointer<typename THandler::TPointer>::type;
|
||||
};
|
||||
|
||||
template<typename Ser, typename T, typename Fnc>
|
||||
void serializeImpl(PointerLinkingContextSerialization &, Ser &ser, const T &obj, Fnc &&,
|
||||
std::true_type) const {
|
||||
InheritanceTreeSerialize<Ser, T, HelperTypes> tree{};
|
||||
auto handler = tree.getHandler(obj);
|
||||
|
||||
assert(handler.second);
|
||||
ser.object(handler.first);
|
||||
handler.second->process(ser, const_cast<T &>(obj));
|
||||
}
|
||||
|
||||
template<typename Ser, typename T, typename Fnc>
|
||||
void serializeImpl(PointerLinkingContextSerialization &, Ser &, const T &obj, Fnc &&fnc,
|
||||
std::false_type) const {
|
||||
auto handler = Handler<T>{};
|
||||
fnc(*handler.getPtr(const_cast<T &>(obj)));
|
||||
}
|
||||
|
||||
template<typename Des, typename T, typename Fnc, typename Reader>
|
||||
void deserializeImpl(PointerLinkingContextDeserialization &, Des &des, T &obj, Fnc &&,
|
||||
Reader &r, std::true_type) const {
|
||||
InheritanceTreeTypeId id{};
|
||||
des.object(id);
|
||||
|
||||
InheritanceTreeDeserialize<Des, T, HelperTypes> tree{};
|
||||
auto handler = tree.getHandler(id);
|
||||
if (handler) {
|
||||
handler->create(obj);
|
||||
handler->process(des, obj);
|
||||
} else {
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Des, typename T, typename Fnc, typename Reader>
|
||||
void deserializeImpl(PointerLinkingContextDeserialization &, Des &, T &obj, Fnc &&fnc,
|
||||
Reader &, std::false_type) const {
|
||||
using TValue = typename HelperTypes<T>::TValue;
|
||||
auto handler = Handler<T>{};
|
||||
if (auto ptr = handler.getPtr(obj)) {
|
||||
fnc(*ptr);
|
||||
} else {
|
||||
handler.template create<TValue>(obj);
|
||||
fnc(*handler.getPtr(obj));
|
||||
}
|
||||
}
|
||||
|
||||
PointerType _ptrType;
|
||||
public:
|
||||
|
||||
explicit PointerOwnerManager(PointerType ptrType = PointerType::Nullable) : _ptrType{ptrType} {}
|
||||
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&fnc) const {
|
||||
auto handler = Handler<T>{};
|
||||
auto ptr = handler.getPtr(obj);
|
||||
if (ptr) {
|
||||
auto ctx = ser.template context<PointerLinkingContext>();
|
||||
assert(ctx != nullptr);
|
||||
auto &ptrInfo = ctx->getInfoByPtr(ptr, Config<T>::OwnershipType);
|
||||
details::writeSize(w, ptrInfo.id);
|
||||
if (ptrInfo.sharedCount == 0) {
|
||||
serializeImpl(*ctx, ser, obj, std::forward<Fnc>(fnc),
|
||||
std::is_polymorphic<typename HelperTypes<T>::TValue>{});
|
||||
}
|
||||
} else {
|
||||
assert(_ptrType == PointerType::Nullable);
|
||||
details::writeSize(w, 0);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Des, typename Reader, typename T, typename Fnc>
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc &&fnc) const {
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
auto handler = Handler<T>{};
|
||||
if (id) {
|
||||
auto ctx = des.template context<PointerLinkingContext>();
|
||||
assert(ctx != nullptr);
|
||||
auto &ptrInfo = ctx->getInfoById(id, Config<T>::OwnershipType);
|
||||
//todo add deserialization checking
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Owner) {
|
||||
deserializeImpl(*ctx, des, obj, std::forward<Fnc>(fnc), r,
|
||||
std::is_polymorphic<typename HelperTypes<T>::TValue>{});
|
||||
ptrInfo.processOwner(handler.getPtr(obj));
|
||||
} else {
|
||||
if (!ptrInfo.sharedContext) {
|
||||
deserializeImpl(*ctx, des, obj, std::forward<Fnc>(fnc), r,
|
||||
std::is_polymorphic<typename HelperTypes<T>::TValue>{});
|
||||
ptrInfo.processOwner(handler.getPtr(obj));
|
||||
ptrInfo.sharedContext = Config<T>::createSharedContext(obj);
|
||||
} else {
|
||||
Config<T>::restoreFromSharedContext(obj, ptrInfo.sharedContext.get());
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if (_ptrType == PointerType::Nullable && handler.getPtr(obj)) {
|
||||
handler.destroy(obj);
|
||||
} else
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//this class is for convenience
|
||||
class PointerLinkingContext :
|
||||
public utils::PointerLinkingContextSerialization,
|
||||
public utils::PointerLinkingContextDeserialization {
|
||||
public:
|
||||
bool isValid() {
|
||||
return isPointerSerializationValid() && isPointerDeserializationValid();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //BITSERY_POINTER_UTILS_H
|
||||
225
include/bitsery/ext/utils/polymorphism_utils.h
Normal file
225
include/bitsery/ext/utils/polymorphism_utils.h
Normal file
@@ -0,0 +1,225 @@
|
||||
//
|
||||
// Created by fraillt on 17.11.3.
|
||||
//
|
||||
|
||||
#ifndef BITSERY_EXT_POLYMORPHISM_H
|
||||
#define BITSERY_EXT_POLYMORPHISM_H
|
||||
|
||||
#include "../inheritance.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace bitsery {
|
||||
|
||||
namespace ext {
|
||||
namespace details_polymorphism {
|
||||
//stores template types
|
||||
template<typename ...>
|
||||
struct List {
|
||||
};
|
||||
}
|
||||
|
||||
//specialize for your base class by deriving from DerivedClasses with list of derivatives that DIRECTLY inherits from your base class.
|
||||
//e.g.
|
||||
// template <> PolymorphicBase<Animal>: DerivedClasses<Dog, Cat>{};
|
||||
// template <> PolymorphicBase<Dog>: DerivedClasses<Bulldog, GoldenRetriever> {};
|
||||
// IMPORTANT !!!
|
||||
// although you can add all derivates to same base like this:
|
||||
// SuperClass<Animal>:DerivedClasses<Dog, Cat, Bulldog, GoldenRetriever>{};
|
||||
// it will not work when you try to serialize Dog*, because it will not find Bulldog and GoldenRetriever
|
||||
template<typename TBase>
|
||||
struct PolymorphicBaseClass {
|
||||
using childs = details_polymorphism::List<>;
|
||||
};
|
||||
|
||||
//derive from this class when specifying childs for your base class, atleast one child must exists, hence T1
|
||||
//e.g.
|
||||
// template <> SuperClass<Animal>: DerivedClasses<Dog, Cat>{};
|
||||
template<typename T1, typename ... Tn>
|
||||
struct DerivedClasses {
|
||||
using childs = details_polymorphism::List<T1, Tn...>;
|
||||
};
|
||||
|
||||
namespace utils {
|
||||
//this object will be used to serialize/deserialize polymorphic type id within inheritance tree from base class
|
||||
struct InheritanceTreeTypeId {
|
||||
uint16_t depth{};
|
||||
uint16_t index{};
|
||||
template <typename S>
|
||||
void serialize(S& s) {
|
||||
s.value2b(depth);
|
||||
s.value2b(index);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename S, typename TObject>
|
||||
struct PolymorphicObjectHandlerInterface {
|
||||
virtual void process(S& s, TObject& obj) const = 0;
|
||||
virtual void create(TObject& obj) const = 0;
|
||||
virtual ~PolymorphicObjectHandlerInterface() = default;
|
||||
};
|
||||
}
|
||||
|
||||
namespace details_polymorphism {
|
||||
|
||||
|
||||
template<typename S, typename TObject, typename TDerived, typename RTTI, typename ObjectHandler>
|
||||
struct PolymorphicObjectHandlerBase : public utils::PolymorphicObjectHandlerInterface<S, TObject> {
|
||||
void process(S &s, TObject &obj) const final {
|
||||
s.object(dynamic_cast<TDerived &>(*_handler.getPtr(obj)));
|
||||
};
|
||||
|
||||
void create(TObject &obj) const final {
|
||||
auto ptr = _handler.getPtr(obj);
|
||||
if (ptr && RTTI::get(*ptr) != RTTI::template get<TDerived>()) {
|
||||
_handler.destroy(obj);
|
||||
_handler.template create<TDerived>(obj);
|
||||
} else {
|
||||
if (ptr == nullptr)
|
||||
_handler.template create<TDerived>(obj);
|
||||
}
|
||||
}
|
||||
ObjectHandler _handler{};
|
||||
};
|
||||
|
||||
template<typename S, typename TObject, template<typename> class TObjectManager>
|
||||
class PolymorphicHandlersGenerator {
|
||||
public:
|
||||
template<typename Fnc>
|
||||
static void generate(Fnc &&addHandlerFnc) {
|
||||
PolymorphicHandlersGenerator tmp{std::forward<Fnc>(addHandlerFnc)};
|
||||
}
|
||||
|
||||
private:
|
||||
using RTTI = typename TObjectManager<TObject>::RTTI;
|
||||
|
||||
template <typename TDerived>
|
||||
using TPolymorphicObjectHandler = PolymorphicObjectHandlerBase<S, TObject, TDerived, RTTI, typename TObjectManager<TObject>::THandler>;
|
||||
|
||||
template<typename Fnc>
|
||||
explicit PolymorphicHandlersGenerator(Fnc &&addHandlerFnc):_addHandler(std::forward<Fnc>(addHandlerFnc)) {
|
||||
//fill inheritance tree
|
||||
using TBase = typename TObjectManager<TObject>::TValue;
|
||||
add<TBase>();
|
||||
}
|
||||
|
||||
template<typename TClass>
|
||||
void add() {
|
||||
addClass<TClass>();
|
||||
//save current index and increase depth
|
||||
auto saveIndex = index;
|
||||
index = 0;
|
||||
depth++;
|
||||
addChilds<TClass>(typename PolymorphicBaseClass<TClass>::childs{});
|
||||
//restore index and depth
|
||||
depth--;
|
||||
index = saveIndex;
|
||||
}
|
||||
|
||||
template<typename TClassBase, typename T1, typename ... Tn>
|
||||
void addChilds(details_polymorphism::List<T1, Tn...>) {
|
||||
static_assert(std::is_base_of<TClassBase, T1>::value,
|
||||
"PolymorphicBaseClass<TBase> must derive a list of derived classes from TBase.");
|
||||
add<T1>();
|
||||
addChilds<TClassBase>(details_polymorphism::List<Tn...>{});
|
||||
}
|
||||
|
||||
template<typename>
|
||||
void addChilds(details_polymorphism::List<>) {
|
||||
}
|
||||
|
||||
|
||||
template<typename TClass>
|
||||
void addClass() {
|
||||
if (!std::is_abstract<TClass>::value) {
|
||||
utils::InheritanceTreeTypeId tid{};
|
||||
tid.index = index;
|
||||
tid.depth = depth;
|
||||
#if __cplusplus > 201103L
|
||||
auto handler = std::make_unique<TPolymorphicObjectHandler<TClass>>();
|
||||
#else
|
||||
auto handler = std::unique_ptr<TPolymorphicObjectHandler<TClass>>(
|
||||
new TPolymorphicObjectHandler<TClass>{});
|
||||
#endif
|
||||
if (_addHandler(RTTI::template get<TClass>(), tid, std::move(handler)))
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
std::function<bool(size_t, utils::InheritanceTreeTypeId,
|
||||
std::unique_ptr<utils::PolymorphicObjectHandlerInterface<S, TObject>> &&)> _addHandler;
|
||||
uint16_t depth{};
|
||||
uint16_t index{};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace utils {
|
||||
|
||||
template<typename Ser, typename TObject, template<typename> class TObjectManager>
|
||||
class InheritanceTreeSerialize {
|
||||
public:
|
||||
|
||||
InheritanceTreeSerialize() {
|
||||
details_polymorphism::PolymorphicHandlersGenerator<Ser, TObject, TObjectManager>::generate(
|
||||
[this](size_t typeHash, InheritanceTreeTypeId id,
|
||||
std::unique_ptr<PolymorphicObjectHandlerInterface<Ser, TObject>> &&handler) {
|
||||
return _map.emplace(std::make_pair(typeHash, std::make_pair(id, std::move(handler)))).second;
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
const std::pair<InheritanceTreeTypeId, PolymorphicObjectHandlerInterface<Ser, TObject>*> getHandler(const TObject &obj) {
|
||||
using RTTI = typename TObjectManager<TObject>::RTTI;
|
||||
auto handler = typename TObjectManager<TObject>::THandler{};
|
||||
auto it = _map.find(RTTI::get(*handler.getPtr(obj)));
|
||||
if (it != _map.end()) {
|
||||
return std::make_pair(it->second.first, it->second.second.get());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, std::pair<InheritanceTreeTypeId, std::unique_ptr<PolymorphicObjectHandlerInterface<Ser, TObject>>>> _map{};
|
||||
};
|
||||
|
||||
|
||||
template<typename Des, typename TObject, template<typename> class TObjectManager>
|
||||
class InheritanceTreeDeserialize {
|
||||
public:
|
||||
InheritanceTreeDeserialize() {
|
||||
details_polymorphism::PolymorphicHandlersGenerator<Des, TObject, TObjectManager>::generate(
|
||||
[this](size_t, InheritanceTreeTypeId id, std::unique_ptr<PolymorphicObjectHandlerInterface<Des, TObject>> &&handler) {
|
||||
return _map.emplace(std::make_pair(getHashFromId(id), std::move(handler))).second;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const PolymorphicObjectHandlerInterface<Des, TObject> *getHandler(const InheritanceTreeTypeId &id) {
|
||||
auto it = _map.find(getHashFromId(id));
|
||||
if (it != _map.end())
|
||||
return it->second.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t getHashFromId(const InheritanceTreeTypeId &id) const {
|
||||
size_t res = id.depth << 16;
|
||||
return res + id.index;
|
||||
}
|
||||
|
||||
std::unordered_map<size_t, std::unique_ptr<PolymorphicObjectHandlerInterface<Des, TObject>>> _map{};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //BITSERY_EXT_POLYMORPHISM_H
|
||||
28
include/bitsery/ext/utils/rtti_utils.h
Normal file
28
include/bitsery/ext/utils/rtti_utils.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by fraillt on 17.11.30.
|
||||
//
|
||||
|
||||
#ifndef BITSERY_RTTI_UTILS_H
|
||||
#define BITSERY_RTTI_UTILS_H
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
namespace bitsery {
|
||||
namespace ext {
|
||||
namespace utils {
|
||||
struct StandardRTTI {
|
||||
template<typename T>
|
||||
static size_t get() {
|
||||
return typeid(T).hash_code();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static size_t get(T &&obj) {
|
||||
return typeid(obj).hash_code();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif //BITSERY_RTTI_UTILS_H
|
||||
7
scripts/CTestConfig.cmake
Normal file
7
scripts/CTestConfig.cmake
Normal file
@@ -0,0 +1,7 @@
|
||||
set(CTEST_PROJECT_NAME "bitsery")
|
||||
|
||||
set(CTEST_DROP_METHOD "http")
|
||||
set(CTEST_DROP_SITE "my.cdash.org")
|
||||
set(CTEST_DROP_LOCATION "/submit.php?project=bitsery")
|
||||
set(CTEST_DROP_SITE_CDASH TRUE)
|
||||
|
||||
20
scripts/build.bitsery.cmake
Normal file
20
scripts/build.bitsery.cmake
Normal file
@@ -0,0 +1,20 @@
|
||||
# run from linux shell:
|
||||
#$ ctest -S build.bitsery.cmake
|
||||
set(CTEST_SOURCE_DIRECTORY "..") #path to bitsery root (top-level) directory
|
||||
set(CTEST_BINARY_DIRECTORY "build")
|
||||
|
||||
set(ENV{CXXFLAGS} "--coverage")
|
||||
#when using Ninja generator, ctest_coverage cannot find files...
|
||||
set(CTEST_CMAKE_GENERATOR "CodeBlocks - Unix Makefiles")
|
||||
set(CTEST_USE_LAUNCHERS 1)
|
||||
|
||||
set(CTEST_COVERAGE_COMMAND "gcov")
|
||||
|
||||
configure_file(CTestConfig.cmake ${CTEST_SOURCE_DIRECTORY}/CTestConfig.cmake)
|
||||
|
||||
ctest_start("Continuous")
|
||||
ctest_configure(OPTIONS "-DBITSERY_BUILD_EXAMPLES=OFF;-DBITSERY_BUILD_TESTS=ON")
|
||||
ctest_build()
|
||||
ctest_test(BUILD ${CTEST_BINARY_DIRECTORY}/tests)
|
||||
ctest_coverage()
|
||||
#ctest_submit()
|
||||
8
scripts/show_coverage.sh
Normal file
8
scripts/show_coverage.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
BUILD_DIR=$1
|
||||
TESTS_BUILD_DIR=$BUILD_DIR/tests/CMakeFiles/
|
||||
COV_INFO=$TESTS_BUILD_DIR/bitsery_coverage.info
|
||||
lcov --directory $TESTS_BUILD_DIR --capture --output-file $COV_INFO
|
||||
lcov --extract $COV_INFO '*include/bitsery*' --output-file $COV_INFO.clean
|
||||
genhtml --output-directory $TESTS_BUILD_DIR/coverage_web $COV_INFO.clean
|
||||
xdg-open $TESTS_BUILD_DIR/coverage_web/index.html
|
||||
@@ -20,25 +20,14 @@
|
||||
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
#SOFTWARE.
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
set(TestProjectName bitsery_tests)
|
||||
project(${TestProjectName} C CXX)
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(bitsery_tests CXX)
|
||||
|
||||
find_package(GTest 1.8 REQUIRED)
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
#add googletest external project
|
||||
#USE_GMOCK enable gmock
|
||||
#exports variables GTEST_INCLUDE_DIRS, GTEST_LIBS_DIR, GTEST_LIBNAME, GTEST_MAIN_LIBNAME
|
||||
set(ExtCMakeFilesDir ${CMAKE_SOURCE_DIR}/ext)
|
||||
set(UseGMock ON)
|
||||
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
|
||||
include_directories(SYSTEM ${GTestIncludeDirs})
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
if (NOT TARGET Bitsery::bitsery)
|
||||
message(FATAL_ERROR "Bitsery::bitsery alias not set. Please generate CMake from bitsery root directory.")
|
||||
endif()
|
||||
|
||||
file(GLOB TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
|
||||
@@ -47,28 +36,27 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
||||
list(REMOVE_ITEM TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/serialization_ext_std_optional.cpp)
|
||||
endif()
|
||||
|
||||
include(${ExtCMakeFilesDir}/LinkTestLib.cmake)
|
||||
enable_testing()
|
||||
|
||||
FOREACH(TestFile ${TestSourceFiles})
|
||||
foreach(TestFile ${TestSourceFiles})
|
||||
get_filename_component(TestName ${TestFile} NAME_WE)
|
||||
set(TestName TEST_${TestName})
|
||||
add_executable(${TestName} ${TestFile} ${IncludeHeaders} serialization_test_utils.h)
|
||||
LinkTestLib(${TestName})
|
||||
|
||||
set(TestName bitsery.test.${TestName})
|
||||
add_executable(${TestName} ${TestFile})
|
||||
target_link_libraries(${TestName} PRIVATE GTest::Main Bitsery::bitsery)
|
||||
add_test(NAME ${TestName} COMMAND $<TARGET_FILE:${TestName}>)
|
||||
endforeach()
|
||||
|
||||
ENDFOREACH()
|
||||
#======================= setup development environment ====================
|
||||
|
||||
#all in one tests for code coverage
|
||||
add_executable(${TestProjectName} ${TestSourceFiles})
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
include(${ExtCMakeFilesDir}/CodeCoverage.cmake)
|
||||
target_compile_options(${TestProjectName} PUBLIC -O0 -fprofile-arcs -ftest-coverage)
|
||||
target_link_libraries(${TestProjectName} -O0 -fprofile-arcs -ftest-coverage)
|
||||
setup_target_for_coverage(tests_coverage ${TestProjectName} coverage)
|
||||
# get all header files for IDE (in my case Clion) and create dummy project that consumes theses files
|
||||
get_directory_property(ParentDir PARENT_DIRECTORY)
|
||||
if (ParentDir)
|
||||
# only include when working from root project (Bitsery)
|
||||
file(GLOB_RECURSE HeadersForIDE ${ParentDir}/include/bitsery/*.h)
|
||||
# create dummy target IDE
|
||||
file(WRITE ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp "//generated by CMake to create dummy target with all includes for IDE.")
|
||||
add_library(bitsery.dummy_for_ide ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp )
|
||||
# add headers so IDE correctly show them
|
||||
target_sources(bitsery.dummy_for_ide PRIVATE ${HeadersForIDE} serialization_test_utils.h)
|
||||
target_link_libraries(bitsery.dummy_for_ide PRIVATE GTest::Main Bitsery::bitsery)
|
||||
endif()
|
||||
|
||||
LinkTestLib(${TestProjectName})
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ TEST(FlexibleSyntax, FundamentalTypesAndBool) {
|
||||
int ti = 8745;
|
||||
MyEnumClass te = MyEnumClass::E4;
|
||||
float tf = 485.042f;
|
||||
double_t td = -454184.48445;
|
||||
double td = -454184.48445;
|
||||
bool tb=true;
|
||||
SerializationContext ctx{};
|
||||
ctx.createSerializer().archive(ti,te,tf,td,tb);
|
||||
@@ -53,7 +53,7 @@ TEST(FlexibleSyntax, FundamentalTypesAndBool) {
|
||||
int ri{};
|
||||
MyEnumClass re{};
|
||||
float rf{};
|
||||
double_t rd{};
|
||||
double rd{};
|
||||
bool rb{};
|
||||
ctx.createDeserializer().archive(ri,re,rf,rd,rb);
|
||||
|
||||
@@ -69,7 +69,7 @@ TEST(FlexibleSyntax, UseObjectFncInsteadOfValueN) {
|
||||
int ti = 8745;
|
||||
MyEnumClass te = MyEnumClass::E4;
|
||||
float tf = 485.042f;
|
||||
double_t td = -454184.48445;
|
||||
double td = -454184.48445;
|
||||
bool tb=true;
|
||||
SerializationContext ctx;
|
||||
auto& ser = ctx.createSerializer();
|
||||
@@ -83,7 +83,7 @@ TEST(FlexibleSyntax, UseObjectFncInsteadOfValueN) {
|
||||
int ri{};
|
||||
MyEnumClass re{};
|
||||
float rf{};
|
||||
double_t rd{};
|
||||
double rd{};
|
||||
bool rb{};
|
||||
auto& des = ctx.createDeserializer();
|
||||
des.object(ri);
|
||||
@@ -104,7 +104,7 @@ TEST(FlexibleSyntax, MixDifferentSyntax) {
|
||||
int ti = 8745;
|
||||
MyEnumClass te = MyEnumClass::E4;
|
||||
float tf = 485.042f;
|
||||
double_t td = -454184.48445;
|
||||
double td = -454184.48445;
|
||||
bool tb=true;
|
||||
SerializationContext ctx;
|
||||
auto& ser = ctx.createSerializer();
|
||||
@@ -116,7 +116,7 @@ TEST(FlexibleSyntax, MixDifferentSyntax) {
|
||||
int ri{};
|
||||
MyEnumClass re{};
|
||||
float rf{};
|
||||
double_t rd{};
|
||||
double rd{};
|
||||
bool rb{};
|
||||
auto& des = ctx.createDeserializer();
|
||||
des.archive(ri, re, rf);
|
||||
|
||||
@@ -250,4 +250,20 @@ TYPED_TEST(SerializeContainerFixedSizeCompositeTypes, CustomFunctionThatSerializ
|
||||
EXPECT_THAT(res, ContainerEq(src));
|
||||
}
|
||||
|
||||
class SerializeContainer : public ::testing::TestWithParam<size_t> {
|
||||
};
|
||||
|
||||
TEST_P(SerializeContainer, SizeHasVariableLength) {
|
||||
SerializationContext ctx{};
|
||||
auto emptyFnc = [](uint8_t &) {};
|
||||
|
||||
std::vector<uint8_t > src(GetParam());
|
||||
std::vector<uint8_t > res{};
|
||||
ctx.createSerializer().container(src, std::numeric_limits<size_t>::max(), emptyFnc);
|
||||
ctx.createDeserializer().container(res, std::numeric_limits<size_t>::max(), emptyFnc);
|
||||
|
||||
EXPECT_THAT(res.size(), Eq(src.size()));
|
||||
EXPECT_THAT(ctx.getBufferSize(), Eq(ctx.containerSizeSerializedBytesCount(src.size())));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(LargeContainerSize, SerializeContainer, ::testing::Values(0x01, 0x80, 0x4000));
|
||||
@@ -345,6 +345,7 @@ TEST(SerializeExtensionInheritance, CanSerializeAbstractClass) {
|
||||
ImplementedBase data{};
|
||||
data.x = 4;
|
||||
data.y = 2;
|
||||
data.exec();
|
||||
ImplementedBase res{};
|
||||
SerContext ctx{};
|
||||
ctx.createSerializer().object(data);
|
||||
|
||||
@@ -111,13 +111,13 @@ TEST_F(SerializeExtensionPointerSerialization, WhenPointersAreNullThenIsValid) {
|
||||
}
|
||||
#ifndef NDEBUG
|
||||
|
||||
TEST(SerializeExtensionPointer, WhenPointerLinkingContextIsNullThenAssert) {
|
||||
MyStruct1* data = nullptr;
|
||||
TEST(SerializeExtensionPointer, WhenPointerLinkingContextIsNullAndPointerIsNotNullThenAssert) {
|
||||
MyStruct1 tmp;
|
||||
MyStruct1* data = &tmp;
|
||||
//linking context
|
||||
PointerLinkingContext plctx1{};
|
||||
SerContext sctx1;
|
||||
EXPECT_DEATH(sctx1.createSerializer(nullptr).ext(data, PointerOwner{}), "");
|
||||
EXPECT_DEATH(sctx1.createDeserializer(nullptr).ext(data, PointerObserver{}), "");
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerSerialization, WhenPointerOwnerIsNotUniqueThenAssert) {
|
||||
|
||||
249
tests/serialization_ext_pointer_polymorphic_types.cpp
Normal file
249
tests/serialization_ext_pointer_polymorphic_types.cpp
Normal file
@@ -0,0 +1,249 @@
|
||||
//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 "serialization_test_utils.h"
|
||||
#include <bitsery/ext/pointer.h>
|
||||
|
||||
using bitsery::ext::BaseClass;
|
||||
using bitsery::ext::VirtualBaseClass;
|
||||
|
||||
using bitsery::ext::PointerOwner;
|
||||
using bitsery::ext::PointerObserver;
|
||||
using bitsery::ext::ReferencedByPointer;
|
||||
using bitsery::ext::PointerLinkingContext;
|
||||
using bitsery::ext::PointerType;
|
||||
|
||||
using testing::Eq;
|
||||
|
||||
using TContext = std::tuple<PointerLinkingContext, bitsery::ext::InheritanceContext>;
|
||||
using SerContext = BasicSerializationContext<bitsery::DefaultConfig, TContext>;
|
||||
|
||||
/*
|
||||
* base class
|
||||
*/
|
||||
struct Base {
|
||||
uint8_t x{};
|
||||
virtual ~Base() = default;
|
||||
};
|
||||
|
||||
template <typename S>
|
||||
void serialize(S& s, Base& o) {
|
||||
s.value1b(o.x);
|
||||
}
|
||||
|
||||
struct Derived1:virtual Base {
|
||||
uint8_t y1{};
|
||||
};
|
||||
|
||||
template <typename S>
|
||||
void serialize(S& s, Derived1& o) {
|
||||
s.ext(o, VirtualBaseClass<Base>{});
|
||||
s.value1b(o.y1);
|
||||
}
|
||||
|
||||
struct Derived2:virtual Base {
|
||||
uint8_t y2{};
|
||||
};
|
||||
|
||||
template <typename S>
|
||||
void serialize(S& s, Derived2& o) {
|
||||
s.ext(o, VirtualBaseClass<Base>{});
|
||||
s.value1b(o.y2);
|
||||
}
|
||||
|
||||
struct MultipleVirtualInheritance: Derived1, Derived2 {
|
||||
int8_t z{};
|
||||
MultipleVirtualInheritance() = default;
|
||||
|
||||
MultipleVirtualInheritance(uint8_t x_, uint8_t y1_, uint8_t y2_, uint8_t z_) {
|
||||
x = x_;
|
||||
y1 = y1_;
|
||||
y2 = y2_;
|
||||
z = z_;
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
void serialize(S& s) {
|
||||
s.ext(*this, BaseClass<Derived1>{});
|
||||
s.ext(*this, BaseClass<Derived2>{});
|
||||
s.value1b(z);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//define PolymorphicBase relationships for runtime polymorphism
|
||||
|
||||
namespace bitsery {
|
||||
namespace ext {
|
||||
|
||||
template <>
|
||||
struct PolymorphicBaseClass<Base>: DerivedClasses<Derived1, Derived2> {};
|
||||
|
||||
template <>
|
||||
struct PolymorphicBaseClass<Derived1>: DerivedClasses<MultipleVirtualInheritance> {};
|
||||
|
||||
template <>
|
||||
struct PolymorphicBaseClass<Derived2>: DerivedClasses<MultipleVirtualInheritance> {};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SerializeExtensionPointerPolymorphicTypes: public testing::Test {
|
||||
public:
|
||||
|
||||
TContext plctx1{};
|
||||
SerContext sctx1{};
|
||||
|
||||
typename SerContext::TSerializer& createSerializer() {
|
||||
return sctx1.createSerializer(&plctx1);
|
||||
}
|
||||
|
||||
typename SerContext::TDeserializer& createDeserializer() {
|
||||
return sctx1.createDeserializer(&plctx1);
|
||||
}
|
||||
|
||||
bool isPointerContextValid() {
|
||||
return std::get<0>(plctx1).isValid();
|
||||
}
|
||||
|
||||
virtual void TearDown() override {
|
||||
EXPECT_TRUE(isPointerContextValid());
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, Data0Result0) {
|
||||
Base* baseData = nullptr;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
Base* baseRes = nullptr;
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
|
||||
EXPECT_THAT(baseRes, ::testing::IsNull());
|
||||
EXPECT_THAT(baseData, ::testing::IsNull());
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, Data0Result1) {
|
||||
Base* baseData = nullptr;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
|
||||
Base* baseRes = new Derived1{};
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
|
||||
EXPECT_THAT(baseRes, ::testing::IsNull());
|
||||
EXPECT_THAT(baseData, ::testing::IsNull());
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, Data1Result0) {
|
||||
Derived1 d1{};
|
||||
d1.x = 3;
|
||||
d1.y1 = 78;
|
||||
Base* baseData = &d1;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
Base* baseRes = nullptr;
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
|
||||
auto* data = dynamic_cast<Derived1*>(baseData);
|
||||
auto* res = dynamic_cast<Derived1*>(baseRes);
|
||||
|
||||
EXPECT_THAT(baseRes, ::testing::NotNull());
|
||||
EXPECT_THAT(data, ::testing::NotNull());
|
||||
EXPECT_THAT(res, ::testing::NotNull());
|
||||
EXPECT_THAT(res->x, Eq(data->x));
|
||||
EXPECT_THAT(res->y1, Eq(data->y1));
|
||||
delete baseRes;
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, Data1Result1) {
|
||||
Derived1 d1{};
|
||||
d1.x = 3;
|
||||
d1.y1 = 78;
|
||||
Base* baseData = &d1;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
Base* baseRes = &d1;
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
|
||||
auto* data = dynamic_cast<Derived1*>(baseData);
|
||||
auto* res = dynamic_cast<Derived1*>(baseRes);
|
||||
|
||||
EXPECT_THAT(baseRes, ::testing::NotNull());
|
||||
EXPECT_THAT(data, ::testing::NotNull());
|
||||
EXPECT_THAT(res, ::testing::NotNull());
|
||||
EXPECT_THAT(res->x, Eq(data->x));
|
||||
EXPECT_THAT(res->y1, Eq(data->y1));
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, ComplexTypeData1Result0) {
|
||||
MultipleVirtualInheritance md1{};
|
||||
md1.x = 3;
|
||||
md1.y1 = 78;
|
||||
md1.y2 = 14;
|
||||
md1.z = -33;
|
||||
Base* baseData = &md1;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
Base* baseRes = nullptr;
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
|
||||
auto* data = dynamic_cast<MultipleVirtualInheritance*>(baseData);
|
||||
auto* res = dynamic_cast<MultipleVirtualInheritance*>(baseRes);
|
||||
|
||||
EXPECT_THAT(baseRes, ::testing::NotNull());
|
||||
EXPECT_THAT(data, ::testing::NotNull());
|
||||
EXPECT_THAT(res, ::testing::NotNull());
|
||||
EXPECT_THAT(res->x, Eq(data->x));
|
||||
EXPECT_THAT(res->y1, Eq(data->y1));
|
||||
EXPECT_THAT(res->y2, Eq(data->y2));
|
||||
EXPECT_THAT(res->z, Eq(data->z));
|
||||
delete baseRes;
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, WhenResultIsDifferentTypeThenRecreate) {
|
||||
MultipleVirtualInheritance md1{};
|
||||
md1.x = 3;
|
||||
md1.y1 = 78;
|
||||
md1.y2 = 14;
|
||||
md1.z = -33;
|
||||
Base* baseData = &md1;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
Base* baseRes = new Derived1{};
|
||||
EXPECT_THAT(dynamic_cast<MultipleVirtualInheritance*>(baseRes), ::testing::IsNull());
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
EXPECT_THAT(dynamic_cast<MultipleVirtualInheritance*>(baseRes), ::testing::NotNull());
|
||||
delete baseRes;
|
||||
}
|
||||
|
||||
//struct UnknownType:Base {
|
||||
//};
|
||||
//
|
||||
//template <typename S>
|
||||
//void serialize(S& s, UnknownType& o) {
|
||||
// s.ext(o, VirtualBaseClass<Base>{});
|
||||
//}
|
||||
|
||||
// todo reimplement whole polymorphism thing, because with current solution this test fails
|
||||
//TEST(SerializeExtensionPointerPolymorphicTypesErrors, WhenSerializingUnknownTypeThenAssert) {
|
||||
// TContext plctx1{};
|
||||
// SerContext sctx1{};
|
||||
// UnknownType obj{};
|
||||
// UnknownType* unknownPtr = &obj;
|
||||
// EXPECT_DEATH(sctx1.createSerializer(&plctx1).ext(unknownPtr, PointerOwner{}), "");
|
||||
//}
|
||||
@@ -29,7 +29,7 @@ using bitsery::details::RangeSpec;
|
||||
using bitsery::ext::BitsConstraint;
|
||||
using bitsery::ext::ValueRange;
|
||||
|
||||
#if __cplusplus > 201402L
|
||||
#if __cplusplus > 201103L
|
||||
|
||||
TEST(SerializeExtensionValueRange, RequiredBitsIsConstexpr) {
|
||||
constexpr RangeSpec<int> r1{0, 31};
|
||||
|
||||
Reference in New Issue
Block a user