libstdc++
uses_allocator.h
00001 // Uses-allocator Construction -*- C++ -*-
00002 
00003 // Copyright (C) 2010-2015 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 #if __cplusplus < 201103L
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   template<typename _Tp, typename _Alloc, typename = __void_t<>>
00044     struct __uses_allocator_helper
00045     : false_type { };
00046 
00047   template<typename _Tp, typename _Alloc>
00048     struct __uses_allocator_helper<_Tp, _Alloc,
00049                                    __void_t<typename _Tp::allocator_type>>
00050     : is_convertible<_Alloc, typename _Tp::allocator_type>::type
00051     { };
00052 
00053   /// [allocator.uses.trait]
00054   template<typename _Tp, typename _Alloc>
00055     struct uses_allocator
00056     : __uses_allocator_helper<_Tp, _Alloc>::type
00057     { };
00058 
00059   struct __uses_alloc_base { };
00060 
00061   struct __uses_alloc0 : __uses_alloc_base
00062   {
00063     struct _Sink { void operator=(const void*) { } } _M_a;
00064   };
00065 
00066   template<typename _Alloc>
00067     struct __uses_alloc1 : __uses_alloc_base { const _Alloc* _M_a; };
00068 
00069   template<typename _Alloc>
00070     struct __uses_alloc2 : __uses_alloc_base { const _Alloc* _M_a; };
00071 
00072   template<bool, typename _Tp, typename _Alloc, typename... _Args>
00073     struct __uses_alloc;
00074 
00075   template<typename _Tp, typename _Alloc, typename... _Args>
00076     struct __uses_alloc<true, _Tp, _Alloc, _Args...>
00077     : conditional<
00078         is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value,
00079         __uses_alloc1<_Alloc>,
00080         __uses_alloc2<_Alloc>>::type
00081     { };
00082 
00083   template<typename _Tp, typename _Alloc, typename... _Args>
00084     struct __uses_alloc<false, _Tp, _Alloc, _Args...>
00085     : __uses_alloc0 { };
00086 
00087   template<typename _Tp, typename _Alloc, typename... _Args>
00088     using __uses_alloc_t =
00089       __uses_alloc<uses_allocator<_Tp, _Alloc>::value, _Tp, _Alloc, _Args...>;
00090 
00091   template<typename _Tp, typename _Alloc, typename... _Args>
00092     inline __uses_alloc_t<_Tp, _Alloc, _Args...>
00093     __use_alloc(const _Alloc& __a)
00094     {
00095       __uses_alloc_t<_Tp, _Alloc, _Args...> __ret;
00096       __ret._M_a = &__a;
00097       return __ret;
00098     }
00099 
00100 _GLIBCXX_END_NAMESPACE_VERSION
00101 } // namespace std
00102 
00103 #endif
00104 #endif