9
3

serialize.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright 2006-2015 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/serialization/nvp.hpp>
  18. #include <boost/serialization/split_free.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <memory>
  21. /* Serialization routines for flyweight<T>.
  22. */
  23. namespace boost{
  24. namespace serialization{
  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. 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(
  71. archive::archive_exception(archive::archive_exception::other_exception));
  72. }
  73. else if(n==hlp.size()){
  74. ::boost::flyweights::detail::archive_constructed<key_type> k(
  75. "key",ar,version);
  76. hlp.push_back(flyweight(k.get()));
  77. }
  78. f=hlp[n];
  79. }
  80. } /* namespace serialization */
  81. } /* namespace boost */
  82. #endif