• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/variant_reference_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, Itay Maman
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.hpp"
14 #include "boost/core/lightweight_test.hpp"
15 
16 #include "boost/mpl/bool.hpp"
17 #include "boost/type_traits/add_reference.hpp"
18 #include "boost/type_traits/is_pointer.hpp"
19 
20 /////
21 // support types and functions
22 
23 struct base_t { };
24 struct derived_t : base_t { };
25 
26 template <typename Base, typename Derived>
check_base_derived(Base * b,Derived * d,long)27 bool check_base_derived(Base* b, Derived* d, long)
28 {
29     return b == d;
30 }
31 
32 template <typename Base, typename Derived>
check_base_derived(Base & b,Derived & d,int)33 bool check_base_derived(Base& b, Derived& d, int)
34 {
35     return &b == &d;
36 }
37 
38 template <typename T>
39     typename boost::add_reference<T>::type
wknd_get(boost::variant<T &> & var,long)40 wknd_get(boost::variant<T&>& var, long)
41 {
42     return boost::get<T>(var);
43 }
44 
45 template <typename T>
46     typename boost::add_reference<T>::type
wknd_get(boost::variant<T> & var,int)47 wknd_get(boost::variant<T>& var, int)
48 {
49     return boost::get<T>(var);
50 }
51 
52 /////
53 // test functions
54 
55 template <typename T>
test_reference_content(T & t,const T & value1,const T & value2)56 void test_reference_content(T& t, const T& value1, const T& value2)
57 {
58     BOOST_TEST( !(value1 == value2) );
59 
60     /////
61 
62     boost::variant< T& > var(t);
63     BOOST_TEST(( boost::get<T>(&var) == &t ));
64 
65     t = value1;
66     BOOST_TEST(( boost::get<T>(var) == value1 ));
67 
68     /////
69 
70     boost::variant< T > var2(var);
71     BOOST_TEST(( boost::get<T>(var2) == value1 ));
72 
73     t = value2;
74     BOOST_TEST(( boost::get<T>(var2) == value1 ));
75 }
76 
77 template <typename Base, typename Derived>
base_derived_test(Derived d)78 void base_derived_test(Derived d)
79 {
80     Base b(d);
81     BOOST_TEST((check_base_derived(
82           b
83         , d
84         , 1L
85         )));
86 
87     boost::variant<Base> base_var(d);
88     BOOST_TEST((check_base_derived(
89           wknd_get(base_var, 1L)
90         , d
91         , 1L
92         )));
93 
94     boost::variant<Derived> derived_var(d);
95     boost::variant<Base> base_from_derived_var(derived_var);
96     BOOST_TEST((check_base_derived(
97           wknd_get(base_from_derived_var, 1L)
98         , wknd_get(derived_var, 1L)
99         , 1L
100         )));
101 }
102 
main()103 int main()
104 {
105     int i = 0;
106     test_reference_content(i, 1, 2);
107 
108     /////
109 
110     derived_t d;
111     base_derived_test< int&,int >(i);
112     base_derived_test< base_t*,derived_t* >(&d);
113     base_derived_test< base_t&,derived_t& >(d);
114 
115     return boost::report_errors();
116 }
117