test_unique_ptr.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_unique_ptr.cpp
  3. // (C) Copyright 2002-14 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <fstream>
  8. #include <cstdio> // remove, std::auto_ptr interface wrong in dinkumware
  9. #include <boost/config.hpp>
  10. #if defined(BOOST_NO_STDC_NAMESPACE)
  11. namespace std{
  12. using ::remove;
  13. }
  14. #endif
  15. #include <boost/serialization/nvp.hpp>
  16. #include "test_tools.hpp"
  17. /////////////////////////////////////////////////////////////
  18. // test std::unique_ptr serialization
  19. class A
  20. {
  21. private:
  22. friend class boost::serialization::access;
  23. int x;
  24. template<class Archive>
  25. void serialize(Archive &ar, const unsigned int /* file_version */){
  26. ar & BOOST_SERIALIZATION_NVP(x);
  27. }
  28. public:
  29. A(){} // default constructor
  30. ~A(){} // default destructor
  31. };
  32. #ifndef BOOST_NO_CXX11_SMART_PTR
  33. #include <boost/serialization/unique_ptr.hpp>
  34. int test_main(int /* argc */, char * /* argv */[]){
  35. const char * filename = boost::archive::tmpnam(NULL);
  36. BOOST_REQUIRE(NULL != filename);
  37. // create a new auto pointer to ta new object of type A
  38. std::unique_ptr<A> spa(new A);
  39. {
  40. test_ostream os(filename, TEST_STREAM_FLAGS);
  41. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  42. oa << BOOST_SERIALIZATION_NVP(spa);
  43. }
  44. {
  45. // reset the unique_ptr to NULL
  46. // thereby destroying the object of type A
  47. // note that the reset automagically maintains the reference count
  48. spa.reset();
  49. test_istream is(filename, TEST_STREAM_FLAGS);
  50. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  51. ia >> BOOST_SERIALIZATION_NVP(spa);
  52. std::remove(filename);
  53. }
  54. return EXIT_SUCCESS;
  55. }
  56. #else
  57. int test_main(int /* argc */, char * /* argv */[]){
  58. return EXIT_SUCCESS;
  59. }
  60. #endif // BOOST_NO_CXX11_SMART_PTR