# cast.h

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