First public contribution.
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test2.cpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
7 // Eric Friedman, Itay Maman
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)
13 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
16 #include "boost/config.hpp"
17 #include "boost/test/minimal.hpp"
18 #include "boost/variant.hpp"
27 #include "std_log_result.h"
28 #define LOG_FILENAME_LINE __FILE__, __LINE__
30 using boost::apply_visitor;
34 BOOST_STATIC_CONSTANT(size_t, e_limit = 101);
36 short_string() : len_(0)
41 short_string(const char* src)
43 #ifndef BOOST_NO_STDC_NAMESPACE
45 #endif // BOOST_NO_STDC_NAMESPACE
47 size_t e_limit = this->e_limit; // avoid warnings on some compilers
48 size_t src_len = strlen(src);
50 len_ = (std::min)(src_len, e_limit-1);
51 std::copy(src, src + len_, buffer_);
55 short_string(const short_string& other) : len_(other.len_)
57 std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
60 void swap(short_string& other)
64 std::copy(buffer_, buffer_ + e_limit, temp);
65 std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
66 std::copy(temp, temp + e_limit, other.buffer_);
68 std::swap(len_, other.len_);
71 short_string& operator=(const short_string& rhs)
73 short_string temp(rhs);
79 operator const char*() const
86 char buffer_[e_limit];
91 std::ostream& operator<<(std::ostream& out, const short_string& s)
93 out << static_cast<const char*>(s);
101 using boost::variant;
103 variant<short, short_string> v0;
104 variant<char, const char*> v1;
105 variant<short_string, char > v2;
108 // Default construction
110 verify(v0, spec<short>());
111 verify(v1, spec<char>());
112 verify(v2, spec<short_string>());
115 // Implicit conversion to bounded type
118 verify(v1, spec<const char*>(), "[V] I am v1");
121 verify(v2, spec<short_string>(), "[V] I am v2");
124 // Variant-to-variant assignment
128 verify(v0, spec<short_string>(), "[V] I am v1");
131 verify(v1, spec<const char*>(), "[V] I am v1");
138 // Implicit conversion to bounded type
140 verify(v0, spec<short>(), "[V] 88");
141 verify(v1, spec<char>(), "[V] X");
145 int test_main(int , char* [])
147 std_log(LOG_FILENAME_LINE,"[Test Case for test2]");
150 testResultXml("test2");