xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/ext/cast.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // <cast.h> -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2008-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /** @file ext/cast.h
26*38fd1498Szrj  *  This is an internal header file, included by other library headers.
27*38fd1498Szrj  *  Do not attempt to use it directly. @headername{ext/pointer.h}
28*38fd1498Szrj  */
29*38fd1498Szrj 
30*38fd1498Szrj #ifndef _GLIBCXX_CAST_H
31*38fd1498Szrj #define _GLIBCXX_CAST_H 1
32*38fd1498Szrj 
_GLIBCXX_VISIBILITY(default)33*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
34*38fd1498Szrj {
35*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
36*38fd1498Szrj 
37*38fd1498Szrj   /**
38*38fd1498Szrj    * These functions are here to allow containers to support non standard
39*38fd1498Szrj    * pointer types.  For normal pointers, these resolve to the use of the
40*38fd1498Szrj    * standard cast operation.  For other types the functions will perform
41*38fd1498Szrj    * the appropriate cast to/from the custom pointer class so long as that
42*38fd1498Szrj    * class meets the following conditions:
43*38fd1498Szrj    * 1) has a typedef element_type which names tehe type it points to.
44*38fd1498Szrj    * 2) has a get() const method which returns element_type*.
45*38fd1498Szrj    * 3) has a constructor which can take one element_type* argument.
46*38fd1498Szrj    */
47*38fd1498Szrj 
48*38fd1498Szrj   /**
49*38fd1498Szrj    * This type supports the semantics of the pointer cast operators (below.)
50*38fd1498Szrj    */
51*38fd1498Szrj   template<typename _ToType>
52*38fd1498Szrj     struct _Caster
53*38fd1498Szrj     { typedef typename _ToType::element_type*  type; };
54*38fd1498Szrj 
55*38fd1498Szrj   template<typename _ToType>
56*38fd1498Szrj     struct _Caster<_ToType*>
57*38fd1498Szrj     { typedef _ToType*  type; };
58*38fd1498Szrj 
59*38fd1498Szrj   /**
60*38fd1498Szrj    * Casting operations for cases where _FromType is not a standard pointer.
61*38fd1498Szrj    * _ToType can be a standard or non-standard pointer.  Given that _FromType
62*38fd1498Szrj    * is not a pointer, it must have a get() method that returns the standard
63*38fd1498Szrj    * pointer equivalent of the address it points to, and must have an
64*38fd1498Szrj    * element_type typedef which names the type it points to.
65*38fd1498Szrj    */
66*38fd1498Szrj   template<typename _ToType, typename _FromType>
67*38fd1498Szrj     inline _ToType
68*38fd1498Szrj     __static_pointer_cast(const _FromType& __arg)
69*38fd1498Szrj     { return _ToType(static_cast<typename _Caster<_ToType>::
70*38fd1498Szrj 		     type>(__arg.get())); }
71*38fd1498Szrj 
72*38fd1498Szrj   template<typename _ToType, typename _FromType>
73*38fd1498Szrj     inline _ToType
74*38fd1498Szrj     __dynamic_pointer_cast(const _FromType& __arg)
75*38fd1498Szrj     { return _ToType(dynamic_cast<typename _Caster<_ToType>::
76*38fd1498Szrj 		     type>(__arg.get())); }
77*38fd1498Szrj 
78*38fd1498Szrj   template<typename _ToType, typename _FromType>
79*38fd1498Szrj     inline _ToType
80*38fd1498Szrj     __const_pointer_cast(const _FromType& __arg)
81*38fd1498Szrj     { return _ToType(const_cast<typename _Caster<_ToType>::
82*38fd1498Szrj 		     type>(__arg.get())); }
83*38fd1498Szrj 
84*38fd1498Szrj   template<typename _ToType, typename _FromType>
85*38fd1498Szrj     inline _ToType
86*38fd1498Szrj     __reinterpret_pointer_cast(const _FromType& __arg)
87*38fd1498Szrj     { return _ToType(reinterpret_cast<typename _Caster<_ToType>::
88*38fd1498Szrj 		     type>(__arg.get())); }
89*38fd1498Szrj 
90*38fd1498Szrj   /**
91*38fd1498Szrj    * Casting operations for cases where _FromType is a standard pointer.
92*38fd1498Szrj    * _ToType can be a standard or non-standard pointer.
93*38fd1498Szrj    */
94*38fd1498Szrj   template<typename _ToType, typename _FromType>
95*38fd1498Szrj     inline _ToType
96*38fd1498Szrj     __static_pointer_cast(_FromType* __arg)
97*38fd1498Szrj     { return _ToType(static_cast<typename _Caster<_ToType>::
98*38fd1498Szrj 		     type>(__arg)); }
99*38fd1498Szrj 
100*38fd1498Szrj   template<typename _ToType, typename _FromType>
101*38fd1498Szrj     inline _ToType
102*38fd1498Szrj     __dynamic_pointer_cast(_FromType* __arg)
103*38fd1498Szrj     { return _ToType(dynamic_cast<typename _Caster<_ToType>::
104*38fd1498Szrj 		     type>(__arg)); }
105*38fd1498Szrj 
106*38fd1498Szrj   template<typename _ToType, typename _FromType>
107*38fd1498Szrj     inline _ToType
108*38fd1498Szrj     __const_pointer_cast(_FromType* __arg)
109*38fd1498Szrj     { return _ToType(const_cast<typename _Caster<_ToType>::
110*38fd1498Szrj 		     type>(__arg)); }
111*38fd1498Szrj 
112*38fd1498Szrj   template<typename _ToType, typename _FromType>
113*38fd1498Szrj     inline _ToType
114*38fd1498Szrj     __reinterpret_pointer_cast(_FromType* __arg)
115*38fd1498Szrj     { return _ToType(reinterpret_cast<typename _Caster<_ToType>::
116*38fd1498Szrj 		     type>(__arg)); }
117*38fd1498Szrj 
118*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
119*38fd1498Szrj } // namespace
120*38fd1498Szrj 
121*38fd1498Szrj #endif // _GLIBCXX_CAST_H
122