ptr_map_inserter.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Boost.Assign library
  2. //
  3. // Copyright Thorsten Ottosen 2006. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/assign/
  9. //
  10. #ifndef BOOST_ASSIGN_PTR_CONTAINER_PTR_MAP_INSERTER_HPP
  11. #define BOOST_ASSIGN_PTR_CONTAINER_PTR_MAP_INSERTER_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/assign/list_inserter.hpp>
  16. #include <boost/type_traits/remove_reference.hpp>
  17. #include <boost/type_traits/remove_pointer.hpp>
  18. #include <boost/move/utility.hpp>
  19. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  20. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  21. #include <boost/preprocessor/repetition/enum_params.hpp>
  22. #include <boost/preprocessor/iteration/local.hpp>
  23. #endif
  24. namespace boost
  25. {
  26. namespace assign
  27. {
  28. template< class PtrMap, class Obj >
  29. class ptr_map_inserter
  30. {
  31. typedef BOOST_DEDUCED_TYPENAME
  32. remove_pointer< BOOST_DEDUCED_TYPENAME
  33. remove_reference<Obj>::type >::type
  34. obj_type;
  35. typedef BOOST_DEDUCED_TYPENAME PtrMap::key_type
  36. key_type;
  37. public:
  38. ptr_map_inserter( PtrMap& m ) : m_( m )
  39. {}
  40. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  41. template< class Key >
  42. ptr_map_inserter& operator()( const Key& t )
  43. {
  44. key_type k(t);
  45. m_.insert( k, new obj_type );
  46. return *this;
  47. }
  48. #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
  49. #define BOOST_ASSIGN_MAX_PARAMS 6
  50. #endif
  51. #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
  52. #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class T)
  53. #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& t)
  54. #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, t)
  55. #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
  56. #define BOOST_PP_LOCAL_MACRO(n) \
  57. template< class T, BOOST_ASSIGN_PARAMS1(n) > \
  58. ptr_map_inserter& operator()( const T& t, BOOST_ASSIGN_PARAMS2(n) ) \
  59. { \
  60. key_type k(t); \
  61. m_.insert( k, new obj_type( BOOST_ASSIGN_PARAMS3(n) ) ); \
  62. return *this; \
  63. } \
  64. /**/
  65. #include BOOST_PP_LOCAL_ITERATE()
  66. #else
  67. template< class Key, class... Ts >
  68. ptr_map_inserter& operator()(Key&& k, Ts&&... ts)
  69. {
  70. key_type key(boost::forward<Key>(k));
  71. m_.insert(key, new obj_type(boost::forward<Ts>(ts)...));
  72. return *this;
  73. }
  74. #endif
  75. private:
  76. ptr_map_inserter& operator=( const ptr_map_inserter& );
  77. PtrMap& m_;
  78. };
  79. template< class PtrMap >
  80. inline ptr_map_inserter< PtrMap, typename PtrMap::mapped_reference >
  81. ptr_map_insert( PtrMap& m )
  82. {
  83. return ptr_map_inserter< PtrMap, typename PtrMap::mapped_reference >( m );
  84. }
  85. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  86. template< class T, class PtrMap >
  87. inline ptr_map_inserter< PtrMap, T >
  88. ptr_map_insert( PtrMap& m )
  89. {
  90. return ptr_map_inserter< PtrMap, T >( m );
  91. }
  92. #endif
  93. } // namespace 'assign'
  94. } // namespace 'boost'
  95. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  96. #undef BOOST_ASSIGN_PARAMS1
  97. #undef BOOST_ASSIGN_PARAMS2
  98. #undef BOOST_ASSIGN_PARAMS3
  99. #undef BOOST_ASSIGN_MAX_PARAMETERS
  100. #endif
  101. #endif