00001
00002
00003 #ifndef OBJECTREF_H
00004 #define OBJECTREF_H
00005
00006
00007 #include "Object.h"
00008 #include "net_types.h"
00009 #include "BaseException.h"
00010 #include <typeinfo>
00011
00012 namespace FD {
00013
00019 template <class T>
00020 class CastException : public GenericCastException {
00021 public:
00023 CastException(std::string _type) : type(_type)
00024 {}
00025
00027 virtual void print(std::ostream &out = std::cerr)
00028 {
00029 out << "Cast error: Trying to cast ObjectRef (" << type << ") into " << typeid(T).name() << std::endl;
00030 }
00031
00032 protected:
00034 std::string type;
00035 };
00036
00038 template <class T>
00039 inline T object_ptr_cast (const ObjectRef &ref)
00040 {
00041 T tmp = dynamic_cast<T>(&(*ref));
00042 if (!tmp)
00043 throw new CastException<T> (typeid ((*ref)).name());
00044 return tmp;
00045 }
00046
00048 template <class T>
00049 inline T &object_cast (const ObjectRef &ref)
00050 {
00051 T *tmp = dynamic_cast<T *>(&(*ref));
00052 if (!tmp)
00053 throw new CastException<T> (typeid ((*ref)).name());
00054 return *tmp;
00055 }
00056
00058 template <class T>
00059 inline T &dereference_cast (const ObjectRef &ref)
00060 {
00061 GenericType<T> *tmp = (dynamic_cast<GenericType<T> * >(&(*ref)));
00062 if (!tmp)
00063 throw new CastException<T> (typeid ((*ref)).name());
00064 return tmp->val();
00065 }
00066
00067
00069 template <class T>
00070 inline bool &object_kind_of (const ObjectRef &ref)
00071 {
00072 return dynamic_cast<T *>(&(*ref));
00073 }
00074
00075
00077 template <class T>
00078 inline bool &object_has_type (const ObjectRef &ref)
00079 {
00080 return typeid(*ref) == typeid(T);
00081 }
00082
00083 }
00084
00085 #endif