libstdc++
uses_allocator.h
00001 // Uses-allocator Construction -*- C++ -*-
00002 
00003 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef _USES_ALLOCATOR_H
00026 #define _USES_ALLOCATOR_H 1
00027 
00028 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00029 # include <bits/c++0x_warning.h>
00030 #else
00031 
00032 #include <type_traits>
00033 
00034 namespace std _GLIBCXX_VISIBILITY(default)
00035 {
00036 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00037 
00038   /// [allocator.tag]
00039   struct allocator_arg_t { };
00040 
00041   constexpr allocator_arg_t allocator_arg = allocator_arg_t();
00042 
00043 _GLIBCXX_HAS_NESTED_TYPE(allocator_type)
00044 
00045   template<typename _Tp, typename _Alloc,
00046        bool = __has_allocator_type<_Tp>::value>
00047     struct __uses_allocator_helper
00048     : public false_type { };
00049 
00050   template<typename _Tp, typename _Alloc>
00051     struct __uses_allocator_helper<_Tp, _Alloc, true>
00052     : public integral_constant<bool, is_convertible<_Alloc,
00053                      typename _Tp::allocator_type>::value>
00054     { };
00055 
00056   /// [allocator.uses.trait]
00057   template<typename _Tp, typename _Alloc>
00058     struct uses_allocator
00059     : public integral_constant<bool,
00060                    __uses_allocator_helper<_Tp, _Alloc>::value>
00061     { };
00062 
00063   template<typename _Tp, typename _Alloc, typename... _Args>
00064     struct __uses_allocator_arg
00065     : is_constructible<_Tp, _Alloc, _Args...>
00066     { static_assert( uses_allocator<_Tp, _Alloc>::value, "uses allocator" ); };
00067 
00068   struct __uses_alloc_base { };
00069   struct __uses_alloc0 : __uses_alloc_base
00070   { struct _Anything { _Anything(...) { } } _M_a; };
00071   template<typename _Alloc>
00072     struct __uses_alloc1 : __uses_alloc_base { const _Alloc* _M_a; };
00073   template<typename _Alloc>
00074     struct __uses_alloc2 : __uses_alloc_base { const _Alloc* _M_a; };
00075 
00076   template<bool, typename _Alloc, typename... _Args>
00077     struct __uses_alloc;
00078 
00079   template<typename _Tp, typename _Alloc, typename... _Args>
00080     struct __uses_alloc<true, _Tp, _Alloc, _Args...>
00081     : conditional<
00082         is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value,
00083         __uses_alloc1<_Alloc>,
00084         __uses_alloc2<_Alloc>>::type
00085     { };
00086 
00087   template<typename _Tp, typename _Alloc, typename... _Args>
00088     struct __uses_alloc<false, _Tp, _Alloc, _Args...>
00089     : __uses_alloc0 { };
00090 
00091   template<typename _Tp, typename _Alloc, typename... _Args>
00092     struct __uses_alloc_impl
00093     : __uses_alloc<uses_allocator<_Tp, _Alloc>::value, _Tp,  _Alloc, _Args...>
00094     { };
00095 
00096   template<typename _Tp, typename _Alloc, typename... _Args>
00097     __uses_alloc_impl<_Tp, _Alloc, _Args...>
00098     __use_alloc(const _Alloc& __a)
00099     {
00100       __uses_alloc_impl<_Tp, _Alloc, _Args...> __ret;
00101       __ret._M_a = &__a;
00102       return __ret;
00103     }
00104 
00105 _GLIBCXX_END_NAMESPACE_VERSION
00106 } // namespace std
00107 
00108 #endif
00109 #endif