non_std_allocator.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Used in Boost.MultiIndex tests.
  2. *
  3. * Copyright 2003-2018 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. #ifndef BOOST_MULTI_INDEX_TEST_NON_STD_ALLOCATOR_HPP
  11. #define BOOST_MULTI_INDEX_TEST_NON_STD_ALLOCATOR_HPP
  12. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  13. #include <boost/throw_exception.hpp>
  14. #include <iterator>
  15. #include <cstddef>
  16. #include <stdexcept>
  17. template<typename T>
  18. class non_raw_pointer
  19. {
  20. public:
  21. typedef std::ptrdiff_t difference_type;
  22. typedef T value_type;
  23. typedef T* pointer;
  24. typedef T& reference;
  25. typedef std::random_access_iterator_tag iterator_category;
  26. non_raw_pointer(){}
  27. explicit non_raw_pointer(T* p_):p(p_){}
  28. T& operator*()const
  29. {
  30. #if !defined(BOOST_NO_EXCEPTIONS)
  31. if(!p)boost::throw_exception(std::runtime_error("null indirection"));
  32. #endif
  33. return *p;
  34. }
  35. T* operator->()const{return p;}
  36. non_raw_pointer& operator++(){++p;return *this;}
  37. non_raw_pointer operator++(int){non_raw_pointer t(*this);++p;return t;}
  38. non_raw_pointer& operator--(){--p;return *this;}
  39. non_raw_pointer operator--(int){non_raw_pointer t(*this);--p;return t;}
  40. non_raw_pointer& operator+=(std::ptrdiff_t n){p+=n;return *this;}
  41. non_raw_pointer& operator-=(std::ptrdiff_t n){p-=n;return *this;}
  42. T& operator[](std::ptrdiff_t n)const{return p[n];}
  43. T* raw_ptr()const{return p;}
  44. private:
  45. T* p;
  46. };
  47. template<typename T>
  48. non_raw_pointer<T> operator+(const non_raw_pointer<T>& x,std::ptrdiff_t n)
  49. {return non_raw_pointer<T>(x.raw_ptr()+n);}
  50. template<typename T>
  51. non_raw_pointer<T> operator+(std::ptrdiff_t n,const non_raw_pointer<T>& x)
  52. {return non_raw_pointer<T>(n+x.raw_ptr());}
  53. template<typename T>
  54. non_raw_pointer<T> operator-(const non_raw_pointer<T>& x,std::ptrdiff_t n)
  55. {return non_raw_pointer<T>(x.raw_ptr()-n);}
  56. template<typename T>
  57. std::ptrdiff_t operator-(
  58. const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
  59. {return x.raw_ptr()-y.raw_ptr();}
  60. template<typename T>
  61. bool operator==(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
  62. {return x.raw_ptr()==y.raw_ptr();}
  63. template<typename T>
  64. bool operator!=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
  65. {return x.raw_ptr()!=y.raw_ptr();}
  66. template<typename T>
  67. bool operator<(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
  68. {return x.raw_ptr()<y.raw_ptr();}
  69. template<typename T>
  70. bool operator>(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
  71. {return x.raw_ptr()>y.raw_ptr();}
  72. template<typename T>
  73. bool operator>=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
  74. {return x.raw_ptr()>=y.raw_ptr();}
  75. template<typename T>
  76. bool operator<=(const non_raw_pointer<T>& x,const non_raw_pointer<T>& y)
  77. {return x.raw_ptr()<=y.raw_ptr();}
  78. template<typename T>
  79. class non_std_allocator
  80. {
  81. public:
  82. typedef std::size_t size_type;
  83. typedef std::ptrdiff_t difference_type;
  84. typedef non_raw_pointer<T> pointer;
  85. typedef non_raw_pointer<const T> const_pointer;
  86. typedef void* void_pointer;
  87. typedef const void* const_void_pointer;
  88. typedef T& reference;
  89. typedef const T& const_reference;
  90. typedef T value_type;
  91. template<class U>struct rebind{typedef non_std_allocator<U> other;};
  92. non_std_allocator(){}
  93. non_std_allocator(const non_std_allocator<T>&){}
  94. template<class U>non_std_allocator(const non_std_allocator<U>&,int=0){}
  95. pointer allocate(size_type n)
  96. {
  97. return pointer((T*)(new char[n*sizeof(T)]));
  98. }
  99. void deallocate(pointer p,size_type)
  100. {
  101. delete[](char *)&*p;
  102. }
  103. size_type max_size() const{return (size_type )(-1);}
  104. friend bool operator==(const non_std_allocator&,const non_std_allocator&)
  105. {
  106. return true;
  107. }
  108. friend bool operator!=(const non_std_allocator&,const non_std_allocator&)
  109. {
  110. return false;
  111. }
  112. };
  113. #endif