summaryrefslogtreecommitdiff
path: root/libs/fusion/test/sequence/adt_attribute_proxy.cpp
blob: d5a947afb6b5d51a44482b5d944acb8e0d868fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*=============================================================================
    Copyright (c) 2010 Christopher Schmidt

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/

#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/adapted/adt/adapt_assoc_adt.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_same.hpp>
#include <iostream>
#include <string>

namespace fusion=boost::fusion;

template<typename Name, typename Age>
struct employee
{
private:
    Name name;
    Age age;

public:
    template<typename OtherName>
    void
    set_name(OtherName const& n)
    {
        name=n;
    }

    template<typename OtherAge>
    void
    set_age(OtherAge const& a)
    {
        age=a;
    }

    Name& get_name()
    {
        return name;
    }

    Name const& get_name()const
    {
        return name;
    }

    Age& get_age()
    {
        return age;
    }

    Age const& get_age()const
    {
        return age;
    }
};

namespace keys
{
    struct name;
    struct age;
}

BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
    (Name)(Age),
    (employee) (Name)(Age),
    (Name&, Name const&, obj.get_name(), obj.template set_name<Val>(val), keys::name)
        (Age&, Age const&, obj.get_age(), obj.template set_age<Val>(val), keys::age))

int main()
{
    typedef employee<std::string, int> et;
    typedef et const etc;
    et e;
    etc& ec=e;

    fusion::at_key<keys::name>(e)="marshall mathers";
    fusion::at_key<keys::age>(e)=37;

    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::value_at_key<et, keys::name>::type,
            std::string
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::value_at_key<et, keys::name>::type,
            boost::fusion::result_of::value_at_c<et, 0>::type
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::value_at_key<et, keys::age>::type,
            int
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::value_at_key<et, keys::age>::type,
            boost::fusion::result_of::value_at_c<et, 1>::type
        >));

    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<et, keys::name>::type,
            fusion::extension::adt_attribute_proxy<et, 0, false>
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<et, keys::age>::type,
            fusion::extension::adt_attribute_proxy<et, 1, false>
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<et, keys::name>::type,
            boost::fusion::result_of::front<et>::type
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<et, keys::age>::type,
            boost::fusion::result_of::back<et>::type
        >));

    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<etc, keys::name>::type,
            fusion::extension::adt_attribute_proxy<et, 0, true>
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<etc, keys::age>::type,
            fusion::extension::adt_attribute_proxy<et, 1, true>
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<etc, keys::name>::type,
            boost::fusion::result_of::front<etc>::type
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            boost::fusion::result_of::at_key<etc, keys::age>::type,
            boost::fusion::result_of::back<etc>::type
        >));

    BOOST_MPL_ASSERT((
        boost::is_same<
            fusion::extension::adt_attribute_proxy<et, 0, false>::type,
            std::string&
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            fusion::extension::adt_attribute_proxy<et, 0, true>::type,
            std::string const&
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            fusion::extension::adt_attribute_proxy<et, 1, false>::type,
            int&
        >));
    BOOST_MPL_ASSERT((
        boost::is_same<
            fusion::extension::adt_attribute_proxy<et, 1, true>::type,
            int const&
        >));

    {
        std::string& name=fusion::at_key<keys::name>(e);
        int& age=fusion::at_key<keys::age>(e);
        BOOST_TEST(name=="marshall mathers");
        BOOST_TEST(age==37);
        BOOST_TEST(fusion::at_key<keys::name>(e).get()=="marshall mathers");
        BOOST_TEST(fusion::at_key<keys::age>(e).get()==37);
        BOOST_TEST(fusion::front(e).get()=="marshall mathers");
        BOOST_TEST(fusion::back(e).get()==37);
    }

    {
        std::string const& name=fusion::at_key<keys::name>(ec);
        int const& age=fusion::at_key<keys::age>(ec);
        BOOST_TEST(name=="marshall mathers");
        BOOST_TEST(age==37);
        BOOST_TEST(fusion::at_key<keys::name>(ec).get()=="marshall mathers");
        BOOST_TEST(fusion::at_key<keys::age>(ec).get()==37);
        BOOST_TEST(fusion::front(ec).get()=="marshall mathers");
        BOOST_TEST(fusion::back(ec).get()==37);
    }
}