Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 1 | /////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2006. Distributed under the Boost |
| 4 | // Software License, Version 1.0. (See accompanying file |
| 5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // See http://www.boost.org/libs/container for documentation. |
| 8 | // |
| 9 | /////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef BOOST_CONTAINER_TEST_MOVABLE_INT_HEADER |
| 12 | #define BOOST_CONTAINER_TEST_MOVABLE_INT_HEADER |
| 13 | |
| 14 | #include <boost/container/detail/config_begin.hpp> |
| 15 | #include <boost/container/detail/workaround.hpp> |
| 16 | #include <boost/move/utility_core.hpp> |
| 17 | #include <ostream> |
| 18 | #include <climits> |
| 19 | #include <boost/assert.hpp> |
| 20 | |
| 21 | namespace boost { |
| 22 | namespace container { |
| 23 | namespace test { |
| 24 | |
| 25 | template<class T> |
| 26 | struct is_copyable; |
| 27 | |
| 28 | template<> |
| 29 | struct is_copyable<int> |
| 30 | { |
| 31 | static const bool value = true; |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | class movable_int |
| 36 | { |
| 37 | BOOST_MOVABLE_BUT_NOT_COPYABLE(movable_int) |
| 38 | |
| 39 | public: |
| 40 | |
| 41 | static unsigned int count; |
| 42 | |
| 43 | movable_int() |
| 44 | : m_int(0) |
| 45 | { ++count; } |
| 46 | |
| 47 | explicit movable_int(int a) |
| 48 | : m_int(a) |
| 49 | { |
| 50 | //Disallow INT_MIN |
| 51 | BOOST_ASSERT(this->m_int != INT_MIN); |
| 52 | ++count; |
| 53 | } |
| 54 | |
| 55 | movable_int(BOOST_RV_REF(movable_int) mmi) |
| 56 | : m_int(mmi.m_int) |
| 57 | { mmi.m_int = 0; ++count; } |
| 58 | |
| 59 | movable_int & operator= (BOOST_RV_REF(movable_int) mmi) |
| 60 | { this->m_int = mmi.m_int; mmi.m_int = 0; return *this; } |
| 61 | |
| 62 | movable_int & operator= (int i) |
| 63 | { this->m_int = i; BOOST_ASSERT(this->m_int != INT_MIN); return *this; } |
| 64 | |
| 65 | ~movable_int() |
| 66 | { |
| 67 | //Double destructor called |
| 68 | BOOST_ASSERT(this->m_int != INT_MIN); |
| 69 | this->m_int = INT_MIN; |
| 70 | --count; |
| 71 | } |
| 72 | |
| 73 | friend bool operator ==(const movable_int &l, const movable_int &r) |
| 74 | { return l.m_int == r.m_int; } |
| 75 | |
| 76 | friend bool operator !=(const movable_int &l, const movable_int &r) |
| 77 | { return l.m_int != r.m_int; } |
| 78 | |
| 79 | friend bool operator <(const movable_int &l, const movable_int &r) |
| 80 | { return l.m_int < r.m_int; } |
| 81 | |
| 82 | friend bool operator <=(const movable_int &l, const movable_int &r) |
| 83 | { return l.m_int <= r.m_int; } |
| 84 | |
| 85 | friend bool operator >=(const movable_int &l, const movable_int &r) |
| 86 | { return l.m_int >= r.m_int; } |
| 87 | |
| 88 | friend bool operator >(const movable_int &l, const movable_int &r) |
| 89 | { return l.m_int > r.m_int; } |
| 90 | |
| 91 | int get_int() const |
| 92 | { return m_int; } |
| 93 | |
| 94 | friend bool operator==(const movable_int &l, int r) |
| 95 | { return l.get_int() == r; } |
| 96 | |
| 97 | friend bool operator==(int l, const movable_int &r) |
| 98 | { return l == r.get_int(); } |
| 99 | |
| 100 | friend bool operator<(const movable_int &l, int r) |
| 101 | { return l.get_int() < r; } |
| 102 | |
| 103 | friend bool operator<(int l, const movable_int &r) |
| 104 | { return l < r.get_int(); } |
| 105 | |
| 106 | private: |
| 107 | int m_int; |
| 108 | }; |
| 109 | |
| 110 | unsigned int movable_int::count = 0; |
| 111 | |
| 112 | inline movable_int produce_movable_int() |
| 113 | { return movable_int(); } |
| 114 | |
| 115 | template<class E, class T> |
| 116 | std::basic_ostream<E, T> & operator<< |
| 117 | (std::basic_ostream<E, T> & os, movable_int const & p) |
| 118 | |
| 119 | { |
| 120 | os << p.get_int(); |
| 121 | return os; |
| 122 | } |
| 123 | |
| 124 | template<> |
| 125 | struct is_copyable<movable_int> |
| 126 | { |
| 127 | static const bool value = false; |
| 128 | }; |
| 129 | |
| 130 | class movable_and_copyable_int |
| 131 | { |
| 132 | BOOST_COPYABLE_AND_MOVABLE(movable_and_copyable_int) |
| 133 | |
| 134 | public: |
| 135 | |
| 136 | static unsigned int count; |
| 137 | |
| 138 | movable_and_copyable_int() |
| 139 | : m_int(0) |
| 140 | { ++count; } |
| 141 | |
| 142 | explicit movable_and_copyable_int(int a) |
| 143 | : m_int(a) |
| 144 | { |
| 145 | //Disallow INT_MIN |
| 146 | BOOST_ASSERT(this->m_int != INT_MIN); |
| 147 | ++count; |
| 148 | } |
| 149 | |
| 150 | movable_and_copyable_int(const movable_and_copyable_int& mmi) |
| 151 | : m_int(mmi.m_int) |
| 152 | { ++count; } |
| 153 | |
| 154 | movable_and_copyable_int(BOOST_RV_REF(movable_and_copyable_int) mmi) |
| 155 | : m_int(mmi.m_int) |
| 156 | { mmi.m_int = 0; ++count; } |
| 157 | |
| 158 | ~movable_and_copyable_int() |
| 159 | { |
| 160 | //Double destructor called |
| 161 | BOOST_ASSERT(this->m_int != INT_MIN); |
| 162 | this->m_int = INT_MIN; |
| 163 | --count; |
| 164 | } |
| 165 | |
| 166 | movable_and_copyable_int &operator= (BOOST_COPY_ASSIGN_REF(movable_and_copyable_int) mi) |
| 167 | { this->m_int = mi.m_int; return *this; } |
| 168 | |
| 169 | movable_and_copyable_int & operator= (BOOST_RV_REF(movable_and_copyable_int) mmi) |
| 170 | { this->m_int = mmi.m_int; mmi.m_int = 0; return *this; } |
| 171 | |
| 172 | movable_and_copyable_int & operator= (int i) |
| 173 | { this->m_int = i; BOOST_ASSERT(this->m_int != INT_MIN); return *this; } |
| 174 | |
| 175 | friend bool operator ==(const movable_and_copyable_int &l, const movable_and_copyable_int &r) |
| 176 | { return l.m_int == r.m_int; } |
| 177 | |
| 178 | friend bool operator !=(const movable_and_copyable_int &l, const movable_and_copyable_int &r) |
| 179 | { return l.m_int != r.m_int; } |
| 180 | |
| 181 | friend bool operator <(const movable_and_copyable_int &l, const movable_and_copyable_int &r) |
| 182 | { return l.m_int < r.m_int; } |
| 183 | |
| 184 | friend bool operator <=(const movable_and_copyable_int &l, const movable_and_copyable_int &r) |
| 185 | { return l.m_int <= r.m_int; } |
| 186 | |
| 187 | friend bool operator >=(const movable_and_copyable_int &l, const movable_and_copyable_int &r) |
| 188 | { return l.m_int >= r.m_int; } |
| 189 | |
| 190 | friend bool operator >(const movable_and_copyable_int &l, const movable_and_copyable_int &r) |
| 191 | { return l.m_int > r.m_int; } |
| 192 | |
| 193 | int get_int() const |
| 194 | { return m_int; } |
| 195 | |
| 196 | friend bool operator==(const movable_and_copyable_int &l, int r) |
| 197 | { return l.get_int() == r; } |
| 198 | |
| 199 | friend bool operator==(int l, const movable_and_copyable_int &r) |
| 200 | { return l == r.get_int(); } |
| 201 | |
| 202 | friend bool operator<(const movable_and_copyable_int &l, int r) |
| 203 | { return l.get_int() < r; } |
| 204 | |
| 205 | friend bool operator<(int l, const movable_and_copyable_int &r) |
| 206 | { return l < r.get_int(); } |
| 207 | |
| 208 | private: |
| 209 | int m_int; |
| 210 | }; |
| 211 | |
| 212 | unsigned int movable_and_copyable_int::count = 0; |
| 213 | |
| 214 | inline movable_and_copyable_int produce_movable_and_copyable_int() |
| 215 | { return movable_and_copyable_int(); } |
| 216 | |
| 217 | template<class E, class T> |
| 218 | std::basic_ostream<E, T> & operator<< |
| 219 | (std::basic_ostream<E, T> & os, movable_and_copyable_int const & p) |
| 220 | |
| 221 | { |
| 222 | os << p.get_int(); |
| 223 | return os; |
| 224 | } |
| 225 | |
| 226 | template<> |
| 227 | struct is_copyable<movable_and_copyable_int> |
| 228 | { |
| 229 | static const bool value = true; |
| 230 | }; |
| 231 | |
| 232 | class copyable_int |
| 233 | { |
| 234 | public: |
| 235 | |
| 236 | static unsigned int count; |
| 237 | |
| 238 | copyable_int() |
| 239 | : m_int(0) |
| 240 | { ++count; } |
| 241 | |
| 242 | explicit copyable_int(int a) |
| 243 | : m_int(a) |
| 244 | { |
| 245 | //Disallow INT_MIN |
| 246 | BOOST_ASSERT(this->m_int != INT_MIN); |
| 247 | ++count; |
| 248 | } |
| 249 | |
| 250 | copyable_int(const copyable_int& mmi) |
| 251 | : m_int(mmi.m_int) |
| 252 | { ++count; } |
| 253 | |
| 254 | copyable_int & operator= (int i) |
| 255 | { this->m_int = i; BOOST_ASSERT(this->m_int != INT_MIN); return *this; } |
| 256 | |
| 257 | copyable_int & operator= (const copyable_int &ci) |
| 258 | { this->m_int = ci.m_int; BOOST_ASSERT(this->m_int != INT_MIN); return *this; } |
| 259 | |
| 260 | ~copyable_int() |
| 261 | { |
| 262 | //Double destructor called |
| 263 | BOOST_ASSERT(this->m_int != INT_MIN); |
| 264 | this->m_int = INT_MIN; |
| 265 | --count; |
| 266 | } |
| 267 | |
| 268 | friend bool operator ==(const copyable_int &l, const copyable_int &r) |
| 269 | { return l.m_int == r.m_int; } |
| 270 | |
| 271 | friend bool operator !=(const copyable_int &l, const copyable_int &r) |
| 272 | { return l.m_int != r.m_int; } |
| 273 | |
| 274 | friend bool operator <(const copyable_int &l, const copyable_int &r) |
| 275 | { return l.m_int < r.m_int; } |
| 276 | |
| 277 | friend bool operator <=(const copyable_int &l, const copyable_int &r) |
| 278 | { return l.m_int <= r.m_int; } |
| 279 | |
| 280 | friend bool operator >=(const copyable_int &l, const copyable_int &r) |
| 281 | { return l.m_int >= r.m_int; } |
| 282 | |
| 283 | friend bool operator >(const copyable_int &l, const copyable_int &r) |
| 284 | { return l.m_int > r.m_int; } |
| 285 | |
| 286 | int get_int() const |
| 287 | { return m_int; } |
| 288 | |
| 289 | friend bool operator==(const copyable_int &l, int r) |
| 290 | { return l.get_int() == r; } |
| 291 | |
| 292 | friend bool operator==(int l, const copyable_int &r) |
| 293 | { return l == r.get_int(); } |
| 294 | |
| 295 | friend bool operator<(const copyable_int &l, int r) |
| 296 | { return l.get_int() < r; } |
| 297 | |
| 298 | friend bool operator<(int l, const copyable_int &r) |
| 299 | { return l < r.get_int(); } |
| 300 | |
| 301 | private: |
| 302 | int m_int; |
| 303 | }; |
| 304 | |
| 305 | unsigned int copyable_int::count = 0; |
| 306 | |
| 307 | inline copyable_int produce_copyable_int() |
| 308 | { return copyable_int(); } |
| 309 | |
| 310 | template<class E, class T> |
| 311 | std::basic_ostream<E, T> & operator<< |
| 312 | (std::basic_ostream<E, T> & os, copyable_int const & p) |
| 313 | |
| 314 | { |
| 315 | os << p.get_int(); |
| 316 | return os; |
| 317 | } |
| 318 | |
| 319 | template<> |
| 320 | struct is_copyable<copyable_int> |
| 321 | { |
| 322 | static const bool value = true; |
| 323 | }; |
| 324 | |
| 325 | class non_copymovable_int |
| 326 | { |
| 327 | non_copymovable_int(const non_copymovable_int& mmi); |
| 328 | non_copymovable_int & operator= (const non_copymovable_int &mi); |
| 329 | |
| 330 | public: |
| 331 | |
| 332 | static unsigned int count; |
| 333 | |
| 334 | non_copymovable_int() |
| 335 | : m_int(0) |
| 336 | { ++count; } |
| 337 | |
| 338 | explicit non_copymovable_int(int a) |
| 339 | : m_int(a) |
| 340 | { ++count; } |
| 341 | |
| 342 | ~non_copymovable_int() |
| 343 | { m_int = 0; --count; } |
| 344 | |
| 345 | bool operator ==(const non_copymovable_int &mi) const |
| 346 | { return this->m_int == mi.m_int; } |
| 347 | |
| 348 | bool operator !=(const non_copymovable_int &mi) const |
| 349 | { return this->m_int != mi.m_int; } |
| 350 | |
| 351 | bool operator <(const non_copymovable_int &mi) const |
| 352 | { return this->m_int < mi.m_int; } |
| 353 | |
| 354 | bool operator <=(const non_copymovable_int &mi) const |
| 355 | { return this->m_int <= mi.m_int; } |
| 356 | |
| 357 | bool operator >=(const non_copymovable_int &mi) const |
| 358 | { return this->m_int >= mi.m_int; } |
| 359 | |
| 360 | bool operator >(const non_copymovable_int &mi) const |
| 361 | { return this->m_int > mi.m_int; } |
| 362 | |
| 363 | int get_int() const |
| 364 | { return m_int; } |
| 365 | |
| 366 | friend bool operator==(const non_copymovable_int &l, int r) |
| 367 | { return l.get_int() == r; } |
| 368 | |
| 369 | friend bool operator==(int l, const non_copymovable_int &r) |
| 370 | { return l == r.get_int(); } |
| 371 | |
| 372 | friend bool operator<(const non_copymovable_int &l, int r) |
| 373 | { return l.get_int() < r; } |
| 374 | |
| 375 | friend bool operator<(int l, const non_copymovable_int &r) |
| 376 | { return l < r.get_int(); } |
| 377 | |
| 378 | private: |
| 379 | int m_int; |
| 380 | }; |
| 381 | |
| 382 | unsigned int non_copymovable_int::count = 0; |
| 383 | |
| 384 | template<class T> |
| 385 | struct life_count |
| 386 | { |
| 387 | static unsigned check(unsigned) { return true; } |
| 388 | }; |
| 389 | |
| 390 | template<> |
| 391 | struct life_count< movable_int > |
| 392 | { |
| 393 | static unsigned check(unsigned c) |
| 394 | { return c == movable_int::count; } |
| 395 | }; |
| 396 | |
| 397 | template<> |
| 398 | struct life_count< copyable_int > |
| 399 | { |
| 400 | static unsigned check(unsigned c) |
| 401 | { return c == copyable_int::count; } |
| 402 | }; |
| 403 | |
| 404 | template<> |
| 405 | struct life_count< movable_and_copyable_int > |
| 406 | { |
| 407 | static unsigned check(unsigned c) |
| 408 | { return c == movable_and_copyable_int::count; } |
| 409 | }; |
| 410 | |
| 411 | template<> |
| 412 | struct life_count< non_copymovable_int > |
| 413 | { |
| 414 | static unsigned check(unsigned c) |
| 415 | { return c == non_copymovable_int::count; } |
| 416 | }; |
| 417 | |
| 418 | |
| 419 | } //namespace test { |
| 420 | } //namespace container { |
| 421 | } //namespace boost { |
| 422 | |
| 423 | #include <boost/container/detail/config_end.hpp> |
| 424 | |
| 425 | #endif //#ifndef BOOST_CONTAINER_TEST_MOVABLE_INT_HEADER |