File: joint_view.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (188 lines) | stat: -rw-r--r-- 6,769 bytes parent folder | download | duplicates (16)
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) 2001-2011 Joel de Guzman

    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/container/map.hpp>
#include <boost/fusion/container/set.hpp>
#include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/view/joint_view/joint_view.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/sequence/comparison/equal_to.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/sequence/intrinsic/has_key.hpp>
#include <boost/fusion/sequence/intrinsic/begin.hpp>
#include <boost/fusion/iterator/next.hpp>
#include <boost/fusion/iterator/key_of.hpp>
#include <boost/fusion/iterator/value_of.hpp>
#include <boost/fusion/iterator/deref_data.hpp>
#include <boost/mpl/assert.hpp>
#include <string>

struct X
{
    operator char const*() const
    {
        return "<X-object>";
    }
};

int
main()
{
    using namespace boost::fusion;
    namespace fusion = boost::fusion;

    std::cout << tuple_open('[');
    std::cout << tuple_close(']');
    std::cout << tuple_delimiter(", ");

/// Testing joint_view

    {
        vector<int> t1(3);
        vector<X> t2;
        typedef joint_view<vector<int>, vector<X> > view_type;
        view_type view(t1, t2);

        std::cout << view << std::endl;
        BOOST_TEST((view == make_vector(3, X())));
    }

    {
        vector<int, char> t1(3, 'x');
        vector<X> t2;
        typedef joint_view<vector<int, char>, vector<X> > view_type;
        view_type view(t1, t2);
        std::cout << view << std::endl;
        BOOST_TEST((view == make_vector(3, 'x', X())));

        *begin(view) = 4;
        BOOST_TEST(at_c<0>(t1) == 4);
    }

    {
        vector<int, char> t1(3, 'x');
        vector<X, int> t2;
        typedef joint_view<vector<int, char>, vector<X, int> > view_type;
        view_type view(t1, t2);
        std::cout << view << std::endl;
        BOOST_TEST((view == make_vector(3, 'x', X(), 0)));
    }

    {
        typedef vector<int> t1_type;
        t1_type t1(777);
        typedef vector<int, char, double> t2_type;
        t2_type t2(1, 'x', 3.3);

        {
            typedef joint_view<t1_type, t2_type> view_type;
            view_type view(t1, t2);
            std::cout << view << std::endl;
            BOOST_TEST((view == make_vector(777, 1, 'x', 3.3)));
        }

        {
            typedef joint_view<t2_type, t1_type> view_type;
            view_type view(t2, t1);
            std::cout << view << std::endl;
            BOOST_TEST((view == make_vector(1, 'x', 3.3, 777)));
        }

        {
            typedef joint_view<t2_type, t1_type> jv_type;
            typedef joint_view<jv_type, jv_type> jv2_type;

            jv_type jv(t2, t1);
            jv2_type jv2(jv, jv);

            std::cout << jv << std::endl;
            std::cout << jv2 << std::endl;

            BOOST_TEST(jv2
                == make_vector(1, 'x', 3.3, 777, 1, 'x', 3.3, 777));
        }

        {
            typedef joint_view<t2_type, t1_type> jt_type;
            typedef joint_view<t1_type, t2_type> jv2_type;
            typedef joint_view<jt_type, jv2_type> jv3_type;

            jt_type jt(t2, t1);
            jv2_type jv2(t1, t2);
            jv3_type jv3(jt, jv2);

            std::cout << jt << std::endl;
            std::cout << jv2 << std::endl;
            std::cout << jv3 << std::endl;

            BOOST_TEST(jv3
                == make_vector(1, 'x', 3.3, 777, 777, 1, 'x', 3.3));
        }

        {
            typedef joint_view<vector<>, t1_type> jt_type;
            vector<> empty;
            jt_type jt(empty, t1);
            std::cout << jt << std::endl;
            BOOST_TEST(jt == make_vector(777));
        }

        {
            typedef joint_view<t1_type, vector<> > jt_type;
            vector<> empty;
            jt_type jt(t1, empty);
            std::cout << jt << std::endl;
            BOOST_TEST(jt == make_vector(777));
        }

        {
            typedef joint_view<vector<>, vector<> > jt_type;
            vector<> empty;
            jt_type jt(empty, empty);
            std::cout << jt << std::endl;
            BOOST_TEST(jt == make_vector());
        }
    }

    {
        typedef map<pair<void,int> > map_type;
        map_type m(make_pair<void>(0));

        typedef set<std::string, float> set_type;
        set_type s("foo", 1.3f);

        typedef joint_view<map_type, set_type> joint_view_type;
        joint_view_type j(m,s);

        BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, void>::type));
        BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, std::string>::type));
        BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, float>::type));

        BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<joint_view_type>::type>::type, void>));
        BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type, std::string>));
        BOOST_MPL_ASSERT((boost::is_same<
                boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type>::type
              , float>));

        BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<joint_view_type>::type>::type, int>));
        BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type, std::string>));
        BOOST_MPL_ASSERT((boost::is_same<
                boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type>::type
              , float>));

        std::cout << deref_data(begin(j)) << std::endl;
        std::cout << deref_data(fusion::next(begin(j))) << std::endl;
        std::cout << deref_data(fusion::next(fusion::next(begin(j)))) << std::endl;
        BOOST_TEST((deref_data(begin(j)) == 0));
        BOOST_TEST((deref_data(fusion::next(begin(j))) == "foo"));
        BOOST_TEST((deref_data(fusion::next(fusion::next(begin(j)))) == 1.3f));
    }

    return boost::report_errors();
}