1 /* Used in Boost.MultiIndex tests.
2 *
3 * Copyright 2003-2020 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 #include <stdexcept>
19
20 template<typename T>
21 class non_raw_pointer
22 {
23 public:
24 typedef std::ptrdiff_t difference_type;
25 typedef T value_type;
26 typedef T* pointer;
27 typedef T& reference;
28 typedef std::random_access_iterator_tag iterator_category;
29
non_raw_pointer()30 non_raw_pointer(){}
non_raw_pointer(T * p_)31 explicit non_raw_pointer(T* p_):p(p_){}
32
operator *() const33 T& operator*()const
34 {
35 #if !defined(BOOST_NO_EXCEPTIONS)
36 if(!p)boost::throw_exception(std::runtime_error("null indirection"));
37 #endif
38
39 return *p;
40 }
41
operator ->() const42 T* operator->()const{return p;}
operator ++()43 non_raw_pointer& operator++(){++p;return *this;}
operator ++(int)44 non_raw_pointer operator++(int){non_raw_pointer t(*this);++p;return t;}
operator --()45 non_raw_pointer& operator--(){--p;return *this;}
operator --(int)46 non_raw_pointer operator--(int){non_raw_pointer t(*this);--p;return t;}
operator +=(std::ptrdiff_t n)47 non_raw_pointer& operator+=(std::ptrdiff_t n){p+=n;return *this;}
operator -=(std::ptrdiff_t n)48 non_raw_pointer& operator-=(std::ptrdiff_t n){p-=n;return *this;}
operator [](std::ptrdiff_t n) const49 T& operator[](std::ptrdiff_t n)const{return p[n];}
50
raw_ptr() const51 T* raw_ptr()const{return p;}
52
53 private:
54 T* p;
55 };
56
57 template<typename T>
operator +(const non_raw_pointer<T> & x,std::ptrdiff_t n)58 non_raw_pointer<T> operator+(const non_raw_pointer<T>& x,std::ptrdiff_t n)
59 {return non_raw_pointer<T>(x.raw_ptr()+n);}
60
61 template<typename T>
operator +(std::ptrdiff_t n,const non_raw_pointer<T> & x)62 non_raw_pointer<T> operator+(std::ptrdiff_t n,const non_raw_pointer<T>& x)
63 {return non_raw_pointer<T>(n+x.raw_ptr());}
64
65 template<typename T>
operator -(const non_raw_pointer<T> & x,std::ptrdiff_t n)66 non_raw_pointer<T> operator-(const non_raw_pointer<T>& x,std::ptrdiff_t n)
67 {return non_raw_pointer<T>(x.raw_ptr()-n);}
68
69 template<typename T>
operator -(const non_raw_pointer<T> & x,const non_raw_pointer<T> & y)70 std::ptrdiff_t operator-(
71 const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
72 {return x.raw_ptr()-y.raw_ptr();}
73
74 template<typename T>
operator ==(const non_raw_pointer<T> & x,const non_raw_pointer<T> & y)75 bool operator==(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
76 {return x.raw_ptr()==y.raw_ptr();}
77
78 template<typename T>
operator !=(const non_raw_pointer<T> & x,const non_raw_pointer<T> & y)79 bool operator!=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
80 {return x.raw_ptr()!=y.raw_ptr();}
81
82 template<typename T>
operator <(const non_raw_pointer<T> & x,const non_raw_pointer<T> & y)83 bool operator<(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
84 {return x.raw_ptr()<y.raw_ptr();}
85
86 template<typename T>
operator >(const non_raw_pointer<T> & x,const non_raw_pointer<T> & y)87 bool operator>(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
88 {return x.raw_ptr()>y.raw_ptr();}
89
90 template<typename T>
operator >=(const non_raw_pointer<T> & x,const non_raw_pointer<T> & y)91 bool operator>=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
92 {return x.raw_ptr()>=y.raw_ptr();}
93
94 template<typename T>
operator <=(const non_raw_pointer<T> & x,const non_raw_pointer<T> & y)95 bool operator<=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
96 {return x.raw_ptr()<=y.raw_ptr();}
97
98 template<typename T>
99 class non_std_allocator
100 {
101 public:
102 typedef std::size_t size_type;
103 typedef std::ptrdiff_t difference_type;
104 typedef non_raw_pointer<T> pointer;
105 typedef non_raw_pointer<const T> const_pointer;
106 typedef void* void_pointer;
107 typedef const void* const_void_pointer;
108 typedef T& reference;
109 typedef const T& const_reference;
110 typedef T value_type;
111 template<class U>struct rebind{typedef non_std_allocator<U> other;};
112
non_std_allocator(int id_=0)113 non_std_allocator(int id_=0):id(id_){}
non_std_allocator(const non_std_allocator<T> & x)114 non_std_allocator(const non_std_allocator<T>& x):id(x.id){}
non_std_allocator(const non_std_allocator<U> & x,int=0)115 template<class U>non_std_allocator(const non_std_allocator<U>& x,int=0):
116 id(x.id){}
operator =(const non_std_allocator<T> & x)117 non_std_allocator& operator=(const non_std_allocator<T>& x)
118 {id=x.id; return *this;}
119
allocate(size_type n)120 pointer allocate(size_type n)
121 {
122 return pointer((T*)(new char[n*sizeof(T)]));
123 }
124
deallocate(pointer p,size_type)125 void deallocate(pointer p,size_type)
126 {
127 delete[](char *)&*p;
128 }
129
max_size() const130 size_type max_size() const{return (size_type )(-1);}
131
operator ==(const non_std_allocator & x,const non_std_allocator & y)132 friend bool operator==(const non_std_allocator& x,const non_std_allocator& y)
133 {
134 return x.id==y.id;
135 }
136
operator !=(const non_std_allocator & x,const non_std_allocator & y)137 friend bool operator!=(const non_std_allocator& x,const non_std_allocator& y)
138 {
139 return !(x==y);
140 }
141
142 int id;
143 };
144
145 #endif
146