test_allocator.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2015 KONDO Takatoshi
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef TEST_ALLOCATOR_HPP
  11. #define TEST_ALLOCATOR_HPP
  12. #include <memory>
  13. namespace test {
  14. template <typename T>
  15. struct allocator {
  16. typedef typename std::allocator<T>::value_type value_type;
  17. typedef typename std::allocator<T>::pointer pointer;
  18. typedef typename std::allocator<T>::reference reference;
  19. typedef typename std::allocator<T>::const_pointer const_pointer;
  20. typedef typename std::allocator<T>::const_reference const_reference;
  21. typedef typename std::allocator<T>::size_type size_type;
  22. typedef typename std::allocator<T>::difference_type difference_type;
  23. template <class U> struct rebind { typedef allocator<U> other; };
  24. #if defined(MSGPACK_USE_CPP03)
  25. allocator() throw() {}
  26. allocator (const allocator& alloc) throw()
  27. :alloc_(alloc.alloc_) {}
  28. template <class U>
  29. allocator (const allocator<U>& alloc) throw()
  30. :alloc_(alloc.alloc_) {}
  31. void construct ( pointer p, const_reference val ) {
  32. return alloc_.construct(p, val);
  33. }
  34. size_type max_size() const throw() { return alloc_.max_size(); }
  35. #else // defined(MSGPACK_USE_CPP03)
  36. allocator() noexcept {}
  37. allocator (const allocator& alloc) noexcept
  38. :alloc_(alloc.alloc_) {}
  39. template <class U>
  40. allocator (const allocator<U>& alloc) noexcept
  41. :alloc_(alloc.alloc_) {}
  42. template <class U, class... Args>
  43. void construct (U* p, Args&&... args) {
  44. return alloc_.construct(p, std::forward<Args>(args)...);
  45. }
  46. size_type max_size() const noexcept { return alloc_.max_size(); }
  47. #endif // defined(MSGPACK_USE_CPP03)
  48. pointer allocate (size_type n) {
  49. return alloc_.allocate(n);
  50. }
  51. void deallocate (pointer p, size_type n) {
  52. return alloc_.deallocate(p, n);
  53. }
  54. void destroy (pointer p) {
  55. alloc_.destroy(p);
  56. }
  57. std::allocator<T> alloc_;
  58. };
  59. template <typename T>
  60. inline bool operator==(allocator<T> const& lhs, allocator<T> const& rhs) {
  61. return lhs.alloc_ == rhs.alloc_;
  62. }
  63. template <typename T>
  64. inline bool operator!=(allocator<T> const& lhs, allocator<T> const& rhs) {
  65. return lhs.alloc_ != rhs.alloc_;
  66. }
  67. } // namespace test
  68. #endif // TEST_ALLOCATOR_HPP