libstdc++
|
00001 // <cast.h> -*- C++ -*- 00002 00003 // Copyright (C) 2008, 2009, 2010 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 /** @file ext/cast.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{ext/pointer.h} 00028 */ 00029 00030 #ifndef _GLIBCXX_CAST_H 00031 #define _GLIBCXX_CAST_H 1 00032 00033 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00034 { 00035 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00036 00037 /** 00038 * These functions are here to allow containers to support non standard 00039 * pointer types. For normal pointers, these resolve to the use of the 00040 * standard cast operation. For other types the functions will perform 00041 * the apprpriate cast to/from the custom pointer class so long as that 00042 * class meets the following conditions: 00043 * 1) has a typedef element_type which names tehe type it points to. 00044 * 2) has a get() const method which returns element_type*. 00045 * 3) has a constructor which can take one element_type* argument. 00046 */ 00047 00048 /** 00049 * This type supports the semantics of the pointer cast operators (below.) 00050 */ 00051 template<typename _ToType> 00052 struct _Caster 00053 { typedef typename _ToType::element_type* type; }; 00054 00055 template<typename _ToType> 00056 struct _Caster<_ToType*> 00057 { typedef _ToType* type; }; 00058 00059 /** 00060 * Casting operations for cases where _FromType is not a standard pointer. 00061 * _ToType can be a standard or non-standard pointer. Given that _FromType 00062 * is not a pointer, it must have a get() method that returns the standard 00063 * pointer equivalent of the address it points to, and must have an 00064 * element_type typedef which names the type it points to. 00065 */ 00066 template<typename _ToType, typename _FromType> 00067 inline _ToType 00068 __static_pointer_cast(const _FromType& __arg) 00069 { return _ToType(static_cast<typename _Caster<_ToType>:: 00070 type>(__arg.get())); } 00071 00072 template<typename _ToType, typename _FromType> 00073 inline _ToType 00074 __dynamic_pointer_cast(const _FromType& __arg) 00075 { return _ToType(dynamic_cast<typename _Caster<_ToType>:: 00076 type>(__arg.get())); } 00077 00078 template<typename _ToType, typename _FromType> 00079 inline _ToType 00080 __const_pointer_cast(const _FromType& __arg) 00081 { return _ToType(const_cast<typename _Caster<_ToType>:: 00082 type>(__arg.get())); } 00083 00084 template<typename _ToType, typename _FromType> 00085 inline _ToType 00086 __reinterpret_pointer_cast(const _FromType& __arg) 00087 { return _ToType(reinterpret_cast<typename _Caster<_ToType>:: 00088 type>(__arg.get())); } 00089 00090 /** 00091 * Casting operations for cases where _FromType is a standard pointer. 00092 * _ToType can be a standard or non-standard pointer. 00093 */ 00094 template<typename _ToType, typename _FromType> 00095 inline _ToType 00096 __static_pointer_cast(_FromType* __arg) 00097 { return _ToType(static_cast<typename _Caster<_ToType>:: 00098 type>(__arg)); } 00099 00100 template<typename _ToType, typename _FromType> 00101 inline _ToType 00102 __dynamic_pointer_cast(_FromType* __arg) 00103 { return _ToType(dynamic_cast<typename _Caster<_ToType>:: 00104 type>(__arg)); } 00105 00106 template<typename _ToType, typename _FromType> 00107 inline _ToType 00108 __const_pointer_cast(_FromType* __arg) 00109 { return _ToType(const_cast<typename _Caster<_ToType>:: 00110 type>(__arg)); } 00111 00112 template<typename _ToType, typename _FromType> 00113 inline _ToType 00114 __reinterpret_pointer_cast(_FromType* __arg) 00115 { return _ToType(reinterpret_cast<typename _Caster<_ToType>:: 00116 type>(__arg)); } 00117 00118 _GLIBCXX_END_NAMESPACE_VERSION 00119 } // namespace 00120 00121 #endif // _GLIBCXX_CAST_H