demo_portable_archive.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. //
  3. // demo_portable_archive.cpp
  4. //
  5. // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
  6. // Use, modification and distribution is subject to the Boost Software
  7. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // should pass compilation and execution
  10. // note:: this example can only be built with the static library
  11. // (at least with MSVC - due to conflicts related to import of library
  12. // code and instantiation of templates.
  13. #include <sstream>
  14. #include "portable_binary_oarchive.hpp"
  15. #include "portable_binary_iarchive.hpp"
  16. #include <cstdlib>
  17. #include <boost/config.hpp>
  18. #if defined(BOOST_NO_STDC_NAMESPACE)
  19. namespace std{ using ::rand; }
  20. #endif
  21. class A
  22. {
  23. friend class boost::serialization::access;
  24. char c;
  25. A *pa;
  26. int i;
  27. int i2; // special tricky case to check sign extension
  28. unsigned int ui;
  29. long l;
  30. unsigned long ul;
  31. template<class Archive>
  32. void serialize(Archive & ar, const unsigned int /* version */){
  33. ar & c & i & i2 & ui & l & ul ;
  34. }
  35. public:
  36. bool operator==(const A & rhs) const {
  37. return
  38. c == rhs.c
  39. && i == rhs.i
  40. && i2 == rhs.i2
  41. && ui == rhs.ui
  42. && l == rhs.l
  43. && ul == rhs.ul
  44. ;
  45. }
  46. A() :
  47. c(0xFF & std::rand()),
  48. pa(0),
  49. i(std::rand()),
  50. i2(0x80),
  51. ui(std::rand()),
  52. l(std::rand() * std::rand()),
  53. ul(std::rand())
  54. {}
  55. };
  56. int main( int /* argc */, char* /* argv */[] )
  57. {
  58. const A a;
  59. A a1;
  60. std::stringstream ss;
  61. {
  62. portable_binary_oarchive pboa(ss);
  63. pboa << a;
  64. }
  65. {
  66. portable_binary_iarchive pbia(ss);
  67. pbia >> a1;
  68. }
  69. if(! (a == a1))
  70. return 1;
  71. ss.clear();
  72. {
  73. portable_binary_oarchive pboa(ss, endian_big);
  74. pboa << a;
  75. }
  76. {
  77. portable_binary_iarchive pbia(ss, endian_big);
  78. pbia >> a1;
  79. }
  80. if(! (a == a1))
  81. return 1;
  82. ss.clear();
  83. {
  84. portable_binary_oarchive pboa(ss, endian_big);
  85. pboa << a;
  86. }
  87. {
  88. portable_binary_iarchive pbia(ss, endian_big);
  89. pbia >> a1;
  90. }
  91. return !(a == a1);
  92. }