]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/cast.h
1 // <cast.h> -*- C++ -*-
3 // Copyright (C) 2008 Free Software Foundation, Inc.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
);
36 * These functions are here to allow containers to support non standard
37 * pointer types. For normal pointers, these resolve to the use of the
38 * standard cast operation. For other types the functions will perform
39 * the apprpriate cast to/from the custom pointer class so long as that
40 * class meets the following conditions:
41 * 1) has a typedef element_type which names tehe type it points to.
42 * 2) has a get() const method which returns element_type*.
43 * 3) has a constructor which can take one element_type* argument.
47 * This type supports the semantics of the pointer cast operators (below.)
49 template<typename _ToType
>
51 { typedef typename
_ToType::element_type
* type
; };
53 template<typename _ToType
>
54 struct _Caster
<_ToType
*>
55 { typedef _ToType
* type
; };
58 * Casting operations for cases where _FromType is not a standard pointer.
59 * _ToType can be a standard or non-standard pointer. Given that _FromType
60 * is not a pointer, it must have a get() method that returns the standard
61 * pointer equivalent of the address it points to, and must have an
62 * element_type typedef which names the type it points to.
64 template<typename _ToType
, typename _FromType
>
66 __static_pointer_cast(const _FromType
& __arg
)
67 { return _ToType(static_cast<typename _Caster
<_ToType
>::
68 type
>(__arg
.get())); }
70 template<typename _ToType
, typename _FromType
>
72 __dynamic_pointer_cast(const _FromType
& __arg
)
73 { return _ToType(dynamic_cast<typename _Caster
<_ToType
>::
74 type
>(__arg
.get())); }
76 template<typename _ToType
, typename _FromType
>
78 __const_pointer_cast(const _FromType
& __arg
)
79 { return _ToType(const_cast<typename _Caster
<_ToType
>::
80 type
>(__arg
.get())); }
82 template<typename _ToType
, typename _FromType
>
84 __reinterpret_pointer_cast(const _FromType
& __arg
)
85 { return _ToType(reinterpret_cast<typename _Caster
<_ToType
>::
86 type
>(__arg
.get())); }
89 * Casting operations for cases where _FromType is a standard pointer.
90 * _ToType can be a standard or non-standard pointer.
92 template<typename _ToType
, typename _FromType
>
94 __static_pointer_cast(_FromType
* __arg
)
95 { return _ToType(static_cast<typename _Caster
<_ToType
>::
98 template<typename _ToType
, typename _FromType
>
100 __dynamic_pointer_cast(_FromType
* __arg
)
101 { return _ToType(dynamic_cast<typename _Caster
<_ToType
>::
104 template<typename _ToType
, typename _FromType
>
106 __const_pointer_cast(_FromType
* __arg
)
107 { return _ToType(const_cast<typename _Caster
<_ToType
>::
110 template<typename _ToType
, typename _FromType
>
112 __reinterpret_pointer_cast(_FromType
* __arg
)
113 { return _ToType(reinterpret_cast<typename _Caster
<_ToType
>::
116 _GLIBCXX_END_NAMESPACE