ゴミ箱
handler_alloc.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_HANDLER_ALLOC_HPP
11 #define BOOST_BEAST_HANDLER_ALLOC_HPP
12 
13 #include <boost/beast/config.hpp>
14 #include <boost/asio/handler_alloc_hook.hpp>
15 #include <boost/config.hpp>
16 #include <cstddef>
17 #include <cstdlib>
18 #include <memory>
19 #include <type_traits>
20 #include <utility>
21 
22 namespace boost {
23 namespace beast {
24 
25 // Guidance from
26 // http://howardhinnant.github.io/allocator_boilerplate.html
27 
43 #if BOOST_BEAST_DOXYGEN
44 template<class T, class Handler>
45 class handler_alloc;
46 #else
47 template<class T, class Handler>
49 {
50 private:
51  // We want a partial template specialization as a friend
52  // but that isn't allowed so we friend all versions. This
53  // should produce a compile error if Handler is not
54  // constructible from H.
55  //
56  template<class U, class H>
57  friend class handler_alloc;
58 
59  Handler& h_;
60 
61 public:
62  using value_type = T;
63  using is_always_equal = std::true_type;
64  using pointer = T*;
65  using reference = T&;
66  using const_pointer = T const*;
67  using const_reference = T const&;
68  using size_type = std::size_t;
69  using difference_type = std::ptrdiff_t;
70 
71  template<class U>
72  struct rebind
73  {
75  };
76 
77  handler_alloc() = delete;
78  handler_alloc(handler_alloc&&) = default;
79  handler_alloc(handler_alloc const&) = default;
81  handler_alloc& operator=(handler_alloc const&) = default;
82 
88  explicit
89  handler_alloc(Handler& h)
90  : h_(h)
91  {
92  }
93 
95  template<class U>
98  : h_(other.h_)
99  {
100  }
101 
102  value_type*
104  {
105  auto const size = n * sizeof(T);
106  using boost::asio::asio_handler_allocate;
107  return static_cast<value_type*>(
108  asio_handler_allocate(size, std::addressof(h_)));
109  }
110 
111  void
113  {
114  auto const size = n * sizeof(T);
115  using boost::asio::asio_handler_deallocate;
116  asio_handler_deallocate(p, size, std::addressof(h_));
117  }
118 
119 //#if BOOST_WORKAROUND(BOOST_GCC, < 60000) // Works, but too coarse
120 
121 #if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000
122  template<class U, class... Args>
123  void
124  construct(U* ptr, Args&&... args)
125  {
126  ::new((void*)ptr) U(std::forward<Args>(args)...);
127  }
128 
129  template<class U>
130  void
131  destroy(U* ptr)
132  {
133  ptr->~U();
134  }
135 #endif
136 
137  template<class U>
138  friend
139  bool
141  handler_alloc const&,
143  {
144  return true;
145  }
146 
147  template<class U>
148  friend
149  bool
151  handler_alloc const& lhs,
152  handler_alloc<U, Handler> const& rhs)
153  {
154  return ! (lhs == rhs);
155  }
156 };
157 #endif
158 
159 } // beast
160 } // boost
161 
162 #endif
handler_alloc< U, Handler > other
Definition: handler_alloc.hpp:74
Definition: handler_alloc.hpp:48
T const * const_pointer
Definition: handler_alloc.hpp:66
T const & const_reference
Definition: handler_alloc.hpp:67
Definition: async_result.hpp:20
friend bool operator!=(handler_alloc const &lhs, handler_alloc< U, Handler > const &rhs)
Definition: handler_alloc.hpp:150
Definition: handler_alloc.hpp:72
std::size_t size_type
Definition: handler_alloc.hpp:68
std::true_type is_always_equal
Definition: handler_alloc.hpp:63
handler_alloc(handler_alloc< U, Handler > const &other)
Copy constructor.
Definition: handler_alloc.hpp:96
value_type * allocate(size_type n)
Definition: handler_alloc.hpp:103
void deallocate(value_type *p, size_type n)
Definition: handler_alloc.hpp:112
T * pointer
Definition: handler_alloc.hpp:64
friend bool operator==(handler_alloc const &, handler_alloc< U, Handler > const &)
Definition: handler_alloc.hpp:140
handler_alloc(Handler &h)
Definition: handler_alloc.hpp:89
handler_alloc & operator=(handler_alloc &&)=default
std::ptrdiff_t difference_type
Definition: handler_alloc.hpp:69
T value_type
Definition: handler_alloc.hpp:62
T & reference
Definition: handler_alloc.hpp:65