]> Creatis software - bbtk.git/blob - kernel/src/bbtkRTTI.cxx
ae8d18ea3062561619a24e50f7ab893c3feb5a61
[bbtk.git] / kernel / src / bbtkRTTI.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkRTTI.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/05/28 08:12:06 $
6   Version:   $Revision: 1.4 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31
32 #include "bbtkRTTI.h"
33 #include "bbtkMessageManager.h"
34
35
36  
37 namespace bbtk
38 {
39   // Adapted from https://savannah.cern.ch/support/download.php?file_id=1972
40   
41   // Something to which we can cast the void*s, to allow finding the
42   // dynamic typeid.
43   struct Dummy {
44     virtual ~Dummy() {}
45   };
46   
47
48  void*  run_time_up_or_down_cast( const std::type_info& target_type,
49                                const std::type_info& source_type,
50                                const void*  source_pointer
51                                )
52  {
53    return run_time_up_or_down_cast(target_type,
54                                    source_type,
55                                    const_cast<void*>(source_pointer));
56  }
57
58   void*   run_time_up_or_down_cast( const std::type_info& target_type,
59                                const std::type_info& source_type,
60                                void*  source_pointer
61                                )
62   {
63     bbtkDebugMessage("data",5,
64                      "run_time_up_or_down_cast : Casting pointer to '" 
65                      << TypeName(typeid(*(Dummy*)source_pointer)) 
66                      << "' from " << TypeName(source_type) 
67                      << " to " << TypeName(target_type) << std::endl);
68
69     void* target_pointer = 0;
70 #if __GNUC__ > 3 ||                                     \
71   (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 ||              \
72                      (__GNUC_MINOR__ == 1 &&            \
73                       __GNUC_PATCHLEVEL__ > 0)))
74     
75     const abi::__class_type_info* targetTI = 
76       (const abi::__class_type_info *)( &(target_type));
77     
78     bbtkDebugMessage("data",7," * source   = "<<source_pointer<<std::endl);
79
80     void* tmp = source_pointer;
81     if (source_type.__do_upcast(targetTI,&tmp)) 
82       {
83         target_pointer = tmp;
84       }
85     else 
86       {
87         bbtkDebugMessage("data",7,
88                          " * upcast failed : trying dynamic down cast"
89                          <<std::endl);
90         const abi::__class_type_info* sourceTI = 
91           (const abi::__class_type_info *)( &(source_type));
92         
93         
94         target_pointer = abi::__dynamic_cast(source_pointer, 
95                                              sourceTI, 
96                                              targetTI, 
97                                              -1);   
98       }
99     
100     bbtkDebugMessage("data",7," * target   = "<<target_pointer<<std::endl);
101     
102 #else
103     bbtkWarning("run_time_up_or_down_cast not impl. on Win : to do");
104     // target_pointer = __RTDynamicCast(source_pointer, 0, source_type, target_type, 0);
105 #endif
106     return target_pointer;
107     
108   }
109
110 }
111