test_assoc_cont_factory.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Boost.Flyweight test of assoc_container_factory.
  2. *
  3. * Copyright 2006-2018 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. #include "test_assoc_cont_factory.hpp"
  11. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  12. #include <boost/flyweight/assoc_container_factory.hpp>
  13. #include <boost/flyweight/detail/is_placeholder_expr.hpp>
  14. #include <boost/flyweight/flyweight.hpp>
  15. #include <boost/flyweight/refcounted.hpp>
  16. #include <boost/flyweight/simple_locking.hpp>
  17. #include <boost/flyweight/static_holder.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <functional>
  20. #include <set>
  21. #include "test_basic_template.hpp"
  22. using namespace boost::flyweights;
  23. struct reverse_set_specifier
  24. {
  25. template<typename Entry,typename Key>
  26. struct apply
  27. {
  28. typedef std::set<Entry,std::greater<Key> > type;
  29. };
  30. };
  31. struct assoc_container_factory_flyweight_specifier1
  32. {
  33. template<typename T>
  34. struct apply
  35. {
  36. typedef flyweight<
  37. T,
  38. assoc_container_factory<reverse_set_specifier>
  39. > type;
  40. };
  41. };
  42. /* flyweight<..., assoc_container_factory_class<std::set<...> >, ...> pulls
  43. * the type std::set<...> in as part of its associated ADL set and causes it
  44. * to be instantiated when doing any unqualified function call like, for
  45. * instance, comparing flyweights for equality, which can trigger a static
  46. * assertion in concept-checked STL implementations when std::set<...> is an
  47. * MPL placeholder expression. We avoid this mess with protected_set<...>.
  48. */
  49. struct protected_set_empty_base{};
  50. template<typename K,typename C,typename A>
  51. struct protected_set:
  52. boost::mpl::if_c<
  53. boost::flyweights::detail::is_placeholder_expression<
  54. protected_set<K,C,A>
  55. >::value,
  56. protected_set_empty_base,
  57. std::set<K,C,A>
  58. >::type
  59. {};
  60. struct assoc_container_factory_flyweight_specifier2
  61. {
  62. template<typename T>
  63. struct apply
  64. {
  65. typedef flyweight<
  66. T,
  67. assoc_container_factory_class<
  68. protected_set<
  69. boost::mpl::_1,
  70. std::greater<boost::mpl::_2>,
  71. std::allocator<boost::mpl::_1>
  72. >
  73. >
  74. > type;
  75. };
  76. };
  77. void test_assoc_container_factory()
  78. {
  79. test_basic_template<assoc_container_factory_flyweight_specifier1>();
  80. test_basic_template<assoc_container_factory_flyweight_specifier2>();
  81. }