123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- /* Copyright 2023 Joaquin M Lopez Munoz.
- * Distributed under the Boost Software License, Version 1.0.
- * (See accompanying file LICENSE_1_0.txt or copy at
- * http://www.boost.org/LICENSE_1_0.txt)
- *
- * See https://www.boost.org/libs/unordered for library home page.
- */
- #ifndef BOOST_UNORDERED_DETAIL_SERIALIZE_CONTAINER_HPP
- #define BOOST_UNORDERED_DETAIL_SERIALIZE_CONTAINER_HPP
- #include <boost/core/serialization.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/unordered/detail/archive_constructed.hpp>
- #include <boost/unordered/detail/bad_archive_exception.hpp>
- #include <boost/unordered/detail/serialization_version.hpp>
- #include <cstddef>
- namespace boost{
- namespace unordered{
- namespace detail{
- /* serialize_container(ar,x,v) serializes any of the unordered associative
- * containers in Boost.Unordered. Iterator serialization is also supported
- * through the following protocol:
- * - At saving time, for each iterator it in [x.begin(),x.end()),
- * serialization_track(ar,it) is ADL-called to instruct the archive to
- * track the positions internally pointed to by the iterator via
- * track_address().
- * - At loading time, these addresses are mapped to those of the equivalent
- * reconstructed positions using again serialization_track(ar,it).
- * - Serializing an iterator reduces to serializing pointers to previously
- * tracked addresses via serialize_address().
- */
- template<typename Iterator>
- std::pair<Iterator,bool> adapt_insert_return_type(Iterator it)
- {
- return std::pair<Iterator,bool>(it,true);
- }
- template<typename Iterator>
- std::pair<Iterator,bool> adapt_insert_return_type(std::pair<Iterator,bool> p)
- {
- return p;
- }
- template<typename Set,bool IsSaving> struct load_or_save_unordered_set;
- template<typename Set> struct load_or_save_unordered_set<Set,true> /* save */
- {
- template<typename Archive>
- void operator()(Archive& ar,const Set& x,unsigned int)const
- {
- typedef typename Set::value_type value_type;
- typedef typename Set::const_iterator const_iterator;
- const std::size_t s=x.size();
- const serialization_version<value_type> value_version;
- ar<<core::make_nvp("count",s);
- ar<<core::make_nvp("value_version",value_version);
- for(const_iterator first=x.begin(),last=x.end();first!=last;++first){
- core::save_construct_data_adl(ar,std::addressof(*first),value_version);
- ar<<core::make_nvp("item",*first);
- serialization_track(ar,first);
- }
- }
- };
- template<typename Set> struct load_or_save_unordered_set<Set,false> /* load */
- {
- template<typename Archive>
- void operator()(Archive& ar,Set& x,unsigned int)const
- {
- typedef typename Set::value_type value_type;
- typedef typename Set::iterator iterator;
- std::size_t s;
- serialization_version<value_type> value_version;
- ar>>core::make_nvp("count",s);
- ar>>core::make_nvp("value_version",value_version);
- x.clear();
- x.reserve(s); /* critical so that iterator tracking is stable */
- for(std::size_t n=0;n<s;++n){
- archive_constructed<value_type> value("item",ar,value_version);
- std::pair<iterator,bool> p=adapt_insert_return_type(
- x.insert(std::move(value.get())));
- if(!p.second)throw_exception(bad_archive_exception());
- ar.reset_object_address(
- std::addressof(*p.first),std::addressof(value.get()));
- serialization_track(ar,p.first);
- }
- }
- };
- template<typename Map,bool IsSaving> struct load_or_save_unordered_map;
- template<typename Map> struct load_or_save_unordered_map<Map,true> /* save */
- {
- template<typename Archive>
- void operator()(Archive& ar,const Map& x,unsigned int)const
- {
- typedef typename std::remove_const<
- typename Map::key_type>::type key_type;
- typedef typename std::remove_const<
- typename Map::mapped_type>::type mapped_type;
- typedef typename Map::const_iterator const_iterator;
- const std::size_t s=x.size();
- const serialization_version<key_type> key_version;
- const serialization_version<mapped_type> mapped_version;
- ar<<core::make_nvp("count",s);
- ar<<core::make_nvp("key_version",key_version);
- ar<<core::make_nvp("mapped_version",mapped_version);
- for(const_iterator first=x.begin(),last=x.end();first!=last;++first){
- /* To remain lib-independent from Boost.Serialization and not rely on
- * the user having included the serialization code for std::pair
- * (boost/serialization/utility.hpp), we serialize the key and the
- * mapped value separately.
- */
- core::save_construct_data_adl(
- ar,std::addressof(first->first),key_version);
- ar<<core::make_nvp("key",first->first);
- core::save_construct_data_adl(
- ar,std::addressof(first->second),mapped_version);
- ar<<core::make_nvp("mapped",first->second);
- serialization_track(ar,first);
- }
- }
- };
- template<typename Map> struct load_or_save_unordered_map<Map,false> /* load */
- {
- template<typename Archive>
- void operator()(Archive& ar,Map& x,unsigned int)const
- {
- typedef typename std::remove_const<
- typename Map::key_type>::type key_type;
- typedef typename std::remove_const<
- typename Map::mapped_type>::type mapped_type;
- typedef typename Map::iterator iterator;
- std::size_t s;
- serialization_version<key_type> key_version;
- serialization_version<mapped_type> mapped_version;
- ar>>core::make_nvp("count",s);
- ar>>core::make_nvp("key_version",key_version);
- ar>>core::make_nvp("mapped_version",mapped_version);
- x.clear();
- x.reserve(s); /* critical so that iterator tracking is stable */
- for(std::size_t n=0;n<s;++n){
- archive_constructed<key_type> key("key",ar,key_version);
- archive_constructed<mapped_type> mapped("mapped",ar,mapped_version);
- std::pair<iterator,bool> p=adapt_insert_return_type(
- x.emplace(std::move(key.get()),std::move(mapped.get())));
- if(!p.second)throw_exception(bad_archive_exception());
- ar.reset_object_address(
- std::addressof(p.first->first),std::addressof(key.get()));
- ar.reset_object_address(
- std::addressof(p.first->second),std::addressof(mapped.get()));
- serialization_track(ar,p.first);
- }
- }
- };
- template<typename Container,bool IsSet,bool IsSaving>
- struct load_or_save_container;
-
- template<typename Set,bool IsSaving>
- struct load_or_save_container<Set,true,IsSaving>:
- load_or_save_unordered_set<Set,IsSaving>{};
- template<typename Map,bool IsSaving>
- struct load_or_save_container<Map,false,IsSaving>:
- load_or_save_unordered_map<Map,IsSaving>{};
- template<typename Archive,typename Container>
- void serialize_container(Archive& ar,Container& x,unsigned int version)
- {
- load_or_save_container<
- Container,
- std::is_same<
- typename Container::key_type,typename Container::value_type>::value,
- Archive::is_saving::value>()(ar,x,version);
- }
- } /* namespace detail */
- } /* namespace unordered */
- } /* namespace boost */
- #endif
|