OSDN Git Service

2011-07-09 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / scoped_allocator
1 // <scoped_allocator> -*- C++ -*-
2
3 // Copyright (C) 2011 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/scoped_allocator
26  *  This is a Standard C++ Library header.
27  */
28
29 #ifndef _SCOPED_ALLOCATOR
30 #define _SCOPED_ALLOCATOR 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <utility>
39 #include <tuple>
40 #include <bits/alloc_traits.h>
41
42 namespace std _GLIBCXX_VISIBILITY(default)
43 {
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46   template<template<typename> class _Pred, typename... _Allocs>
47     struct __any_of;
48
49   template<template<typename> class _Pred, typename _Alloc, typename... _Allocs>
50     struct __any_of<_Pred, _Alloc, _Allocs...>
51     : __or_<_Pred<_Alloc>, __any_of<_Pred, _Allocs...>>
52     { };
53
54   template<template<typename> class _Pred, typename _Alloc>
55     struct __any_of<_Pred, _Alloc>
56     : _Pred<_Alloc>
57     { };
58
59   /**
60    * @addtogroup allocators
61    * @{
62    */
63
64   template<typename _Alloc>
65     struct __propagate_on_copy
66     : allocator_traits<_Alloc>::propagate_on_container_copy_assignment
67     { };
68   template<typename _Alloc>
69     struct __propagate_on_move
70     : allocator_traits<_Alloc>::propagate_on_container_move_assignment
71     { };
72   template<typename _Alloc>
73     struct __propagate_on_swap
74     : allocator_traits<_Alloc>::propagate_on_container_swap
75     { };
76
77   
78   template<typename _Alloc>
79     inline auto
80     __do_outermost(_Alloc& __a, _Alloc&) -> decltype(__a.outer_allocator())
81     { return __a.outer_allocator(); }
82
83   template<typename _Alloc>
84     inline _Alloc&
85     __do_outermost(_Alloc& __a, ...)
86     { return __a; }
87
88   template<typename _Alloc>
89     inline auto
90     __outermost(_Alloc& __a) -> decltype(__do_outermost(__a, __a))
91     { return __do_outermost(__a, __a); }
92
93   template<typename _OuterAlloc, typename... _InnerAllocs>
94     class scoped_allocator_adaptor;
95
96       template<typename...> struct __inner_type_impl;
97
98       template<typename _Outer>
99       struct __inner_type_impl<_Outer>
100       {
101         typedef scoped_allocator_adaptor<_Outer> __type;
102
103         __inner_type_impl() = default;
104         __inner_type_impl(const __inner_type_impl&) = default;
105         __inner_type_impl(__inner_type_impl&&) = default;
106
107         template<typename _Alloc>
108           __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
109           { }
110
111         template<typename _Alloc>
112           __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
113           { }
114
115         __type& _M_get(__type* __p) noexcept { return *__p; }
116         const __type& _M_get(const __type* __p) const noexcept { return *__p; }
117
118         tuple<> _M_tie() const noexcept { return tuple<>(); }
119
120         bool operator==(const __inner_type_impl&) const noexcept
121         { return true; }
122       };
123
124       template<typename _Outer, typename _InnerHead, typename... _InnerTail>
125       struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
126       {
127         typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
128
129         __inner_type_impl() = default;
130         __inner_type_impl(const __inner_type_impl&) = default;
131         __inner_type_impl(__inner_type_impl&&) = default;
132
133         template<typename... _Allocs>
134           __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
135           : _M_inner(__other._M_inner) { }
136
137         template<typename... _Allocs>
138           __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
139           : _M_inner(std::move(__other._M_inner)) { }
140
141         template<typename... _Args>
142           explicit
143           __inner_type_impl(_Args&&... __args)
144           : _M_inner(std::forward<_Args>(__args)...) { }
145
146         __type& _M_get(void*) noexcept { return _M_inner; }
147         const __type& _M_get(const void*) const noexcept { return _M_inner; }
148
149         tuple<const _InnerHead&, const _InnerTail&...> _M_tie() const noexcept
150         { return _M_inner._M_tie(); }
151
152         bool operator==(const __inner_type_impl& __other) const noexcept
153         { return _M_inner == __other._M_inner; }
154
155       private:
156         template<typename...> friend class __inner_type_impl;
157         template<typename, typename...> friend class scoped_allocator_adaptor;
158
159         __type _M_inner;
160       };
161
162   template<typename _OuterAlloc, typename... _InnerAllocs>
163     class scoped_allocator_adaptor
164     : public _OuterAlloc
165     {
166       typedef allocator_traits<_OuterAlloc> __traits;
167
168       typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
169       __inner_type _M_inner;
170
171       template<typename _Outer, typename... _Inner>
172         friend class scoped_allocator_adaptor;
173
174       template<typename...>
175         friend class __inner_type_impl;
176
177       tuple<const _OuterAlloc&, const _InnerAllocs&...>
178       _M_tie() const noexcept
179       { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
180
181       
182       template<typename _Tp, typename... _Args>
183         void _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
184         {
185           auto& __outer = __outermost(*this);
186           typedef typename std::decay<decltype(__outer)>::type __outer_type;
187           typedef allocator_traits<__outer_type> __o_traits;
188           __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...);
189         }
190
191       typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
192       typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
193
194       template<typename _Tp, typename... _Args>
195         void _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
196         {
197           auto& __outer = __outermost(*this);
198           typedef typename std::decay<decltype(__outer)>::type __outer_type;
199           typedef allocator_traits<__outer_type> __o_traits;
200           __o_traits::construct(__outer, __p, allocator_arg, inner_allocator(),
201                                 std::forward<_Args>(__args)...);
202         }
203
204       template<typename _Tp, typename... _Args>
205         void _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
206         {
207           auto& __outer = __outermost(*this);
208           typedef typename std::decay<decltype(__outer)>::type __outer_type;
209           typedef allocator_traits<__outer_type> __o_traits;
210           __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...,
211                                 inner_allocator());
212         }
213
214       template<typename _Alloc>
215         static _Alloc
216         _S_select_on_copy(const _Alloc& __a)
217         {
218           typedef allocator_traits<_Alloc> __a_traits;
219           return __a_traits::select_on_container_copy_construction(__a);
220         }
221
222       template<int... _Indices>
223         scoped_allocator_adaptor(tuple<const _OuterAlloc&,
224                                        const _InnerAllocs&...> __refs,
225                                  _Index_tuple<_Indices...>)
226         : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
227           _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
228         { }
229
230     public:
231       typedef _OuterAlloc                       outer_allocator_type;
232       typedef typename __inner_type::__type     inner_allocator_type;
233
234       typedef typename __traits::value_type             value_type;
235       typedef typename __traits::size_type              size_type;
236       typedef typename __traits::difference_type        difference_type;
237       typedef typename __traits::pointer                pointer;
238       typedef typename __traits::const_pointer          const_pointer;
239       typedef typename __traits::void_pointer           void_pointer;
240       typedef typename __traits::const_void_pointer     const_void_pointer;
241
242       typedef typename conditional<
243         __any_of<__propagate_on_copy, _OuterAlloc, _InnerAllocs...>::value,
244         true_type, false_type>::type propagate_on_container_copy_assignment;
245       typedef typename conditional<
246         __any_of<__propagate_on_move, _OuterAlloc, _InnerAllocs...>::value,
247         true_type, false_type>::type propagate_on_container_move_assignment;
248       typedef typename conditional<
249         __any_of<__propagate_on_swap, _OuterAlloc, _InnerAllocs...>::value,
250         true_type, false_type>::type propagate_on_container_swap;
251
252       template <class _Tp>
253         struct rebind
254         {
255           // TODO: use rebind_alloc<Tp> instead of __rebind_alloc<Tp>::__type
256           typedef scoped_allocator_adaptor<
257             typename __traits::template __rebind_alloc<_Tp>::__type,
258             _InnerAllocs...> other;
259         };
260
261       scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
262
263       template<typename _Outer2>
264         scoped_allocator_adaptor(_Outer2&& __outer,
265                                  const _InnerAllocs&... __inner)
266         : _OuterAlloc(std::forward<_Outer2>(__outer)),
267           _M_inner(__inner...)
268         { }
269
270       scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
271       : _OuterAlloc(__other.outer_allocator()),
272         _M_inner(__other._M_inner)
273       { }
274
275       scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
276       : _OuterAlloc(std::move(__other.outer_allocator())),
277         _M_inner(std::move(__other._M_inner))
278       { }
279
280       template<typename _Outer2>
281         scoped_allocator_adaptor(
282             const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
283         : _OuterAlloc(__other.outer_allocator()),
284           _M_inner(__other._M_inner)
285         { }
286
287       template<typename _Outer2>
288         scoped_allocator_adaptor(
289             scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
290         : _OuterAlloc(std::move(__other.outer_allocator())),
291           _M_inner(std::move(__other._M_inner))
292         { }
293
294       inner_allocator_type& inner_allocator() noexcept
295       { return _M_inner._M_get(this); }
296
297       const inner_allocator_type& inner_allocator() const noexcept
298       { return _M_inner._M_get(this); }
299
300       outer_allocator_type& outer_allocator() noexcept
301       { return static_cast<_OuterAlloc&>(*this); }
302
303       const outer_allocator_type& outer_allocator() const noexcept
304       { return static_cast<const _OuterAlloc&>(*this); }
305
306       pointer allocate(size_type __n)
307       { return __traits::allocate(outer_allocator(), __n); }
308
309       pointer allocate(size_type __n, const_void_pointer __hint)
310       { return __traits::allocate(outer_allocator(), __n, __hint); }
311
312       void deallocate(pointer __p, size_type __n)
313       { return __traits::deallocate(outer_allocator(), __p, __n); }
314
315       size_type max_size() const
316       { return __traits::max_size(outer_allocator()); }
317
318       template<typename _Tp, typename... _Args>
319         void construct(_Tp* __p, _Args&&... __args)
320         {
321           auto& __inner = inner_allocator();
322           auto __use_tag
323             = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
324           _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
325         }
326
327       // TODO: construct pairs
328
329       template<typename _Tp>
330         void destroy(_Tp* __p)
331         {
332           auto& __outer = __outermost(*this);
333           typedef typename std::decay<decltype(__outer)>::type __outer_type;
334           allocator_traits<__outer_type>::destroy(__outer, __p);
335         }
336
337       scoped_allocator_adaptor
338       select_on_container_copy_construction() const
339       {
340         typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
341             _Indices;
342         return scoped_allocator_adaptor(_M_tie(), _Indices());
343       }
344
345       template <typename _OutA1, typename _OutA2, typename... _InA>
346       friend bool
347       operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
348                  const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
349     };
350
351   template <typename _OutA1, typename _OutA2, typename... _InA>
352     inline bool
353     operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
354                const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
355     {
356       return __a.outer_allocator() == __b.outer_allocator()
357           && __a._M_inner == __b._M_inner;
358     }
359
360   template <typename _OutA1, typename _OutA2, typename... _InA>
361     inline bool
362     operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
363                const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
364     { return !(__a == __b); }
365
366   /// @}
367
368 _GLIBCXX_END_NAMESPACE_VERSION
369 } // namespace
370
371 #endif // __GXX_EXPERIMENTAL_CXX0X__
372
373 #endif // _SCOPED_ALLOCATOR