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/config.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
26 # pragma warning(disable: 4456) // declaration hides previous local declaration
32 enum value_type { no, up, dn };
34 change(value_type v =no) : value_(v) {}
35 bool operator==(change v) const { return value_ == v.value_; }
36 value_type value() const { return value_; }
38 private: value_type value_;
41 //[change_stream_operators
42 std::istream& operator>>(std::istream& stream, change& chg)
44 std::string str; stream >> str;
46 /**/ if (str == "up") chg = change::up;
47 else if (str == "dn") chg = change::dn;
48 else if (str == "no") chg = change::no;
49 else stream.setstate(std::ios_base::failbit);
54 std::ostream& operator<<(std::ostream& stream, change const& chg)
56 return stream << (chg == change::up ? "up" : chg == change::dn ? "dn" : "no");
59 //[change_convert_operators
60 inline void operator>>(change chg, boost::optional<std::string>& str)
62 str = chg == change::up ? "up" : chg == change::dn ? "dn" : "no";
65 inline void operator>>(std::string const& str, boost::optional<change>& chg)
67 /**/ if (str == "up") chg = change::up;
68 else if (str == "dn") chg = change::dn;
69 else if (str == "no") chg = change::no;
72 //[direction_declaration
75 // Note: the class does NOT have the default constructor.
77 enum value_type { up, dn };
79 direction(value_type value) : value_(value) {}
80 bool operator==(direction that) const { return value_ == that.value_; }
81 value_type value() const { return value_; }
83 private: value_type value_;
86 //[direction_stream_operators
87 std::istream& operator>>(std::istream& stream, direction& dir)
89 std::string str; stream >> str;
91 /**/ if (str == "up") dir = direction::up;
92 else if (str == "dn") dir = direction::dn;
93 else stream.setstate(std::ios_base::failbit);
97 std::ostream& operator<<(std::ostream& stream, direction const& dir)
99 return stream << (dir.value() == direction::up ? "up" : "dn");
102 //[direction_declaration_make_default
105 template<> inline direction make_default<direction>()
107 return direction(direction::up);
111 // Quick and dirty small-string implementation for performance tests.
112 //[my_string_declaration
115 typedef my_string this_type;
116 typedef char value_type;
117 typedef value_type* iterator;
118 typedef value_type const* const_iterator;
121 my_string (const_iterator, const_iterator =0);
123 char const* c_str () const { return storage_; }
124 const_iterator begin () const { return storage_; }
125 const_iterator end () const { return storage_ + strlen(storage_); }
126 this_type& operator= (char const*);
130 static size_t const size_ = 12;
131 char storage_[size_];
135 my_string::my_string()
141 my_string::my_string(const_iterator beg, const_iterator end)
143 std::size_t const sz = end ? (end - beg) : strlen(beg);
145 BOOST_ASSERT(sz < size_);
146 memcpy(storage_, beg, sz); storage_[sz] = 0;
151 my_string::operator=(char const* str)
153 BOOST_ASSERT(strlen(str) < size_);
154 strcpy(storage_, str);
158 inline bool operator==(char const* s1, my_string const& s2) { return strcmp(s1, s2.c_str()) == 0; }
159 inline bool operator==(my_string const& s1, char const* s2) { return strcmp(s2, s1.c_str()) == 0; }
165 #if defined(__QNXNTO__)
166 static bool const is_qnx = true;
168 static bool const is_qnx = false;
171 #if defined(_MSC_VER) && _MSC_VER < 1900
172 static bool const is_msc = true;
173 static bool const is_old_msc = true;
174 #elif defined(_MSC_VER)
175 static bool const is_msc = true;
176 static bool const is_old_msc = false;
177 #elif defined(__MINGW32__)
178 static bool const is_msc = true;
179 static bool const is_old_msc = true;
181 static bool const is_msc = false;
182 static bool const is_old_msc = false;
187 #endif // BOOST_CONVERT_TEST_HPP