1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test1.cpp header 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/config.hpp"
14 
15 #ifdef BOOST_MSVC
16 #pragma warning(disable:4244) // conversion from const int to const short
17 #endif
18 
19 #include "boost/core/lightweight_test.hpp"
20 #include "boost/variant.hpp"
21 
22 #include "class_a.h"
23 #include "jobs.h"
24 
25 #include <iostream>
26 #include <string>
27 #include <vector>
28 
29 
30 
run()31 void run()
32 {
33 
34    using boost::apply_visitor;
35    using boost::variant;
36    using std::string;
37    using std::vector;
38    using std::cout;
39    using std::endl;
40 
41    typedef variant< char*, string, short > t_var0;
42    typedef variant< int, string, double > t_var1;
43    typedef variant< short, const char* > t_var2;
44    typedef variant< string, char > t_var3;
45    typedef variant< unsigned short, const char* > t_var4;
46    typedef variant< unsigned short, const char*, t_var2 > t_var5;
47    typedef variant< unsigned short, const char*, t_var5 > t_var6;
48    typedef variant< class_a, const void* > t_var7;
49    typedef variant< t_var6, int > t_var8;
50    typedef variant< t_var8, unsigned short > t_var9;
51    typedef variant< char, unsigned char > t_var10;
52    typedef variant< short, int, vector<int>, long> t_var11;
53 
54    t_var1 v1;
55    t_var0 v0;
56    t_var2 v2;
57    t_var3 v3;
58    t_var4 v4;
59    t_var5 v5;
60    t_var6 v6;
61    t_var7 v7;
62    t_var8 v8;
63    t_var9 v9;
64    t_var10 v10;
65    t_var11 v11;
66 
67 
68    //
69    // Check assignment rules
70    //
71 
72    v2 = 4;
73    v4 = v2;
74    verify(v4, spec<unsigned short>());
75 
76    v2 = "abc";
77    v4 = v2;
78    verify(v4, spec<const char*>(), "[V] abc");
79 
80    v5 = "def";
81    verify(v5, spec<const char*>(), "[V] def");
82 
83    v5 = v2;
84    verify(v5, spec<t_var2>(), "[V] [V] abc");
85 
86    v6 = 58;
87    verify(v6, spec<unsigned short>(), "[V] 58");
88 
89    v6 = v5;
90    verify(v6, spec<t_var5>(), "[V] [V] [V] abc");
91 
92    v8 = v2;
93    verify(v8, spec<t_var6>(), "[V] [V] abc");
94 
95    v8 = v6;
96    verify(v8, spec<t_var6>(), "[V] [V] [V] [V] abc");
97 
98    v7 = v2;
99    verify(v7, spec<const void*>());
100 
101    v7 = 199;
102    verify(v7, spec<class_a>(), "[V] class_a(199)");
103 
104    v2 = 200;
105    v7 = v2;
106    verify(v7, spec<class_a>(), "[V] class_a(200)");
107 
108 
109 
110    //
111    // Check sizes of held values
112    //
113    total_sizeof ts;
114 
115    v1 = 5.9;
116    apply_visitor(ts, v1);
117 
118    v1 = 'B';
119    apply_visitor(ts, v1);
120 
121    v1 = 3.4f;
122    apply_visitor(ts, v1);
123 
124    BOOST_TEST(ts.result() == sizeof(int) + sizeof(double)*2);
125 
126    v11 = 5;
127    string res_s = apply_visitor(int_printer(), v11);
128    BOOST_TEST(res_s == "5");
129 
130    //
131    // A variant object holding an std::vector
132    //
133    vector<int> int_vec_1;
134    int_vec_1.push_back(512);
135    int_vec_1.push_back(256);
136    int_vec_1.push_back(128);
137    int_vec_1.push_back(64);
138 
139    v11 = int_vec_1;
140    res_s = apply_visitor(int_printer(), v11);
141    BOOST_TEST(res_s == ",512,256,128,64");
142 }
143 
144 
145 
main()146 int main()
147 {
148     run();
149     return boost::report_errors();
150 }
151