• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/config.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 #include <time.h>
17 
18 #if defined(_MSC_VER)
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
27 #endif
28 
29 //[change_declaration
30 struct change
31 {
32     enum value_type { no, up, dn };
33 
changechange34     change(value_type v =no) : value_(v) {}
operator ==change35     bool operator==(change v) const { return value_ == v.value_; }
valuechange36     value_type value() const { return value_; }
37 
38     private: value_type value_;
39 };
40 //]
41 //[change_stream_operators
operator >>(std::istream & stream,change & chg)42 std::istream& operator>>(std::istream& stream, change& chg)
43 {
44     std::string str; stream >> str;
45 
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);
50 
51     return stream;
52 }
53 
operator <<(std::ostream & stream,change const & chg)54 std::ostream& operator<<(std::ostream& stream, change const& chg)
55 {
56     return stream << (chg == change::up ? "up" : chg == change::dn ? "dn" : "no");
57 }
58 //]
59 //[change_convert_operators
operator >>(change chg,boost::optional<std::string> & str)60 inline void operator>>(change chg, boost::optional<std::string>& str)
61 {
62     str = chg == change::up ? "up" : chg == change::dn ? "dn" : "no";
63 }
64 
operator >>(std::string const & str,boost::optional<change> & chg)65 inline void operator>>(std::string const& str, boost::optional<change>& chg)
66 {
67     /**/ if (str == "up") chg = change::up;
68     else if (str == "dn") chg = change::dn;
69     else if (str == "no") chg = change::no;
70 }
71 //]
72 //[direction_declaration
73 struct direction
74 {
75     // Note: the class does NOT have the default constructor.
76 
77     enum value_type { up, dn };
78 
directiondirection79     direction(value_type value) : value_(value) {}
operator ==direction80     bool operator==(direction that) const { return value_ == that.value_; }
valuedirection81     value_type value() const { return value_; }
82 
83     private: value_type value_;
84 };
85 //]
86 //[direction_stream_operators
operator >>(std::istream & stream,direction & dir)87 std::istream& operator>>(std::istream& stream, direction& dir)
88 {
89     std::string str; stream >> str;
90 
91     /**/ if (str == "up") dir = direction::up;
92     else if (str == "dn") dir = direction::dn;
93     else stream.setstate(std::ios_base::failbit);
94 
95     return stream;
96 }
operator <<(std::ostream & stream,direction const & dir)97 std::ostream& operator<<(std::ostream& stream, direction const& dir)
98 {
99     return stream << (dir.value() == direction::up ? "up" : "dn");
100 }
101 //]
102 //[direction_declaration_make_default
103 namespace boost
104 {
make_default()105     template<> inline direction make_default<direction>()
106     {
107         return direction(direction::up);
108     }
109 }
110 //]
111 // Quick and dirty small-string implementation for performance tests.
112 //[my_string_declaration
113 struct my_string
114 {
115     typedef my_string              this_type;
116     typedef char                  value_type;
117     typedef value_type*             iterator;
118     typedef value_type const* const_iterator;
119 
120     my_string ();
121     my_string (const_iterator, const_iterator =0);
122 
c_strmy_string123     char const*    c_str () const { return storage_; }
beginmy_string124     const_iterator begin () const { return storage_; }
endmy_string125     const_iterator   end () const { return storage_ + strlen(storage_); }
126     this_type& operator= (char const*);
127 
128     private:
129 
130     static size_t const size_ = 12;
131     char storage_[size_];
132 };
133 //]
134 inline
my_string()135 my_string::my_string()
136 {
137     storage_[0] = 0;
138 }
139 
140 inline
my_string(const_iterator beg,const_iterator end)141 my_string::my_string(const_iterator beg, const_iterator end)
142 {
143     std::size_t const sz = end ? (end - beg) : strlen(beg);
144 
145     BOOST_ASSERT(sz < size_);
146     memcpy(storage_, beg, sz); storage_[sz] = 0;
147 }
148 
149 inline
150 my_string&
operator =(char const * str)151 my_string::operator=(char const* str)
152 {
153     BOOST_ASSERT(strlen(str) < size_);
154     strcpy(storage_, str);
155     return *this;
156 }
157 
operator ==(char const * s1,my_string const & s2)158 inline bool operator==(char const* s1, my_string const& s2) { return strcmp(s1, s2.c_str()) == 0; }
operator ==(my_string const & s1,char const * s2)159 inline bool operator==(my_string const& s1, char const* s2) { return strcmp(s2, s1.c_str()) == 0; }
160 
161 namespace test
162 {
163     struct cnv
164     {
165 #if defined(__QNXNTO__)
166         static bool const     is_qnx = true;
167 #else
168         static bool const     is_qnx = false;
169 #endif
170 
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;
180 #else
181         static bool const     is_msc = false;
182         static bool const is_old_msc = false;
183 #endif
184     };
185 }
186 
187 #endif // BOOST_CONVERT_TEST_HPP
188