• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files

plugin.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 plugin.h - facilitates construction of plugin classes
00003  
00004 Copyright (C) 2007 by Chris Morley
00005  
00006 This file is part of the Open Babel project.
00007 For more information, see <http://openbabel.sourceforge.net/>
00008  
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation version 2 of the License.
00012  
00013 This program is distributed in the hope that it will be useful, but
00014 WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 General Public License for more details.
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   //Maps of thistype are used to store 
00054   // (a)a list of the plugin types in OBPlugin, and
00055   // (b)a list of the sub-types in each type class derived from OBPlugin.
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 //Macro to be added to definition of the base class
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 } // end namespce
00296 
00297 #endif

This file is part of the documentation for Open Babel, version 2.2.0.

Documentation copyright © 1998-2007, the Open Babel Developers.
Open Babel is hosted by: SourceForge Logo
Generated on Thu Jul 3 14:30:34 2008 by doxygen 1.5.6.