assoc_container_factory.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2006-2009 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/flyweight for library home page.
  7. */
  8. #ifndef BOOST_FLYWEIGHT_ASSOC_CONTAINER_FACTORY_HPP
  9. #define BOOST_FLYWEIGHT_ASSOC_CONTAINER_FACTORY_HPP
  10. #if defined(_MSC_VER)&&(_MSC_VER>=1200)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/flyweight/assoc_container_factory_fwd.hpp>
  15. #include <boost/flyweight/detail/is_placeholder_expr.hpp>
  16. #include <boost/flyweight/detail/nested_xxx_if_not_ph.hpp>
  17. #include <boost/flyweight/factory_tag.hpp>
  18. #include <boost/mpl/apply.hpp>
  19. #include <boost/mpl/aux_/lambda_support.hpp>
  20. #include <boost/mpl/if.hpp>
  21. namespace boost{namespace flyweights{namespace detail{
  22. BOOST_FLYWEIGHT_NESTED_XXX_IF_NOT_PLACEHOLDER_EXPRESSION_DEF(iterator);
  23. BOOST_FLYWEIGHT_NESTED_XXX_IF_NOT_PLACEHOLDER_EXPRESSION_DEF(value_type);
  24. }}} /* namespace boost::flyweights::detail */
  25. /* Factory class using a given associative container.
  26. */
  27. namespace boost{
  28. namespace flyweights{
  29. template<typename Container>
  30. class assoc_container_factory_class:public factory_marker
  31. {
  32. public:
  33. /* When assoc_container_factory_class<Container> is an MPL placeholder
  34. * expression, referring to Container::iterator and Container::value_type
  35. * force the MPL placeholder expression Container to be instantiated, which
  36. * is wasteful and can fail in concept-checked STL implementations.
  37. * We protect ourselves against this circumstance.
  38. */
  39. typedef typename detail::nested_iterator_if_not_placeholder_expression<
  40. Container
  41. >::type handle_type;
  42. typedef typename detail::nested_value_type_if_not_placeholder_expression<
  43. Container
  44. >::type entry_type;
  45. handle_type insert(const entry_type& x)
  46. {
  47. return cont.insert(x).first;
  48. }
  49. void erase(handle_type h)
  50. {
  51. cont.erase(h);
  52. }
  53. static const entry_type& entry(handle_type h){return *h;}
  54. private:
  55. /* As above, avoid instantiating Container if it is an
  56. * MPL placeholder expression.
  57. */
  58. typedef typename mpl::if_<
  59. detail::is_placeholder_expression<Container>,
  60. int,
  61. Container
  62. >::type container_type;
  63. container_type cont;
  64. public:
  65. typedef assoc_container_factory_class type;
  66. BOOST_MPL_AUX_LAMBDA_SUPPORT(1,assoc_container_factory_class,(Container))
  67. };
  68. /* assoc_container_factory_class specifier */
  69. template<
  70. typename ContainerSpecifier
  71. BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
  72. >
  73. struct assoc_container_factory:factory_marker
  74. {
  75. template<typename Entry,typename Key>
  76. struct apply
  77. {
  78. typedef assoc_container_factory_class<
  79. typename mpl::apply2<ContainerSpecifier,Entry,Key>::type
  80. > type;
  81. };
  82. };
  83. } /* namespace flyweights */
  84. } /* namespace boost */
  85. #endif