00001
00002
00003 #ifndef CONVERSION_H
00004 #define CONVERSION_H
00005
00006
00007 #include "typemap.h"
00008
00009
00010 typedef ObjectRef (*conv_func)(ObjectRef);
00011
00012 class Conversion {
00013 static TypeMap<TypeMap<conv_func> > &conv_table();
00014 public:
00015 template<class T, class U>
00016 static int addConvFunction(conv_func func)
00017 {
00018 conv_table()[&typeid(T)][&typeid(U)] = func;
00019 return 0;
00020 }
00021
00022 template <class T>
00023 static ObjectRef convertTo(ObjectRef x)
00024 {
00025 TypeMap<TypeMap<conv_func> >::iterator it = conv_table().find(&typeid(*x));
00026 if (it != conv_table().end())
00027 {
00028 TypeMap<conv_func>::iterator it2 = it->second.find(&typeid(T));
00029 if (it2 != it->second.end())
00030 {
00031 return it2->second(x);
00032 } else {
00033 std::cerr << "Cannot cast this to type requested\nThis needs to throw an exception\n";
00034 return nilObject;
00035 }
00036 } else {
00037 std::cerr << "Cannot cast\nThis needs to throw an exception\n";
00038 return nilObject;
00039 }
00040 }
00041 };
00042
00043
00044 template<class T, class U>
00045 class dummy_conv_table_init_class {
00046 static int dummy_var;
00047 };
00048
00049 #define REGISTER_CONVERSION(from, to, func) \
00050 template<> \
00051 int dummy_conv_table_init_class<from,to >::dummy_var = \
00052 Conversion::addConvFunction<from,to >(func);
00053
00054 #define REGISTER_CONVERSION_TEMPLATE(from, to, func) \
00055 template<> \
00056 int dummy_conv_table_init_class<from,to >::dummy_var = \
00057 Conversion::addConvFunction<from,to >(func<from,to >);
00058
00059
00060 #endif