]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/proto/example/map_assign.cpp
2 // Copyright 2008 Eric Niebler. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // This is a port of map_list_of() from the Boost.Assign library.
7 // It has the advantage of being more efficient at runtime by not
8 // building any temporary container that requires dynamic allocation.
13 #include <boost/proto/core.hpp>
14 #include <boost/proto/transform.hpp>
15 #include <boost/type_traits/add_reference.hpp>
16 namespace proto
= boost::proto
;
19 struct map_list_of_tag
22 // A simple callable function object that inserts a
23 // (key,value) pair into a map.
27 template<typename Sig
>
30 template<typename This
, typename Map
, typename Key
, typename Value
>
31 struct result
<This(Map
, Key
, Value
)>
32 : boost::add_reference
<Map
>
35 template<typename Map
, typename Key
, typename Value
>
36 Map
&operator()(Map
&map
, Key
const &key
, Value
const &value
) const
38 map
.insert(typename
Map::value_type(key
, value
));
43 // Work-arounds for Microsoft Visual C++ 7.1
44 #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
45 #define MapListOf(x) proto::call<MapListOf(x)>
46 #define _value(x) call<proto::_value(x)>
49 // The grammar for valid map-list expressions, and a
50 // transform that populates the map.
56 proto::terminal
<map_list_of_tag
>
62 , proto::_value(proto::_child1
)
63 , proto::_value(proto::_child2
)
67 // map_list_of(a,b)(c,d)...
74 MapListOf(proto::_child0
)
75 , proto::_value(proto::_child1
)
76 , proto::_value(proto::_child2
)
82 #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
87 template<typename Expr
>
88 struct map_list_of_expr
;
90 struct map_list_of_dom
91 : proto::domain
<proto::pod_generator
<map_list_of_expr
>, MapListOf
>
94 // An expression wrapper that provides a conversion to a
95 // map that uses the MapListOf
96 template<typename Expr
>
97 struct map_list_of_expr
99 BOOST_PROTO_BASIC_EXTENDS(Expr
, map_list_of_expr
, map_list_of_dom
)
100 BOOST_PROTO_EXTENDS_FUNCTION()
102 template<typename Key
, typename Value
, typename Cmp
, typename Al
>
103 operator std::map
<Key
, Value
, Cmp
, Al
> () const
105 BOOST_MPL_ASSERT((proto::matches
<Expr
, MapListOf
>));
106 std::map
<Key
, Value
, Cmp
, Al
> map
;
107 return MapListOf()(*this, 0, map
);
111 map_list_of_expr
<proto::terminal
<map_list_of_tag
>::type
> const map_list_of
= {{{}}};
116 std::map
<std::string
, int> op
=
126 std::cout
<< "\"<\" --> " << op
["<"] << std::endl
;
127 std::cout
<< "\"<=\" --> " << op
["<="] << std::endl
;
128 std::cout
<< "\">\" --> " << op
[">"] << std::endl
;
129 std::cout
<< "\">=\" --> " << op
[">="] << std::endl
;
130 std::cout
<< "\"=\" --> " << op
["="] << std::endl
;
131 std::cout
<< "\"<>\" --> " << op
["<>"] << std::endl
;