test_serialization_template.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Boost.Flyweight test template for serialization capabilities.
  2. *
  3. * Copyright 2006-2014 Joaquin M Lopez Munoz.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org/libs/flyweight for library home page.
  9. */
  10. #ifndef BOOST_FLYWEIGHT_TEST_SERIALIZATION_TEMPLATE_HPP
  11. #define BOOST_FLYWEIGHT_TEST_SERIALIZATION_TEMPLATE_HPP
  12. #if defined(_MSC_VER)&&(_MSC_VER>=1200)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  16. #include <boost/archive/text_oarchive.hpp>
  17. #include <boost/archive/text_iarchive.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. #include <boost/flyweight/key_value.hpp>
  20. #include <boost/flyweight/serialize.hpp>
  21. #include <boost/functional/hash.hpp>
  22. #include <boost/mpl/apply.hpp>
  23. #include <boost/serialization/vector.hpp>
  24. #include <string>
  25. #include <sstream>
  26. #include <vector>
  27. #include "heavy_objects.hpp"
  28. #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
  29. struct tracked_string
  30. {
  31. typedef tracked_string type;
  32. tracked_string(){}
  33. tracked_string(const char* str_):str(str_){}
  34. const std::string& get()const{return str;}
  35. friend bool operator==(const type& x,const type& y){return x.str==y.str;}
  36. friend bool operator< (const type& x,const type& y){return x.str< y.str;}
  37. friend bool operator!=(const type& x,const type& y){return x.str!=y.str;}
  38. friend bool operator> (const type& x,const type& y){return x.str> y.str;}
  39. friend bool operator>=(const type& x,const type& y){return x.str>=y.str;}
  40. friend bool operator<=(const type& x,const type& y){return x.str<=y.str;}
  41. private:
  42. friend class boost::serialization::access;
  43. template<class Archive>
  44. void serialize(Archive& ar,const unsigned int){ar&str;}
  45. std::string str;
  46. };
  47. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  48. namespace boost{
  49. #endif
  50. inline std::size_t hash_value(const tracked_string& x)
  51. {
  52. boost::hash<std::string> h;
  53. return h(x.get());
  54. }
  55. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  56. } /* namespace boost */
  57. #endif
  58. template<typename Flyweight,typename ForwardIterator>
  59. void test_serialization_template(
  60. ForwardIterator first,ForwardIterator last
  61. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Flyweight))
  62. {
  63. std::vector<Flyweight> v1;
  64. while(first!=last)v1.push_back(Flyweight(*first++));
  65. std::ostringstream oss;
  66. {
  67. const std::vector<Flyweight>& crv1=v1;
  68. boost::archive::text_oarchive oa(oss);
  69. oa<<crv1;
  70. }
  71. std::vector<Flyweight> v2;
  72. {
  73. std::istringstream iss(oss.str());
  74. boost::archive::text_iarchive ia(iss);
  75. ia>>v2;
  76. }
  77. BOOST_TEST(v1==v2);
  78. }
  79. template<typename FlyweightSpecifier>
  80. void test_serialization_template(
  81. BOOST_EXPLICIT_TEMPLATE_TYPE(FlyweightSpecifier))
  82. {
  83. typedef typename boost::mpl::apply1<
  84. FlyweightSpecifier,std::string
  85. >::type string_flyweight;
  86. typedef typename boost::mpl::apply1<
  87. FlyweightSpecifier,tracked_string
  88. >::type tracked_string_flyweight;
  89. typedef typename boost::mpl::apply1<
  90. FlyweightSpecifier,
  91. boost::flyweights::key_value<std::string,texture,from_texture_to_string>
  92. >::type texture_flyweight;
  93. const char* words[]={"hello","boost","flyweight","boost","bye","c++","c++"};
  94. test_serialization_template<string_flyweight>(
  95. &words[0],&words[0]+LENGTHOF(words));
  96. test_serialization_template<tracked_string_flyweight>(
  97. &words[0],&words[0]+LENGTHOF(words));
  98. const char* textures[]={
  99. "wood","grass","sand","granite","terracotta","wood","sand","grass"};
  100. test_serialization_template<texture_flyweight>(
  101. &textures[0],&textures[0]+LENGTHOF(textures));
  102. }
  103. #undef LENGTHOF
  104. #endif