1 | /* Copyright 2003-2005 Joaquín M López Muñoz.
|
---|
2 | * Distributed under the Boost Software License, Version 1.0.
|
---|
3 | * (See accompanying file LICENSE_1_0.txt or copy at
|
---|
4 | * http://www.boost.org/LICENSE_1_0.txt)
|
---|
5 | *
|
---|
6 | * See http://www.boost.org/libs/multi_index for library home page.
|
---|
7 | */
|
---|
8 |
|
---|
9 | #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
|
---|
10 | #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
|
---|
11 |
|
---|
12 | #if defined(_MSC_VER)&&(_MSC_VER>=1200)
|
---|
13 | #pragma once
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
|
---|
17 | #include <algorithm>
|
---|
18 | #include <boost/detail/allocator_utilities.hpp>
|
---|
19 | #include <boost/noncopyable.hpp>
|
---|
20 | #include <memory>
|
---|
21 |
|
---|
22 | namespace boost{
|
---|
23 |
|
---|
24 | namespace multi_index{
|
---|
25 |
|
---|
26 | namespace detail{
|
---|
27 |
|
---|
28 | /* auto_space provides uninitialized space suitably to store
|
---|
29 | * a given number of elements of a given type.
|
---|
30 | */
|
---|
31 |
|
---|
32 | /* NB: it is not clear whether using an allocator to handle
|
---|
33 | * zero-sized arrays of elements is conformant or not. GCC 3.3.1
|
---|
34 | * and prior fail here, other stdlibs handle the issue gracefully.
|
---|
35 | * To be on the safe side, the case n==0 is given special treatment.
|
---|
36 | * References:
|
---|
37 | * GCC Bugzilla, "standard allocator crashes when deallocating segment
|
---|
38 | * "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
|
---|
39 | * C++ Standard Library Defect Report List (Revision 28), issue 199
|
---|
40 | * "What does allocate(0) return?",
|
---|
41 | * http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#199
|
---|
42 | */
|
---|
43 |
|
---|
44 | template<typename T,typename Allocator=std::allocator<T> >
|
---|
45 | struct auto_space:private noncopyable
|
---|
46 | {
|
---|
47 | explicit auto_space(const Allocator& al=Allocator(),std::size_t n=1):
|
---|
48 | al_(al),n_(n),data_(n_?al_.allocate(n_):0)
|
---|
49 | {}
|
---|
50 |
|
---|
51 | ~auto_space()
|
---|
52 | {
|
---|
53 | if(n_)al_.deallocate(data_,n_);
|
---|
54 | }
|
---|
55 |
|
---|
56 | T* data()const{return data_;}
|
---|
57 |
|
---|
58 | void swap(auto_space& x)
|
---|
59 | {
|
---|
60 | std::swap(n_,x.n_);
|
---|
61 | std::swap(data_,x.data_);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private:
|
---|
65 | typename boost::detail::allocator::rebind_to<
|
---|
66 | Allocator,T>::type al_;
|
---|
67 | std::size_t n_;
|
---|
68 | T* data_;
|
---|
69 | };
|
---|
70 |
|
---|
71 | template<typename T,typename Allocator>
|
---|
72 | void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
|
---|
73 | {
|
---|
74 | x.swap(y);
|
---|
75 | }
|
---|
76 |
|
---|
77 | } /* namespace multi_index::detail */
|
---|
78 |
|
---|
79 | } /* namespace multi_index */
|
---|
80 |
|
---|
81 | } /* namespace boost */
|
---|
82 |
|
---|
83 | #endif
|
---|