1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test2.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 "jobs.h"
23
24 #include <cassert>
25 #include <iostream>
26 #include <algorithm>
27 #include <cstring>
28
29 using boost::apply_visitor;
30
31 struct short_string
32 {
33 BOOST_STATIC_CONSTANT(size_t, e_limit = 101);
34
short_stringshort_string35 short_string() : len_(0)
36 {
37 buffer_[0] = '\0';
38 }
39
short_stringshort_string40 short_string(const char* src)
41 {
42 #ifndef BOOST_NO_STDC_NAMESPACE
43 using std::strlen;
44 #endif // BOOST_NO_STDC_NAMESPACE
45
46 size_t limit = this->e_limit; // avoid warnings on some compilers
47 size_t src_len = strlen(src);
48
49 len_ = (std::min)(src_len, limit-1);
50 std::copy(src, src + len_, buffer_);
51 buffer_[len_] = '\0';
52 }
53
short_stringshort_string54 short_string(const short_string& other) : len_(other.len_)
55 {
56 std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
57 }
58
swapshort_string59 void swap(short_string& other)
60 {
61 char temp[e_limit];
62
63 std::copy(buffer_, buffer_ + e_limit, temp);
64 std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
65 std::copy(temp, temp + e_limit, other.buffer_);
66
67 std::swap(len_, other.len_);
68 }
69
operator =short_string70 short_string& operator=(const short_string& rhs)
71 {
72 short_string temp(rhs);
73 swap(temp);
74
75 return *this;
76 }
77
operator const char*short_string78 operator const char*() const
79 {
80 return buffer_;
81 }
82
83
84 private:
85 char buffer_[e_limit];
86 size_t len_;
87 }; //short_string
88
89
operator <<(std::ostream & out,const short_string & s)90 std::ostream& operator<<(std::ostream& out, const short_string& s)
91 {
92 out << static_cast<const char*>(s);
93 return out;
94 }
95
96
97
run()98 void run()
99 {
100 using boost::variant;
101
102 variant<short, short_string> v0;
103 variant<char, const char*> v1;
104 variant<short_string, char > v2;
105
106 //
107 // Default construction
108 //
109 verify(v0, spec<short>());
110 verify(v1, spec<char>());
111 verify(v2, spec<short_string>());
112
113 //
114 // Implicit conversion to bounded type
115 //
116 v1 = "I am v1";
117 verify(v1, spec<const char*>(), "[V] I am v1");
118
119 v2 = "I am v2";
120 verify(v2, spec<short_string>(), "[V] I am v2");
121
122 //
123 // Variant-to-variant assignment
124 //
125
126 v0 = v1;
127 verify(v0, spec<short_string>(), "[V] I am v1");
128
129 v1 = v0;
130 verify(v1, spec<const char*>(), "[V] I am v1");
131
132 const int n0 = 88;
133 v1 = n0;
134 v0 = v1;
135
136 //
137 // Implicit conversion to bounded type
138 //
139 verify(v0, spec<short>(), "[V] 88");
140 verify(v1, spec<char>(), "[V] X");
141 }
142
143
main()144 int main()
145 {
146 run();
147 return boost::report_errors();
148 }
149
150