• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2010 Christopher Schmidt
3 
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 
8 #include <boost/core/lightweight_test.hpp>
9 #include <boost/fusion/adapted/adt/adapt_assoc_adt.hpp>
10 #include <boost/fusion/sequence.hpp>
11 #include <boost/mpl/assert.hpp>
12 #include <boost/type_traits/is_same.hpp>
13 #include <iostream>
14 #include <string>
15 
16 namespace fusion=boost::fusion;
17 
18 template<typename Name, typename Age>
19 struct employee
20 {
21 private:
22     Name name;
23     Age age;
24 
25 public:
26     template<typename OtherName>
27     void
set_nameemployee28     set_name(OtherName const& n)
29     {
30         name=n;
31     }
32 
33     template<typename OtherAge>
34     void
set_ageemployee35     set_age(OtherAge const& a)
36     {
37         age=a;
38     }
39 
get_nameemployee40     Name& get_name()
41     {
42         return name;
43     }
44 
get_nameemployee45     Name const& get_name()const
46     {
47         return name;
48     }
49 
get_ageemployee50     Age& get_age()
51     {
52         return age;
53     }
54 
get_ageemployee55     Age const& get_age()const
56     {
57         return age;
58     }
59 };
60 
61 namespace keys
62 {
63     struct name;
64     struct age;
65 }
66 
67 BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
68     (Name)(Age),
69     (employee) (Name)(Age),
70     (Name&, Name const&, obj.get_name(), obj.template set_name<Val>(val), keys::name)
71         (Age&, Age const&, obj.get_age(), obj.template set_age<Val>(val), keys::age))
72 
main()73 int main()
74 {
75     typedef employee<std::string, int> et;
76     typedef et const etc;
77     et e;
78     etc& ec=e;
79 
80     fusion::at_key<keys::name>(e)="marshall mathers";
81     fusion::at_key<keys::age>(e)=37;
82 
83     BOOST_MPL_ASSERT((
84         boost::is_same<
85             boost::fusion::result_of::value_at_key<et, keys::name>::type,
86             std::string
87         >));
88     BOOST_MPL_ASSERT((
89         boost::is_same<
90             boost::fusion::result_of::value_at_key<et, keys::name>::type,
91             boost::fusion::result_of::value_at_c<et, 0>::type
92         >));
93     BOOST_MPL_ASSERT((
94         boost::is_same<
95             boost::fusion::result_of::value_at_key<et, keys::age>::type,
96             int
97         >));
98     BOOST_MPL_ASSERT((
99         boost::is_same<
100             boost::fusion::result_of::value_at_key<et, keys::age>::type,
101             boost::fusion::result_of::value_at_c<et, 1>::type
102         >));
103 
104     BOOST_MPL_ASSERT((
105         boost::is_same<
106             boost::fusion::result_of::at_key<et, keys::name>::type,
107             fusion::extension::adt_attribute_proxy<et, 0, false>
108         >));
109     BOOST_MPL_ASSERT((
110         boost::is_same<
111             boost::fusion::result_of::at_key<et, keys::age>::type,
112             fusion::extension::adt_attribute_proxy<et, 1, false>
113         >));
114     BOOST_MPL_ASSERT((
115         boost::is_same<
116             boost::fusion::result_of::at_key<et, keys::name>::type,
117             boost::fusion::result_of::front<et>::type
118         >));
119     BOOST_MPL_ASSERT((
120         boost::is_same<
121             boost::fusion::result_of::at_key<et, keys::age>::type,
122             boost::fusion::result_of::back<et>::type
123         >));
124 
125     BOOST_MPL_ASSERT((
126         boost::is_same<
127             boost::fusion::result_of::at_key<etc, keys::name>::type,
128             fusion::extension::adt_attribute_proxy<et, 0, true>
129         >));
130     BOOST_MPL_ASSERT((
131         boost::is_same<
132             boost::fusion::result_of::at_key<etc, keys::age>::type,
133             fusion::extension::adt_attribute_proxy<et, 1, true>
134         >));
135     BOOST_MPL_ASSERT((
136         boost::is_same<
137             boost::fusion::result_of::at_key<etc, keys::name>::type,
138             boost::fusion::result_of::front<etc>::type
139         >));
140     BOOST_MPL_ASSERT((
141         boost::is_same<
142             boost::fusion::result_of::at_key<etc, keys::age>::type,
143             boost::fusion::result_of::back<etc>::type
144         >));
145 
146     BOOST_MPL_ASSERT((
147         boost::is_same<
148             fusion::extension::adt_attribute_proxy<et, 0, false>::type,
149             std::string&
150         >));
151     BOOST_MPL_ASSERT((
152         boost::is_same<
153             fusion::extension::adt_attribute_proxy<et, 0, true>::type,
154             std::string const&
155         >));
156     BOOST_MPL_ASSERT((
157         boost::is_same<
158             fusion::extension::adt_attribute_proxy<et, 1, false>::type,
159             int&
160         >));
161     BOOST_MPL_ASSERT((
162         boost::is_same<
163             fusion::extension::adt_attribute_proxy<et, 1, true>::type,
164             int const&
165         >));
166 
167     {
168         std::string& name=fusion::at_key<keys::name>(e);
169         int& age=fusion::at_key<keys::age>(e);
170         BOOST_TEST(name=="marshall mathers");
171         BOOST_TEST(age==37);
172         BOOST_TEST(fusion::at_key<keys::name>(e).get()=="marshall mathers");
173         BOOST_TEST(fusion::at_key<keys::age>(e).get()==37);
174         BOOST_TEST(fusion::front(e).get()=="marshall mathers");
175         BOOST_TEST(fusion::back(e).get()==37);
176     }
177 
178     {
179         std::string const& name=fusion::at_key<keys::name>(ec);
180         int const& age=fusion::at_key<keys::age>(ec);
181         BOOST_TEST(name=="marshall mathers");
182         BOOST_TEST(age==37);
183         BOOST_TEST(fusion::at_key<keys::name>(ec).get()=="marshall mathers");
184         BOOST_TEST(fusion::at_key<keys::age>(ec).get()==37);
185         BOOST_TEST(fusion::front(ec).get()=="marshall mathers");
186         BOOST_TEST(fusion::back(ec).get()==37);
187     }
188     return boost::report_errors();
189 }
190