map_index.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/intrusive/detail/minimal_pair_header.hpp>
  22. #include <boost/interprocess/containers/map.hpp>
  23. #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
  24. #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
  25. #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
  26. //!\file
  27. //!Describes index adaptor of boost::map container, to use it
  28. //!as name/shared memory index
  29. namespace boost {
  30. namespace interprocess {
  31. namespace ipcdetail{
  32. //!Helper class to define typedefs from IndexTraits
  33. template <class MapConfig>
  34. struct map_index_aux
  35. {
  36. typedef typename MapConfig::key_type key_type;
  37. typedef typename MapConfig::mapped_type mapped_type;
  38. typedef std::less<key_type> key_less;
  39. typedef std::pair<const key_type, mapped_type> value_type;
  40. typedef private_adaptive_pool
  41. <value_type,
  42. typename MapConfig::
  43. segment_manager_base> allocator_type;
  44. typedef boost::interprocess::map
  45. <key_type, mapped_type,
  46. key_less, allocator_type> index_t;
  47. };
  48. } //namespace ipcdetail {
  49. //!Index type based in boost::interprocess::map. Just derives from boost::interprocess::map
  50. //!and defines the interface needed by managed memory segments
  51. template <class MapConfig>
  52. class map_index
  53. //Derive class from map specialization
  54. : public ipcdetail::map_index_aux<MapConfig>::index_t
  55. {
  56. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  57. typedef ipcdetail::map_index_aux<MapConfig> index_aux;
  58. typedef typename index_aux::index_t base_type;
  59. typedef typename MapConfig::
  60. segment_manager_base segment_manager_base;
  61. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  62. public:
  63. //!Constructor. Takes a pointer to the
  64. //!segment manager. Can throw
  65. map_index(segment_manager_base *segment_mngr)
  66. : base_type(typename index_aux::key_less(),
  67. segment_mngr){}
  68. //!This reserves memory to optimize the insertion of n
  69. //!elements in the index
  70. void reserve(typename segment_manager_base::size_type)
  71. { /*Does nothing, map has not reserve or rehash*/ }
  72. //!This tries to free previously allocate
  73. //!unused memory.
  74. void shrink_to_fit()
  75. { base_type::get_stored_allocator().deallocate_free_blocks(); }
  76. };
  77. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  78. //!Trait class to detect if an index is a node
  79. //!index. This allows more efficient operations
  80. //!when deallocating named objects.
  81. template<class MapConfig>
  82. struct is_node_index
  83. <boost::interprocess::map_index<MapConfig> >
  84. {
  85. static const bool value = true;
  86. };
  87. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  88. }} //namespace boost { namespace interprocess {
  89. #include <boost/interprocess/detail/config_end.hpp>
  90. #endif //#ifndef BOOST_INTERPROCESS_MAP_INDEX_HPP