Ticket #3298: join_map_test_2.cpp

File join_map_test_2.cpp, 3.8 KB (added by Marshall Galbraith, 13 years ago)

Updated example using MPL functions to join maps

Line 
1
2#include <boost/mpl/map.hpp>
3#include <boost/mpl/pair.hpp>
4#include <boost/mpl/erase_key.hpp>
5#include <boost/mpl/for_each.hpp>
6#include <boost/mpl/insert.hpp>
7#include <boost/mpl/begin_end.hpp>
8#include <boost/mpl/less.hpp>
9#include <boost/mpl/size.hpp>
10#include <boost/mpl/deref.hpp>
11#include <boost/mpl/insert_range.hpp>
12#include <iostream>
13
14
15namespace mpl = boost::mpl;
16using namespace mpl::placeholders;
17
18//=============================================================================
19template<class MapL, class MapR>
20struct join_maps_1
21{
22 typedef typename mpl::copy< mpl::joint_view<MapL, MapR>
23 , mpl::inserter< mpl::map<>, mpl::insert<_1, _2> >
24 >::type type;
25};
26
27//=============================================================================
28template<class MapL, class MapR>
29struct join_maps_2
30{
31 typedef typename mpl::copy< MapR
32 , mpl::inserter< MapL, mpl::insert<_1, _2> >
33 >::type type;
34};
35
36//=============================================================================
37template<int i>
38struct print {};
39
40//=============================================================================
41template<int i>
42std::ostream& operator<<(std::ostream& out, const print<i>&)
43{
44 out << i;
45 return out;
46}
47
48//=============================================================================
49 struct map_printer
50 {
51 template <class T>
52 inline void operator() (T) const
53 {
54 std::cout << " " << typename T::second();
55 }
56 };
57
58/*###################################################################*/
59int main(int argc, const char* argv[])
60{
61 //Create a couple of maps using insert (this replicates the actual code)
62 typedef mpl::insert<mpl::map<>, mpl::pair< mpl::int_<0>, print<0> > >::type map0;
63 typedef mpl::insert<map0 , mpl::pair< mpl::int_<1>, print<1> > >::type map1;
64 typedef mpl::insert<map1 , mpl::pair< mpl::int_<2>, print<2> > >::type map2;
65
66 typedef mpl::insert<mpl::map<>, mpl::pair< mpl::int_<3>, print<3> > >::type map3;
67 typedef mpl::insert<map3 , mpl::pair< mpl::int_<4>, print<4> > >::type map4;
68 typedef mpl::insert<map4 , mpl::pair< mpl::int_<5>, print<5> > >::type map5;
69
70 //Erease a couple of keys
71 typedef mpl::erase_key<map2, mpl::int_<1> >::type map_erase1;
72 typedef mpl::erase_key<map5, mpl::int_<4> >::type map_erase2;
73
74 //Join the maps into a single map using method 1
75 typedef join_maps_1< map2 , map5 >::type join0_1;
76 typedef join_maps_1< map2 , map_erase2 >::type join1_1;
77 typedef join_maps_1< map_erase1, map5 >::type join2_1;
78 typedef join_maps_1< map_erase1, map_erase2 >::type join3_1;
79
80 //Print out the resuling maps
81 std::cout << std::endl;
82 boost::mpl::for_each< join0_1 >(map_printer()); //Works
83
84 std::cout << std::endl;
85 boost::mpl::for_each< join1_1 >(map_printer()); //Works
86
87 std::cout << std::endl;
88 boost::mpl::for_each< join2_1 >(map_printer()); //Works
89
90 std::cout << std::endl;
91 boost::mpl::for_each< join3_1 >(map_printer()); //Works
92
93 std::cout << std::endl;
94
95
96 //Join the maps into a single map using method 2
97 typedef join_maps_2< map2 , map5 >::type join0_2;
98 typedef join_maps_2< map2 , map_erase2 >::type join1_2;
99 typedef join_maps_2< map_erase1, map5 >::type join2_2;
100 typedef join_maps_2< map_erase1, map_erase2 >::type join3_2;
101
102 //Print out the resuling maps
103 std::cout << std::endl;
104 boost::mpl::for_each< join0_2 >(map_printer()); //Works
105
106 std::cout << std::endl;
107 boost::mpl::for_each< join1_2 >(map_printer()); //Works
108
109 std::cout << std::endl;
110 boost::mpl::for_each< join2_2 >(map_printer()); //MSVC 9 error (works with CYGWIN GCC 4.3.2 )
111
112 std::cout << std::endl;
113 boost::mpl::for_each< join3_2 >(map_printer()); //MSVC 9 error (works with CYGWIN GCC 4.3.2 )
114
115 std::cout << std::endl;
116
117}
118