serialize.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright 2006-2023 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_SERIALIZE_HPP
  9. #define BOOST_FLYWEIGHT_SERIALIZE_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/flyweight/flyweight_fwd.hpp>
  15. #include <boost/flyweight/detail/archive_constructed.hpp>
  16. #include <boost/flyweight/detail/serialization_helper.hpp>
  17. #include <boost/core/serialization.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include <memory>
  20. #include <stdexcept>
  21. /* Serialization routines for flyweight<T>.
  22. */
  23. namespace boost{
  24. namespace flyweights{
  25. template<
  26. class Archive,
  27. typename T,typename Arg1,typename Arg2,typename Arg3
  28. >
  29. inline void serialize(
  30. Archive& ar,::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3>& f,
  31. const unsigned int version)
  32. {
  33. core::split_free(ar,f,version);
  34. }
  35. template<
  36. class Archive,
  37. typename T,typename Arg1,typename Arg2,typename Arg3
  38. >
  39. void save(
  40. Archive& ar,const ::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3>& f,
  41. const unsigned int /*version*/)
  42. {
  43. typedef ::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3> flyweight;
  44. typedef ::boost::flyweights::detail::save_helper<flyweight> helper;
  45. typedef typename helper::size_type size_type;
  46. helper& hlp=ar.template get_helper<helper>();
  47. size_type n=hlp.find(f);
  48. ar<<make_nvp("item",n);
  49. if(n==hlp.size()){
  50. ar<<make_nvp("key",f.get_key());
  51. hlp.push_back(f);
  52. }
  53. }
  54. template<
  55. class Archive,
  56. typename T,typename Arg1,typename Arg2,typename Arg3
  57. >
  58. void load(
  59. Archive& ar,::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3>& f,
  60. const unsigned int version)
  61. {
  62. typedef ::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3> flyweight;
  63. typedef typename flyweight::key_type key_type;
  64. typedef ::boost::flyweights::detail::load_helper<flyweight> helper;
  65. typedef typename helper::size_type size_type;
  66. helper& hlp=ar.template get_helper<helper>();
  67. size_type n=0;
  68. ar>>make_nvp("item",n);
  69. if(n>hlp.size()){
  70. throw_exception(std::runtime_error("Invalid or corrupted archive"));
  71. }
  72. else if(n==hlp.size()){
  73. ::boost::flyweights::detail::archive_constructed<key_type> k(
  74. "key",ar,version);
  75. hlp.push_back(flyweight(k.get()));
  76. }
  77. f=hlp[n];
  78. }
  79. } /* namespace flyweights */
  80. } /* namespace boost */
  81. #endif