hashed_factory.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_HASHED_FACTORY_HPP
  9. #define BOOST_FLYWEIGHT_HASHED_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/factory_tag.hpp>
  15. #include <boost/flyweight/hashed_factory_fwd.hpp>
  16. #include <boost/multi_index_container.hpp>
  17. #include <boost/multi_index/identity.hpp>
  18. #include <boost/multi_index/hashed_index.hpp>
  19. #include <boost/mpl/aux_/lambda_support.hpp>
  20. #include <boost/mpl/if.hpp>
  21. /* Flyweight factory based on a hashed container implemented
  22. * with Boost.MultiIndex.
  23. */
  24. namespace boost{
  25. namespace flyweights{
  26. template<
  27. typename Entry,typename Key,
  28. typename Hash,typename Pred,typename Allocator
  29. >
  30. class hashed_factory_class:public factory_marker
  31. {
  32. struct index_list:
  33. boost::mpl::vector1<
  34. multi_index::hashed_unique<
  35. multi_index::identity<Entry>,
  36. typename boost::mpl::if_<
  37. mpl::is_na<Hash>,
  38. hash<Key>,
  39. Hash
  40. >::type,
  41. typename boost::mpl::if_<
  42. mpl::is_na<Pred>,
  43. std::equal_to<Key>,
  44. Pred
  45. >::type
  46. >
  47. >
  48. {};
  49. typedef multi_index::multi_index_container<
  50. Entry,
  51. index_list,
  52. typename boost::mpl::if_<
  53. mpl::is_na<Allocator>,
  54. std::allocator<Entry>,
  55. Allocator
  56. >::type
  57. > container_type;
  58. public:
  59. typedef const Entry* handle_type;
  60. handle_type insert(const Entry& x)
  61. {
  62. return &*cont.insert(x).first;
  63. }
  64. void erase(handle_type h)
  65. {
  66. cont.erase(cont.iterator_to(*h));
  67. }
  68. static const Entry& entry(handle_type h){return *h;}
  69. private:
  70. container_type cont;
  71. public:
  72. typedef hashed_factory_class type;
  73. BOOST_MPL_AUX_LAMBDA_SUPPORT(
  74. 5,hashed_factory_class,(Entry,Key,Hash,Pred,Allocator))
  75. };
  76. /* hashed_factory_class specifier */
  77. template<
  78. typename Hash,typename Pred,typename Allocator
  79. BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
  80. >
  81. struct hashed_factory:factory_marker
  82. {
  83. template<typename Entry,typename Key>
  84. struct apply:
  85. mpl::apply2<
  86. hashed_factory_class<
  87. boost::mpl::_1,boost::mpl::_2,Hash,Pred,Allocator
  88. >,
  89. Entry,Key
  90. >
  91. {};
  92. };
  93. } /* namespace flyweights */
  94. } /* namespace boost */
  95. #endif