Skip to content
Commits on Source (8)
......@@ -18,14 +18,16 @@
*.suo
*.user
*/x64
*/Debug*
*/Release*
*\Debug*
*\Release*
*.log
*.tlog*
*.obj
*.VC.db
*.VC.VC.opendb
*.pdb
*.idb
*\build_*
# misc files mostly used for testing
out.txt
......
# Portions of this file based on https://github.com/Microsoft/GSL/blob/master/.travis.yml
language: cpp
os: linux
dist: trusty
sudo: false
group: beta
addons:
apt:
sources: &default_sources
- ubuntu-toolchain-r-test
- libboost-latest
packages: &default_packages
- libboost-serialization-dev
- libboost-dev
matrix:
include:
# |---------- LINUX GCC ----------|
- compiler: g++-4.7
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=g++-4.7"]
addons:
apt:
sources: *default_sources
packages: ['g++-4.7', *default_packages]
- compiler: g++-4.8
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=g++-4.8"]
addons:
apt:
sources: *default_sources
packages: ['g++-4.8', *default_packages]
- compiler: g++-4.9
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=g++-4.9"]
addons:
apt:
sources: *default_sources
packages: ['g++-4.9', *default_packages]
- compiler: g++-5
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=g++-5"]
addons:
apt:
sources: *default_sources
packages: ['g++-5', *default_packages]
- compiler: g++-5
name: "g++-5 multilib"
env: ["COMPILER=g++-5"]
addons:
apt:
sources: *default_sources
packages: ['gcc-multilib g++-5-multilib linux-libc-dev', *default_packages]
- compiler: g++-6
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=g++-6"]
addons:
apt:
sources: *default_sources
packages: ['g++-6', *default_packages]
- compiler: g++-7
name: "g++-7 c++17"
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON -DCMAKE_CXX_STANDARD=17'", "COMPILER=g++-7"]
addons:
apt:
sources: *default_sources
packages: ['g++-7', *default_packages]
# |---------- LINUX GCC ----------|
- dist: xenial
compiler: g++-8
name: "g++-8 c++17"
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON -DCMAKE_CXX_STANDARD=17'", "COMPILER=g++-8"]
addons:
apt:
sources: *default_sources
packages: ['g++-8', *default_packages]
# |---------- LINUX CLANG ----------|
- compiler: clang++-3.5
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=clang++-3.5"]
addons:
apt:
sources: [*default_sources, llvm-toolchain-precise-3.5]
packages: ['clang-3.5', *default_packages]
- compiler: clang++-3.6
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=clang++-3.6"]
addons:
apt:
sources: [*default_sources, llvm-toolchain-precise-3.6]
packages: ['clang-3.6', *default_packages]
- compiler: clang++-3.7
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=clang++-3.7"]
addons:
apt:
sources: [*default_sources, llvm-toolchain-precise-3.7]
packages: ['clang-3.7', *default_packages]
- compiler: clang++-3.8
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=clang++-3.8"]
addons:
apt:
sources: [*default_sources, llvm-toolchain-precise-3.8]
packages: ['clang-3.8', *default_packages]
- compiler: clang++-3.9
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=clang++-3.9"]
addons:
apt:
sources: [*default_sources, llvm-toolchain-precise-3.9]
packages: ['clang-3.9', *default_packages]
- compiler: clang++-4.0
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=clang++-4.0"]
addons:
apt:
sources: [*default_sources, llvm-toolchain-trusty-4.0]
packages: ['clang-4.0', 'g++-5', *default_packages]
- compiler: clang++-5.0
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON'", "COMPILER=clang++-5.0"]
addons: &clang50
apt:
packages:
- clang-5.0
- g++-7
- *default_packages
sources:
- 'ubuntu-toolchain-r-test'
- 'boost-latest'
- *default_sources
- llvm-toolchain-trusty-5.0
- sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-5.0 main'
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
- env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON -DCMAKE_CXX_STANDARD=17'", "COMPILER=clang++-5.0"]
name: "clang++-5.0 c++17"
addons: *clang50
- compiler: clang++-7
name: "clang++-7 c++17"
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON -DCMAKE_CXX_STANDARD=17'", "COMPILER=clang++-7"]
addons:
apt:
packages:
- 'g++-multilib'
- 'libboost-serialization-dev'
# - 'libboost-test-dev'
- clang-7
- g++-7
- *default_packages
sources:
- *default_sources
- llvm-toolchain-trusty-7
compiler:
- gcc
- compiler: clang++-8
name: "clang++-8 c++17 libc++"
env: ["CMAKE_OPTIONS='-DSKIP_PORTABILITY_TEST=ON -DCMAKE_CXX_STANDARD=17 -DCLANG_USE_LIBCPP=ON -DSKIP_PERFORMANCE_COMPARISON=ON'", "COMPILER=clang++-8"]
addons:
apt:
packages:
- clang-8
- g++-8
- libc++-8-dev
- libc++abi-8-dev
- *default_packages
sources:
- *default_sources
- llvm-toolchain-trusty-8
matrix:
include:
- os: linux
compiler: clang
env: CMAKE_OPTIONS="-DSKIP_PORTABILITY_TEST=ON"
# # |---------- LINUX CLANG (32-bit) ----------|
# # Doesn't work.
# - compiler: clang++
# addons:
# apt:
# sources: [*default_sources]
# packages: ['clang', 'gcc-multilib', 'g++-multilib', *default_packages]
# TODO: Add an entry for valgrind
# after_script: make valgrind
- os: osx
# |---------- OSX CLANG ----------|
- compiler: clang++
os: osx
osx_image: xcode7.3
env: COMPILER=clang++
- compiler: clang++
os: osx
osx_image: xcode8
compiler: clang
env: COMPILER=clang++
# # Missing CMake
# - compiler: clang++
# os: osx
# osx_image: xcode8.1
- compiler: clang++
os: osx
osx_image: xcode8.2
env: COMPILER=clang++
- compiler: clang++
os: osx
osx_image: xcode8.3
env: COMPILER=clang++
- compiler: clang++
env: ["CMAKE_OPTIONS='-DWITH_WERROR=OFF'"]
os: osx
osx_image: xcode9
env: COMPILER=clang++
- compiler: clang++
env: ["CMAKE_OPTIONS='-DWITH_WERROR=OFF'"]
os: osx
osx_image: xcode10
env: COMPILER=clang++
install:
# Set the ${CXX} variable properly
- export CXX=${COMPILER}
- ${CXX} --version
# Dependencies required by the CI are installed in ${TRAVIS_BUILD_DIR}/deps/
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
- mkdir -p "${DEPS_DIR}"
- cd "${DEPS_DIR}"
- JOBS=2
# [linux]: Install the right version of libc++
- |
LLVM_INSTALL=${DEPS_DIR}/llvm/install
# if in linux and compiler clang and llvm not installed
if [[ "${TRAVIS_OS_NAME}" == "linux" && "${CXX%%+*}" == "clang" && -n "$(ls -A ${LLVM_INSTALL})" ]]; then
if [[ "${CXX}" == "clang++-3.6" ]]; then LLVM_VERSION="3.6.2";
elif [[ "${CXX}" == "clang++-3.7" ]]; then LLVM_VERSION="3.7.1";
elif [[ "${CXX}" == "clang++-3.8" ]]; then LLVM_VERSION="3.8.1";
elif [[ "${CXX}" == "clang++-3.9" ]]; then LLVM_VERSION="3.9.1";
fi
LLVM_URL="http://llvm.org/releases/${LLVM_VERSION}/llvm-${LLVM_VERSION}.src.tar.xz"
LIBCXX_URL="http://llvm.org/releases/${LLVM_VERSION}/libcxx-${LLVM_VERSION}.src.tar.xz"
LIBCXXABI_URL="http://llvm.org/releases/${LLVM_VERSION}/libcxxabi-${LLVM_VERSION}.src.tar.xz"
mkdir -p llvm llvm/build llvm/projects/libcxx llvm/projects/libcxxabi
travis_retry wget -O - ${LLVM_URL} | tar --strip-components=1 -xJ -C llvm
travis_retry wget -O - ${LIBCXX_URL} | tar --strip-components=1 -xJ -C llvm/projects/libcxx
travis_retry wget -O - ${LIBCXXABI_URL} | tar --strip-components=1 -xJ -C llvm/projects/libcxxabi
(cd llvm/build && cmake .. -DCMAKE_INSTALL_PREFIX=${LLVM_INSTALL})
(cd llvm/build/projects/libcxx && make install -j2)
(cd llvm/build/projects/libcxxabi && make install -j2)
export CXXFLAGS="-isystem ${LLVM_INSTALL}/include/c++/v1"
export LDFLAGS="-L ${LLVM_INSTALL}/lib -l c++ -l c++abi"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${LLVM_INSTALL}/lib"
fi
script:
- cd "${TRAVIS_BUILD_DIR}"
- if [[ "${COMPILERCC}" != "" ]]; then export CC="${COMPILERCC}"; fi
- if [[ "${COMPILER}" != "" ]]; then export CXX="${COMPILER}"; fi
- $CXX --version
- cmake --version
- mkdir build && cd build
- cmake ${CMAKE_OPTIONS} .. && make -j4
- ctest . --output-on-failure
branches:
only:
- develop
- master
......@@ -2,6 +2,7 @@ cmake_minimum_required (VERSION 2.6.2)
project (cereal)
option(SKIP_PORTABILITY_TEST "Skip portability (32 bit) tests" OFF)
option(SKIP_PERFORMANCE_COMPARISON "Skip building performance comparison (requires boost)" OFF)
if(NOT CMAKE_VERSION VERSION_LESS 3.0) # installing cereal requires INTERFACE lib
option(JUST_INSTALL_CEREAL "Don't do anything besides installing the library" OFF)
endif()
......@@ -22,14 +23,27 @@ else()
if(WITH_WERROR)
set(CMAKE_CXX_FLAGS "-Werror ${CMAKE_CXX_FLAGS}")
endif(WITH_WERROR)
option(CLANG_USE_LIBCPP "Use libc++ for clang compilation" OFF)
if(CLANG_USE_LIBCPP)
set(CMAKE_CXX_FLAGS "-stdlib=libc++ ${CMAKE_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
endif()
if(CMAKE_VERSION VERSION_LESS 3.1)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
else()
if(NOT DEFINED CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "98")
set(CMAKE_CXX_STANDARD 11)
endif()
if(CMAKE_CXX_STANDARD GREATER 14)
cmake_minimum_required(VERSION 3.8)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
endif()
if(NOT CMAKE_VERSION VERSION_LESS 3.0)
......@@ -51,6 +65,10 @@ endif()
include_directories(./include)
if(NOT CMAKE_VERSION VERSION_LESS 3.12)
cmake_policy(VERSION 3.12)
endif()
# Boost serialization for performance sandbox
find_package(Boost COMPONENTS serialization)
......
......@@ -77,8 +77,8 @@ cereal is licensed under the [BSD license](http://opensource.org/licenses/BSD-3-
## cereal build status
* develop : [![Build Status](https://travis-ci.org/USCiLab/cereal.png?branch=develop)](https://travis-ci.org/USCiLab/cereal)
[![Build status](https://ci.appveyor.com/api/projects/status/91aou6smj36or0vb/branch/develop?svg=true)](https://ci.appveyor.com/project/AzothAmmo/cereal/branch/develop)
* master : [![Build Status](https://travis-ci.com/USCiLab/cereal.svg?branch=master)](https://travis-ci.com/USCiLab/cereal)
[![Build status](https://ci.appveyor.com/api/projects/status/91aou6smj36or0vb/branch/master?svg=true)](https://ci.appveyor.com/project/AzothAmmo/cereal/branch/master)
---
......
......@@ -5,7 +5,7 @@ pull_requests:
branches:
only:
- develop
- master
configuration:
- Debug
......@@ -13,9 +13,18 @@ configuration:
environment:
matrix:
- VS_VERSION_MAJOR: 12
- VS_VERSION_MAJOR: 14
BOOST_ROOT: C:\Libraries\boost_1_59_0
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
VS_VERSION_MAJOR: 12
BOOST_ROOT: C:\Libraries\boost_1_58_0
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
VS_VERSION_MAJOR: 14
BOOST_ROOT: C:\Libraries\boost_1_60_0
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
VS_VERSION_MAJOR: 15
BOOST_ROOT: C:\Libraries\boost_1_66_0
# - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 Preview
# VS_VERSION_MAJOR: 16
# BOOST_ROOT: C:\Libraries\boost_1_66_0
platform:
- Win32
......
libcereal (1.3.0-1) unstable; urgency=medium
* New upstream version
* debhelper-compat 12
* Standards-Version: 4.4.0
-- Michael R. Crusoe <michael.crusoe@gmail.com> Sun, 27 Oct 2019 17:23:31 +0100
libcereal (1.2.2-3) unstable; urgency=medium
* Team upload.
......
......@@ -3,12 +3,12 @@ Maintainer: Debian Med Packaging Team <debian-med-packaging@lists.alioth.debian.
Uploaders: Michael R. Crusoe <michael.crusoe@gmail.com>
Section: libdevel
Priority: optional
Build-Depends: debhelper (>= 11~),
Build-Depends: debhelper-compat (= 12),
libboost-serialization-dev,
libboost-test-dev,
cmake,
doxygen
Standards-Version: 4.2.1
Standards-Version: 4.4.0
Vcs-Browser: https://salsa.debian.org/med-team/libcereal
Vcs-Git: https://salsa.debian.org/med-team/libcereal.git
Homepage: http://uscilab.github.io/cereal/
......
......@@ -3,8 +3,8 @@ Description: build just the tests, as an option for autopkgtest later
With assistance from Kevin D. Murray; thanks!
--- libcereal.orig/CMakeLists.txt
+++ libcereal/CMakeLists.txt
@@ -32,6 +32,8 @@
endif()
@@ -46,6 +46,8 @@
endif()
+option(ONLY_TESTS "Don't build docs, or sandbox" OFF)
......@@ -12,21 +12,7 @@ With assistance from Kevin D. Murray; thanks!
if(NOT CMAKE_VERSION VERSION_LESS 3.0)
add_library(cereal INTERFACE)
target_include_directories(cereal INTERFACE
@@ -49,18 +51,21 @@
return()
endif()
-include_directories(./include)
# Boost serialization for performance sandbox
find_package(Boost COMPONENTS serialization)
if(Boost_FOUND)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
+ if(NOT ONLY_TEST)
+ include_directories(./include)
+ endif(NOT ONLY_TEST)
endif(Boost_FOUND)
@@ -78,7 +80,8 @@
enable_testing()
add_subdirectory(unittests)
......
Author: Andreas Tille <tille@debian.org>
last-Update: Thu, 31 Aug 2017 23:24:39 +0200
Bug-Debian: https://bugs.debian.org/853486
Description: Do not set -Werror which makes build fail when using gcc-7
--- libcereal.orig/CMakeLists.txt
+++ libcereal/CMakeLists.txt
@@ -18,10 +18,6 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /W3 /WX")
else()
set(CMAKE_CXX_FLAGS "-Wall -g -Wextra -Wshadow -pedantic -Wold-style-cast ${CMAKE_CXX_FLAGS}")
- option(WITH_WERROR "Compile with '-Werror' C++ compiler flag" ON)
- if(WITH_WERROR)
- set(CMAKE_CXX_FLAGS "-Werror ${CMAKE_CXX_FLAGS}")
- endif(WITH_WERROR)
if(CMAKE_VERSION VERSION_LESS 3.1)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
else()
reproducible-doxygen
drop-google-analytics
add-only-tests-target
no_-Werror.patch
/*! \file access.hpp
\brief Access control, default construction, and serialization disambiguation */
\brief Access control and default construction */
/*
Copyright (c) 2014, Randolph Voorhies, Shane Grant
All rights reserved.
......@@ -35,6 +35,7 @@
#include <functional>
#include "cereal/macros.hpp"
#include "cereal/specialize.hpp"
#include "cereal/details/helpers.hpp"
namespace cereal
......@@ -110,6 +111,7 @@ namespace cereal
// forward decl for construct
//! @cond PRIVATE_NEVERDEFINED
namespace memory_detail{ template <class Ar, class T> struct LoadAndConstructLoadWrapper; }
namespace boost_variant_detail{ template <class Ar, class T> struct LoadAndConstructLoadWrapper; }
//! @endcond
//! Used to construct types with no default constructor
......@@ -202,7 +204,8 @@ namespace cereal
}
private:
template <class A, class B> friend struct ::cereal::memory_detail::LoadAndConstructLoadWrapper;
template <class Ar, class TT> friend struct ::cereal::memory_detail::LoadAndConstructLoadWrapper;
template <class Ar, class TT> friend struct ::cereal::boost_variant_detail::LoadAndConstructLoadWrapper;
construct( T * p ) : itsPtr( p ), itsEnableSharedRestoreFunction( [](){} ), itsValid( false ) {}
construct( T * p, std::function<void()> enableSharedFunc ) : // g++4.7 ice with default lambda to std func
......@@ -331,106 +334,6 @@ namespace cereal
}
}; // end class access
// ######################################################################
//! A specifier used in conjunction with cereal::specialize to disambiguate
//! serialization in special cases
/*! @relates specialize
@ingroup Access */
enum class specialization
{
member_serialize, //!< Force the use of a member serialize function
member_load_save, //!< Force the use of a member load/save pair
member_load_save_minimal, //!< Force the use of a member minimal load/save pair
non_member_serialize, //!< Force the use of a non-member serialize function
non_member_load_save, //!< Force the use of a non-member load/save pair
non_member_load_save_minimal //!< Force the use of a non-member minimal load/save pair
};
//! A class used to disambiguate cases where cereal cannot detect a unique way of serializing a class
/*! cereal attempts to figure out which method of serialization (member vs. non-member serialize
or load/save pair) at compile time. If for some reason cereal cannot find a non-ambiguous way
of serializing a type, it will produce a static assertion complaining about this.
This can happen because you have both a serialize and load/save pair, or even because a base
class has a serialize (public or private with friend access) and a derived class does not
overwrite this due to choosing some other serialization type.
Specializing this class will tell cereal to explicitly use the serialization type you specify
and it will not complain about ambiguity in its compile time selection. However, if cereal detects
an ambiguity in specializations, it will continue to issue a static assertion.
@code{.cpp}
class MyParent
{
friend class cereal::access;
template <class Archive>
void serialize( Archive & ar ) {}
};
// Although serialize is private in MyParent, to cereal::access it will look public,
// even through MyDerived
class MyDerived : public MyParent
{
public:
template <class Archive>
void load( Archive & ar ) {}
template <class Archive>
void save( Archive & ar ) {}
};
// The load/save pair in MyDerived is ambiguous because serialize in MyParent can
// be accessed from cereal::access. This looks the same as making serialize public
// in MyParent, making it seem as though MyDerived has both a serialize and a load/save pair.
// cereal will complain about this at compile time unless we disambiguate:
namespace cereal
{
// This struct specialization will tell cereal which is the right way to serialize the ambiguity
template <class Archive> struct specialize<Archive, MyDerived, cereal::specialization::member_load_save> {};
// If we only had a disambiguation for a specific archive type, it would look something like this
template <> struct specialize<cereal::BinaryOutputArchive, MyDerived, cereal::specialization::member_load_save> {};
}
@endcode
You can also choose to use the macros CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES or
CEREAL_SPECIALIZE_FOR_ARCHIVE if you want to type a little bit less.
@tparam T The type to specialize the serialization for
@tparam S The specialization type to use for T
@ingroup Access */
template <class Archive, class T, specialization S>
struct specialize : public std::false_type {};
//! Convenient macro for performing specialization for all archive types
/*! This performs specialization for the specific type for all types of archives.
This macro should be placed at the global namespace.
@code{cpp}
struct MyType {};
CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( MyType, cereal::specialization::member_load_save );
@endcode
@relates specialize
@ingroup Access */
#define CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( Type, Specialization ) \
namespace cereal { template <class Archive> struct specialize<Archive, Type, Specialization> {}; }
//! Convenient macro for performing specialization for a single archive type
/*! This performs specialization for the specific type for a single type of archive.
This macro should be placed at the global namespace.
@code{cpp}
struct MyType {};
CEREAL_SPECIALIZE_FOR_ARCHIVE( cereal::XMLInputArchive, MyType, cereal::specialization::member_load_save );
@endcode
@relates specialize
@ingroup Access */
#define CEREAL_SPECIALIZE_FOR_ARCHIVE( Archive, Type, Specialization ) \
namespace cereal { template <> struct specialize<Archive, Type, Specialization> {}; }
// ######################################################################
// Deferred Implementation, see construct for more information
template <class T> template <class ... Args> inline
......
......@@ -62,9 +62,9 @@ namespace cereal
~BinaryOutputArchive() CEREAL_NOEXCEPT = default;
//! Writes size bytes of data to the output stream
void saveBinary( const void * data, std::size_t size )
void saveBinary( const void * data, std::streamsize size )
{
auto const writtenSize = static_cast<std::size_t>( itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size ) );
auto const writtenSize = itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size );
if(writtenSize != size)
throw Exception("Failed to write " + std::to_string(size) + " bytes to output stream! Wrote " + std::to_string(writtenSize));
......@@ -97,9 +97,9 @@ namespace cereal
~BinaryInputArchive() CEREAL_NOEXCEPT = default;
//! Reads size bytes of data from the input stream
void loadBinary( void * const data, std::size_t size )
void loadBinary( void * const data, std::streamsize size )
{
auto const readSize = static_cast<std::size_t>( itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size ) );
auto const readSize = itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size );
if(readSize != size)
throw Exception("Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize));
......@@ -148,14 +148,14 @@ namespace cereal
template <class T> inline
void CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive & ar, BinaryData<T> const & bd)
{
ar.saveBinary( bd.data, static_cast<std::size_t>( bd.size ) );
ar.saveBinary( bd.data, static_cast<std::streamsize>( bd.size ) );
}
//! Loading binary data
template <class T> inline
void CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive & ar, BinaryData<T> & bd)
{
ar.loadBinary(bd.data, static_cast<std::size_t>(bd.size));
ar.loadBinary(bd.data, static_cast<std::streamsize>( bd.size ) );
}
} // namespace cereal
......
......@@ -40,6 +40,11 @@ namespace cereal
{ RapidJSONException( const char * what_ ) : Exception( what_ ) {} };
}
// Inform rapidjson that assert will throw
#ifndef CEREAL_RAPIDJSON_ASSERT_THROWS
#define CEREAL_RAPIDJSON_ASSERT_THROWS
#endif // CEREAL_RAPIDJSON_ASSERT_THROWS
// Override rapidjson assertions to throw exceptions by default
#ifndef CEREAL_RAPIDJSON_ASSERT
#define CEREAL_RAPIDJSON_ASSERT(x) if(!(x)){ \
......@@ -47,8 +52,14 @@ namespace cereal
#endif // RAPIDJSON_ASSERT
// Enable support for parsing of nan, inf, -inf
#ifndef CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS
#define CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNanAndInfFlag
#endif
// Enable support for parsing of nan, inf, -inf
#ifndef CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS
#define CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS kParseFullPrecisionFlag | kParseNanAndInfFlag
#endif
#include "cereal/external/rapidjson/prettywriter.h"
#include "cereal/external/rapidjson/ostreamwrapper.h"
......@@ -209,11 +220,13 @@ namespace cereal
{
case NodeType::StartArray:
itsWriter.StartArray();
// fall through
case NodeType::InArray:
itsWriter.EndArray();
break;
case NodeType::StartObject:
itsWriter.StartObject();
// fall through
case NodeType::InObject:
itsWriter.EndObject();
break;
......@@ -477,7 +490,7 @@ namespace cereal
}
Iterator(ValueIterator begin, ValueIterator end) :
itsValueItBegin(begin), itsValueItEnd(end), itsIndex(0), itsType(Value)
itsValueItBegin(begin), itsIndex(0), itsType(Value)
{
if( std::distance( begin, end ) == 0 )
itsType = Null_;
......@@ -532,7 +545,7 @@ namespace cereal
private:
MemberIterator itsMemberItBegin, itsMemberItEnd; //!< The member iterator (object)
ValueIterator itsValueItBegin, itsValueItEnd; //!< The value iterator (array)
ValueIterator itsValueItBegin; //!< The value iterator (array)
size_t itsIndex; //!< The current index of this iterator
enum Type {Value, Member, Null_} itsType; //!< Whether this holds values (array) or members (objects) or nothing
};
......@@ -751,6 +764,31 @@ namespace cereal
void epilogue( JSONInputArchive &, NameValuePair<T> const & )
{ }
// ######################################################################
//! Prologue for deferred data for JSON archives
/*! Do nothing for the defer wrapper */
template <class T> inline
void prologue( JSONOutputArchive &, DeferredData<T> const & )
{ }
//! Prologue for deferred data for JSON archives
template <class T> inline
void prologue( JSONInputArchive &, DeferredData<T> const & )
{ }
// ######################################################################
//! Epilogue for deferred for JSON archives
/*! NVPs do not start or finish nodes - they just set up the names */
template <class T> inline
void epilogue( JSONOutputArchive &, DeferredData<T> const & )
{ }
//! Epilogue for deferred for JSON archives
/*! Do nothing for the defer wrapper */
template <class T> inline
void epilogue( JSONInputArchive &, DeferredData<T> const & )
{ }
// ######################################################################
//! Prologue for SizeTags for JSON archives
/*! SizeTags are strictly ignored for JSON, they just indicate
......
......@@ -128,19 +128,19 @@ namespace cereal
~PortableBinaryOutputArchive() CEREAL_NOEXCEPT = default;
//! Writes size bytes of data to the output stream
template <std::size_t DataSize> inline
void saveBinary( const void * data, std::size_t size )
template <std::streamsize DataSize> inline
void saveBinary( const void * data, std::streamsize size )
{
std::size_t writtenSize = 0;
std::streamsize writtenSize = 0;
if( itsConvertEndianness )
{
for( std::size_t i = 0; i < size; i += DataSize )
for( std::size_t j = 0; j < DataSize; ++j )
writtenSize += static_cast<std::size_t>( itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ) + DataSize - j - 1 + i, 1 ) );
for( std::streamsize i = 0; i < size; i += DataSize )
for( std::streamsize j = 0; j < DataSize; ++j )
writtenSize += itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ) + DataSize - j - 1 + i, 1 );
}
else
writtenSize = static_cast<std::size_t>( itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size ) );
writtenSize = itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size );
if(writtenSize != size)
throw Exception("Failed to write " + std::to_string(size) + " bytes to output stream! Wrote " + std::to_string(writtenSize));
......@@ -235,11 +235,11 @@ namespace cereal
/*! @param data The data to save
@param size The number of bytes in the data
@tparam DataSize T The size of the actual type of the data elements being loaded */
template <std::size_t DataSize> inline
void loadBinary( void * const data, std::size_t size )
template <std::streamsize DataSize> inline
void loadBinary( void * const data, std::streamsize size )
{
// load data
auto const readSize = static_cast<std::size_t>( itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size ) );
auto const readSize = itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size );
if(readSize != size)
throw Exception("Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize));
......@@ -248,7 +248,7 @@ namespace cereal
if( itsConvertEndianness )
{
std::uint8_t * ptr = reinterpret_cast<std::uint8_t*>( data );
for( std::size_t i = 0; i < size; i += DataSize )
for( std::streamsize i = 0; i < size; i += DataSize )
portable_binary_detail::swap_bytes<DataSize>( ptr + i );
}
}
......@@ -308,7 +308,7 @@ namespace cereal
(std::is_floating_point<TT>::value && std::numeric_limits<TT>::is_iec559),
"Portable binary only supports IEEE 754 standardized floating point" );
ar.template saveBinary<sizeof(TT)>( bd.data, static_cast<std::size_t>( bd.size ) );
ar.template saveBinary<sizeof(TT)>( bd.data, static_cast<std::streamsize>( bd.size ) );
}
//! Loading binary data from portable binary
......@@ -320,7 +320,7 @@ namespace cereal
(std::is_floating_point<TT>::value && std::numeric_limits<TT>::is_iec559),
"Portable binary only supports IEEE 754 standardized floating point" );
ar.template loadBinary<sizeof(TT)>( bd.data, static_cast<std::size_t>( bd.size ) );
ar.template loadBinary<sizeof(TT)>( bd.data, static_cast<std::streamsize>( bd.size ) );
}
} // namespace cereal
......
......@@ -101,31 +101,58 @@ namespace cereal
//! @{
//! A class containing various advanced options for the XML archive
/*! Options can either be directly passed to the constructor, or chained using the
modifier functions for an interface analogous to named parameters */
class Options
{
public:
//! Default options
static Options Default(){ return Options(); }
//! Default options with no indentation
static Options NoIndent(){ return Options( std::numeric_limits<double>::max_digits10, false ); }
//! Specify specific options for the XMLOutputArchive
/*! @param precision The precision used for floating point numbers
@param indent Whether to indent each line of XML
@param outputType Whether to output the type of each serialized object as an attribute */
explicit Options( int precision = std::numeric_limits<double>::max_digits10,
bool indent = true,
bool outputType = false ) :
itsPrecision( precision ),
itsIndent( indent ),
itsOutputType( outputType ) { }
/*! @param precision_ The precision used for floating point numbers
@param indent_ Whether to indent each line of XML
@param outputType_ Whether to output the type of each serialized object as an attribute
@param sizeAttributes_ Whether dynamically sized containers output the size=dynamic attribute */
explicit Options( int precision_ = std::numeric_limits<double>::max_digits10,
bool indent_ = true,
bool outputType_ = false,
bool sizeAttributes_ = true ) :
itsPrecision( precision_ ),
itsIndent( indent_ ),
itsOutputType( outputType_ ),
itsSizeAttributes( sizeAttributes_ )
{ }
/*! @name Option Modifiers
An interface for setting option settings analogous to named parameters.
@code{cpp}
cereal::XMLOutputArchive ar( myStream,
cereal::XMLOutputArchive::Options()
.indent(true)
.sizeAttributes(false) );
@endcode
*/
//! @{
//! Sets the precision used for floaing point numbers
Options & precision( int value ){ itsPrecision = value; return * this; }
//! Whether to indent each line of XML
Options & indent( bool enable ){ itsIndent = enable; return *this; }
//! Whether to output the type of each serialized object as an attribute
Options & outputType( bool enable ){ itsOutputType = enable; return *this; }
//! Whether dynamically sized containers (e.g. vector) output the size=dynamic attribute
Options & sizeAttributes( bool enable ){ itsSizeAttributes = enable; return *this; }
//! @}
private:
friend class XMLOutputArchive;
int itsPrecision;
bool itsIndent;
bool itsOutputType;
bool itsSizeAttributes;
};
//! Construct, outputting to the provided stream upon destruction
......@@ -137,7 +164,8 @@ namespace cereal
OutputArchive<XMLOutputArchive>(this),
itsStream(stream),
itsOutputType( options.itsOutputType ),
itsIndent( options.itsIndent )
itsIndent( options.itsIndent ),
itsSizeAttributes(options.itsSizeAttributes)
{
// rapidxml will delete all allocations when xml_document is cleared
auto node = itsXML.allocate_node( rapidxml::node_declaration );
......@@ -182,7 +210,7 @@ namespace cereal
itsNodes.top().node->append_attribute( itsXML.allocate_attribute( "type", "cereal binary data" ) );
finishNode();
};
}
//! @}
/*! @name Internal Functionality
......@@ -289,6 +317,8 @@ namespace cereal
itsNodes.top().node->append_attribute( itsXML.allocate_attribute( namePtr, valuePtr ) );
}
bool hasSizeAttributes() const { return itsSizeAttributes; }
protected:
//! A struct that contains metadata about a node
struct NodeInfo
......@@ -330,6 +360,7 @@ namespace cereal
std::ostringstream itsOS; //!< Used to format strings internally
bool itsOutputType; //!< Controls whether type information is printed
bool itsIndent; //!< Controls whether indenting is used
bool itsSizeAttributes; //!< Controls whether lists have a size attribute
}; // XMLOutputArchive
// ######################################################################
......@@ -436,7 +467,7 @@ namespace cereal
std::memcpy( data, decoded.data(), decoded.size() );
finishNode();
};
}
//! @}
/*! @name Internal Functionality
......@@ -758,14 +789,42 @@ namespace cereal
void epilogue( XMLInputArchive &, NameValuePair<T> const & )
{ }
// ######################################################################
//! Prologue for deferred data for XML archives
/*! Do nothing for the defer wrapper */
template <class T> inline
void prologue( XMLOutputArchive &, DeferredData<T> const & )
{ }
//! Prologue for deferred data for XML archives
template <class T> inline
void prologue( XMLInputArchive &, DeferredData<T> const & )
{ }
// ######################################################################
//! Epilogue for deferred for XML archives
/*! NVPs do not start or finish nodes - they just set up the names */
template <class T> inline
void epilogue( XMLOutputArchive &, DeferredData<T> const & )
{ }
//! Epilogue for deferred for XML archives
/*! Do nothing for the defer wrapper */
template <class T> inline
void epilogue( XMLInputArchive &, DeferredData<T> const & )
{ }
// ######################################################################
//! Prologue for SizeTags for XML output archives
/*! SizeTags do not start or finish nodes */
template <class T> inline
void prologue( XMLOutputArchive & ar, SizeTag<T> const & )
{
if (ar.hasSizeAttributes())
{
ar.appendAttribute("size", "dynamic");
}
}
template <class T> inline
void prologue( XMLInputArchive &, SizeTag<T> const & )
......
......@@ -32,6 +32,7 @@
#include <type_traits>
#include <string>
#include <memory>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <vector>
......@@ -97,6 +98,59 @@ namespace cereal
return {std::forward<T>(sz)};
}
// ######################################################################
//! Marks data for deferred serialization
/*! cereal performs a recursive depth-first traversal of data it serializes. When
serializing smart pointers to large, nested, or cyclical data structures, it
is possible to encounter a stack overflow from excessive recursion when following
a chain of pointers.
Deferment can help in these situations if the data can be serialized separately from
the pointers used to traverse the structure. For example, a graph structure can have its
nodes serialized before its edges:
@code{.cpp}
struct MyEdge
{
std::shared_ptr<MyNode> connection;
int some_value;
template<class Archive>
void serialize(Archive & archive)
{
// when we serialize an edge, we'll defer serializing the associated node
archive( cereal::defer( connection ),
some_value );
}
};
struct MyGraphStructure
{
std::vector<MyEdge> edges;
std::vector<MyNodes> nodes;
template<class Archive>
void serialize(Archive & archive)
{
// because of the deferment, we ensure all nodes are fully serialized
// before any connection pointers to those nodes are serialized
archive( edges, nodes );
// we have to explicitly inform the archive when it is safe to serialize
// the deferred data
archive.serializeDeferments();
}
};
@endcode
@relates DeferredData
@ingroup Utility */
template <class T> inline
DeferredData<T> defer( T && value )
{
return {std::forward<T>(value)};
}
// ######################################################################
//! Called before a type is serialized to set up any special archive state
//! for processing some type
......@@ -145,6 +199,14 @@ namespace cereal
instantiate_polymorphic_binding( T*, Archive*, BindingTag, adl_tag ); \
} } /* end namespaces */
//! Helper macro to omit unused warning
#if defined(__GNUC__)
// GCC / clang don't want the function
#define CEREAL_UNUSED_FUNCTION
#else
#define CEREAL_UNUSED_FUNCTION static void unused() { (void)version; }
#endif
// ######################################################################
//! Defines a class version for some type
/*! Versioning information is optional and adds some small amount of
......@@ -207,7 +269,7 @@ namespace cereal
std::type_index(typeid(TYPE)).hash_code(), VERSION_NUMBER ); \
return VERSION_NUMBER; \
} \
static void unused() { (void)version; } \
CEREAL_UNUSED_FUNCTION \
}; /* end Version */ \
const std::uint32_t Version<TYPE>::version = \
Version<TYPE>::registerVersion(); \
......@@ -250,6 +312,14 @@ namespace cereal
return *self;
}
//! Serializes any data marked for deferment using defer
/*! This will cause any data wrapped in DeferredData to be immediately serialized */
void serializeDeferments()
{
for( auto & deferment : itsDeferments )
deferment();
}
/*! @name Boost Transition Layer
Functionality that mirrors the syntax for Boost. This is useful if you are transitioning
a large project from Boost to cereal. The preferred interface for cereal is using operator(). */
......@@ -379,6 +449,17 @@ namespace cereal
return *self;
}
std::vector<std::function<void(void)>> itsDeferments;
template <class T> inline
ArchiveType & processImpl(DeferredData<T> const & d)
{
std::function<void(void)> deferment( [=](){ self->process( d.value ); } );
itsDeferments.emplace_back( std::move(deferment) );
return *self;
}
//! Helper macro that expands the requirements for activating an overload
/*! Requirements:
Has the requested serialization function
......@@ -483,8 +564,7 @@ namespace cereal
/*! If this is the first time this class has been serialized, we will record its
version number and serialize that.
@tparam T The type of the class being serialized
@param version The version number associated with it */
@tparam T The type of the class being serialized */
template <class T> inline
std::uint32_t registerClassVersion()
{
......@@ -620,6 +700,14 @@ namespace cereal
return *self;
}
//! Serializes any data marked for deferment using defer
/*! This will cause any data wrapped in DeferredData to be immediately serialized */
void serializeDeferments()
{
for( auto & deferment : itsDeferments )
deferment();
}
/*! @name Boost Transition Layer
Functionality that mirrors the syntax for Boost. This is useful if you are transitioning
a large project from Boost to cereal. The preferred interface for cereal is using operator(). */
......@@ -667,6 +755,7 @@ namespace cereal
/*! This is used to retrieve a previously registered shared_ptr
which has already been loaded.
@internal
@param id The unique id that was serialized for the pointer
@return A shared pointer to the data
@throw Exception if the id does not exist */
......@@ -685,6 +774,7 @@ namespace cereal
/*! After a shared pointer has been allocated for the first time, it should
be registered with its loaded id for future references to it.
@internal
@param id The unique identifier for the shared pointer
@param ptr The actual shared pointer */
inline void registerSharedPointer(std::uint32_t const id, std::shared_ptr<void> ptr)
......@@ -697,6 +787,7 @@ namespace cereal
/*! This is used to retrieve a string previously registered during
a polymorphic load.
@internal
@param id The unique id that was serialized for the polymorphic type
@return The string identifier for the tyep */
inline std::string getPolymorphicName(std::uint32_t const id)
......@@ -713,6 +804,7 @@ namespace cereal
/*! After a polymorphic type has been loaded for the first time, it should
be registered with its loaded id for future references to it.
@internal
@param id The unique identifier for the polymorphic type
@param name The name associated with the tyep */
inline void registerPolymorphicName(std::uint32_t const id, std::string const & name)
......@@ -762,6 +854,17 @@ namespace cereal
return *self;
}
std::vector<std::function<void(void)>> itsDeferments;
template <class T> inline
ArchiveType & processImpl(DeferredData<T> const & d)
{
std::function<void(void)> deferment( [=](){ self->process( d.value ); } );
itsDeferments.emplace_back( std::move(deferment) );
return *self;
}
//! Helper macro that expands the requirements for activating an overload
/*! Requirements:
Has the requested serialization function
......@@ -875,8 +978,7 @@ namespace cereal
/*! If this is the first time this class has been serialized, we will record its
version number and serialize that.
@tparam T The type of the class being serialized
@param version The version number associated with it */
@tparam T The type of the class being serialized */
template <class T> inline
std::uint32_t loadClassVersion()
{
......
......@@ -68,8 +68,10 @@ namespace cereal
namespace detail
{
struct NameValuePairCore {}; //!< Traits struct for NVPs
struct DeferredDataCore {}; //!< Traits struct for DeferredData
}
// ######################################################################
//! For holding name value pairs
/*! This pairs a name (some string) with some value such that an archive
can potentially take advantage of the pairing.
......@@ -210,7 +212,7 @@ namespace cereal
{
//! Internally store the pointer as a void *, keeping const if created with
//! a const pointer
using PT = typename std::conditional<std::is_const<typename std::remove_pointer<T>::type>::value,
using PT = typename std::conditional<std::is_const<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::value,
const void *,
void *>::type;
......@@ -220,6 +222,43 @@ namespace cereal
uint64_t size; //!< size in bytes
};
// ######################################################################
//! A wrapper around data that should be serialized after all non-deferred data
/*! This class is used to demarcate data that can only be safely serialized after
any data not wrapped in this class.
@internal */
template <class T>
class DeferredData : detail::DeferredDataCore
{
private:
// If we get passed an array, keep the type as is, otherwise store
// a reference if we were passed an l value reference, else copy the value
using Type = typename std::conditional<std::is_array<typename std::remove_reference<T>::type>::value,
typename std::remove_cv<T>::type,
typename std::conditional<std::is_lvalue_reference<T>::value,
T,
typename std::decay<T>::type>::type>::type;
// prevent nested nvps
static_assert( !std::is_base_of<detail::DeferredDataCore, T>::value,
"Cannot defer DeferredData" );
DeferredData & operator=( DeferredData const & ) = delete;
public:
//! Constructs a new NameValuePair
/*! @param v The value to defer. Ideally this should be an l-value reference so that
the value can be both loaded and saved to. If you pass an r-value reference,
the DeferredData will store a copy of it instead of a reference. Thus you should
only pass r-values in cases where this makes sense, such as the result of some
size() call.
@internal */
DeferredData( T && v ) : value(std::forward<T>(v)) {}
Type value;
};
// ######################################################################
namespace detail
{
......@@ -256,7 +295,7 @@ namespace cereal
struct adl_tag;
// used during saving pointers
static const int32_t msb_32bit = 0x80000000;
static const uint32_t msb_32bit = 0x80000000;
static const int32_t msb2_32bit = 0x40000000;
}
......
......@@ -56,6 +56,14 @@
#include <set>
#include <stack>
//! Helper macro to omit unused warning
#if defined(__GNUC__)
// GCC / clang don't want the function
#define CEREAL_BIND_TO_ARCHIVES_UNUSED_FUNCTION
#else
#define CEREAL_BIND_TO_ARCHIVES_UNUSED_FUNCTION static void unused() { (void)b; }
#endif
//! Binds a polymorhic type to all registered archives
/*! This binds a polymorphic type to all compatible registered archives that
have been registered with CEREAL_REGISTER_ARCHIVE. This must be called
......@@ -67,7 +75,7 @@
template<> \
struct init_binding<__VA_ARGS__> { \
static bind_to_archives<__VA_ARGS__> const & b; \
static void unused() { (void)b; } \
CEREAL_BIND_TO_ARCHIVES_UNUSED_FUNCTION \
}; \
bind_to_archives<__VA_ARGS__> const & init_binding<__VA_ARGS__>::b = \
::cereal::detail::StaticObject< \
......@@ -115,8 +123,10 @@ namespace cereal
all registered mappings between base and derived types. */
struct PolymorphicCasters
{
//! Maps from a derived type index to a set of chainable casters
using DerivedCasterMap = std::unordered_map<std::type_index, std::vector<PolymorphicCaster const *>>;
//! Maps from base type index to a map from derived type index to caster
std::map<std::type_index, std::map<std::type_index, std::vector<PolymorphicCaster const*>>> map;
std::unordered_map<std::type_index, DerivedCasterMap> map;
std::multimap<std::type_index, std::type_index> reverseMap;
......@@ -127,24 +137,26 @@ namespace cereal
"Make sure you either serialize the base class at some point via cereal::base_class or cereal::virtual_base_class.\n" \
"Alternatively, manually register the association with CEREAL_REGISTER_POLYMORPHIC_RELATION.");
//! Checks if the mapping object that can perform the upcast or downcast
//! Checks if the mapping object that can perform the upcast or downcast exists, and returns it if so
/*! Uses the type index from the base and derived class to find the matching
registered caster. If no matching caster exists, returns false. */
static bool exists( std::type_index const & baseIndex, std::type_index const & derivedIndex )
registered caster. If no matching caster exists, the bool in the pair will be false and the vector
reference should not be used. */
static std::pair<bool, std::vector<PolymorphicCaster const *> const &>
lookup_if_exists( std::type_index const & baseIndex, std::type_index const & derivedIndex )
{
// First phase of lookup - match base type index
auto const & baseMap = StaticObject<PolymorphicCasters>::getInstance().map;
auto baseIter = baseMap.find( baseIndex );
if (baseIter == baseMap.end())
return false;
return {false, {}};
// Second phase - find a match from base to derived
auto & derivedMap = baseIter->second;
auto const & derivedMap = baseIter->second;
auto derivedIter = derivedMap.find( derivedIndex );
if (derivedIter == derivedMap.end())
return false;
return {false, {}};
return true;
return {true, derivedIter->second};
}
//! Gets the mapping object that can perform the upcast or downcast
......@@ -162,7 +174,7 @@ namespace cereal
exceptionFunc();
// Second phase - find a match from base to derived
auto & derivedMap = baseIter->second;
auto const & derivedMap = baseIter->second;
auto derivedIter = derivedMap.find( derivedIndex );
if( derivedIter == derivedMap.end() )
exceptionFunc();
......@@ -176,8 +188,8 @@ namespace cereal
{
auto const & mapping = lookup( baseInfo, typeid(Derived), [&](){ UNREGISTERED_POLYMORPHIC_CAST_EXCEPTION(save) } );
for( auto const * map : mapping )
dptr = map->downcast( dptr );
for( auto const * dmap : mapping )
dptr = dmap->downcast( dptr );
return static_cast<Derived const *>( dptr );
}
......@@ -213,6 +225,14 @@ namespace cereal
#undef UNREGISTERED_POLYMORPHIC_CAST_EXCEPTION
};
#ifdef CEREAL_OLDER_GCC
#define CEREAL_EMPLACE_MAP(map, key, value) \
map.insert( std::make_pair(std::move(key), std::move(value)) );
#else // NOT CEREAL_OLDER_GCC
#define CEREAL_EMPLACE_MAP(map, key, value) \
map.emplace( key, value );
#endif // NOT_CEREAL_OLDER_GCC
//! Strongly typed derivation of PolymorphicCaster
template <class Base, class Derived>
struct PolymorphicVirtualCaster : PolymorphicCaster
......@@ -229,18 +249,16 @@ namespace cereal
// First insert the relation Base->Derived
const auto lock = StaticObject<PolymorphicCasters>::lock();
auto & baseMap = StaticObject<PolymorphicCasters>::getInstance().map;
auto lb = baseMap.lower_bound(baseKey);
{
auto & derivedMap = baseMap.insert( lb, {baseKey, {}} )->second;
auto lbd = derivedMap.lower_bound(derivedKey);
auto & derivedVec = derivedMap.insert( lbd, { std::move(derivedKey), {}} )->second;
auto & derivedMap = baseMap.insert( {baseKey, PolymorphicCasters::DerivedCasterMap{}} ).first->second;
auto & derivedVec = derivedMap.insert( {derivedKey, {}} ).first->second;
derivedVec.push_back( this );
}
// Insert reverse relation Derived->Base
auto & reverseMap = StaticObject<PolymorphicCasters>::getInstance().reverseMap;
reverseMap.insert( {derivedKey, baseKey} );
CEREAL_EMPLACE_MAP(reverseMap, derivedKey, baseKey);
// Find all chainable unregistered relations
/* The strategy here is to process only the nodes in the class hierarchy graph that have been
......@@ -254,28 +272,40 @@ namespace cereal
// Checks whether there is a path from parent->child and returns a <dist, path> pair
// dist is set to MAX if the path does not exist
auto checkRelation = [](std::type_index const & parentInfo, std::type_index const & childInfo) ->
std::pair<size_t, std::vector<PolymorphicCaster const *>>
std::pair<size_t, std::vector<PolymorphicCaster const *> const &>
{
if( PolymorphicCasters::exists( parentInfo, childInfo ) )
auto result = PolymorphicCasters::lookup_if_exists( parentInfo, childInfo );
if( result.first )
{
auto const & path = PolymorphicCasters::lookup( parentInfo, childInfo, [](){} );
auto const & path = result.second;
return {path.size(), path};
}
else
return {std::numeric_limits<size_t>::max(), {}};
return {(std::numeric_limits<size_t>::max)(), {}};
};
std::stack<std::type_index> parentStack; // Holds the parent nodes to be processed
std::set<std::type_index> dirtySet; // Marks child nodes that have been changed
std::set<std::type_index> processedParents; // Marks parent nodes that have been processed
std::vector<std::type_index> dirtySet; // Marks child nodes that have been changed
std::unordered_set<std::type_index> processedParents; // Marks parent nodes that have been processed
// Checks if a child has been marked dirty
auto isDirty = [&](std::type_index const & c)
{
auto const dirtySetSize = dirtySet.size();
for( size_t i = 0; i < dirtySetSize; ++i )
if( dirtySet[i] == c )
return true;
return false;
};
// Begin processing the base key and mark derived as dirty
parentStack.push( baseKey );
dirtySet.insert( derivedKey );
dirtySet.emplace_back( derivedKey );
while( !parentStack.empty() )
{
using Relations = std::multimap<std::type_index, std::pair<std::type_index, std::vector<PolymorphicCaster const *>>>;
using Relations = std::unordered_multimap<std::type_index, std::pair<std::type_index, std::vector<PolymorphicCaster const *>>>;
Relations unregisteredRelations; // Defer insertions until after main loop to prevent iterator invalidation
const auto parent = parentStack.top();
......@@ -285,7 +315,7 @@ namespace cereal
for( auto const & childPair : baseMap[parent] )
{
const auto child = childPair.first;
if( dirtySet.count( child ) && baseMap.count( child ) )
if( isDirty( child ) && baseMap.count( child ) )
{
auto parentChildPath = checkRelation( parent, child );
......@@ -340,11 +370,11 @@ namespace cereal
{
auto & derivedMap = baseMap.find( it.first )->second;
derivedMap[it.second.first] = it.second.second;
reverseMap.insert( {it.second.first, it.first} );
CEREAL_EMPLACE_MAP(reverseMap, it.second.first, it.first );
}
// Mark current parent as modified
dirtySet.insert( parent );
dirtySet.emplace_back( parent );
// Insert all parents of the current parent node that haven't yet been processed
auto parentRange = reverseMap.equal_range( parent );
......@@ -361,6 +391,8 @@ namespace cereal
} // end chainable relations
} // end PolymorphicVirtualCaster()
#undef CEREAL_EMPLACE_MAP
//! Performs the proper downcast with the templated types
void const * downcast( void const * const ptr ) const override
{
......
......@@ -48,7 +48,7 @@
# define CEREAL_DLL_EXPORT __declspec(dllexport)
# define CEREAL_USED
#else // clang or gcc
# define CEREAL_DLL_EXPORT
# define CEREAL_DLL_EXPORT __attribute__ ((visibility("default")))
# define CEREAL_USED __attribute__ ((__used__))
#endif
......@@ -67,13 +67,12 @@ namespace cereal
class CEREAL_DLL_EXPORT StaticObject
{
private:
//! Forces instantiation at pre-execution time
static void instantiate( T const & ) {}
static T & create()
{
static T t;
instantiate(instance);
//! Forces instantiation at pre-execution time
(void)instance;
return t;
}
......@@ -95,6 +94,7 @@ namespace cereal
std::unique_lock<std::mutex> lock;
#else
public:
LockGuard(LockGuard const &) = default; // prevents implicit copy ctor warning
~LockGuard() CEREAL_NOEXCEPT {} // prevents variable not used
#endif
};
......