00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef OB_PLUGIN_H
00020 #define OB_PLUGIN_H
00021
00022 #include <openbabel/babelconfig.h>
00023 #include <string>
00024 #include <iostream>
00025 #include <vector>
00026 #include <map>
00027 #include <sstream>
00028 #include <cstring>
00029
00030 #ifndef OBERROR
00031 #define OBERROR
00032 #endif
00033
00034 namespace OpenBabel
00035 {
00036
00038 struct OBERROR CharPtrLess : public std::binary_function<const char*,const char*, bool>
00039 {
00040 bool operator()(const char* p1,const char* p2) const
00041 { return strcasecmp(p1,p2)<0; }
00042 };
00043
00049 class OBERROR OBPlugin
00050 {
00051 public:
00052
00053
00054
00055
00056 typedef std::map<const char*, OBPlugin*, CharPtrLess> PluginMapType;
00057 typedef PluginMapType::const_iterator PluginIterator;
00058
00060 virtual ~OBPlugin(){};
00061
00063 virtual const char* Description() { return NULL;} ;
00064
00070 virtual bool Display(std::string&txt, const char* param, const char* ID=NULL);
00071
00076 virtual OBPlugin* MakeInstance(const std::vector<std::string>&){return NULL;}
00077
00079 static OBPlugin* GetPlugin(const char* Type, const char* ID)
00080 { return BaseFindType(GetTypeMap(Type), ID); }
00081
00083 const char* GetID()const{return _id;};
00084
00088 static bool ListAsVector(const char* PluginID, const char* param, std::vector<std::string>& vlist);
00089
00091 static void List(const char* PluginID, const char* param=NULL, std::ostream* os=&std::cout);
00092
00094 static std::string ListAsString(const char* PluginID, const char* param=NULL);
00095
00097 static std::string FirstLine(const char* txt);
00098
00101 static PluginIterator Begin(const char* PluginID)
00102 {
00103 if( !strcmp(PluginID, "plugins") || GetTypeMap(PluginID)!=PluginMap())
00104 return GetTypeMap(PluginID).begin();
00105 else
00106 return PluginMap().end();
00107 }
00108
00109 static PluginIterator End(const char* PluginID)
00110 {
00111 return GetTypeMap(PluginID).end();
00112 }
00113
00115 virtual PluginMapType& GetMap() const =0;
00116
00117 protected:
00120 static PluginMapType& PluginMap()
00121 {
00122 static PluginMapType m;
00123 return m;
00124 }
00125
00127 static PluginMapType& GetTypeMap(const char* PluginID);
00128
00131 static OBPlugin* BaseFindType(PluginMapType& Map, const char* ID)
00132 {
00133 PluginMapType::iterator itr = Map.find(ID);
00134 if(itr==Map.end())
00135 return NULL;
00136 else
00137 return itr->second;
00138 }
00139
00140 protected:
00141 const char* _id;
00142 };
00143
00144
00145 #define MAKE_PLUGIN(BaseClass)\
00146 protected:\
00147 virtual PluginMapType& GetMap()const{return Map();}\
00148 static PluginMapType& Map(){static PluginMapType m;return m;}\
00149 public:\
00150 static BaseClass*& Default(){static BaseClass* d;return d;}\
00151 BaseClass(const char* ID, bool IsDefault=false)\
00152 {_id=ID;if(ID&&*ID){if(IsDefault || Map().empty()) Default() = this;\
00153 Map()[ID]=this;PluginMap()[TypeID()] =this;}}\
00154 static BaseClass* FindType(const char* ID)\
00155 {if(!ID || *ID==0) return Default();\
00156 return static_cast<BaseClass*>(BaseFindType(Map(),ID));}
00157
00286
00295 }
00296
00297 #endif