1 /* Copyright 2006-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/flyweight for library home page.
7  */
8 
9 #ifndef BOOST_FLYWEIGHT_DETAIL_ARCHIVE_CONSTRUCTED_HPP
10 #define BOOST_FLYWEIGHT_DETAIL_ARCHIVE_CONSTRUCTED_HPP
11 
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
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 flyweights{
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::flyweights::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::flyweights::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::flyweights::detail::archive_constructed62   ~archive_constructed()
63   {
64     (&get())->~T();
65   }
66 
getboost::flyweights::detail::archive_constructed67   T& get(){return *static_cast<T*>(static_cast<void*>(&space));}
68 
69 private:
70   typename aligned_storage<sizeof(T),alignment_of<T>::value>::type space;
71 };
72 
73 } /* namespace flyweights::detail */
74 
75 } /* namespace flyweights */
76 
77 } /* namespace boost */
78 
79 #endif
80