]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multi_index/test/non_std_allocator.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multi_index / test / non_std_allocator.hpp
1 /* Used in Boost.MultiIndex tests.
2 *
3 * Copyright 2003-2015 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * See http://www.boost.org/libs/multi_index for library home page.
9 */
10
11 #ifndef BOOST_MULTI_INDEX_TEST_NON_STD_ALLOCATOR_HPP
12 #define BOOST_MULTI_INDEX_TEST_NON_STD_ALLOCATOR_HPP
13
14 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
15 #include <boost/throw_exception.hpp>
16 #include <iterator>
17 #include <cstddef>
18
19 template<typename T>
20 class non_raw_pointer
21 {
22 public:
23 typedef std::ptrdiff_t difference_type;
24 typedef T value_type;
25 typedef T* pointer;
26 typedef T& reference;
27 typedef std::random_access_iterator_tag iterator_category;
28
29 non_raw_pointer(){}
30 explicit non_raw_pointer(T* p_):p(p_){}
31
32 T& operator*()const
33 {
34 #if !defined(BOOST_NO_EXCEPTIONS)
35 if(!p)boost::throw_exception(std::runtime_error("null indirection"));
36 #endif
37
38 return *p;
39 }
40
41 T* operator->()const{return p;}
42 non_raw_pointer& operator++(){++p;return *this;}
43 non_raw_pointer operator++(int){non_raw_pointer t(*this);++p;return t;}
44 non_raw_pointer& operator--(){--p;return *this;}
45 non_raw_pointer operator--(int){non_raw_pointer t(*this);--p;return t;}
46 non_raw_pointer& operator+=(std::ptrdiff_t n){p+=n;return *this;}
47 non_raw_pointer& operator-=(std::ptrdiff_t n){p-=n;return *this;}
48 T& operator[](std::ptrdiff_t n)const{return p[n];}
49
50 T* raw_ptr()const{return p;}
51
52 private:
53 T* p;
54 };
55
56 template<typename T>
57 non_raw_pointer<T> operator+(const non_raw_pointer<T>& x,std::ptrdiff_t n)
58 {return non_raw_pointer<T>(x.raw_ptr()+n);}
59
60 template<typename T>
61 non_raw_pointer<T> operator+(std::ptrdiff_t n,const non_raw_pointer<T>& x)
62 {return non_raw_pointer<T>(n+x.raw_ptr());}
63
64 template<typename T>
65 non_raw_pointer<T> operator-(const non_raw_pointer<T>& x,std::ptrdiff_t n)
66 {return non_raw_pointer<T>(x.raw_ptr()-n);}
67
68 template<typename T>
69 std::ptrdiff_t operator-(
70 const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
71 {return x.raw_ptr()-y.raw_ptr();}
72
73 template<typename T>
74 bool operator==(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
75 {return x.raw_ptr()==y.raw_ptr();}
76
77 template<typename T>
78 bool operator!=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
79 {return x.raw_ptr()!=y.raw_ptr();}
80
81 template<typename T>
82 bool operator<(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
83 {return x.raw_ptr()<y.raw_ptr();}
84
85 template<typename T>
86 bool operator>(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
87 {return x.raw_ptr()>y.raw_ptr();}
88
89 template<typename T>
90 bool operator>=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
91 {return x.raw_ptr()>=y.raw_ptr();}
92
93 template<typename T>
94 bool operator<=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
95 {return x.raw_ptr()<=y.raw_ptr();}
96
97 template<typename T>
98 class non_std_allocator
99 {
100 public:
101 typedef std::size_t size_type;
102 typedef std::ptrdiff_t difference_type;
103 typedef non_raw_pointer<T> pointer;
104 typedef non_raw_pointer<const T> const_pointer;
105 typedef T& reference;
106 typedef const T& const_reference;
107 typedef T value_type;
108 template<class U>struct rebind{typedef non_std_allocator<U> other;};
109
110 non_std_allocator(){}
111 non_std_allocator(const non_std_allocator<T>&){}
112 template<class U>non_std_allocator(const non_std_allocator<U>&,int=0){}
113
114 pointer allocate(size_type n)
115 {
116 return pointer((T*)(new char[n*sizeof(T)]));
117 }
118
119 void deallocate(pointer p,size_type)
120 {
121 delete[](char *)&*p;
122 }
123
124 size_type max_size() const{return (size_type )(-1);}
125
126 friend bool operator==(const non_std_allocator&,const non_std_allocator&)
127 {
128 return true;
129 }
130
131 friend bool operator!=(const non_std_allocator&,const non_std_allocator&)
132 {
133 return false;
134 }
135 };
136
137 #endif