00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00048 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_PTR_CAST
00049 #define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_PTR_CAST
00050 
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_PTR_CAST_MAJOR      4
00053 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_PTR_CAST_MINOR      0
00054 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_PTR_CAST_REVISION   8
00055 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_PTR_CAST_EDIT       38
00056 #endif 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00075 # include <stlsoft/stlsoft.h>
00076 #endif 
00077 #ifndef STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS
00078 # include <stlsoft/meta/base_type_traits.hpp>
00079 #endif 
00080 
00081 #ifndef STLSOFT_INCL_TYPEINFO
00082 # define STLSOFT_INCL_TYPEINFO
00083 # include <typeinfo>
00084 #endif 
00085 
00086 
00087 
00088 
00089 
00090 #ifndef _STLSOFT_NO_NAMESPACE
00091 namespace stlsoft
00092 {
00093 #endif 
00094 
00095 
00096 
00097 
00098 
00204 template <ss_typename_param_k Target>
00205 struct ptr_cast
00206 {
00207 public:
00209     typedef Target                                                          target_type;
00211     typedef ptr_cast<Target>                                                class_type;
00213     typedef ss_typename_type_k stlsoft::base_type_traits<Target>::cv_type   target_base_type;
00215     typedef target_base_type&                                               reference_type;
00217     typedef target_base_type*                                               pointer_type;
00218 
00219 private:
00220     typedef ss_typename_type_k stlsoft::base_type_traits<Target>::base_type target_base_type_;
00221     static target_base_type_* manage_const(target_base_type_* p)
00222     {
00223         return p;
00224     }
00225 #if defined(STLSOFT_COMPILER_IS_BORLAND)
00226     static target_base_type_* manage_const(target_base_type_ const* p)
00227     {
00228         return const_cast<target_base_type_*>(p);
00229     }
00230 #else 
00231     static target_base_type_ const* manage_const(target_base_type_ const* p)
00232     {
00233         return p;
00234     }
00235 #endif 
00236 
00237 public:
00239     template <ss_typename_param_k Source>
00240     ptr_cast(Source &s)
00241         : m_p(manage_const(&dynamic_cast<Target>(s)))
00242     {
00243         
00244     }
00245 
00247     template <ss_typename_param_k Source>
00248 #if defined(STLSOFT_COMPILER_IS_BORLAND) || \
00249     defined(STLSOFT_COMPILER_IS_GCC) || \
00250     defined(STLSOFT_COMPILER_IS_MWERKS)
00251     ptr_cast(Source*& s)
00252 #else 
00253     ptr_cast(Source* s)
00254 #endif 
00255         : m_p(manage_const(dynamic_cast<Target>(s)))
00256     {
00257         if(NULL == m_p)
00258         {
00259             STLSOFT_THROW_X(stlsoft_ns_qual_std(bad_cast)());
00260         }
00261     }
00262 
00263 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00264     ptr_cast(pointer_type pt)
00265         : m_p(pt)
00266     {}
00267     ptr_cast(reference_type t)
00268         : m_p(&t)
00269     {}
00270 #endif 
00271 
00272 public:
00274     operator reference_type () const
00275     {
00276         return const_cast<reference_type>(*m_p);
00277     }
00279     operator pointer_type () const
00280     {
00281         return const_cast<pointer_type>(m_p);
00282     }
00283 
00284 private:
00285     pointer_type  m_p;
00286 
00287 
00288 private:
00289     ptr_cast(class_type const&);
00290 };
00291 
00293 
00294 
00295 #ifdef STLSOFT_UNITTEST
00296 # include "./unittest/ptr_cast_unittest_.h"
00297 #endif 
00298 
00299 
00300 
00301 #ifndef _STLSOFT_NO_NAMESPACE
00302 } 
00303 #endif 
00304 
00305 
00306 
00307 #endif 
00308 
00309