Image Component Library (ICL)
PluginRegister.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/PluginRegister.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/Function.h>
34 #include <ICLUtils/TextTable.h>
35 
36 #include <string>
37 #include <map>
38 #include <sstream>
39 
40 namespace icl{
41  namespace utils{
42 
44  template<class T>
46  public:
47 
49  typedef std::map<std::string,std::string> Data;
50 
53 
55  struct Plugin{
56  std::string name;
57  std::string description;
58  std::string creationSyntax;
60  };
61 
64  static PluginRegister<T> inst;
65  return inst;
66  }
67 
69  inline void add(const std::string &name, CreateFunction create,
70  const std::string &description, const std::string &creationSyntax){
71  Plugin p = { name, description, creationSyntax, create };
72  plugins[name] = p;
73  }
74 
76  inline T *createInstance(const std::string &name, const Data &data)
77  {
78  typename std::map<std::string,Plugin>::iterator it = plugins.find(name);
79  if(it == plugins.end()){
80  std::ostringstream all;
81  for(typename std::map<std::string,Plugin>::iterator jt = plugins.begin(); jt != plugins.end();){
82  all << jt->first;
83  if(++jt != plugins.end()) all << ",";
84  }
85  throw utils::ICLException("PluginRegister<T>: unknow type: '"+ name + "' (registered are: " +all.str() + ")");
86  return 0;
87  }
88  return it->second.create(data);
89  }
90 
92  inline std::string getRegisteredInstanceDescription(){
93  utils::TextTable t(3,plugins.size()+1,40);
94  t(0,0) = "ID";
95  t(1,0) = "Description";
96  t(2,0) = "Creation Syntax";
97  int i=1;
98  for(typename std::map<std::string,Plugin>::iterator it = plugins.begin();
99  it != plugins.end(); ++it, ++i){
100  t(0,i) = it->second.name;
101  t(1,i) = it->second.description;
102  t(2,i) = it->second.creationSyntax;
103  }
104  return t.toString();
105  }
106 
108  const std::map<std::string,Plugin> &getRegisteredPlugins() const{
109  return plugins;
110  }
111 
112  private:
115 
117  std::map<std::string,Plugin> plugins;
118 
119  };
120  }
121 }
122 
123 #define REGISTER_PLUGIN(TYPE,NAME,CREATE_FUNCTION,DESCRIPTION,SYNTAX) \
124  struct Static_##TYPE##_PluginRegistration__##NAME{ \
125  Static_##TYPE##_PluginRegistration__##NAME(){ \
126  PluginRegister<TYPE> &r = PluginRegister<TYPE>::instance(); \
127  r.add(#NAME,CREATE_FUNCTION,DESCRIPTION,SYNTAX); \
128  } \
129  } static_##TYPE##_PluginRegistration__##NAME;
130 
const std::map< std::string, Plugin > & getRegisteredPlugins() const
returns a all registered plugins
Definition: PluginRegister.h:108
PluginRegister()
private constructor (use singelton creator function instance())
Definition: PluginRegister.h:114
undocument this line if you encounter any issues!
Definition: Any.h:37
ICLQt_API core::Img< T > create(const std::string &name, core::format fmt=icl::core::formatRGB)
create a test image (converted to destination core::format) (affinity for floats)
static PluginRegister< T > & instance()
returns the static instance for the specific type
Definition: PluginRegister.h:63
T * createInstance(const std::string &name, const Data &data)
creates an instance (or throws
Definition: PluginRegister.h:76
CreateFunction create
factory function
Definition: PluginRegister.h:59
std::string description
description
Definition: PluginRegister.h:57
ICLQt_API ImgROI data(ImgQ &r)
creates full ROI ROI-struct
std::map< std::string, Plugin > plugins
internal plugin list
Definition: PluginRegister.h:117
std::map< std::string, std::string > Data
data used for instance creation
Definition: PluginRegister.h:49
std::string creationSyntax
syntax used for creation
Definition: PluginRegister.h:58
utils::Function< T *, const Data & > CreateFunction
creator function for instances
Definition: PluginRegister.h:52
Utility class for plugin registration.
Definition: PluginRegister.h:45
Utility class for pretty console output.
Definition: TextTable.h:85
std::string getRegisteredInstanceDescription()
returns a string representation of all registered types
Definition: PluginRegister.h:92
std::string toString() const
serializes the table to a std::string
void add(const std::string &name, CreateFunction create, const std::string &description, const std::string &creationSyntax)
adds a new plugin
Definition: PluginRegister.h:69
Base class for Exception handling in the ICL.
Definition: Exception.h:42
internally used instance type:
Definition: PluginRegister.h:55
std::string name
instance ID
Definition: PluginRegister.h:56