1 /* Used in Boost.MultiIndex tests.
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)
8 * See http://www.boost.org/libs/multi_index for library home page.
11 #ifndef BOOST_MULTI_INDEX_TEST_NON_STD_ALLOCATOR_HPP
12 #define BOOST_MULTI_INDEX_TEST_NON_STD_ALLOCATOR_HPP
14 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
15 #include <boost/throw_exception.hpp>
24 typedef std::ptrdiff_t difference_type;
28 typedef std::random_access_iterator_tag iterator_category;
31 explicit non_raw_pointer(T* p_):p(p_){}
35 #if !defined(BOOST_NO_EXCEPTIONS)
36 if(!p)boost::throw_exception(std::runtime_error("null indirection"));
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];}
51 T* raw_ptr()const{return p;}
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);}
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());}
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);}
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();}
75 bool operator==(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
76 {return x.raw_ptr()==y.raw_ptr();}
79 bool operator!=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
80 {return x.raw_ptr()!=y.raw_ptr();}
83 bool operator<(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
84 {return x.raw_ptr()<y.raw_ptr();}
87 bool operator>(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
88 {return x.raw_ptr()>y.raw_ptr();}
91 bool operator>=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
92 {return x.raw_ptr()>=y.raw_ptr();}
95 bool operator<=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
96 {return x.raw_ptr()<=y.raw_ptr();}
99 class non_std_allocator
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;};
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):
117 non_std_allocator& operator=(const non_std_allocator<T>& x)
118 {id=x.id; return *this;}
120 pointer allocate(size_type n)
122 return pointer((T*)(new char[n*sizeof(T)]));
125 void deallocate(pointer p,size_type)
130 size_type max_size() const{return (size_type )(-1);}
132 friend bool 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)