00001 #ifndef rgrl_cast_h_ 00002 #define rgrl_cast_h_ 00003 //: 00004 // \file 00005 // \author Amitha Perera 00006 // \brief Cast operations to simplify downcasting in the hierarchy. 00007 00008 #include "rgrl_transformation_sptr.h" 00009 #include "rgrl_converge_status_sptr.h" 00010 #include "rgrl_invariant_sptr.h" 00011 #include "rgrl_feature_sptr.h" 00012 #include "rgrl_feature_set_sptr.h" 00013 #include "rgrl_mask_sptr.h" 00014 #include <vcl_cassert.h> 00015 00016 //: Cast down the hierarchy. 00017 // 00018 // This does a dynamic_cast and then asserts that the result is not 00019 // null. Therefore, you are guaranteed that the result is a valid 00020 // pointer, or else the program will halt. 00021 // 00022 // Example of usage: 00023 // \code 00024 // rgrl_transformation_sptr trans = new rgrl_trans_affine(...); 00025 // rgrl_trans_affine* aff = rgrl_cast<rgrl_trans_affine*>(trans); 00026 // \endcode 00027 // 00028 template<typename ToType> 00029 class rgrl_cast 00030 { 00031 public: 00032 //: Downcast a feature object 00033 rgrl_cast( rgrl_feature_sptr feature ) 00034 { 00035 ptr = dynamic_cast<ToType>( feature.as_pointer() ); 00036 assert ( ptr ); 00037 } 00038 00039 //: Downcast a feature object 00040 rgrl_cast( rgrl_feature_set_sptr feature_set ) 00041 { 00042 ptr = dynamic_cast<ToType>( feature_set.as_pointer() ); 00043 assert ( ptr ); 00044 } 00045 00046 //: Downcast a transformation object. 00047 rgrl_cast( rgrl_transformation_sptr trans ) 00048 { 00049 ptr = dynamic_cast<ToType>( trans.as_pointer() ); 00050 assert ( ptr ); 00051 } 00052 00053 //: Downcast a convergence status object. 00054 rgrl_cast( rgrl_converge_status_sptr status ) 00055 { 00056 ptr = dynamic_cast<ToType>( status.as_pointer() ); 00057 assert ( ptr ); 00058 } 00059 00060 //: Downcast an invariant feature object. 00061 rgrl_cast( rgrl_invariant_sptr invariant ) 00062 { 00063 ptr = dynamic_cast<ToType>( invariant.as_pointer() ); 00064 assert ( ptr ); 00065 } 00066 00067 //: 00068 operator ToType() const 00069 { 00070 return reinterpret_cast<ToType>(ptr); 00071 } 00072 00073 //: 00074 ToType operator->() const 00075 { 00076 return reinterpret_cast<ToType>(ptr); 00077 } 00078 00079 //: Downcast a mask object 00080 rgrl_cast( rgrl_mask_sptr mask ) 00081 { 00082 ptr = dynamic_cast<ToType>( mask.as_pointer() ); 00083 assert ( ptr ); 00084 } 00085 00086 private: 00087 void *ptr; 00088 }; 00089 00090 00091 //: Cast down the hierarchy. 00092 // 00093 // This does a dynamic_cast and then asserts that the result is not 00094 // null. Therefore, you are guaranteed that the result is a valid 00095 // pointer, or else the program will halt. 00096 // 00097 // Example of usage: 00098 // \code 00099 // rgrl_transformation_sptr trans = new rgrl_trans_affine(...); 00100 // const rgrl_trans_affine* aff = rgrl_const_cast<rgrl_trans_affine*>(trans); 00101 // \endcode 00102 // 00103 template<typename ToType> 00104 class rgrl_const_cast 00105 { 00106 public: 00107 //: Downcast a feature object 00108 rgrl_const_cast( rgrl_feature_sptr const& feature ) 00109 { 00110 ptr = dynamic_cast<const ToType>( feature.as_pointer() ); 00111 assert ( ptr ); 00112 } 00113 00114 //: Downcast a feature object 00115 rgrl_const_cast( rgrl_feature_set_sptr const& feature_set ) 00116 { 00117 ptr = dynamic_cast<const ToType>( feature_set.as_pointer() ); 00118 assert ( ptr ); 00119 } 00120 00121 //: Downcast a transformation object. 00122 rgrl_const_cast( rgrl_transformation_sptr const& trans ) 00123 { 00124 ptr = dynamic_cast<const ToType>( trans.as_pointer() ); 00125 assert ( ptr ); 00126 } 00127 00128 //: Downcast a convergence status object. 00129 rgrl_const_cast( rgrl_converge_status_sptr const& status ) 00130 { 00131 ptr = dynamic_cast<const ToType>( status.as_pointer() ); 00132 assert ( ptr ); 00133 } 00134 00135 //: Downcast an invariant feature object. 00136 rgrl_const_cast( rgrl_invariant_sptr const& invariant ) 00137 { 00138 ptr = dynamic_cast<const ToType>( invariant.as_pointer() ); 00139 assert ( ptr ); 00140 } 00141 00142 //: 00143 operator const ToType() const 00144 { 00145 return reinterpret_cast<const ToType>(const_cast<void*>(ptr)); 00146 } 00147 00148 //: 00149 const ToType operator->() const 00150 { 00151 return reinterpret_cast<const ToType>(const_cast<void*>(ptr)); 00152 } 00153 00154 private: 00155 void *ptr; 00156 }; 00157 00158 #endif // rgrl_cast_h_