Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PluginRegister.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLUtils/src/ICLUtils/PluginRegister.h                 **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/Function.h>
00034 #include <ICLUtils/TextTable.h>
00035 
00036 #include <string>
00037 #include <map>
00038 #include <sstream>
00039 
00040 namespace icl{
00041   namespace utils{
00042 
00044     template<class T>
00045     class PluginRegister{
00046       public:
00047       
00049       typedef std::map<std::string,std::string> Data;
00050       
00052       typedef utils::Function<T*,const Data&> CreateFunction;
00053 
00055       struct Plugin{
00056         std::string name;            
00057         std::string description;     
00058         std::string creationSyntax;  
00059         CreateFunction create;       
00060       };
00061       
00063       static PluginRegister<T> &instance(){
00064         static PluginRegister<T> inst;
00065         return inst;
00066       }
00067         
00069       inline void add(const std::string &name, CreateFunction create, 
00070                const std::string &description, const std::string &creationSyntax){
00071         Plugin p = { name, description, creationSyntax, create };
00072         plugins[name] = p;
00073       }
00074         
00076       inline T *createInstance(const std::string &name, const Data &data) 
00077         throw (utils::ICLException){
00078         typename std::map<std::string,Plugin>::iterator it = plugins.find(name);
00079         if(it == plugins.end()){
00080           std::ostringstream all;
00081           for(typename std::map<std::string,Plugin>::iterator jt = plugins.begin(); jt != plugins.end();){
00082             all << jt->first;
00083             if(++jt != plugins.end()) all << ",";
00084           }
00085           throw utils::ICLException("PluginRegister<T>: unknow type: '"+ name + "' (registered are: " +all.str() + ")");
00086           return 0;
00087         }
00088         return it->second.create(data);
00089       }
00090         
00092       inline std::string getRegisteredInstanceDescription(){
00093         utils::TextTable t(3,plugins.size()+1,40);
00094         t(0,0) = "ID";
00095         t(1,0) = "Description";
00096         t(2,0) = "Creation Syntax";
00097         int i=1;
00098         for(typename std::map<std::string,Plugin>::iterator it = plugins.begin(); 
00099             it != plugins.end(); ++it, ++i){
00100           t(0,i) = it->second.name;
00101           t(1,i) = it->second.description;
00102           t(2,i) = it->second.creationSyntax;
00103         }
00104         return t.toString();
00105       }
00106       
00108       const std::map<std::string,Plugin> &getRegisteredPlugins() const{
00109         return plugins;
00110       }
00111 
00112       private:
00114       PluginRegister(){}
00115       
00117       std::map<std::string,Plugin> plugins;
00118       
00119     };
00120   }
00121 }
00122 
00123 #define REGISTER_PLUGIN(TYPE,NAME,CREATE_FUNCTION,DESCRIPTION,SYNTAX)   \
00124   struct Static_##TYPE##_PluginRegistration__##NAME{                    \
00125     Static_##TYPE##_PluginRegistration__##NAME(){                       \
00126       PluginRegister<TYPE> &r = PluginRegister<TYPE>::instance();       \
00127       r.add(#NAME,CREATE_FUNCTION,DESCRIPTION,SYNTAX);                  \
00128     }                                                                   \
00129   } static_##TYPE##_PluginRegistration__##NAME;
00130 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines