archive_constructed.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Copyright 2003-2016 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. #ifndef BOOST_MULTI_INDEX_DETAIL_ARCHIVE_CONSTRUCTED_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_ARCHIVE_CONSTRUCTED_HPP
  10. #if defined(_MSC_VER)
  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 multi_index{
  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. #include <boost/multi_index/detail/ignore_wstrict_aliasing.hpp>
  57. T& get(){return *reinterpret_cast<T*>(&space);}
  58. #include <boost/multi_index/detail/restore_wstrict_aliasing.hpp>
  59. private:
  60. typename aligned_storage<sizeof(T),alignment_of<T>::value>::type space;
  61. };
  62. } /* namespace multi_index::detail */
  63. } /* namespace multi_index */
  64. } /* namespace boost */
  65. #endif