1 /* Copyright 2003-2020 Joaquin M Lopez Munoz.
2  * Distributed under the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  *
6  * See http://www.boost.org/libs/multi_index for library home page.
7  */
8 
9 #ifndef BOOST_MULTI_INDEX_DETAIL_ARCHIVE_CONSTRUCTED_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_ARCHIVE_CONSTRUCTED_HPP
11 
12 #if defined(_MSC_VER)
13 #pragma once
14 #endif
15 
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <boost/core/no_exceptions_support.hpp>
18 #include <boost/noncopyable.hpp>
19 #include <boost/serialization/serialization.hpp>
20 #include <boost/type_traits/aligned_storage.hpp>
21 #include <boost/type_traits/alignment_of.hpp>
22 
23 namespace boost{
24 
25 namespace multi_index{
26 
27 namespace detail{
28 
29 /* constructs a stack-based object from a serialization archive */
30 
31 template<typename T>
32 struct archive_constructed:private noncopyable
33 {
34   template<class Archive>
archive_constructedboost::multi_index::detail::archive_constructed35   archive_constructed(Archive& ar,const unsigned int version)
36   {
37     serialization::load_construct_data_adl(ar,&get(),version);
38     BOOST_TRY{
39       ar>>get();
40     }
41     BOOST_CATCH(...){
42       (&get())->~T();
43       BOOST_RETHROW;
44     }
45     BOOST_CATCH_END
46   }
47 
48   template<class Archive>
archive_constructedboost::multi_index::detail::archive_constructed49   archive_constructed(const char* name,Archive& ar,const unsigned int version)
50   {
51     serialization::load_construct_data_adl(ar,&get(),version);
52     BOOST_TRY{
53       ar>>serialization::make_nvp(name,get());
54     }
55     BOOST_CATCH(...){
56       (&get())->~T();
57       BOOST_RETHROW;
58     }
59     BOOST_CATCH_END
60   }
61 
~archive_constructedboost::multi_index::detail::archive_constructed62   ~archive_constructed()
63   {
64     (&get())->~T();
65   }
66 
67 #include <boost/multi_index/detail/ignore_wstrict_aliasing.hpp>
68 
getboost::multi_index::detail::archive_constructed69   T& get(){return *reinterpret_cast<T*>(&space);}
70 
71 #include <boost/multi_index/detail/restore_wstrict_aliasing.hpp>
72 
73 private:
74   typename aligned_storage<sizeof(T),alignment_of<T>::value>::type space;
75 };
76 
77 } /* namespace multi_index::detail */
78 
79 } /* namespace multi_index */
80 
81 } /* namespace boost */
82 
83 #endif
84