test_non_default_ctor2.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_non_default_ctor2.cpp
  3. // (C) Copyright 2002 Martin Ecker.
  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. // this tests complex usage of non-default constructor. Specifically
  9. // the case where a constructor serializes a pointer member.
  10. #include <fstream>
  11. #include <boost/config.hpp>
  12. #include "test_tools.hpp"
  13. class IntValueHolder
  14. {
  15. public:
  16. IntValueHolder()
  17. : value(0)
  18. {}
  19. IntValueHolder(int newvalue)
  20. : value(newvalue)
  21. {}
  22. int GetValue() const { return value; }
  23. private:
  24. int value;
  25. friend class boost::serialization::access;
  26. template <class ArchiveT>
  27. void serialize(ArchiveT& ar, const unsigned int /* file_version */)
  28. {
  29. ar & BOOST_SERIALIZATION_NVP(value);
  30. }
  31. };
  32. class FloatValueHolder
  33. {
  34. public:
  35. FloatValueHolder()
  36. : value(0)
  37. {}
  38. FloatValueHolder(float newvalue)
  39. : value(newvalue)
  40. {}
  41. float GetValue() const { return value; }
  42. private:
  43. float value;
  44. friend class boost::serialization::access;
  45. template <class ArchiveT>
  46. void serialize(ArchiveT& ar, const unsigned int /* file_version */)
  47. {
  48. ar & BOOST_SERIALIZATION_NVP(value);
  49. }
  50. };
  51. class A
  52. {
  53. public:
  54. A(const IntValueHolder& initialValue)
  55. : value(initialValue), floatValue(new FloatValueHolder(10.0f))
  56. {}
  57. ~A()
  58. {
  59. delete floatValue;
  60. }
  61. IntValueHolder value;
  62. FloatValueHolder* floatValue;
  63. private:
  64. friend class boost::serialization::access;
  65. template <class ArchiveT>
  66. void serialize(ArchiveT& ar, const unsigned int /* file_version */)
  67. {
  68. ar & BOOST_SERIALIZATION_NVP(floatValue);
  69. }
  70. };
  71. namespace boost {
  72. namespace serialization {
  73. template <class ArchiveT>
  74. void save_construct_data(
  75. ArchiveT& archive,
  76. const A* p,
  77. const unsigned int /*version*/
  78. ){
  79. archive & boost::serialization::make_nvp("initialValue", p->value);
  80. }
  81. template <class ArchiveT>
  82. void load_construct_data(
  83. ArchiveT& archive,
  84. A* p,
  85. const unsigned int /*version*/
  86. ){
  87. IntValueHolder initialValue;
  88. archive & boost::serialization::make_nvp("initialValue", initialValue);
  89. ::new (p) A(initialValue);
  90. }
  91. } // serialization
  92. } // namespace boost
  93. int test_main( int /* argc */, char* /* argv */[] )
  94. {
  95. const char * testfile = boost::archive::tmpnam(NULL);
  96. BOOST_REQUIRE(NULL != testfile);
  97. A* a = new A(5);
  98. {
  99. test_ostream os(testfile, TEST_STREAM_FLAGS);
  100. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  101. oa << BOOST_SERIALIZATION_NVP(a);
  102. }
  103. A* a_new;
  104. {
  105. test_istream is(testfile, TEST_STREAM_FLAGS);
  106. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  107. ia >> BOOST_SERIALIZATION_NVP(a_new);
  108. }
  109. delete a;
  110. delete a_new;
  111. return EXIT_SUCCESS;
  112. }