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