1 | |
---|
2 | // Copyright 2005-2009 Daniel James. |
---|
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
---|
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | // A couple of templates to make using allocators easier. |
---|
7 | |
---|
8 | #ifndef BOOST_UNORDERED_DETAIL_ALLOCATOR_UTILITIES_HPP_INCLUDED |
---|
9 | #define BOOST_UNORDERED_DETAIL_ALLOCATOR_UTILITIES_HPP_INCLUDED |
---|
10 | |
---|
11 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
12 | # pragma once |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <boost/config.hpp> |
---|
16 | |
---|
17 | #if (defined(BOOST_NO_STD_ALLOCATOR) || defined(BOOST_DINKUMWARE_STDLIB)) \ |
---|
18 | && !defined(__BORLANDC__) |
---|
19 | # define BOOST_UNORDERED_USE_ALLOCATOR_UTILITIES |
---|
20 | #endif |
---|
21 | |
---|
22 | #if defined(BOOST_UNORDERED_USE_ALLOCATOR_UTILITIES) |
---|
23 | # include <boost/detail/allocator_utilities.hpp> |
---|
24 | #endif |
---|
25 | |
---|
26 | namespace boost { namespace unordered_detail { |
---|
27 | |
---|
28 | // rebind_wrap |
---|
29 | // |
---|
30 | // Rebind allocators. For some problematic libraries, use rebind_to |
---|
31 | // from <boost/detail/allocator_utilities.hpp>. |
---|
32 | |
---|
33 | #if defined(BOOST_UNORDERED_USE_ALLOCATOR_UTILITIES) |
---|
34 | template <class Alloc, class T> |
---|
35 | struct rebind_wrap : ::boost::detail::allocator::rebind_to<Alloc, T> {}; |
---|
36 | #else |
---|
37 | template <class Alloc, class T> |
---|
38 | struct rebind_wrap |
---|
39 | { |
---|
40 | typedef BOOST_DEDUCED_TYPENAME |
---|
41 | Alloc::BOOST_NESTED_TEMPLATE rebind<T>::other |
---|
42 | type; |
---|
43 | }; |
---|
44 | #endif |
---|
45 | |
---|
46 | // allocator_array_constructor |
---|
47 | // |
---|
48 | // Allocate and construct an array in an exception safe manner, and |
---|
49 | // clean up if an exception is thrown before the container takes charge |
---|
50 | // of it. |
---|
51 | |
---|
52 | template <class Allocator> |
---|
53 | struct allocator_array_constructor |
---|
54 | { |
---|
55 | typedef BOOST_DEDUCED_TYPENAME Allocator::pointer pointer; |
---|
56 | |
---|
57 | Allocator& alloc_; |
---|
58 | pointer ptr_; |
---|
59 | pointer constructed_; |
---|
60 | std::size_t length_; |
---|
61 | |
---|
62 | allocator_array_constructor(Allocator& a) |
---|
63 | : alloc_(a), ptr_(), constructed_(), length_(0) |
---|
64 | { |
---|
65 | constructed_ = pointer(); |
---|
66 | ptr_ = pointer(); |
---|
67 | } |
---|
68 | |
---|
69 | ~allocator_array_constructor() { |
---|
70 | if (ptr_) { |
---|
71 | for(pointer p = ptr_; p != constructed_; ++p) |
---|
72 | alloc_.destroy(p); |
---|
73 | |
---|
74 | alloc_.deallocate(ptr_, length_); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | template <class V> |
---|
79 | void construct(V const& v, std::size_t l) |
---|
80 | { |
---|
81 | BOOST_ASSERT(!ptr_); |
---|
82 | length_ = l; |
---|
83 | ptr_ = alloc_.allocate(length_); |
---|
84 | pointer end = ptr_ + static_cast<std::ptrdiff_t>(length_); |
---|
85 | for(constructed_ = ptr_; constructed_ != end; ++constructed_) |
---|
86 | alloc_.construct(constructed_, v); |
---|
87 | } |
---|
88 | |
---|
89 | pointer get() const |
---|
90 | { |
---|
91 | return ptr_; |
---|
92 | } |
---|
93 | |
---|
94 | pointer release() |
---|
95 | { |
---|
96 | pointer p(ptr_); |
---|
97 | ptr_ = pointer(); |
---|
98 | return p; |
---|
99 | } |
---|
100 | private: |
---|
101 | allocator_array_constructor(allocator_array_constructor const&); |
---|
102 | allocator_array_constructor& operator=( |
---|
103 | allocator_array_constructor const&); |
---|
104 | }; |
---|
105 | }} |
---|
106 | |
---|
107 | #if defined(BOOST_UNORDERED_USE_ALLOCATOR_UTILITIES) |
---|
108 | # undef BOOST_UNORDERED_USE_ALLOCATOR_UTILITIES |
---|
109 | #endif |
---|
110 | |
---|
111 | #endif |
---|