]>
Commit | Line | Data |
---|---|---|
9f4a4394 PC |
1 | // <cast.h> -*- C++ -*- |
2 | ||
748086b7 | 3 | // Copyright (C) 2008, 2009 Free Software Foundation, Inc. |
9f4a4394 PC |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
9f4a4394 PC |
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 | ||
748086b7 JJ |
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. | |
9f4a4394 | 19 | |
748086b7 JJ |
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/>. | |
9f4a4394 | 24 | |
f910786b BK |
25 | /** @file ext/cast.h |
26 | * This is an internal header file, included by other library headers. | |
27 | * Do not attempt to use it directly. @headername{ext/pointer.h} | |
28 | */ | |
29 | ||
30 | #ifndef _GLIBCXX_CAST_H | |
31 | #define _GLIBCXX_CAST_H 1 | |
9f4a4394 | 32 | |
12d74f0d | 33 | _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) |
9f4a4394 PC |
34 | |
35 | /** | |
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. | |
44 | */ | |
45 | ||
46 | /** | |
47 | * This type supports the semantics of the pointer cast operators (below.) | |
48 | */ | |
49 | template<typename _ToType> | |
50 | struct _Caster | |
8d8a4e9d PC |
51 | { typedef typename _ToType::element_type* type; }; |
52 | ||
9f4a4394 PC |
53 | template<typename _ToType> |
54 | struct _Caster<_ToType*> | |
8d8a4e9d | 55 | { typedef _ToType* type; }; |
9f4a4394 PC |
56 | |
57 | /** | |
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. | |
63 | */ | |
64 | template<typename _ToType, typename _FromType> | |
65 | inline _ToType | |
66 | __static_pointer_cast(const _FromType& __arg) | |
67 | { return _ToType(static_cast<typename _Caster<_ToType>:: | |
68 | type>(__arg.get())); } | |
69 | ||
70 | template<typename _ToType, typename _FromType> | |
71 | inline _ToType | |
72 | __dynamic_pointer_cast(const _FromType& __arg) | |
73 | { return _ToType(dynamic_cast<typename _Caster<_ToType>:: | |
74 | type>(__arg.get())); } | |
75 | ||
76 | template<typename _ToType, typename _FromType> | |
77 | inline _ToType | |
78 | __const_pointer_cast(const _FromType& __arg) | |
79 | { return _ToType(const_cast<typename _Caster<_ToType>:: | |
80 | type>(__arg.get())); } | |
81 | ||
82 | template<typename _ToType, typename _FromType> | |
83 | inline _ToType | |
84 | __reinterpret_pointer_cast(const _FromType& __arg) | |
85 | { return _ToType(reinterpret_cast<typename _Caster<_ToType>:: | |
86 | type>(__arg.get())); } | |
87 | ||
88 | /** | |
89 | * Casting operations for cases where _FromType is a standard pointer. | |
90 | * _ToType can be a standard or non-standard pointer. | |
91 | */ | |
92 | template<typename _ToType, typename _FromType> | |
93 | inline _ToType | |
94 | __static_pointer_cast(_FromType* __arg) | |
95 | { return _ToType(static_cast<typename _Caster<_ToType>:: | |
96 | type>(__arg)); } | |
97 | ||
98 | template<typename _ToType, typename _FromType> | |
99 | inline _ToType | |
100 | __dynamic_pointer_cast(_FromType* __arg) | |
101 | { return _ToType(dynamic_cast<typename _Caster<_ToType>:: | |
102 | type>(__arg)); } | |
103 | ||
104 | template<typename _ToType, typename _FromType> | |
105 | inline _ToType | |
106 | __const_pointer_cast(_FromType* __arg) | |
107 | { return _ToType(const_cast<typename _Caster<_ToType>:: | |
108 | type>(__arg)); } | |
109 | ||
110 | template<typename _ToType, typename _FromType> | |
111 | inline _ToType | |
112 | __reinterpret_pointer_cast(_FromType* __arg) | |
113 | { return _ToType(reinterpret_cast<typename _Caster<_ToType>:: | |
114 | type>(__arg)); } | |
115 | ||
116 | _GLIBCXX_END_NAMESPACE | |
117 | ||
f910786b | 118 | #endif // __GLIBCXX_CAST_H |