7 #ifndef __core_Castable_h__ 8 #define __core_Castable_h__ 13 #define FE_CASTABLE_DEBUG FALSE 14 #define FE_CASTABLE_MAP FALSE 19 #define FE_CASTABLE_STATIC FE_USE_TEMPLATE_STATIC 20 #define FE_CASTABLE_STRING_KEY FALSE //* vs hash (if not FE_CASTABLE_STATIC) 24 typedef std::type_info fe_type_info;
27 inline const fe_type_info& getTypeId(
void)
34 class fe_type_info:
public String
37 const char* name(
void)
const {
return c_str(); }
39 bool before(
const fe_type_info& a_rOther)
const 40 {
return (strcmp(name(),a_rOther.name())<0); }
44 class fe_type_info_of:
public fe_type_info
52 inline const fe_type_info& getTypeId(
void)
54 static fe_type_info_of<T> info;
61 U64 value(
void)
const {
return m_value; }
68 class fe_type_hash_of:
public fe_type_hash
73 const String typeString=FE_TYPESTRING(T);
74 m_value=typeString.computeFowlerNollVo1();
82 inline U64 getTypeHash(
void)
84 static fe_type_hash_of<T> hash;
98 #if !FE_CASTABLE_STATIC 99 #if FE_CASTABLE_STRING_KEY 105 typedef void* IdType;
109 class CastMap:
public std::map<IdType,void*>
112 void set(
const IdType a_classId,
void* a_pPtr)
113 { operator[](a_classId)=a_pPtr; }
114 void* lookup(
const IdType a_classId)
const 116 CastMap<IdType,void*>::const_iterator it=
118 return (it==end()? NULL: it->second);
125 Casting(
const IdType a_classId,
void* a_pPtr):
126 m_classId(a_classId),
131 class CastMap:
public std::vector<Casting>
134 void set(
const IdType a_classId,
void* a_pPtr)
135 { push_back(Casting(a_classId,a_pPtr)); }
136 void* lookup(
const IdType a_classId)
const 138 const U32 count=size();
139 for(U32 index=0;index<count;index++)
141 const Casting& rCasting=operator[](index);
142 if(rCasting.m_classId == a_classId)
144 return rCasting.m_pPtr;
154 #if FE_CPLUSPLUS >= 201103L 167 #if !FE_CASTABLE_STATIC 168 #if FE_CASTABLE_STRING_KEY 169 void addCast(
const String& a_classId,
void* a_pPtr)
171 void addCast(
const IdType a_classId,
void* a_pPtr)
173 { m_castMap.set(a_classId,a_pPtr); }
175 void addCast(IdType a_classId,
void* a_pPtr)
176 { m_castMap.set(a_classId,a_pPtr); }
180 X* castTo(
void)
const;
196 #if !FE_CASTABLE_STATIC 200 #if FE_CASTABLE_DEBUG 201 #if FE_CASTABLE_STRING_KEY 202 feLog(
"CastableAs::CastableAs<%s> %p id \"%s\"\n",
203 FE_TYPESTRING(T).c_str(),
204 (T*)
this,classId().c_str());
206 feLog(
"CastableAs::CastableAs<%s> %p id %p\n",
207 FE_TYPESTRING(T).c_str(),(T*)
this,classId());
211 addCast(classId(),(T*)
this);
214 #if FE_CASTABLE_STRING_KEY 215 static const String& classId(
void) {
return getTypeId<T>(); }
217 static IdType classId(
void) {
return getTypeHash<T>(); }
225 #if FE_CASTABLE_DEBUG 226 feLog(
"CastableAs::CastableAs<%s> %p id %p\n",
227 FE_TYPESTRING(T).c_str(),(T*)
this,
id);
230 addCast(
id,(T*)
this);
233 static IdType classId(
void) {
return &ms_classId; }
236 static FE_DL_PUBLIC IdType ms_classId;
244 #if FE_CASTABLE_STATIC 249 inline X* Castable::castTo(
void)
const 251 #if FE_CASTABLE_DEBUG 252 #if !FE_CASTABLE_STATIC 253 #if FE_CASTABLE_STRING_KEY 254 feLog(
"Castable::castTo<%s> id \"%s\"\n",
257 feLog(
"Castable::castTo<%s> id %p\n",
261 for(CastMap::const_iterator it=m_castMap.begin();
262 it!=m_castMap.end();it++)
265 #if FE_CASTABLE_STRING_KEY 266 feLog(
" \"%s\" %p\n",it->first.c_str(),it->second);
268 feLog(
" %p %p\n",it->first,it->second);
271 feLog(
" %p %p\n",it->m_classId,it->m_pPtr);
275 feLog(
"Castable::castTo<%s> id %p\n",
277 for(CastMap::const_iterator it=m_castMap.begin();
278 it!=m_castMap.end();it++)
281 feLog(
" %p %p\n",it->first,it->second);
283 feLog(
" %p %p\n",it->m_classId,it->m_pPtr);
296 template<
class T,
class U>
297 inline T* fe_cast(U* a_pU)
298 {
return a_pU? a_pU->template castTo<T>(): NULL; }
300 template<
class T,
class U>
301 inline T* fe_cast(U* a_pU) {
return dynamic_cast<T*
>(a_pU); }
304 #endif // __core_Castable_h__ Base participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:93
String & operator=(const String &operand)
Compare to another String.
Definition: String.h:584
kernel
Definition: namespace.dox:3
Automatically reference-counted string container.
Definition: String.h:128
Per-class participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:192