1 |
424 |
jeremybenn |
// <cast.h> -*- C++ -*-
|
2 |
|
|
|
3 |
|
|
// Copyright (C) 2008, 2009 Free Software Foundation, Inc.
|
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
|
8 |
|
|
// Free Software Foundation; either version 3, or (at your option)
|
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 |
|
|
|
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.
|
19 |
|
|
|
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/>.
|
24 |
|
|
|
25 |
|
|
#ifndef _CAST_H
|
26 |
|
|
#define _CAST_H 1
|
27 |
|
|
|
28 |
|
|
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
|
29 |
|
|
|
30 |
|
|
/**
|
31 |
|
|
* These functions are here to allow containers to support non standard
|
32 |
|
|
* pointer types. For normal pointers, these resolve to the use of the
|
33 |
|
|
* standard cast operation. For other types the functions will perform
|
34 |
|
|
* the apprpriate cast to/from the custom pointer class so long as that
|
35 |
|
|
* class meets the following conditions:
|
36 |
|
|
* 1) has a typedef element_type which names tehe type it points to.
|
37 |
|
|
* 2) has a get() const method which returns element_type*.
|
38 |
|
|
* 3) has a constructor which can take one element_type* argument.
|
39 |
|
|
*/
|
40 |
|
|
|
41 |
|
|
/**
|
42 |
|
|
* This type supports the semantics of the pointer cast operators (below.)
|
43 |
|
|
*/
|
44 |
|
|
template<typename _ToType>
|
45 |
|
|
struct _Caster
|
46 |
|
|
{ typedef typename _ToType::element_type* type; };
|
47 |
|
|
|
48 |
|
|
template<typename _ToType>
|
49 |
|
|
struct _Caster<_ToType*>
|
50 |
|
|
{ typedef _ToType* type; };
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* Casting operations for cases where _FromType is not a standard pointer.
|
54 |
|
|
* _ToType can be a standard or non-standard pointer. Given that _FromType
|
55 |
|
|
* is not a pointer, it must have a get() method that returns the standard
|
56 |
|
|
* pointer equivalent of the address it points to, and must have an
|
57 |
|
|
* element_type typedef which names the type it points to.
|
58 |
|
|
*/
|
59 |
|
|
template<typename _ToType, typename _FromType>
|
60 |
|
|
inline _ToType
|
61 |
|
|
__static_pointer_cast(const _FromType& __arg)
|
62 |
|
|
{ return _ToType(static_cast<typename _Caster<_ToType>::
|
63 |
|
|
type>(__arg.get())); }
|
64 |
|
|
|
65 |
|
|
template<typename _ToType, typename _FromType>
|
66 |
|
|
inline _ToType
|
67 |
|
|
__dynamic_pointer_cast(const _FromType& __arg)
|
68 |
|
|
{ return _ToType(dynamic_cast<typename _Caster<_ToType>::
|
69 |
|
|
type>(__arg.get())); }
|
70 |
|
|
|
71 |
|
|
template<typename _ToType, typename _FromType>
|
72 |
|
|
inline _ToType
|
73 |
|
|
__const_pointer_cast(const _FromType& __arg)
|
74 |
|
|
{ return _ToType(const_cast<typename _Caster<_ToType>::
|
75 |
|
|
type>(__arg.get())); }
|
76 |
|
|
|
77 |
|
|
template<typename _ToType, typename _FromType>
|
78 |
|
|
inline _ToType
|
79 |
|
|
__reinterpret_pointer_cast(const _FromType& __arg)
|
80 |
|
|
{ return _ToType(reinterpret_cast<typename _Caster<_ToType>::
|
81 |
|
|
type>(__arg.get())); }
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* Casting operations for cases where _FromType is a standard pointer.
|
85 |
|
|
* _ToType can be a standard or non-standard pointer.
|
86 |
|
|
*/
|
87 |
|
|
template<typename _ToType, typename _FromType>
|
88 |
|
|
inline _ToType
|
89 |
|
|
__static_pointer_cast(_FromType* __arg)
|
90 |
|
|
{ return _ToType(static_cast<typename _Caster<_ToType>::
|
91 |
|
|
type>(__arg)); }
|
92 |
|
|
|
93 |
|
|
template<typename _ToType, typename _FromType>
|
94 |
|
|
inline _ToType
|
95 |
|
|
__dynamic_pointer_cast(_FromType* __arg)
|
96 |
|
|
{ return _ToType(dynamic_cast<typename _Caster<_ToType>::
|
97 |
|
|
type>(__arg)); }
|
98 |
|
|
|
99 |
|
|
template<typename _ToType, typename _FromType>
|
100 |
|
|
inline _ToType
|
101 |
|
|
__const_pointer_cast(_FromType* __arg)
|
102 |
|
|
{ return _ToType(const_cast<typename _Caster<_ToType>::
|
103 |
|
|
type>(__arg)); }
|
104 |
|
|
|
105 |
|
|
template<typename _ToType, typename _FromType>
|
106 |
|
|
inline _ToType
|
107 |
|
|
__reinterpret_pointer_cast(_FromType* __arg)
|
108 |
|
|
{ return _ToType(reinterpret_cast<typename _Caster<_ToType>::
|
109 |
|
|
type>(__arg)); }
|
110 |
|
|
|
111 |
|
|
_GLIBCXX_END_NAMESPACE
|
112 |
|
|
|
113 |
|
|
#endif // _CAST_H
|