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.
5 #ifndef BOOST_CONVERT_TEST_HPP
6 #define BOOST_CONVERT_TEST_HPP
8 #include <boost/convert/detail/forward.hpp>
9 #include <boost/make_default.hpp>
10 #include <boost/static_assert.hpp>
13 #include <string.h> // For strlen, strcmp, memcpy
14 #include <memory.h> // Is needed for 'memset'
19 # pragma warning(disable: 4189) // local variable is initialized but not referenced.
20 # pragma warning(disable: 4127) // conditional expression is constant.
21 # pragma warning(disable: 4100) // unreferenced formal parameter.
22 # pragma warning(disable: 4714) // marked as __forceinline not #endif
23 # pragma warning(disable: 4706)
24 # pragma warning(disable: 4005)
25 # pragma warning(disable: 4459) // declaration hides global declaration
31 enum value_type { no, up, dn };
33 change(value_type v =no) : value_(v) {}
34 bool operator==(change v) const { return value_ == v.value_; }
35 value_type value() const { return value_; }
37 private: value_type value_;
40 //[change_stream_operators
41 std::istream& operator>>(std::istream& stream, change& chg)
43 std::string str; stream >> str;
45 /**/ if (str == "up") chg = change::up;
46 else if (str == "dn") chg = change::dn;
47 else if (str == "no") chg = change::no;
48 else stream.setstate(std::ios_base::failbit);
53 std::ostream& operator<<(std::ostream& stream, change const& chg)
55 return stream << (chg == change::up ? "up" : chg == change::dn ? "dn" : "no");
58 //[change_convert_operators
59 inline void operator>>(change chg, boost::optional<std::string>& str)
61 str = chg == change::up ? "up" : chg == change::dn ? "dn" : "no";
64 inline void operator>>(std::string const& str, boost::optional<change>& chg)
66 /**/ if (str == "up") chg = change::up;
67 else if (str == "dn") chg = change::dn;
68 else if (str == "no") chg = change::no;
71 //[direction_declaration
74 // Note: the class does NOT have the default constructor.
76 enum value_type { up, dn };
78 direction(value_type value) : value_(value) {}
79 bool operator==(direction that) const { return value_ == that.value_; }
80 value_type value() const { return value_; }
82 private: value_type value_;
85 //[direction_stream_operators
86 std::istream& operator>>(std::istream& stream, direction& dir)
88 std::string str; stream >> str;
90 /**/ if (str == "up") dir = direction::up;
91 else if (str == "dn") dir = direction::dn;
92 else stream.setstate(std::ios_base::failbit);
96 std::ostream& operator<<(std::ostream& stream, direction const& dir)
98 return stream << (dir.value() == direction::up ? "up" : "dn");
101 //[direction_declaration_make_default
104 template<> inline direction make_default<direction>()
106 return direction(direction::up);
110 // Quick and dirty small-string implementation for performance tests.
111 //[my_string_declaration
114 typedef my_string this_type;
115 typedef char value_type;
116 typedef value_type* iterator;
117 typedef value_type const* const_iterator;
120 my_string (const_iterator, const_iterator =0);
122 char const* c_str () const { return storage_; }
123 const_iterator begin () const { return storage_; }
124 const_iterator end () const { return storage_ + strlen(storage_); }
125 this_type& operator= (char const*);
129 static size_t const size_ = 12;
130 char storage_[size_];
134 my_string::my_string()
140 my_string::my_string(const_iterator beg, const_iterator end)
142 std::size_t const sz = end ? (end - beg) : strlen(beg);
144 BOOST_ASSERT(sz < size_);
145 memcpy(storage_, beg, sz); storage_[sz] = 0;
150 my_string::operator=(char const* str)
152 BOOST_ASSERT(strlen(str) < size_);
153 strcpy(storage_, str);
157 inline bool operator==(char const* s1, my_string const& s2) { return strcmp(s1, s2.c_str()) == 0; }
158 inline bool operator==(my_string const& s1, char const* s2) { return strcmp(s2, s1.c_str()) == 0; }
164 #if defined(__QNXNTO__)
165 static bool const is_qnx = true;
167 static bool const is_qnx = false;
170 #if defined(_MSC_VER) && _MSC_VER < 1900
171 static bool const is_msc = true;
172 static bool const is_old_msc = true;
173 #elif defined(_MSC_VER)
174 static bool const is_msc = true;
175 static bool const is_old_msc = false;
176 #elif defined(__MINGW32__)
177 static bool const is_msc = true;
178 static bool const is_old_msc = true;
180 static bool const is_msc = false;
181 static bool const is_old_msc = false;
186 #endif // BOOST_CONVERT_TEST_HPP