test_null_ptr.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_null_ptr.cpp: test implementation level trait
  3. // (C) Copyright 2002 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. // should pass compilation and execution
  8. #include <cstddef> // NULL
  9. #include <cstdio> // remove
  10. #include <fstream>
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::remove;
  15. }
  16. #endif
  17. #include "test_tools.hpp"
  18. #include <boost/serialization/base_object.hpp>
  19. class polymorphic_base
  20. {
  21. friend class boost::serialization::access;
  22. template<class Archive>
  23. void serialize(Archive & /* ar */, const unsigned int /* file_version */){
  24. }
  25. public:
  26. virtual ~polymorphic_base(){};
  27. };
  28. class polymorphic_derived1 : public polymorphic_base
  29. {
  30. friend class boost::serialization::access;
  31. template<class Archive>
  32. void serialize(Archive &ar, const unsigned int /* file_version */){
  33. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base);
  34. }
  35. };
  36. // save unregistered polymorphic classes
  37. void save(const char *testfile)
  38. {
  39. test_ostream os(testfile, TEST_STREAM_FLAGS);
  40. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  41. polymorphic_base *rb1 = NULL;
  42. polymorphic_derived1 *rd1 = NULL;
  43. oa << BOOST_SERIALIZATION_NVP(rb1);
  44. oa << BOOST_SERIALIZATION_NVP(rd1);
  45. }
  46. // load unregistered polymorphic classes
  47. void load(const char *testfile)
  48. {
  49. test_istream is(testfile, TEST_STREAM_FLAGS);
  50. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  51. polymorphic_derived1 dummy;
  52. polymorphic_base *rb1 = & dummy;
  53. polymorphic_derived1 *rd1 = & dummy;
  54. ia >> BOOST_SERIALIZATION_NVP(rb1);
  55. BOOST_CHECK_MESSAGE(NULL == rb1, "Null pointer not restored");
  56. ia >> BOOST_SERIALIZATION_NVP(rd1);
  57. BOOST_CHECK_MESSAGE(NULL == rd1, "Null pointer not restored");
  58. delete rb1;
  59. delete rd1;
  60. }
  61. int
  62. test_main( int /* argc */, char* /* argv */[] )
  63. {
  64. const char * testfile = boost::archive::tmpnam(NULL);
  65. BOOST_REQUIRE(NULL != testfile);
  66. save(testfile);
  67. load(testfile);
  68. std::remove(testfile);
  69. return EXIT_SUCCESS;
  70. }
  71. // EOF