Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/variant/test/variant_visit_test.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.7 KB
Line 
1//-----------------------------------------------------------------------------
2// boost-libs variant/test/variant_visit_test.cpp source file
3// See http://www.boost.org for updates, documentation, and revision history.
4//-----------------------------------------------------------------------------
5//
6// Copyright (c) 2003
7// Eric Friedman
8//
9// Distributed under the Boost Software License, Version 1.0. (See
10// accompanying file LICENSE_1_0.txt or copy at
11// http://www.boost.org/LICENSE_1_0.txt)
12
13#include "boost/variant/variant.hpp"
14#include "boost/variant/apply_visitor.hpp"
15#include "boost/variant/static_visitor.hpp"
16#include "boost/test/minimal.hpp"
17
18#include "boost/mpl/bool.hpp"
19#include "boost/mpl/and.hpp"
20#include "boost/type_traits/is_same.hpp"
21
22struct udt1
23{
24};
25
26struct udt2
27{
28};
29
30template <typename T>
31class unary_check_content_type
32    : public boost::static_visitor<bool>
33{
34public:
35
36    // not recommended design, but simplifies workarounds:
37
38    template <typename U>
39    bool operator()(U&) const
40    {
41        return ::boost::is_same<T,U>::value;
42    }
43
44};
45
46template <typename T1, typename T2>
47class binary_check_content_type
48    : public boost::static_visitor<bool>
49{
50public:
51
52    // not recommended design, but simplifies workarounds:
53
54    template <typename U1, typename U2>
55    bool operator()(U1&, U2&) const
56    {
57        return ::boost::mpl::and_<
58              boost::is_same<T1,U1>
59            , boost::is_same<T2,U2>
60            >::value;
61    }
62
63};
64
65template <typename Checker, typename Variant>
66inline void unary_test(Variant& var, Checker* = 0)
67{
68    Checker checker;
69    const Checker& const_checker = checker;
70
71    // standard tests
72
73    BOOST_CHECK( boost::apply_visitor(checker, var) );
74    BOOST_CHECK( boost::apply_visitor(const_checker, var) );
75    BOOST_CHECK( boost::apply_visitor(Checker(), var) );
76
77    // delayed tests
78
79    BOOST_CHECK( boost::apply_visitor(checker)(var) );
80    BOOST_CHECK( boost::apply_visitor(const_checker)(var) );
81}
82
83template <typename Checker, typename Variant1, typename Variant2>
84inline void binary_test(Variant1& var1, Variant2& var2, Checker* = 0)
85{
86    Checker checker;
87    const Checker& const_checker = checker;
88
89    // standard tests
90
91    BOOST_CHECK( boost::apply_visitor(checker, var1, var2) );
92    BOOST_CHECK( boost::apply_visitor(const_checker, var1, var2) );
93    BOOST_CHECK( boost::apply_visitor(Checker(), var1, var2) );
94
95    // delayed tests
96
97    BOOST_CHECK( boost::apply_visitor(checker)(var1, var2) );
98    BOOST_CHECK( boost::apply_visitor(const_checker)(var1, var2) );
99}
100
101int test_main(int , char* [])
102{
103    typedef boost::variant<udt1,udt2> var_t;
104    udt1 u1;
105    var_t var1(u1);
106    udt2 u2;
107    var_t var2(u2);
108
109    const var_t& cvar1 = var1;
110    const var_t& cvar2 = var2;
111
112    //
113    // unary tests
114    //
115
116    typedef unary_check_content_type<udt1> check1_t;
117    typedef unary_check_content_type<const udt1> check1_const_t;
118    typedef unary_check_content_type<udt2> check2_t;
119    typedef unary_check_content_type<const udt2> check2_const_t;
120
121    unary_test< check1_t       >(var1);
122    unary_test< check1_const_t >(cvar1);
123
124    unary_test< check2_t       >(var2);
125    unary_test< check2_const_t >(cvar2);
126
127    //
128    // binary tests
129    //
130
131    typedef binary_check_content_type<udt1,udt2> check12_t;
132    typedef binary_check_content_type<const udt1, const udt2> check12_const_t;
133    typedef binary_check_content_type<udt2,udt1> check21_t;
134    typedef binary_check_content_type<const udt2, const udt1> check21_const_t;
135
136    binary_test< check12_t       >(var1,var2);
137    binary_test< check12_const_t >(cvar1,cvar2);
138
139    binary_test< check21_t       >(var2,var1);
140    binary_test< check21_const_t >(cvar2,cvar1);
141
142    return boost::exit_success;
143}
Note: See TracBrowser for help on using the repository browser.