hashed_factory.hpp 2.7 KB

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