1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// Copyright (c) 2009-2016 Vladimir Batov.
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
#ifndef BOOST_CONVERT_TEST_HPP
#define BOOST_CONVERT_TEST_HPP
#include <boost/convert/detail/config.hpp>
#include <boost/make_default.hpp>
#include <boost/static_assert.hpp>
#include <string>
#include <istream>
#include <string.h> // For strlen, strcmp, memcpy
#include <memory.h> // Is needed for 'memset'
#include <stdio.h>
#include <time.h>
#if defined(_MSC_VER)
# pragma warning(disable: 4189) // local variable is initialized but not referenced.
# pragma warning(disable: 4127) // conditional expression is constant.
# pragma warning(disable: 4100) // unreferenced formal parameter.
# pragma warning(disable: 4714) // marked as __forceinline not #endif
# pragma warning(disable: 4706)
# pragma warning(disable: 4005)
# pragma warning(disable: 4459) // declaration hides global declaration
# pragma warning(disable: 4456) // declaration hides previous local declaration
#endif
//[change_declaration
struct change
{
enum value_type { no, up, dn };
change(value_type v =no) : value_(v) {}
bool operator==(change v) const { return value_ == v.value_; }
value_type value() const { return value_; }
private: value_type value_;
};
//]
//[change_stream_operators
std::istream& operator>>(std::istream& stream, change& chg)
{
std::string str; stream >> str;
/**/ if (str == "up") chg = change::up;
else if (str == "dn") chg = change::dn;
else if (str == "no") chg = change::no;
else stream.setstate(std::ios_base::failbit);
return stream;
}
std::ostream& operator<<(std::ostream& stream, change const& chg)
{
return stream << (chg == change::up ? "up" : chg == change::dn ? "dn" : "no");
}
//]
//[change_convert_operators
inline void operator>>(change chg, boost::optional<std::string>& str)
{
str = chg == change::up ? "up" : chg == change::dn ? "dn" : "no";
}
inline void operator>>(std::string const& str, boost::optional<change>& chg)
{
/**/ if (str == "up") chg = change::up;
else if (str == "dn") chg = change::dn;
else if (str == "no") chg = change::no;
}
//]
//[direction_declaration
struct direction
{
// Note: the class does NOT have the default constructor.
enum value_type { up, dn };
direction(value_type value) : value_(value) {}
bool operator==(direction that) const { return value_ == that.value_; }
value_type value() const { return value_; }
private: value_type value_;
};
//]
//[direction_stream_operators
std::istream& operator>>(std::istream& stream, direction& dir)
{
std::string str; stream >> str;
/**/ if (str == "up") dir = direction::up;
else if (str == "dn") dir = direction::dn;
else stream.setstate(std::ios_base::failbit);
return stream;
}
std::ostream& operator<<(std::ostream& stream, direction const& dir)
{
return stream << (dir.value() == direction::up ? "up" : "dn");
}
//]
//[direction_declaration_make_default
namespace boost
{
template<> inline direction make_default<direction>()
{
return direction(direction::up);
}
}
//]
// Quick and dirty small-string implementation for performance tests.
//[my_string_declaration
struct my_string
{
typedef my_string this_type;
typedef char value_type;
typedef value_type* iterator;
typedef value_type const* const_iterator;
my_string ();
my_string (const_iterator, const_iterator =0);
char const* c_str () const { return storage_; }
const_iterator begin () const { return storage_; }
const_iterator end () const { return storage_ + strlen(storage_); }
this_type& operator= (char const*);
private:
static size_t const size_ = 12;
char storage_[size_];
};
//]
inline
my_string::my_string()
{
storage_[0] = 0;
}
inline
my_string::my_string(const_iterator beg, const_iterator end)
{
std::size_t const sz = end ? (end - beg) : strlen(beg);
BOOST_ASSERT(sz < size_);
memcpy(storage_, beg, sz); storage_[sz] = 0;
}
inline
my_string&
my_string::operator=(char const* str)
{
BOOST_ASSERT(strlen(str) < size_);
strcpy(storage_, str);
return *this;
}
inline bool operator==(char const* s1, my_string const& s2) { return strcmp(s1, s2.c_str()) == 0; }
inline bool operator==(my_string const& s1, char const* s2) { return strcmp(s2, s1.c_str()) == 0; }
namespace test
{
struct cnv
{
#if defined(__QNXNTO__)
static bool const is_qnx = true;
#else
static bool const is_qnx = false;
#endif
#if defined(_MSC_VER) && _MSC_VER < 1900
static bool const is_msc = true;
static bool const is_old_msc = true;
#elif defined(_MSC_VER)
static bool const is_msc = true;
static bool const is_old_msc = false;
#elif defined(__MINGW32__)
static bool const is_msc = true;
static bool const is_old_msc = true;
#else
static bool const is_msc = false;
static bool const is_old_msc = false;
#endif
};
}
#endif // BOOST_CONVERT_TEST_HPP
|