123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- #ifndef CEREAL_DETAILS_HELPERS_HPP_
- #define CEREAL_DETAILS_HELPERS_HPP_
- #include <type_traits>
- #include <cstdint>
- #include <utility>
- #include <memory>
- #include <unordered_map>
- #include <stdexcept>
- #include "cereal/macros.hpp"
- #include "cereal/details/static_object.hpp"
- namespace cereal
- {
-
-
-
- struct Exception : public std::runtime_error
- {
- explicit Exception( const std::string & what_ ) : std::runtime_error(what_) {}
- explicit Exception( const char * what_ ) : std::runtime_error(what_) {}
- };
-
-
-
- using size_type = CEREAL_SIZE_TYPE;
-
- class BinaryOutputArchive;
- class BinaryInputArchive;
-
- namespace detail
- {
- struct NameValuePairCore {};
- struct DeferredDataCore {};
- }
-
-
-
- template <class T>
- class NameValuePair : detail::NameValuePairCore
- {
- private:
-
-
- 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;
-
- static_assert( !std::is_base_of<detail::NameValuePairCore, T>::value,
- "Cannot pair a name to a NameValuePair" );
- NameValuePair & operator=( NameValuePair const & ) = delete;
- public:
-
-
- NameValuePair( char const * n, T && v ) : name(n), value(std::forward<T>(v)) {}
- char const * name;
- Type value;
- };
-
-
- template<class Archive, class T> inline
- typename
- std::enable_if<std::is_same<Archive, ::cereal::BinaryInputArchive>::value ||
- std::is_same<Archive, ::cereal::BinaryOutputArchive>::value,
- T && >::type
- make_nvp( const char *, T && value )
- {
- return std::forward<T>(value);
- }
-
-
- template<class Archive, class T> inline
- typename
- std::enable_if<!std::is_same<Archive, ::cereal::BinaryInputArchive>::value &&
- !std::is_same<Archive, ::cereal::BinaryOutputArchive>::value,
- NameValuePair<T> >::type
- make_nvp( const char * name, T && value)
- {
- return {name, std::forward<T>(value)};
- }
-
-
- #define CEREAL_NVP_(name, value) ::cereal::make_nvp<Archive>(name, value)
-
-
-
- template <class T>
- struct BinaryData
- {
-
-
- using PT = typename std::conditional<std::is_const<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::value,
- const void *,
- void *>::type;
- BinaryData( T && d, uint64_t s ) : data(std::forward<T>(d)), size(s) {}
- PT data;
- uint64_t size;
- };
-
-
-
- template <class T>
- class DeferredData : detail::DeferredDataCore
- {
- private:
-
-
- 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;
-
- static_assert( !std::is_base_of<detail::DeferredDataCore, T>::value,
- "Cannot defer DeferredData" );
- DeferredData & operator=( DeferredData const & ) = delete;
- public:
-
-
- DeferredData( T && v ) : value(std::forward<T>(v)) {}
- Type value;
- };
-
- namespace detail
- {
-
-
- class OutputArchiveBase
- {
- public:
- OutputArchiveBase() = default;
- OutputArchiveBase( OutputArchiveBase && ) CEREAL_NOEXCEPT {}
- OutputArchiveBase & operator=( OutputArchiveBase && ) CEREAL_NOEXCEPT { return *this; }
- virtual ~OutputArchiveBase() CEREAL_NOEXCEPT = default;
- private:
- virtual void rtti() {}
- };
- class InputArchiveBase
- {
- public:
- InputArchiveBase() = default;
- InputArchiveBase( InputArchiveBase && ) CEREAL_NOEXCEPT {}
- InputArchiveBase & operator=( InputArchiveBase && ) CEREAL_NOEXCEPT { return *this; }
- virtual ~InputArchiveBase() CEREAL_NOEXCEPT = default;
- private:
- virtual void rtti() {}
- };
-
- template <class Archive, class T> struct polymorphic_serialization_support;
- struct adl_tag;
-
- static const uint32_t msb_32bit = 0x80000000;
- static const int32_t msb2_32bit = 0x40000000;
- }
-
-
-
- template <class T>
- class SizeTag
- {
- private:
-
-
- using Type = typename std::conditional<std::is_lvalue_reference<T>::value,
- T,
- typename std::decay<T>::type>::type;
- SizeTag & operator=( SizeTag const & ) = delete;
- public:
- SizeTag( T && sz ) : size(std::forward<T>(sz)) {}
- Type size;
- };
-
-
-
- template <class Key, class Value>
- struct MapItem
- {
- using KeyType = typename std::conditional<
- std::is_lvalue_reference<Key>::value,
- Key,
- typename std::decay<Key>::type>::type;
- using ValueType = typename std::conditional<
- std::is_lvalue_reference<Value>::value,
- Value,
- typename std::decay<Value>::type>::type;
-
-
- MapItem( Key && key_, Value && value_ ) : key(std::forward<Key>(key_)), value(std::forward<Value>(value_)) {}
- MapItem & operator=( MapItem const & ) = delete;
- KeyType key;
- ValueType value;
-
- template <class Archive> inline
- void CEREAL_SERIALIZE_FUNCTION_NAME(Archive & archive)
- {
- archive( make_nvp<Archive>("key", key),
- make_nvp<Archive>("value", value) );
- }
- };
-
-
- template <class KeyType, class ValueType> inline
- MapItem<KeyType, ValueType> make_map_item(KeyType && key, ValueType && value)
- {
- return {std::forward<KeyType>(key), std::forward<ValueType>(value)};
- }
- namespace detail
- {
-
-
-
- namespace{ struct version_binding_tag {}; }
-
-
-
- template <class T, class BindingTag = version_binding_tag> struct Version
- {
- static const std::uint32_t version = 0;
-
-
- };
-
- struct Versions
- {
- std::unordered_map<std::size_t, std::uint32_t> mapping;
- std::uint32_t find( std::size_t hash, std::uint32_t version )
- {
- const auto result = mapping.emplace( hash, version );
- return result.first->second;
- }
- };
- }
- }
- #endif
|