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
20template<typename T>
21class non_raw_pointer
22{
23public:
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
30 non_raw_pointer(){}
31 explicit non_raw_pointer(T* p_):p(p_){}
32
33 T& operator*()const
34 {
35#if !defined(BOOST_NO_EXCEPTIONS)
36 if(!p)boost::throw_exception(e: std::runtime_error("null indirection"));
37#endif
38
39 return *p;
40 }
41
42 T* operator->()const{return p;}
43 non_raw_pointer& operator++(){++p;return *this;}
44 non_raw_pointer operator++(int){non_raw_pointer t(*this);++p;return t;}
45 non_raw_pointer& operator--(){--p;return *this;}
46 non_raw_pointer operator--(int){non_raw_pointer t(*this);--p;return t;}
47 non_raw_pointer& operator+=(std::ptrdiff_t n){p+=n;return *this;}
48 non_raw_pointer& operator-=(std::ptrdiff_t n){p-=n;return *this;}
49 T& operator[](std::ptrdiff_t n)const{return p[n];}
50
51 T* raw_ptr()const{return p;}
52
53private:
54 T* p;
55};
56
57template<typename T>
58non_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
61template<typename T>
62non_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
65template<typename T>
66non_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
69template<typename T>
70std::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
74template<typename T>
75bool operator==(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
76{return x.raw_ptr()==y.raw_ptr();}
77
78template<typename T>
79bool operator!=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
80{return x.raw_ptr()!=y.raw_ptr();}
81
82template<typename T>
83bool operator<(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
84{return x.raw_ptr()<y.raw_ptr();}
85
86template<typename T>
87bool operator>(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
88{return x.raw_ptr()>y.raw_ptr();}
89
90template<typename T>
91bool operator>=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
92{return x.raw_ptr()>=y.raw_ptr();}
93
94template<typename T>
95bool operator<=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
96{return x.raw_ptr()<=y.raw_ptr();}
97
98template<typename T>
99class non_std_allocator
100{
101public:
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
113 non_std_allocator(int id_=0):id(id_){}
114 non_std_allocator(const non_std_allocator<T>& x):id(x.id){}
115 template<class U>non_std_allocator(const non_std_allocator<U>& x,int=0):
116 id(x.id){}
117 non_std_allocator& operator=(const non_std_allocator<T>& x)
118 {id=x.id; return *this;}
119
120 pointer allocate(size_type n)
121 {
122 return pointer((T*)(new char[n*sizeof(T)]));
123 }
124
125 void deallocate(pointer p,size_type)
126 {
127 delete[](char *)&*p;
128 }
129
130 size_type max_size() const{return (size_type )(-1);}
131
132 friend bool operator==(const non_std_allocator& x,const non_std_allocator& y)
133 {
134 return x.id==y.id;
135 }
136
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

source code of boost/libs/multi_index/test/non_std_allocator.hpp