CLAM-Development
1.1
|
00001 #ifndef ProcessingDataPlugin_hxx 00002 #define ProcessingDataPlugin_hxx 00003 00004 #include <string> 00005 #include <cstdlib> 00006 #include <map> 00007 #include <list> 00008 #include <typeinfo> 00009 #include <iostream> 00010 00011 #ifdef __GNUC__ 00012 #include <cxxabi.h> 00013 #endif//__GNUC__ 00014 namespace CLAM 00015 { 00016 00017 class ProcessingDataPlugin 00018 { 00019 public: 00020 typedef std::string Key; 00021 typedef std::type_info Type; 00022 typedef std::map<Key,ProcessingDataPlugin *> TypeMap; 00023 private: 00024 const Type & _type; 00025 std::string _color; 00026 std::string _displayName; 00027 std::string _name; 00028 static std::string demangle(const std::string & mangledName) 00029 { 00030 std::string result = mangledName; 00031 #ifdef __GNUC__ 00032 int demangleError = 0; 00033 char * demangled = abi::__cxa_demangle(mangledName.c_str(),0,0,&demangleError); 00034 if (!demangleError && demangled) 00035 result = demangled; 00036 if (demangled) free(demangled); 00037 #endif//__GNUC__ 00038 return result; 00039 } 00040 ProcessingDataPlugin(const std::type_info & type, const std::string & color, const std::string & displayName) 00041 : _type(type) 00042 , _color(color) 00043 { 00044 _name = _displayName = type.name(); 00045 _displayName = displayName.empty()? 00046 demangle(_name) : displayName; 00047 // std::cout << "Adding TypePlugin " << _name << " shown as " << _displayName << " with color " << color << std::endl; 00048 getTypeMap().insert(std::make_pair(_name, this)); 00049 } 00050 public: 00051 const std::string & color() const { return _color; } 00052 const std::string & name() const { return _name; } 00053 const std::string & displayName() const { return _displayName; } 00054 private: 00055 static TypeMap & getTypeMap(); 00056 public: 00057 static std::list<std::string> types() 00058 { 00059 std::list<std::string> result; 00060 for (TypeMap::iterator it=getTypeMap().begin(); 00061 it!=getTypeMap().end(); it++) 00062 { 00063 result.push_back(it->first); 00064 } 00065 return result; 00066 } 00067 static ProcessingDataPlugin * lookUp(const Type & type) 00068 { 00069 TypeMap::iterator it = getTypeMap().find(type.name()); 00070 if (it==getTypeMap().end()) return 0; 00071 return it->second; 00072 } 00073 static std::string colorFor(const std::type_info & type) 00074 { 00075 CLAM::ProcessingDataPlugin * plugin = lookUp(type); 00076 if (plugin) return plugin->color(); 00077 return ""; 00078 } 00079 static std::string displayNameFor(const std::type_info & type) 00080 { 00081 CLAM::ProcessingDataPlugin * plugin = lookUp(type); 00082 if (plugin) return plugin->displayName(); 00083 return demangle(type.name()); 00084 } 00085 00086 public: 00087 template <typename DataType> 00088 class Registrator 00089 { 00090 ProcessingDataPlugin * _plugin; 00091 public: 00092 Registrator(const std::string & color, const std::string & displayName="") 00093 : _plugin( new ProcessingDataPlugin(typeid(DataType), color, displayName)) 00094 { 00095 } 00096 }; 00097 }; 00098 00099 } 00100 00101 00102 #endif//ProcessingDataPlugin_hxx 00103