archive_constructed.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright 2006-2014 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. #ifndef BOOST_FLYWEIGHT_DETAIL_ARCHIVE_CONSTRUCTED_HPP
  9. #define BOOST_FLYWEIGHT_DETAIL_ARCHIVE_CONSTRUCTED_HPP
  10. #if defined(_MSC_VER)&&(_MSC_VER>=1200)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/detail/no_exceptions_support.hpp>
  15. #include <boost/noncopyable.hpp>
  16. #include <boost/serialization/serialization.hpp>
  17. #include <boost/type_traits/aligned_storage.hpp>
  18. #include <boost/type_traits/alignment_of.hpp>
  19. namespace boost{
  20. namespace flyweights{
  21. namespace detail{
  22. /* constructs a stack-based object from a serialization archive */
  23. template<typename T>
  24. struct archive_constructed:private noncopyable
  25. {
  26. template<class Archive>
  27. archive_constructed(Archive& ar,const unsigned int version)
  28. {
  29. serialization::load_construct_data_adl(ar,&get(),version);
  30. BOOST_TRY{
  31. ar>>get();
  32. }
  33. BOOST_CATCH(...){
  34. (&get())->~T();
  35. BOOST_RETHROW;
  36. }
  37. BOOST_CATCH_END
  38. }
  39. template<class Archive>
  40. archive_constructed(const char* name,Archive& ar,const unsigned int version)
  41. {
  42. serialization::load_construct_data_adl(ar,&get(),version);
  43. BOOST_TRY{
  44. ar>>serialization::make_nvp(name,get());
  45. }
  46. BOOST_CATCH(...){
  47. (&get())->~T();
  48. BOOST_RETHROW;
  49. }
  50. BOOST_CATCH_END
  51. }
  52. ~archive_constructed()
  53. {
  54. (&get())->~T();
  55. }
  56. T& get(){return *static_cast<T*>(static_cast<void*>(&space));}
  57. private:
  58. typename aligned_storage<sizeof(T),alignment_of<T>::value>::type space;
  59. };
  60. } /* namespace flyweights::detail */
  61. } /* namespace flyweights */
  62. } /* namespace boost */
  63. #endif