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