demo_polymorphic.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // demo_polymorphic.cpp
  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 <sstream>
  9. #include <boost/archive/polymorphic_text_iarchive.hpp>
  10. #include <boost/archive/polymorphic_text_oarchive.hpp>
  11. #include <boost/archive/polymorphic_binary_iarchive.hpp>
  12. #include <boost/archive/polymorphic_binary_oarchive.hpp>
  13. #include "demo_polymorphic_A.hpp"
  14. int main(int argc, char* argv[])
  15. {
  16. const A a;
  17. A a1;
  18. {
  19. // test with a text archive
  20. std::stringstream ss;
  21. {
  22. // instantiate archive which inhertis polymorphic interface
  23. // and the normal text archive implementation
  24. boost::archive::polymorphic_text_oarchive oa(ss);
  25. boost::archive::polymorphic_oarchive & oa_interface = oa;
  26. // we can just just the interface for saving
  27. oa_interface << a;
  28. }
  29. {
  30. // or we can use the implementation directly
  31. boost::archive::polymorphic_text_iarchive ia(ss);
  32. ia >> a1;
  33. }
  34. }
  35. if(! (a == a1))
  36. return 1;
  37. {
  38. //test with a binary archive
  39. std::stringstream ss;
  40. {
  41. // instantiate archive which inhertis polymorphic interface
  42. // and the normal binary archive implementation
  43. boost::archive::polymorphic_binary_oarchive oa(ss);
  44. oa << a;
  45. }
  46. {
  47. // see above
  48. boost::archive::polymorphic_binary_iarchive ia(ss);
  49. boost::archive::polymorphic_iarchive & ia_interface = ia;
  50. // use just the polymorphic interface for loading.
  51. ia_interface >> a1;
  52. }
  53. }
  54. if(! (a == a1))
  55. return 1;
  56. return 0;
  57. }