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'
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)
29 enum value_type { no, up, dn };
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_; }
35 private: value_type value_;
38 //[change_stream_operators
39 std::istream& operator>>(std::istream& stream, change& chg)
41 std::string str; stream >> str;
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);
51 std::ostream& operator<<(std::ostream& stream, change const& chg)
53 return stream << (chg == change::up ? "up" : chg == change::dn ? "dn" : "no");
56 //[change_convert_operators
57 inline void operator>>(change chg, boost::optional<std::string>& str)
59 str = chg == change::up ? "up" : chg == change::dn ? "dn" : "no";
62 inline void operator>>(std::string const& str, boost::optional<change>& chg)
64 /**/ if (str == "up") chg = change::up;
65 else if (str == "dn") chg = change::dn;
66 else if (str == "no") chg = change::no;
69 //[direction_declaration
72 // Note: the class does NOT have the default constructor.
74 enum value_type { up, dn };
76 direction(value_type value) : value_(value) {}
77 bool operator==(direction that) const { return value_ == that.value_; }
78 value_type value() const { return value_; }
80 private: value_type value_;
83 //[direction_stream_operators
84 std::istream& operator>>(std::istream& stream, direction& dir)
86 std::string str; stream >> str;
88 /**/ if (str == "up") dir = direction::up;
89 else if (str == "dn") dir = direction::dn;
90 else stream.setstate(std::ios_base::failbit);
94 std::ostream& operator<<(std::ostream& stream, direction const& dir)
96 return stream << (dir.value() == direction::up ? "up" : "dn");
99 //[direction_declaration_make_default
102 template<> inline direction make_default<direction>()
104 return direction(direction::up);
108 // Quick and dirty small-string implementation for performance tests.
109 //[my_string_declaration
112 typedef my_string this_type;
113 typedef char value_type;
114 typedef value_type* iterator;
115 typedef value_type const* const_iterator;
118 my_string (const_iterator, const_iterator =0);
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*);
127 static size_t const size_ = 12;
128 char storage_[size_];
132 my_string::my_string()
138 my_string::my_string(const_iterator beg, const_iterator end)
140 std::size_t const sz = end ? (end - beg) : strlen(beg);
142 BOOST_ASSERT(sz < size_);
143 memcpy(storage_, beg, sz); storage_[sz] = 0;
148 my_string::operator=(char const* str)
150 BOOST_ASSERT(strlen(str) < size_);
151 strcpy(storage_, str);
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; }
162 #if defined(__QNXNTO__)
163 static bool const is_qnx = true;
165 static bool const is_qnx = false;
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;
178 static bool const is_msc = false;
179 static bool const is_old_msc = false;
184 #endif // BOOST_CONVERT_TEST_HPP