CLAM-Development  1.4.0
ProcessingDataPlugin.hxx
Go to the documentation of this file.
1 #ifndef ProcessingDataPlugin_hxx
2 #define ProcessingDataPlugin_hxx
3 
4 #include <string>
5 #include <cstdlib>
6 #include <map>
7 #include <list>
8 #include <typeinfo>
9 #include <iostream>
10 
11 #ifdef __GNUC__
12 #include <cxxabi.h>
13 #endif//__GNUC__
14 namespace CLAM
15 {
16 
18 {
19 public:
20  typedef std::string Key;
21  typedef std::type_info Type;
22  typedef std::map<Key,ProcessingDataPlugin *> TypeMap;
23 private:
24  const Type & _type;
25  std::string _color;
26  std::string _displayName;
27  std::string _name;
28  static std::string demangle(const std::string & mangledName)
29  {
30  std::string result = mangledName;
31  #ifdef __GNUC__
32  int demangleError = 0;
33  char * demangled = abi::__cxa_demangle(mangledName.c_str(),0,0,&demangleError);
34  if (!demangleError && demangled)
35  result = demangled;
36  if (demangled) free(demangled);
37  #endif//__GNUC__
38  return result;
39  }
40  ProcessingDataPlugin(const std::type_info & type, const std::string & color, const std::string & displayName)
41  : _type(type)
42  , _color(color)
43  {
44  _name = _displayName = type.name();
45  _displayName = displayName.empty()?
46  demangle(_name) : displayName;
47 // std::cout << "Adding TypePlugin " << _name << " shown as " << _displayName << " with color " << color << std::endl;
48  getTypeMap().insert(std::make_pair(_name, this));
49  }
50  ~ProcessingDataPlugin()
51  {
52  if (lookUp(_type)==this) getTypeMap().erase(_type.name());
53  }
54 public:
55  const std::string & color() const { return _color; }
56  const std::string & name() const { return _name; }
57  const std::string & displayName() const { return _displayName; }
58 private:
59  static TypeMap & getTypeMap();
60 public:
61  static std::list<std::string> types()
62  {
63  std::list<std::string> result;
64  for (TypeMap::iterator it=getTypeMap().begin();
65  it!=getTypeMap().end(); it++)
66  {
67  result.push_back(it->first);
68  }
69  return result;
70  }
71  static ProcessingDataPlugin * lookUp(const Type & type)
72  {
73  TypeMap::iterator it = getTypeMap().find(type.name());
74  if (it==getTypeMap().end()) return 0;
75  return it->second;
76  }
77  static std::string colorFor(const std::type_info & type)
78  {
79  CLAM::ProcessingDataPlugin * plugin = lookUp(type);
80  if (plugin) return plugin->color();
81  return "";
82  }
83  static std::string displayNameFor(const std::type_info & type)
84  {
85  CLAM::ProcessingDataPlugin * plugin = lookUp(type);
86  if (plugin) return plugin->displayName();
87  return demangle(type.name());
88  }
89 
90 public:
91  template <typename DataType>
93  {
94  ProcessingDataPlugin * _plugin;
95  public:
96  Registrator(const std::string & color, const std::string & displayName="")
97  : _plugin( new ProcessingDataPlugin(typeid(DataType), color, displayName))
98  {
99  }
100  ~Registrator() { delete _plugin; }
101  };
102 };
103 
104 }
105 
106 
107 #endif//ProcessingDataPlugin_hxx
108