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 
11 #ifndef BOOST_ASSIGN_PTR_CONTAINER_PTR_MAP_INSERTER_HPP
12 #define BOOST_ASSIGN_PTR_CONTAINER_PTR_MAP_INSERTER_HPP
13 
14 #if defined(_MSC_VER)
15 # pragma once
16 #endif
17 
18 #include <boost/assign/list_inserter.hpp>
19 #include <boost/type_traits/remove_reference.hpp>
20 #include <boost/type_traits/remove_pointer.hpp>
21 
22 namespace boost
23 {
24 
25 namespace assign
26 {
27     template< class PtrMap, class Obj >
28     class ptr_map_inserter
29     {
30         typedef BOOST_DEDUCED_TYPENAME
31                 remove_pointer< BOOST_DEDUCED_TYPENAME
32                        remove_reference<Obj>::type >::type
33            obj_type;
34         typedef BOOST_DEDUCED_TYPENAME PtrMap::key_type
35            key_type;
36 
37     public:
38 
ptr_map_inserter(PtrMap & m)39         ptr_map_inserter( PtrMap& m ) : m_( m )
40         {}
41 
42         template< class Key >
operator ()(const Key & t)43         ptr_map_inserter& operator()( const Key& t )
44         {
45             key_type k(t);
46             m_.insert( k, new obj_type );
47             return *this;
48         }
49 
50 #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
51 #define BOOST_ASSIGN_MAX_PARAMS 6
52 #endif
53 #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
54 #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class T)
55 #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& t)
56 #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, t)
57 
58 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
59 #define BOOST_PP_LOCAL_MACRO(n) \
60     template< class T, BOOST_ASSIGN_PARAMS1(n) > \
61     ptr_map_inserter& operator()( const T& t, BOOST_ASSIGN_PARAMS2(n) ) \
62     { \
63         key_type k(t); \
64         m_.insert( k, new obj_type( BOOST_ASSIGN_PARAMS3(n) ) ); \
65         return *this; \
66     } \
67     /**/
68 
69 #include BOOST_PP_LOCAL_ITERATE()
70 
71     private:
72 
73         ptr_map_inserter& operator=( const ptr_map_inserter& );
74         PtrMap& m_;
75     };
76 
77     template< class PtrMap >
78     inline ptr_map_inserter< PtrMap, typename PtrMap::mapped_reference >
ptr_map_insert(PtrMap & m)79     ptr_map_insert( PtrMap& m )
80     {
81         return ptr_map_inserter< PtrMap, typename PtrMap::mapped_reference >( m );
82     }
83 
84 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
85 
86     template< class T, class PtrMap >
87     inline ptr_map_inserter< PtrMap, T >
ptr_map_insert(PtrMap & m)88     ptr_map_insert( PtrMap& m )
89     {
90         return ptr_map_inserter< PtrMap, T >( m );
91     }
92 
93 #endif
94 
95 } // namespace 'assign'
96 } // namespace 'boost'
97 
98 #undef BOOST_ASSIGN_PARAMS1
99 #undef BOOST_ASSIGN_PARAMS2
100 #undef BOOST_ASSIGN_PARAMS3
101 #undef BOOST_ASSIGN_MAX_PARAMETERS
102 
103 #endif
104