destroyers.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/container for documentation.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_CONTAINER_DESTROYERS_HPP
  13. #define BOOST_CONTAINER_DESTROYERS_HPP
  14. #if defined(_MSC_VER)
  15. # pragma once
  16. #endif
  17. #include "config_begin.hpp"
  18. #include <boost/container/detail/workaround.hpp>
  19. #include <boost/container/detail/version_type.hpp>
  20. #include <boost/container/detail/utilities.hpp>
  21. #include <boost/container/allocator_traits.hpp>
  22. namespace boost {
  23. namespace container {
  24. namespace container_detail {
  25. //!A deleter for scoped_ptr that deallocates the memory
  26. //!allocated for an object using a STL allocator.
  27. template <class A>
  28. struct scoped_deallocator
  29. {
  30. typedef allocator_traits<A> allocator_traits_type;
  31. typedef typename allocator_traits_type::pointer pointer;
  32. typedef container_detail::integral_constant<unsigned,
  33. boost::container::container_detail::
  34. version<A>::value> alloc_version;
  35. typedef container_detail::integral_constant<unsigned, 1> allocator_v1;
  36. typedef container_detail::integral_constant<unsigned, 2> allocator_v2;
  37. private:
  38. void priv_deallocate(allocator_v1)
  39. { m_alloc.deallocate(m_ptr, 1); }
  40. void priv_deallocate(allocator_v2)
  41. { m_alloc.deallocate_one(m_ptr); }
  42. BOOST_MOVABLE_BUT_NOT_COPYABLE(scoped_deallocator)
  43. public:
  44. pointer m_ptr;
  45. A& m_alloc;
  46. scoped_deallocator(pointer p, A& a)
  47. : m_ptr(p), m_alloc(a)
  48. {}
  49. ~scoped_deallocator()
  50. { if (m_ptr)priv_deallocate(alloc_version()); }
  51. scoped_deallocator(BOOST_RV_REF(scoped_deallocator) o)
  52. : m_ptr(o.m_ptr), m_alloc(o.m_alloc)
  53. { o.release(); }
  54. pointer get() const
  55. { return m_ptr; }
  56. void set(const pointer &p)
  57. { m_ptr = p; }
  58. void release()
  59. { m_ptr = 0; }
  60. };
  61. template <class Allocator>
  62. struct null_scoped_deallocator
  63. {
  64. typedef boost::container::allocator_traits<Allocator> AllocTraits;
  65. typedef typename AllocTraits::pointer pointer;
  66. typedef typename AllocTraits::size_type size_type;
  67. null_scoped_deallocator(pointer, Allocator&, size_type)
  68. {}
  69. void release()
  70. {}
  71. pointer get() const
  72. { return pointer(); }
  73. void set(const pointer &)
  74. {}
  75. };
  76. //!A deleter for scoped_ptr that deallocates the memory
  77. //!allocated for an array of objects using a STL allocator.
  78. template <class Allocator>
  79. struct scoped_array_deallocator
  80. {
  81. typedef boost::container::allocator_traits<Allocator> AllocTraits;
  82. typedef typename AllocTraits::pointer pointer;
  83. typedef typename AllocTraits::size_type size_type;
  84. scoped_array_deallocator(pointer p, Allocator& a, size_type length)
  85. : m_ptr(p), m_alloc(a), m_length(length) {}
  86. ~scoped_array_deallocator()
  87. { if (m_ptr) m_alloc.deallocate(m_ptr, m_length); }
  88. void release()
  89. { m_ptr = 0; }
  90. private:
  91. pointer m_ptr;
  92. Allocator& m_alloc;
  93. size_type m_length;
  94. };
  95. template <class Allocator>
  96. struct null_scoped_array_deallocator
  97. {
  98. typedef boost::container::allocator_traits<Allocator> AllocTraits;
  99. typedef typename AllocTraits::pointer pointer;
  100. typedef typename AllocTraits::size_type size_type;
  101. null_scoped_array_deallocator(pointer, Allocator&, size_type)
  102. {}
  103. void release()
  104. {}
  105. };
  106. template <class Allocator>
  107. struct scoped_destroy_deallocator
  108. {
  109. typedef boost::container::allocator_traits<Allocator> AllocTraits;
  110. typedef typename AllocTraits::pointer pointer;
  111. typedef typename AllocTraits::size_type size_type;
  112. typedef container_detail::integral_constant<unsigned,
  113. boost::container::container_detail::
  114. version<Allocator>::value> alloc_version;
  115. typedef container_detail::integral_constant<unsigned, 1> allocator_v1;
  116. typedef container_detail::integral_constant<unsigned, 2> allocator_v2;
  117. scoped_destroy_deallocator(pointer p, Allocator& a)
  118. : m_ptr(p), m_alloc(a) {}
  119. ~scoped_destroy_deallocator()
  120. {
  121. if(m_ptr){
  122. AllocTraits::destroy(m_alloc, container_detail::to_raw_pointer(m_ptr));
  123. priv_deallocate(m_ptr, alloc_version());
  124. }
  125. }
  126. void release()
  127. { m_ptr = 0; }
  128. private:
  129. void priv_deallocate(const pointer &p, allocator_v1)
  130. { AllocTraits::deallocate(m_alloc, p, 1); }
  131. void priv_deallocate(const pointer &p, allocator_v2)
  132. { m_alloc.deallocate_one(p); }
  133. pointer m_ptr;
  134. Allocator& m_alloc;
  135. };
  136. //!A deleter for scoped_ptr that destroys
  137. //!an object using a STL allocator.
  138. template <class Allocator>
  139. struct scoped_destructor_n
  140. {
  141. typedef boost::container::allocator_traits<Allocator> AllocTraits;
  142. typedef typename AllocTraits::pointer pointer;
  143. typedef typename AllocTraits::value_type value_type;
  144. typedef typename AllocTraits::size_type size_type;
  145. scoped_destructor_n(pointer p, Allocator& a, size_type n)
  146. : m_p(p), m_a(a), m_n(n)
  147. {}
  148. void release()
  149. { m_p = 0; }
  150. void increment_size(size_type inc)
  151. { m_n += inc; }
  152. void increment_size_backwards(size_type inc)
  153. { m_n += inc; m_p -= inc; }
  154. void shrink_forward(size_type inc)
  155. { m_n -= inc; m_p += inc; }
  156. ~scoped_destructor_n()
  157. {
  158. if(!m_p) return;
  159. value_type *raw_ptr = container_detail::to_raw_pointer(m_p);
  160. while(m_n--){
  161. AllocTraits::destroy(m_a, raw_ptr);
  162. }
  163. }
  164. private:
  165. pointer m_p;
  166. Allocator & m_a;
  167. size_type m_n;
  168. };
  169. //!A deleter for scoped_ptr that destroys
  170. //!an object using a STL allocator.
  171. template <class Allocator>
  172. struct null_scoped_destructor_n
  173. {
  174. typedef boost::container::allocator_traits<Allocator> AllocTraits;
  175. typedef typename AllocTraits::pointer pointer;
  176. typedef typename AllocTraits::size_type size_type;
  177. null_scoped_destructor_n(pointer, Allocator&, size_type)
  178. {}
  179. void increment_size(size_type)
  180. {}
  181. void increment_size_backwards(size_type)
  182. {}
  183. void release()
  184. {}
  185. };
  186. template<class A>
  187. class scoped_destructor
  188. {
  189. typedef boost::container::allocator_traits<A> AllocTraits;
  190. public:
  191. typedef typename A::value_type value_type;
  192. scoped_destructor(A &a, value_type *pv)
  193. : pv_(pv), a_(a)
  194. {}
  195. ~scoped_destructor()
  196. {
  197. if(pv_){
  198. AllocTraits::destroy(a_, pv_);
  199. }
  200. }
  201. void release()
  202. { pv_ = 0; }
  203. void set(value_type *ptr) { pv_ = ptr; }
  204. value_type *get() const { return pv_; }
  205. private:
  206. value_type *pv_;
  207. A &a_;
  208. };
  209. template<class A>
  210. class value_destructor
  211. {
  212. typedef boost::container::allocator_traits<A> AllocTraits;
  213. public:
  214. typedef typename A::value_type value_type;
  215. value_destructor(A &a, value_type &rv)
  216. : rv_(rv), a_(a)
  217. {}
  218. ~value_destructor()
  219. {
  220. AllocTraits::destroy(a_, &rv_);
  221. }
  222. private:
  223. value_type &rv_;
  224. A &a_;
  225. };
  226. template <class Allocator>
  227. class allocator_destroyer
  228. {
  229. typedef boost::container::allocator_traits<Allocator> AllocTraits;
  230. typedef typename AllocTraits::value_type value_type;
  231. typedef typename AllocTraits::pointer pointer;
  232. typedef container_detail::integral_constant<unsigned,
  233. boost::container::container_detail::
  234. version<Allocator>::value> alloc_version;
  235. typedef container_detail::integral_constant<unsigned, 1> allocator_v1;
  236. typedef container_detail::integral_constant<unsigned, 2> allocator_v2;
  237. private:
  238. Allocator & a_;
  239. private:
  240. void priv_deallocate(const pointer &p, allocator_v1)
  241. { AllocTraits::deallocate(a_,p, 1); }
  242. void priv_deallocate(const pointer &p, allocator_v2)
  243. { a_.deallocate_one(p); }
  244. public:
  245. allocator_destroyer(Allocator &a)
  246. : a_(a)
  247. {}
  248. void operator()(const pointer &p)
  249. {
  250. AllocTraits::destroy(a_, container_detail::to_raw_pointer(p));
  251. this->priv_deallocate(p, alloc_version());
  252. }
  253. };
  254. template <class A>
  255. class allocator_destroyer_and_chain_builder
  256. {
  257. typedef allocator_traits<A> allocator_traits_type;
  258. typedef typename allocator_traits_type::value_type value_type;
  259. typedef typename A::multiallocation_chain multiallocation_chain;
  260. A & a_;
  261. multiallocation_chain &c_;
  262. public:
  263. allocator_destroyer_and_chain_builder(A &a, multiallocation_chain &c)
  264. : a_(a), c_(c)
  265. {}
  266. void operator()(const typename A::pointer &p)
  267. {
  268. allocator_traits<A>::destroy(a_, container_detail::to_raw_pointer(p));
  269. c_.push_back(p);
  270. }
  271. };
  272. template <class A>
  273. class allocator_multialloc_chain_node_deallocator
  274. {
  275. typedef allocator_traits<A> allocator_traits_type;
  276. typedef typename allocator_traits_type::value_type value_type;
  277. typedef typename A::multiallocation_chain multiallocation_chain;
  278. typedef allocator_destroyer_and_chain_builder<A> chain_builder;
  279. A & a_;
  280. multiallocation_chain c_;
  281. public:
  282. allocator_multialloc_chain_node_deallocator(A &a)
  283. : a_(a), c_()
  284. {}
  285. chain_builder get_chain_builder()
  286. { return chain_builder(a_, c_); }
  287. ~allocator_multialloc_chain_node_deallocator()
  288. {
  289. a_.deallocate_individual(c_);
  290. }
  291. };
  292. } //namespace container_detail {
  293. } //namespace container {
  294. } //namespace boost {
  295. #include <boost/container/detail/config_end.hpp>
  296. #endif //#ifndef BOOST_CONTAINER_DESTROYERS_HPP