bbtkRTTI.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkRTTI.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:14 $
00006   Version:   $Revision: 1.3 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00031 
00032 #include "bbtkRTTI.h"
00033 #include "bbtkMessageManager.h"
00034 
00035 
00036  
00037 namespace bbtk
00038 {
00039   // Adapted from https://savannah.cern.ch/support/download.php?file_id=1972
00040   
00041   // Something to which we can cast the void*s, to allow finding the
00042   // dynamic typeid.
00043   struct Dummy {
00044     virtual ~Dummy() {}
00045   };
00046   
00047 
00048  void*  run_time_up_or_down_cast( const std::type_info& target_type,
00049                                const std::type_info& source_type,
00050                                const void*  source_pointer
00051                                )
00052  {
00053    return run_time_up_or_down_cast(target_type,
00054                                    source_type,
00055                                    const_cast<void*>(source_pointer));
00056  }
00057 
00058   void*   run_time_up_or_down_cast( const std::type_info& target_type,
00059                                const std::type_info& source_type,
00060                                void*  source_pointer
00061                                )
00062   {
00063     bbtkDebugMessage("Data",5,
00064                      "run_time_up_or_down_cast : Casting pointer to '" 
00065                      << TypeName(typeid(*(Dummy*)source_pointer)) 
00066                      << "' from " << TypeName(source_type) 
00067                      << " to " << TypeName(target_type) << std::endl);
00068 
00069     void* target_pointer = 0;
00070 #if __GNUC__ > 3 ||                                     \
00071   (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 ||              \
00072                      (__GNUC_MINOR__ == 1 &&            \
00073                       __GNUC_PATCHLEVEL__ > 0)))
00074     
00075     const abi::__class_type_info* targetTI = 
00076       (const abi::__class_type_info *)( &(target_type));
00077     
00078     bbtkDebugMessage("Data",7," * source   = "<<source_pointer<<std::endl);
00079 
00080     void* tmp = source_pointer;
00081     if (source_type.__do_upcast(targetTI,&tmp)) 
00082       {
00083         target_pointer = tmp;
00084       }
00085     else 
00086       {
00087         bbtkDebugMessage("Data",7,
00088                          " * upcast failed : trying dynamic down cast"
00089                          <<std::endl);
00090         const abi::__class_type_info* sourceTI = 
00091           (const abi::__class_type_info *)( &(source_type));
00092         
00093         
00094         target_pointer = abi::__dynamic_cast(source_pointer, 
00095                                              sourceTI, 
00096                                              targetTI, 
00097                                              -1);   
00098       }
00099     
00100     bbtkDebugMessage("Data",7," * target   = "<<target_pointer<<std::endl);
00101     
00102 #else
00103     bbtkWarning("run_time_up_or_down_cast not impl. on Win : to do");
00104     // target_pointer = __RTDynamicCast(source_pointer, 0, source_type, target_type, 0);
00105 #endif
00106     return target_pointer;
00107     
00108   }
00109 
00110 }
00111 

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6