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 
movable_int()43    movable_int()
44       :  m_int(0)
45    { ++count;  }
46 
movable_int(int a)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 
movable_int(BOOST_RV_REF (movable_int)mmi)55    movable_int(BOOST_RV_REF(movable_int) mmi)
56       :  m_int(mmi.m_int)
57    {  mmi.m_int = 0; ++count; }
58 
operator =(BOOST_RV_REF (movable_int)mmi)59    movable_int & operator= (BOOST_RV_REF(movable_int) mmi)
60    {  this->m_int = mmi.m_int;   mmi.m_int = 0; return *this;  }
61 
operator =(int i)62    movable_int & operator= (int i)
63    {  this->m_int = i;  BOOST_ASSERT(this->m_int != INT_MIN); return *this;  }
64 
~movable_int()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 
operator ==(const movable_int & l,const movable_int & r)73    friend bool operator ==(const movable_int &l, const movable_int &r)
74    {  return l.m_int == r.m_int;   }
75 
operator !=(const movable_int & l,const movable_int & r)76    friend bool operator !=(const movable_int &l, const movable_int &r)
77    {  return l.m_int != r.m_int;   }
78 
operator <(const movable_int & l,const movable_int & r)79    friend bool operator <(const movable_int &l, const movable_int &r)
80    {  return l.m_int < r.m_int;   }
81 
operator <=(const movable_int & l,const movable_int & r)82    friend bool operator <=(const movable_int &l, const movable_int &r)
83    {  return l.m_int <= r.m_int;   }
84 
operator >=(const movable_int & l,const movable_int & r)85    friend bool operator >=(const movable_int &l, const movable_int &r)
86    {  return l.m_int >= r.m_int;   }
87 
operator >(const movable_int & l,const movable_int & r)88    friend bool operator >(const movable_int &l, const movable_int &r)
89    {  return l.m_int > r.m_int;   }
90 
get_int() const91    int get_int() const
92    {  return m_int;  }
93 
operator ==(const movable_int & l,int r)94    friend bool operator==(const movable_int &l, int r)
95    {  return l.get_int() == r;   }
96 
operator ==(int l,const movable_int & r)97    friend bool operator==(int l, const movable_int &r)
98    {  return l == r.get_int();   }
99 
operator <(const movable_int & l,int r)100    friend bool operator<(const movable_int &l, int r)
101    {  return l.get_int() < r;   }
102 
operator <(int l,const movable_int & r)103    friend bool operator<(int l, const movable_int &r)
104    {  return l < r.get_int();   }
105 
hash_value(const movable_int & v)106    friend std::size_t hash_value(const movable_int &v)
107    {  return (std::size_t)v.get_int(); }
108 
109    private:
110    int m_int;
111 };
112 
113 unsigned int movable_int::count = 0;
114 
produce_movable_int()115 inline movable_int produce_movable_int()
116 {  return movable_int();  }
117 
118 template<class E, class T>
operator <<(std::basic_ostream<E,T> & os,movable_int const & p)119 std::basic_ostream<E, T> & operator<<
120    (std::basic_ostream<E, T> & os, movable_int const & p)
121 
122 {
123     os << p.get_int();
124     return os;
125 }
126 
127 template<>
128 struct is_copyable<movable_int>
129 {
130    static const bool value = false;
131 };
132 
133 class movable_and_copyable_int
134 {
135    BOOST_COPYABLE_AND_MOVABLE(movable_and_copyable_int)
136 
137    public:
138 
139    static unsigned int count;
140 
movable_and_copyable_int()141    movable_and_copyable_int()
142       :  m_int(0)
143    { ++count; }
144 
movable_and_copyable_int(int a)145    explicit movable_and_copyable_int(int a)
146       :  m_int(a)
147    {
148       //Disallow INT_MIN
149       BOOST_ASSERT(this->m_int != INT_MIN);
150       ++count;
151    }
152 
movable_and_copyable_int(const movable_and_copyable_int & mmi)153    movable_and_copyable_int(const movable_and_copyable_int& mmi)
154       :  m_int(mmi.m_int)
155    {  ++count; }
156 
movable_and_copyable_int(BOOST_RV_REF (movable_and_copyable_int)mmi)157    movable_and_copyable_int(BOOST_RV_REF(movable_and_copyable_int) mmi)
158       :  m_int(mmi.m_int)
159    {  mmi.m_int = 0; ++count; }
160 
~movable_and_copyable_int()161    ~movable_and_copyable_int()
162    {
163       //Double destructor called
164       BOOST_ASSERT(this->m_int != INT_MIN);
165       this->m_int = INT_MIN;
166       --count;
167    }
168 
operator =(BOOST_COPY_ASSIGN_REF (movable_and_copyable_int)mi)169    movable_and_copyable_int &operator= (BOOST_COPY_ASSIGN_REF(movable_and_copyable_int) mi)
170    {  this->m_int = mi.m_int;    return *this;  }
171 
operator =(BOOST_RV_REF (movable_and_copyable_int)mmi)172    movable_and_copyable_int & operator= (BOOST_RV_REF(movable_and_copyable_int) mmi)
173    {  this->m_int = mmi.m_int;   mmi.m_int = 0;    return *this;  }
174 
operator =(int i)175    movable_and_copyable_int & operator= (int i)
176    {  this->m_int = i;  BOOST_ASSERT(this->m_int != INT_MIN); return *this;  }
177 
operator ==(const movable_and_copyable_int & l,const movable_and_copyable_int & r)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 
operator !=(const movable_and_copyable_int & l,const movable_and_copyable_int & r)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 
operator <(const movable_and_copyable_int & l,const movable_and_copyable_int & r)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 
operator <=(const movable_and_copyable_int & l,const movable_and_copyable_int & r)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 
operator >=(const movable_and_copyable_int & l,const movable_and_copyable_int & r)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 
operator >(const movable_and_copyable_int & l,const movable_and_copyable_int & r)193    friend bool operator >(const movable_and_copyable_int &l, const movable_and_copyable_int &r)
194    {  return l.m_int > r.m_int;   }
195 
get_int() const196    int get_int() const
197    {  return m_int;  }
198 
operator ==(const movable_and_copyable_int & l,int r)199    friend bool operator==(const movable_and_copyable_int &l, int r)
200    {  return l.get_int() == r;   }
201 
operator ==(int l,const movable_and_copyable_int & r)202    friend bool operator==(int l, const movable_and_copyable_int &r)
203    {  return l == r.get_int();   }
204 
operator <(const movable_and_copyable_int & l,int r)205    friend bool operator<(const movable_and_copyable_int &l, int r)
206    {  return l.get_int() < r;   }
207 
operator <(int l,const movable_and_copyable_int & r)208    friend bool operator<(int l, const movable_and_copyable_int &r)
209    {  return l < r.get_int();   }
210 
hash_value(const movable_and_copyable_int & v)211    friend std::size_t hash_value(const movable_and_copyable_int &v)
212    {  return (std::size_t)v.get_int(); }
213 
214    private:
215    int m_int;
216 };
217 
218 unsigned int movable_and_copyable_int::count = 0;
219 
produce_movable_and_copyable_int()220 inline movable_and_copyable_int produce_movable_and_copyable_int()
221 {  return movable_and_copyable_int();  }
222 
223 template<class E, class T>
operator <<(std::basic_ostream<E,T> & os,movable_and_copyable_int const & p)224 std::basic_ostream<E, T> & operator<<
225    (std::basic_ostream<E, T> & os, movable_and_copyable_int const & p)
226 
227 {
228     os << p.get_int();
229     return os;
230 }
231 
232 template<>
233 struct is_copyable<movable_and_copyable_int>
234 {
235    static const bool value = true;
236 };
237 
238 class copyable_int
239 {
240    public:
241 
242    static unsigned int count;
243 
copyable_int()244    copyable_int()
245       :  m_int(0)
246    { ++count; }
247 
copyable_int(int a)248    explicit copyable_int(int a)
249       :  m_int(a)
250    {
251       //Disallow INT_MIN
252       BOOST_ASSERT(this->m_int != INT_MIN);
253       ++count;
254    }
255 
copyable_int(const copyable_int & mmi)256    copyable_int(const copyable_int& mmi)
257       :  m_int(mmi.m_int)
258    { ++count; }
259 
operator =(int i)260    copyable_int & operator= (int i)
261    {  this->m_int = i;  BOOST_ASSERT(this->m_int != INT_MIN); return *this;  }
262 
operator =(const copyable_int & ci)263    copyable_int & operator= (const copyable_int &ci)
264    {  this->m_int = ci.m_int;  BOOST_ASSERT(this->m_int != INT_MIN); return *this;  }
265 
~copyable_int()266    ~copyable_int()
267    {
268       //Double destructor called
269       BOOST_ASSERT(this->m_int != INT_MIN);
270       this->m_int = INT_MIN;
271       --count;
272    }
273 
operator ==(const copyable_int & l,const copyable_int & r)274    friend bool operator ==(const copyable_int &l, const copyable_int &r)
275    {  return l.m_int == r.m_int;   }
276 
operator !=(const copyable_int & l,const copyable_int & r)277    friend bool operator !=(const copyable_int &l, const copyable_int &r)
278    {  return l.m_int != r.m_int;   }
279 
operator <(const copyable_int & l,const copyable_int & r)280    friend bool operator <(const copyable_int &l, const copyable_int &r)
281    {  return l.m_int < r.m_int;   }
282 
operator <=(const copyable_int & l,const copyable_int & r)283    friend bool operator <=(const copyable_int &l, const copyable_int &r)
284    {  return l.m_int <= r.m_int;   }
285 
operator >=(const copyable_int & l,const copyable_int & r)286    friend bool operator >=(const copyable_int &l, const copyable_int &r)
287    {  return l.m_int >= r.m_int;   }
288 
operator >(const copyable_int & l,const copyable_int & r)289    friend bool operator >(const copyable_int &l, const copyable_int &r)
290    {  return l.m_int > r.m_int;   }
291 
get_int() const292    int get_int() const
293    {  return m_int;  }
294 
operator ==(const copyable_int & l,int r)295    friend bool operator==(const copyable_int &l, int r)
296    {  return l.get_int() == r;   }
297 
operator ==(int l,const copyable_int & r)298    friend bool operator==(int l, const copyable_int &r)
299    {  return l == r.get_int();   }
300 
operator <(const copyable_int & l,int r)301    friend bool operator<(const copyable_int &l, int r)
302    {  return l.get_int() < r;   }
303 
operator <(int l,const copyable_int & r)304    friend bool operator<(int l, const copyable_int &r)
305    {  return l < r.get_int();   }
306 
hash_value(const copyable_int & v)307    friend std::size_t hash_value(const copyable_int &v)
308    {  return (std::size_t)v.get_int(); }
309 
310    private:
311    int m_int;
312 };
313 
314 unsigned int copyable_int::count = 0;
315 
produce_copyable_int()316 inline copyable_int produce_copyable_int()
317 {  return copyable_int();  }
318 
319 template<class E, class T>
operator <<(std::basic_ostream<E,T> & os,copyable_int const & p)320 std::basic_ostream<E, T> & operator<<
321    (std::basic_ostream<E, T> & os, copyable_int const & p)
322 
323 {
324     os << p.get_int();
325     return os;
326 }
327 
328 template<>
329 struct is_copyable<copyable_int>
330 {
331    static const bool value = true;
332 };
333 
334 class non_copymovable_int
335 {
336    non_copymovable_int(const non_copymovable_int& mmi);
337    non_copymovable_int & operator= (const non_copymovable_int &mi);
338 
339    public:
340 
341    static unsigned int count;
342 
non_copymovable_int()343    non_copymovable_int()
344       :  m_int(0)
345    { ++count; }
346 
non_copymovable_int(int a)347    explicit non_copymovable_int(int a)
348       :  m_int(a)
349    { ++count; }
350 
~non_copymovable_int()351    ~non_copymovable_int()
352    {  m_int = 0;  --count; }
353 
operator ==(const non_copymovable_int & mi) const354    bool operator ==(const non_copymovable_int &mi) const
355    {  return this->m_int == mi.m_int;   }
356 
operator !=(const non_copymovable_int & mi) const357    bool operator !=(const non_copymovable_int &mi) const
358    {  return this->m_int != mi.m_int;   }
359 
operator <(const non_copymovable_int & mi) const360    bool operator <(const non_copymovable_int &mi) const
361    {  return this->m_int < mi.m_int;   }
362 
operator <=(const non_copymovable_int & mi) const363    bool operator <=(const non_copymovable_int &mi) const
364    {  return this->m_int <= mi.m_int;   }
365 
operator >=(const non_copymovable_int & mi) const366    bool operator >=(const non_copymovable_int &mi) const
367    {  return this->m_int >= mi.m_int;   }
368 
operator >(const non_copymovable_int & mi) const369    bool operator >(const non_copymovable_int &mi) const
370    {  return this->m_int > mi.m_int;   }
371 
get_int() const372    int get_int() const
373    {  return m_int;  }
374 
operator ==(const non_copymovable_int & l,int r)375    friend bool operator==(const non_copymovable_int &l, int r)
376    {  return l.get_int() == r;   }
377 
operator ==(int l,const non_copymovable_int & r)378    friend bool operator==(int l, const non_copymovable_int &r)
379    {  return l == r.get_int();   }
380 
operator <(const non_copymovable_int & l,int r)381    friend bool operator<(const non_copymovable_int &l, int r)
382    {  return l.get_int() < r;   }
383 
operator <(int l,const non_copymovable_int & r)384    friend bool operator<(int l, const non_copymovable_int &r)
385    {  return l < r.get_int();   }
386 
hash_value(const non_copymovable_int & v)387    friend std::size_t hash_value(const non_copymovable_int &v)
388    {  return (std::size_t)v.get_int(); }
389 
390    private:
391    int m_int;
392 };
393 
394 unsigned int non_copymovable_int::count = 0;
395 
396 template<class T>
397 struct life_count
398 {
checkboost::container::test::life_count399    static unsigned check(unsigned) {  return true;   }
400 };
401 
402 template<>
403 struct life_count< movable_int >
404 {
checkboost::container::test::life_count405    static unsigned check(unsigned c)
406    {  return c == movable_int::count;   }
407 };
408 
409 template<>
410 struct life_count< copyable_int >
411 {
checkboost::container::test::life_count412    static unsigned check(unsigned c)
413    {  return c == copyable_int::count;   }
414 };
415 
416 template<>
417 struct life_count< movable_and_copyable_int >
418 {
checkboost::container::test::life_count419    static unsigned check(unsigned c)
420    {  return c == movable_and_copyable_int::count;   }
421 };
422 
423 template<>
424 struct life_count< non_copymovable_int >
425 {
checkboost::container::test::life_count426    static unsigned check(unsigned c)
427    {  return c == non_copymovable_int::count;   }
428 };
429 
430 
431 }  //namespace test {
432 }  //namespace container {
433 }  //namespace boost {
434 
435 #include <boost/container/detail/config_end.hpp>
436 
437 #endif   //#ifndef BOOST_CONTAINER_TEST_MOVABLE_INT_HEADER
438