Open Babel  3.0
plugin.h
Go to the documentation of this file.
1 /**********************************************************************
2 plugin.h - facilitates construction of plugin classes
3 
4 Copyright (C) 2007 by Chris Morley
5 
6 This file is part of the Open Babel project.
7 For more information, see <http://openbabel.org/>
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation version 2 of the License.
12 
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17 ***********************************************************************/
18 
19 #ifndef OB_PLUGIN_H
20 #define OB_PLUGIN_H
21 
22 #include <openbabel/babelconfig.h>
23 #include <openbabel/dlhandler.h>
24 #include <string>
25 #include <iostream>
26 #include <vector>
27 #include <map>
28 #include <sstream>
29 #include <cstring>
30 
31 #ifndef OBERROR
32  #define OBERROR
33 #endif
34 
35 namespace OpenBabel
36 {
39 
41 struct OBERROR CharPtrLess : public std::binary_function<const char*,const char*, bool>
42 {
43  bool operator()(const char* p1,const char* p2) const
44  { return strcasecmp(p1,p2)<0; }
45 };
46 
52 class OBERROR OBPlugin
53 {
54 public:
55 
56  //Maps of thistype are used to store
57  // (a)a list of the plugin types in OBPlugin, and
58  // (b)a list of the sub-types in each type class derived from OBPlugin.
59  typedef std::map<const char*, OBPlugin*, CharPtrLess> PluginMapType;
60  typedef PluginMapType::const_iterator PluginIterator;
61 
63  virtual ~OBPlugin(){};
64 
66  virtual const char* Description() { return NULL;} ;
67 
69  virtual const char* TypeID(){ return "plugins"; }
70 
76  virtual bool Display(std::string&txt, const char* param, const char* ID=NULL);
77 
82  virtual OBPlugin* MakeInstance(const std::vector<std::string>&){return NULL;}
83 
86  virtual void Init(){};
87 
90  static OBPlugin* GetPlugin(const char* Type, const char* ID);
91 
93  const char* GetID()const{return _id;};
94 
98  static bool ListAsVector(const char* PluginID, const char* param, std::vector<std::string>& vlist);
99 
101  static void List(const char* PluginID, const char* param=NULL, std::ostream* os=&std::cout);
102 
104  static std::string ListAsString(const char* PluginID, const char* param=NULL);
105 
107  static std::string FirstLine(const char* txt);
108 
111  static PluginIterator Begin(const char* PluginID)
112  {
113  if( !strcmp(PluginID, "plugins") || GetTypeMap(PluginID)!=PluginMap())
114  return GetTypeMap(PluginID).begin();
115  else
116  return PluginMap().end();
117  }
118 
119  static PluginIterator End(const char* PluginID)
120  {
121  return GetTypeMap(PluginID).end();
122  }
123 
125  virtual PluginMapType& GetMap() const =0;
126 
128  static void LoadAllPlugins();
129 
130 protected:
133  static PluginMapType& PluginMap()
134  {
135  static PluginMapType m;
136  return m;
137  }
138 
140  static int AllPluginsLoaded;
141 
143  static PluginMapType& GetTypeMap(const char* PluginID);
144 
147  static OBPlugin* BaseFindType(PluginMapType& Map, const char* ID);
148 
149 protected:
150  const char* _id;
151 };
152 
153 #if defined(__CYGWIN__) || defined(__MINGW32__)
154 
155 //Macro to be added to definition of the base class
156 #define MAKE_PLUGIN(BaseClass)\
157 protected:\
158  static PluginMapType& Map();\
159  virtual PluginMapType& GetMap() const {\
160  return Map();\
161  }\
162 public:\
163  static BaseClass*& Default() {\
164  static BaseClass* d;\
165  return d;\
166  }\
167  BaseClass(const char* ID, bool IsDefault=false) {\
168  _id=ID;\
169  if (ID&&*ID) {\
170  if (IsDefault || Map().empty()) {\
171  Default() = this;\
172  }\
173  if (Map().count(ID) == 0) {\
174  Map()[ID] = this;\
175  PluginMap()[TypeID()] = this;\
176  }\
177  }\
178  }\
179  static BaseClass* FindType(const char* ID) {\
180  if (!ID || *ID==0 || *ID==' ') {\
181  return Default();\
182  }\
183  return static_cast<BaseClass*>(BaseFindType(Map(),ID));\
184  }
185 
186 #define PLUGIN_CPP_FILE(BaseClass)\
187 OBPlugin::PluginMapType& BaseClass::Map() {\
188  static OBPlugin::PluginMapType map;\
189  return map;\
190 }
191 
192 #else // __CYGWIN__ || __MINGW32__
193 
194 //Macro to be added to definition of the base class
195 #define MAKE_PLUGIN(BaseClass)\
196 protected:\
197  static PluginMapType& Map() {\
198  static PluginMapType m;\
199  return m;\
200  }\
201  virtual PluginMapType& GetMap() const {\
202  return Map();\
203  }\
204 public:\
205  static BaseClass*& Default() {\
206  static BaseClass* d;\
207  return d;\
208  }\
209  BaseClass(const char* ID, bool IsDefault=false) {\
210  _id=ID;\
211  if (ID&&*ID) {\
212  if (IsDefault || Map().empty()) {\
213  Default() = this;\
214  }\
215  if (Map().count(ID) == 0) {\
216  Map()[ID] = this;\
217  PluginMap()[TypeID()] = this;\
218  }\
219  }\
220  }\
221  static BaseClass* FindType(const char* ID) {\
222  if (!ID || *ID==0 || *ID==' ') {\
223  return Default();\
224  }\
225  return static_cast<BaseClass*>(BaseFindType(Map(),ID));\
226  }
227 
228 #endif // __CYGWIN__ || __MINGW32__
229 
358 
366 /*\@}*/
367 
368 #ifndef SWIG // Skipped by SWIG (for the moment)
369 
370 #ifndef USING_DYNAMIC_LIBS
371 
372 #define OB_STATIC_PLUGIN(className,instanceName) \
373  class className; \
374  OBAPI EXTERN className instanceName;
375 
376  // formats
377  OB_STATIC_PLUGIN(ABINITFormat, theABINITFormat)
378  OB_STATIC_PLUGIN(AcesOutputFormat, theAcesOutputFormat)
379  OB_STATIC_PLUGIN(AcesInputFormat, theAcesInputFormat)
380  OB_STATIC_PLUGIN(ACRFormat, theACRFormat)
381  OB_STATIC_PLUGIN(ADFOutputFormat, theADFOutputFormat)
382  OB_STATIC_PLUGIN(ADFInputFormat, theADFInputFormat)
383  OB_STATIC_PLUGIN(AlchemyFormat, theAlchemyFormat)
384  OB_STATIC_PLUGIN(AmberPrepFormat, theAmberPrepFormat)
385  OB_STATIC_PLUGIN(AoforceFormat, theAoforceFormat)
386  OB_STATIC_PLUGIN(OBAPIInterface, theOBAPIInterface)
387  OB_STATIC_PLUGIN(BallStickFormat, theBallStickFormat)
388  OB_STATIC_PLUGIN(BGFFormat, theBGFFormat)
389  OB_STATIC_PLUGIN(BoxFormat, theBoxFormat)
390  OB_STATIC_PLUGIN(CacaoFormat, theCacaoFormat)
391  OB_STATIC_PLUGIN(CacheFormat, theCacheFormat)
392  OB_STATIC_PLUGIN(CARFormat, theCARFormat)
393  OB_STATIC_PLUGIN(CASTEPFormat, theCASTEPFormat)
394  OB_STATIC_PLUGIN(CCCFormat, theCCCFormat)
395  OB_STATIC_PLUGIN(CHEM3D1Format, theCHEM3D1Format)
396  OB_STATIC_PLUGIN(CHEM3D2Format, theCHEM3D2Format)
397  OB_STATIC_PLUGIN(ChemDrawBinaryXFormat, theChemDrawBinaryXFormat)
398  OB_STATIC_PLUGIN(ChemDrawFormat, theChemDrawFormat)
399  OB_STATIC_PLUGIN(ChemKinFormat, theChemKinFormat)
400  OB_STATIC_PLUGIN(CHTFormat, theCHTFormat)
401  OB_STATIC_PLUGIN(CIFFormat, theCIFFormat)
402  OB_STATIC_PLUGIN(CopyFormat, theCopyFormat)
403  OB_STATIC_PLUGIN(CRK2DFormat, theCRK2DFormat)
404  OB_STATIC_PLUGIN(CRK3DFormat, theCRK3DFormat)
405  OB_STATIC_PLUGIN(CSRFormat, theCSRFormat)
406  OB_STATIC_PLUGIN(CSSRFormat, theCSSRFormat)
407  OB_STATIC_PLUGIN(DlpolyConfigFormat, theDlpolyConfigFormat)
408  OB_STATIC_PLUGIN(DlpolyHISTORYFormat, theDlpolyHISTORYFormat)
409  OB_STATIC_PLUGIN(DMolFormat, theDMolFormat)
410  OB_STATIC_PLUGIN(EXYZFormat, theEXYZFormat)
411  OB_STATIC_PLUGIN(FASTAFormat, theFASTAFormat)
412  OB_STATIC_PLUGIN(FastSearchFormat, theFastSearchFormat)
413  OB_STATIC_PLUGIN(FCHKFormat, theFCHKFormat)
414  OB_STATIC_PLUGIN(FEATFormat, theFEATFormat)
415  OB_STATIC_PLUGIN(FenskeZmatFormat, theFenskeZmatFormat)
416  OB_STATIC_PLUGIN(FHIaimsFormat,theFHIaimsFormat)
417  OB_STATIC_PLUGIN(FingerprintFormat, theFingerprintFormat)
418  OB_STATIC_PLUGIN(FreeFormFractionalFormat, theFreeFormFractionalFormat)
419  OB_STATIC_PLUGIN(GAMESSOutputFormat, theGAMESSOutputFormat)
420  OB_STATIC_PLUGIN(GAMESSInputFormat, theGAMESSInputFormat)
421  OB_STATIC_PLUGIN(OBGaussianCubeFormat, theGaussianCubeFormat)
422  OB_STATIC_PLUGIN(GaussianOutputFormat, theGaussianOutputFormat)
423  OB_STATIC_PLUGIN(GaussianInputFormat, theGaussianInputFormat)
424  OB_STATIC_PLUGIN(GaussianZMatrixInputFormat, theGaussianZMatrixInputFormat)
425  OB_STATIC_PLUGIN(GenBankFormat, theGenBankFormat)
426  OB_STATIC_PLUGIN(GhemicalFormat, theGhemicalFormat)
427  OB_STATIC_PLUGIN(GROFormat, theGROFormat)
428  OB_STATIC_PLUGIN(GROMOS96Format, theGROMOS96Format)
429  OB_STATIC_PLUGIN(GULPFormat, theGULPFormat)
430  OB_STATIC_PLUGIN(HINFormat, theHINFormat)
431  OB_STATIC_PLUGIN(JaguarOutputFormat, theJaguarOutputFormat)
432  OB_STATIC_PLUGIN(JaguarInputFormat, theJaguarInputFormat)
433  OB_STATIC_PLUGIN(LMPDATFormat, theLMPDATFormat)
434  OB_STATIC_PLUGIN(MCDLFormat, theMCDLFormat)
435  OB_STATIC_PLUGIN(MOLFormat, theMOLFormat)
436  OB_STATIC_PLUGIN(SDFormat, theSDFormat)
437  OB_STATIC_PLUGIN(OBT41Format, t41Format__)
438  OB_STATIC_PLUGIN(OBMoldenFormat, moldenFormat__)
439  OB_STATIC_PLUGIN(mmCIFFormat, themmCIFFormat)
440  OB_STATIC_PLUGIN(MacroModFormat, theMacroModFormat)
441  OB_STATIC_PLUGIN(MNAFormat, theMNAFormat)
442  OB_STATIC_PLUGIN(MOL2Format, theMOL2Format)
443  OB_STATIC_PLUGIN(MolproOutputFormat, theMolproOutputFormat)
444  OB_STATIC_PLUGIN(MolproInputFormat, theMolproInputFormat)
445  OB_STATIC_PLUGIN(MolReportFormat, theMolReportFormat)
446  OB_STATIC_PLUGIN(MOPACFormat, theMOPACFormat)
447  OB_STATIC_PLUGIN(MOPACCARTFormat, theMOPACCARTFormat)
448  OB_STATIC_PLUGIN(MOPACINTFormat, theMOPACINTFormat)
449  OB_STATIC_PLUGIN(MPDFormat, theMPDFormat)
450  OB_STATIC_PLUGIN(MPQCFormat, theMPQCFormat)
451  OB_STATIC_PLUGIN(MPQCInputFormat, theMPQCInputFormat)
452  OB_STATIC_PLUGIN(MSIFormat, theMSIFormat)
453  OB_STATIC_PLUGIN(OBMSMSFormat, msmsFormat__)
454  OB_STATIC_PLUGIN(NulFormat, theNulFormat)
455  OB_STATIC_PLUGIN(NWChemOutputFormat, theNWChemOutputFormat)
456  OB_STATIC_PLUGIN(NWChemInputFormat, theNWChemInputFormat)
457  OB_STATIC_PLUGIN(OBOpenDXCubeFormat, theOpenDXCubeFormat)
458  OB_STATIC_PLUGIN(OrcaOutputFormat, theOrcaOutputFormat)
459  OB_STATIC_PLUGIN(OrcaInputFormat, theOrcaInputFormat)
460  OB_STATIC_PLUGIN(OutputFormat, theOutputFormat)
461  OB_STATIC_PLUGIN(PCModelFormat, thePCModelFormat)
462  OB_STATIC_PLUGIN(PDBFormat, thePDBFormat)
463  OB_STATIC_PLUGIN(PDBQTFormat, thePDBQTFormat)
464 #ifdef HAVE_LIBZ
465  OB_STATIC_PLUGIN(PNGFormat, thePNGFormat)
466 #endif
467  OB_STATIC_PLUGIN(PointCloudFormat, thePointCloudFormat)
468  OB_STATIC_PLUGIN(PovrayFormat, thePovrayFormat)
469  OB_STATIC_PLUGIN(PQRFormat, thePQRFormat)
470  OB_STATIC_PLUGIN(PQSFormat, thePQSFormat)
471  OB_STATIC_PLUGIN(PWscfFormat, thePWscfFormat)
472  OB_STATIC_PLUGIN(QChemOutputFormat, theQChemOutputFormat)
473  OB_STATIC_PLUGIN(QChemInputFormat, theQChemInputFormat)
474  OB_STATIC_PLUGIN(ReportFormat, theReportFormat)
475  OB_STATIC_PLUGIN(SmiReactFormat, theSmiReactFormat)
476  OB_STATIC_PLUGIN(RXNFormat, theRXNFormat)
477  OB_STATIC_PLUGIN(ShelXFormat, theShelXFormat)
478  OB_STATIC_PLUGIN(SMIFormat, theSMIFormat)
479  OB_STATIC_PLUGIN(STLFormat, theSTLFormat)
480  OB_STATIC_PLUGIN(CANSMIFormat, theCANSMIFormat)
481  OB_STATIC_PLUGIN(FIXFormat, theFIXFormat)
482  OB_STATIC_PLUGIN(SVGFormat, theSVGFormat)
483  OB_STATIC_PLUGIN(TextFormat, theTextFormat)
484  OB_STATIC_PLUGIN(ThermoFormat, theThermoFormat)
485  OB_STATIC_PLUGIN(TinkerFormat, theTinkerFormat)
486  OB_STATIC_PLUGIN(TitleFormat, theTitleFormat)
487  OB_STATIC_PLUGIN(TurbomoleFormat, theTurbomoleFormat)
488  OB_STATIC_PLUGIN(UniChemFormat, theUniChemFormat)
489  OB_STATIC_PLUGIN(VASPFormat, theVASPFormat)
490  OB_STATIC_PLUGIN(ViewMolFormat, theViewMolFormat)
491  OB_STATIC_PLUGIN(XEDFormat, theXEDFormat)
492  OB_STATIC_PLUGIN(XSFFormat, theXSFFormat)
493  OB_STATIC_PLUGIN(XYZFormat, theXYZFormat)
494  OB_STATIC_PLUGIN(YOBFormat, theYOBFormat)
495  OB_STATIC_PLUGIN(ZINDOFormat, theZINDOFormat)
496 #ifdef HAVE_STATIC_LIBXML
497  OB_STATIC_PLUGIN(ChemDrawXMLFormat, theChemDrawXMLFormat)
498  OB_STATIC_PLUGIN(CMLFormat, theCMLFormat)
499  OB_STATIC_PLUGIN(CMLReactFormat, theCMLReactFormat)
500  OB_STATIC_PLUGIN(PubChemFormat, thePubChemFormat)
501  OB_STATIC_PLUGIN(XMLFormat, theXMLFormat)
502 #endif
503 #ifdef HAVE_STATIC_INCHI
504  OB_STATIC_PLUGIN(InChIFormat, theInChIFormat)
505 #endif
506 #ifdef HAVE_REGEX_H
507  OB_STATIC_PLUGIN(GAMESSUKInputFormat, theGAMESSUKInputFormat)
508  OB_STATIC_PLUGIN(GAMESSUKOutputFormat, theGAMESSUKOutputFormat)
509 #endif
510 #ifdef HAVE_RPC_XDR_H
511  OB_STATIC_PLUGIN(XTCFormat, theXTCFormat)
512 #endif
513 
514  // descriptors
515  OB_STATIC_PLUGIN(CanSmiles, theCanSmiles)
516  OB_STATIC_PLUGIN(CompoundFilter, dummyCmpFilter)
517  OB_STATIC_PLUGIN(MWFilter, theMWFilter)
518  OB_STATIC_PLUGIN(SmartsFilter, firstSmartsFilter)
519  OB_STATIC_PLUGIN(SmartsFilter, secondSmartsFilter)
520  OB_STATIC_PLUGIN(TitleFilter, theTitleFilter)
521  OB_STATIC_PLUGIN(FormulaDescriptor, TheFormulaDescriptor)
522  //OB_STATIC_PLUGIN(FPCount, theFPCount)
523  OB_STATIC_PLUGIN(InChIFilter, theInChIFilter)
524  // smarts descriptors
525  OB_STATIC_PLUGIN(SmartsDescriptor, theHBD)
526  OB_STATIC_PLUGIN(SmartsDescriptor, theHBA1)
527  OB_STATIC_PLUGIN(SmartsDescriptor, theHBA2)
528  OB_STATIC_PLUGIN(SmartsDescriptor, thenF)
529  // group contribution descriptors
530  OB_STATIC_PLUGIN(OBGroupContrib, thelogP)
531  OB_STATIC_PLUGIN(OBGroupContrib, theTPSA)
532  OB_STATIC_PLUGIN(OBGroupContrib, theMR)
533 
534  // fingerprints
535  OB_STATIC_PLUGIN(fingerprint2, thefingerprint2)
536  OB_STATIC_PLUGIN(PatternFP, FP3PatternFP)
537  OB_STATIC_PLUGIN(PatternFP, FP4PatternFP)
538  OB_STATIC_PLUGIN(fingerprintECFP, theECFP0)
539  OB_STATIC_PLUGIN(fingerprintECFP, theECFP2)
540  OB_STATIC_PLUGIN(fingerprintECFP, theECFP4)
541  OB_STATIC_PLUGIN(fingerprintECFP, theECFP6)
542  OB_STATIC_PLUGIN(fingerprintECFP, theECFP8)
543  OB_STATIC_PLUGIN(fingerprintECFP, theECFP10)
544 
545  // forcefields
546  OB_STATIC_PLUGIN(OBForceFieldGaff, theForceFieldGaff)
547  OB_STATIC_PLUGIN(OBForceFieldGhemical, theForceFieldGhemical)
548  OB_STATIC_PLUGIN(OBForceFieldMMFF94, theForceFieldMMFF94)
549  OB_STATIC_PLUGIN(OBForceFieldMMFF94, theForceFieldMMFF94s)
550  OB_STATIC_PLUGIN(OBForceFieldUFF, theForceFieldUFF)
551 
552  // operations
553  OB_STATIC_PLUGIN(OpAddInIndex, theOpAddInIndex)
554  OB_STATIC_PLUGIN(OpAddPolarH, theOpAddPolarH)
555  OB_STATIC_PLUGIN(OpAddNonPolarH, theOpAddNonPolarH)
556  OB_STATIC_PLUGIN(OpChangeCell, theOpChangeCell)
557  OB_STATIC_PLUGIN(OpCanonical, theOpCanonical)
558  OB_STATIC_PLUGIN(OpDelPolarH, theOpDelPolarH)
559  OB_STATIC_PLUGIN(OpDelNonPolarH, theOpDelNonPolarH)
560  OB_STATIC_PLUGIN(OpFillUC, theOpFillUC)
561  OB_STATIC_PLUGIN(OpEnergy, theOpEnergy)
562  OB_STATIC_PLUGIN(OpMinimize, theOpMinimize)
563  OB_STATIC_PLUGIN(OpGen2D, theOpGen2D)
564  OB_STATIC_PLUGIN(OpGen3D, theOpGen3D)
565  OB_STATIC_PLUGIN(OpNewS, theOpNewS)
566  OB_STATIC_PLUGIN(OpPartialCharge, theOpPartialCharge)
567  OB_STATIC_PLUGIN(OpReadConformers, theOpReadConformers)
568  OB_STATIC_PLUGIN(OpSort, theOpSort)
569  OB_STATIC_PLUGIN(OpExtraOut, theOpExtraOut)
570 #ifdef HAVE_STATIC_INCHI
571  OB_STATIC_PLUGIN(OpUnique, theOpUnique)
572 #endif
573 #ifdef HAVE_EIGEN
574  OB_STATIC_PLUGIN(OpConformer, theOpConformer)
575 #endif
576 
577  // charges
578  OB_STATIC_PLUGIN(GasteigerCharges, theGasteigerCharges)
579  OB_STATIC_PLUGIN(MMFF94Charges, theMMFF94Charges)
580  OB_STATIC_PLUGIN(NoCharges, theNoCharges)
581  OB_STATIC_PLUGIN(FromFileCharges, theFromFileCharges)
582 #ifdef HAVE_EIGEN
583  OB_STATIC_PLUGIN(QEqCharges, theQEqCharges)
584  OB_STATIC_PLUGIN(QTPIECharges, theQTPIECharges)
585 #endif
586 #ifdef HAVE_EIGEN3
587  OB_STATIC_PLUGIN(EQEqCharges, theEQEqCharges)
588 #endif
589  OBAPI std::vector<std::string> EnableStaticPlugins();
590 
591 #endif // USING_DYNAMIC_LIBS
592 
593 #endif // SWIG
594 
595 } // end namespce
596 
597 #endif
const char * _id
Definition: plugin.h:150
const char * GetID() const
Return the ID of the sub-type instance.
Definition: plugin.h:93
static int AllPluginsLoaded
Keep a record if all plugins have been loaded.
Definition: plugin.h:140
virtual const char * Description()
Required description of a sub-type.
Definition: plugin.h:66
std::map< const char *, OBPlugin *, CharPtrLess > PluginMapType
Definition: plugin.h:59
virtual void Init()
Definition: plugin.h:86
std::vector< std::string > EnableStaticPlugins()
Definition: plugin.cpp:225
#define OB_STATIC_PLUGIN(className, instanceName)
Definition: plugin.h:372
Base class for all types of dynamic classes discovered at runtime.
Definition: plugin.h:52
Dynamic loader for file format modules.
virtual ~OBPlugin()
Virtual destructor necessary for classes with virtual functions.
Definition: plugin.h:63
bool operator()(const char *p1, const char *p2) const
Definition: plugin.h:43
virtual OBPlugin * MakeInstance(const std::vector< std::string > &)
Definition: plugin.h:82
PluginMapType::const_iterator PluginIterator
Definition: plugin.h:60
Case insensitive string comparison for PluginMapType key.
Definition: plugin.h:41
static PluginMapType & PluginMap()
Returns a reference to the map of the plugin types. Is a function rather than a static member variabl...
Definition: plugin.h:133
static PluginIterator End(const char *PluginID)
Definition: plugin.h:119
static PluginIterator Begin(const char *PluginID)
Definition: plugin.h:111
virtual const char * TypeID()
Redefined by each plugin type: "formats", "fingerprints", etc.
Definition: plugin.h:69
Global namespace for all Open Babel code.
Definition: alias.h:22