unique_ptr.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef BOOST_SERIALIZATION_UNIQUE_PTR_HPP
  2. #define BOOST_SERIALIZATION_UNIQUE_PTR_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // unique_ptr.hpp:
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <memory>
  15. #include <boost/serialization/split_free.hpp>
  16. #include <boost/serialization/nvp.hpp>
  17. namespace boost {
  18. namespace serialization {
  19. /////////////////////////////////////////////////////////////
  20. // implement serialization for unique_ptr< T >
  21. // note: this must be added to the boost namespace in order to
  22. // be called by the library
  23. template<class Archive, class T>
  24. inline void save(
  25. Archive & ar,
  26. const std::unique_ptr< T > &t,
  27. const unsigned int /*file_version*/
  28. ){
  29. // only the raw pointer has to be saved
  30. // the ref count is rebuilt automatically on load
  31. const T * const tx = t.get();
  32. ar << BOOST_SERIALIZATION_NVP(tx);
  33. }
  34. template<class Archive, class T>
  35. inline void load(
  36. Archive & ar,
  37. std::unique_ptr< T > &t,
  38. const unsigned int /*file_version*/
  39. ){
  40. T *tx;
  41. ar >> BOOST_SERIALIZATION_NVP(tx);
  42. // note that the reset automagically maintains the reference count
  43. t.reset(tx);
  44. }
  45. // split non-intrusive serialization function member into separate
  46. // non intrusive save/load member functions
  47. template<class Archive, class T>
  48. inline void serialize(
  49. Archive & ar,
  50. std::unique_ptr< T > &t,
  51. const unsigned int file_version
  52. ){
  53. boost::serialization::split_free(ar, t, file_version);
  54. }
  55. } // namespace serialization
  56. } // namespace boost
  57. #endif // BOOST_SERIALIZATION_UNIQUE_PTR_HPP