map_index.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_MAP_INDEX_HPP
  11. #define BOOST_INTERPROCESS_MAP_INDEX_HPP
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #include <functional>
  15. #include <utility>
  16. #include <boost/interprocess/containers/map.hpp>
  17. #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
  18. //!\file
  19. //!Describes index adaptor of boost::map container, to use it
  20. //!as name/shared memory index
  21. namespace boost {
  22. namespace interprocess {
  23. namespace ipcdetail{
  24. //!Helper class to define typedefs from IndexTraits
  25. template <class MapConfig>
  26. struct map_index_aux
  27. {
  28. typedef typename MapConfig::key_type key_type;
  29. typedef typename MapConfig::mapped_type mapped_type;
  30. typedef std::less<key_type> key_less;
  31. typedef std::pair<const key_type, mapped_type> value_type;
  32. typedef private_adaptive_pool
  33. <value_type,
  34. typename MapConfig::
  35. segment_manager_base> allocator_type;
  36. typedef boost::interprocess::map
  37. <key_type, mapped_type,
  38. key_less, allocator_type> index_t;
  39. };
  40. } //namespace ipcdetail {
  41. //!Index type based in boost::interprocess::map. Just derives from boost::interprocess::map
  42. //!and defines the interface needed by managed memory segments
  43. template <class MapConfig>
  44. class map_index
  45. //Derive class from map specialization
  46. : public ipcdetail::map_index_aux<MapConfig>::index_t
  47. {
  48. /// @cond
  49. typedef ipcdetail::map_index_aux<MapConfig> index_aux;
  50. typedef typename index_aux::index_t base_type;
  51. typedef typename MapConfig::
  52. segment_manager_base segment_manager_base;
  53. /// @endcond
  54. public:
  55. //!Constructor. Takes a pointer to the
  56. //!segment manager. Can throw
  57. map_index(segment_manager_base *segment_mngr)
  58. : base_type(typename index_aux::key_less(),
  59. segment_mngr){}
  60. //!This reserves memory to optimize the insertion of n
  61. //!elements in the index
  62. void reserve(typename segment_manager_base::size_type)
  63. { /*Does nothing, map has not reserve or rehash*/ }
  64. //!This tries to free previously allocate
  65. //!unused memory.
  66. void shrink_to_fit()
  67. { base_type::get_stored_allocator().deallocate_free_blocks(); }
  68. };
  69. /// @cond
  70. //!Trait class to detect if an index is a node
  71. //!index. This allows more efficient operations
  72. //!when deallocating named objects.
  73. template<class MapConfig>
  74. struct is_node_index
  75. <boost::interprocess::map_index<MapConfig> >
  76. {
  77. static const bool value = true;
  78. };
  79. /// @endcond
  80. }} //namespace boost { namespace interprocess {
  81. #include <boost/interprocess/detail/config_end.hpp>
  82. #endif //#ifndef BOOST_INTERPROCESS_MAP_INDEX_HPP