Chris@16
|
1 // Boost.Range library
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright Neil Groves 2010. Use, modification and
|
Chris@16
|
4 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see http://www.boost.org/libs/range/
|
Chris@16
|
9 //
|
Chris@16
|
10 #ifndef BOOST_RANGE_DETAIL_ANY_ITERATOR_BUFFER_HPP_INCLUDED
|
Chris@16
|
11 #define BOOST_RANGE_DETAIL_ANY_ITERATOR_BUFFER_HPP_INCLUDED
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/array.hpp>
|
Chris@16
|
14 #include <boost/assert.hpp>
|
Chris@16
|
15 #include <boost/static_assert.hpp>
|
Chris@16
|
16 #include <boost/noncopyable.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 namespace boost
|
Chris@16
|
19 {
|
Chris@16
|
20 template<std::size_t StackBufferSize>
|
Chris@16
|
21 class any_iterator_buffer
|
Chris@16
|
22 : noncopyable
|
Chris@16
|
23 {
|
Chris@16
|
24 BOOST_STATIC_ASSERT(( StackBufferSize > 0 ));
|
Chris@16
|
25 public:
|
Chris@16
|
26 any_iterator_buffer()
|
Chris@16
|
27 : m_ptr()
|
Chris@16
|
28 {
|
Chris@16
|
29 }
|
Chris@16
|
30
|
Chris@16
|
31 ~any_iterator_buffer()
|
Chris@16
|
32 {
|
Chris@16
|
33 delete [] m_ptr;
|
Chris@16
|
34 }
|
Chris@16
|
35
|
Chris@16
|
36 void* allocate(std::size_t bytes)
|
Chris@16
|
37 {
|
Chris@16
|
38 BOOST_ASSERT( !m_ptr );
|
Chris@16
|
39 if (bytes <= StackBufferSize)
|
Chris@16
|
40 return m_buffer.data();
|
Chris@16
|
41
|
Chris@16
|
42 m_ptr = new char[bytes];
|
Chris@16
|
43 return m_ptr;
|
Chris@16
|
44 }
|
Chris@16
|
45
|
Chris@16
|
46 void deallocate()
|
Chris@16
|
47 {
|
Chris@16
|
48 delete [] m_ptr;
|
Chris@16
|
49 m_ptr = 0;
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 private:
|
Chris@16
|
53 // Rationale:
|
Chris@16
|
54 // Do not use inheritance from noncopyable because this causes
|
Chris@16
|
55 // the concepts to erroneous detect the derived any_iterator
|
Chris@16
|
56 // as noncopyable.
|
Chris@16
|
57 any_iterator_buffer(const any_iterator_buffer&);
|
Chris@16
|
58 void operator=(const any_iterator_buffer&);
|
Chris@16
|
59
|
Chris@16
|
60 char* m_ptr;
|
Chris@16
|
61 boost::array<char, StackBufferSize> m_buffer;
|
Chris@16
|
62 };
|
Chris@16
|
63
|
Chris@16
|
64 class any_iterator_heap_only_buffer
|
Chris@16
|
65 : noncopyable
|
Chris@16
|
66 {
|
Chris@16
|
67 public:
|
Chris@16
|
68 any_iterator_heap_only_buffer()
|
Chris@16
|
69 : m_ptr()
|
Chris@16
|
70 {
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 ~any_iterator_heap_only_buffer()
|
Chris@16
|
74 {
|
Chris@16
|
75 delete [] m_ptr;
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 void* allocate(std::size_t bytes)
|
Chris@16
|
79 {
|
Chris@16
|
80 BOOST_ASSERT( !m_ptr );
|
Chris@16
|
81 m_ptr = new char[bytes];
|
Chris@16
|
82 return m_ptr;
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 void deallocate()
|
Chris@16
|
86 {
|
Chris@16
|
87 delete [] m_ptr;
|
Chris@16
|
88 m_ptr = 0;
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 private:
|
Chris@16
|
92 char* m_ptr;
|
Chris@16
|
93 };
|
Chris@16
|
94
|
Chris@16
|
95 template<std::size_t StackBufferSize>
|
Chris@16
|
96 class any_iterator_stack_only_buffer
|
Chris@16
|
97 {
|
Chris@16
|
98 BOOST_STATIC_ASSERT(( StackBufferSize > 0 ));
|
Chris@16
|
99 public:
|
Chris@16
|
100 void* allocate(std::size_t bytes)
|
Chris@16
|
101 {
|
Chris@16
|
102 BOOST_ASSERT( bytes <= m_buffer.size() );
|
Chris@16
|
103 return m_buffer.data();
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 void deallocate()
|
Chris@16
|
107 {
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 private:
|
Chris@16
|
111 boost::array<char, StackBufferSize> m_buffer;
|
Chris@16
|
112 };
|
Chris@16
|
113
|
Chris@16
|
114 typedef any_iterator_buffer<64> any_iterator_default_buffer;
|
Chris@16
|
115 } // namespace boost
|
Chris@16
|
116
|
Chris@16
|
117 #endif // include guard
|