1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>plugin.h File Reference</title>
<link href="/site.css" rel="stylesheet" type="text/css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="search/search.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!--#include file="header.html" -->
<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.shtml"><span>Main Page</span></a></li>
<li><a href="pages.shtml"><span>Related Pages</span></a></li>
<li><a href="modules.shtml"><span>Modules</span></a></li>
<li><a href="namespaces.shtml"><span>Namespaces</span></a></li>
<li><a href="annotated.shtml"><span>Classes</span></a></li>
<li class="current"><a href="files.shtml"><span>Files</span></a></li>
<li><a href="examples.shtml"><span>Examples</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
<div id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.shtml"><span>File List</span></a></li>
<li><a href="globals.shtml"><span>File Members</span></a></li>
</ul>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Classes</a> |
<a href="#namespaces">Namespaces</a> |
<a href="#define-members">Defines</a> |
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">plugin.h File Reference<div class="ingroups"><a class="el" href="group__plugins.shtml">Plugins</a></div></div> </div>
</div>
<div class="contents">
<div class="textblock"><code>#include <openbabel/babelconfig.h></code><br/>
<code>#include <string></code><br/>
<code>#include <iostream></code><br/>
<code>#include <vector></code><br/>
<code>#include <map></code><br/>
<code>#include <sstream></code><br/>
<code>#include <cstring></code><br/>
</div><div class="textblock"><div class="dynheader">
This graph shows which files directly or indirectly include this file:</div>
<div class="dyncontent">
<div class="center"><img src="plugin_8h__dep__incl.png" border="0" usemap="#plugin_8hdep" alt=""/></div>
<map name="plugin_8hdep" id="plugin_8hdep">
<area shape="rect" id="node3" href="chargemodel_8h.shtml" title="Base class for molecular partial charge models." alt="" coords="117,83,237,112"/><area shape="rect" id="node7" href="descriptor_8h.shtml" title="Base class for molecular descriptors." alt="" coords="365,83,467,112"/><area shape="rect" id="node17" href="fingerprint_8h.shtml" title="Declaration of OBFingerprint base class and fastsearch classes." alt="" coords="1057,83,1159,112"/><area shape="rect" id="node21" href="forcefield_8h.shtml" title="Handle forcefields." alt="" coords="1303,83,1399,112"/><area shape="rect" id="node27" href="format_8h.shtml" title="Declarations for OBFormat." alt="" coords="779,83,859,112"/><area shape="rect" id="node36" href="op_8h.shtml" title="Base plugin class for operations on molecules." alt="" coords="923,83,973,112"/><area shape="rect" id="node44" href="plugin_8cpp.shtml" title="Simplify 'plugin' classes to be discovered and/or loaded at runtime." alt="" coords="1423,83,1511,112"/><area shape="rect" id="node5" href="chargemodel_8cpp.shtml" title="Base class for molecular partial charge models." alt="" coords="5,160,141,189"/><area shape="rect" id="node9" href="groupcontrib_8h.shtml" title="Handle group contribution algorithms." alt="" coords="165,160,283,189"/><area shape="rect" id="node11" href="obmolecformat_8h.shtml" title="Subclass of OBFormat for conversion of OBMol." alt="" coords="485,237,621,267"/><area shape="rect" id="node13" href="descriptor_8cpp.shtml" title="Base class for molecular descriptors." alt="" coords="357,160,475,189"/><area shape="rect" id="node15" href="transform_8cpp.shtml" title="Perform command-line requested transformations for OBMol and SMARTS filtering." alt="" coords="619,160,733,189"/><area shape="rect" id="node19" href="fingerprint_8cpp.shtml" title="Definitions for OBFingerprint base class and fastsearch classes." alt="" coords="1184,160,1301,189"/><area shape="rect" id="node23" href="conformersearch_8cpp.shtml" title="Conformer searching using genetic algorithm." alt="" coords="1461,160,1624,189"/><area shape="rect" id="node25" href="forcefield_8cpp.shtml" title="Handle OBForceField class." alt="" coords="1325,160,1437,189"/><area shape="rect" id="node29" href="obconversion_8h.shtml" title="Handle file conversions. Declaration of OBFormat, OBConversion." alt="" coords="757,160,880,189"/><area shape="rect" id="node34" href="format_8cpp.shtml" title="Base class OBFormat for file formats." alt="" coords="499,160,595,189"/><area shape="rect" id="node32" href="alias_8cpp.shtml" title="OBGenericData class to for atom alias data (e.g., in 2D drawing programs for "COOH")" alt="" coords="829,237,907,267"/><area shape="rect" id="node38" href="optransform_8h.shtml" title="Operations to change molecules using a datafile of chemical transformations OBChemTsfm." alt="" coords="955,160,1069,189"/><area shape="rect" id="node41" href="op_8cpp.shtml" title="Base plugin class for operations on molecules." alt="" coords="1093,160,1160,189"/></map>
</div>
</div>
<p><a href="plugin_8h_source.shtml">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr><td colspan="2"><h2><a name="nested-classes"></a>
Classes</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">struct  </td><td class="memItemRight" valign="bottom"><a class="el" href="structOpenBabel_1_1CharPtrLess.shtml">CharPtrLess</a></td></tr>
<tr><td class="mdescLeft"> </td><td class="mdescRight">Case insensitive string comparison for PluginMapType key. <a href="structOpenBabel_1_1CharPtrLess.shtml#details">More...</a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">class  </td><td class="memItemRight" valign="bottom"><a class="el" href="classOpenBabel_1_1OBPlugin.shtml">OBPlugin</a></td></tr>
<tr><td class="mdescLeft"> </td><td class="mdescRight">Base class for all types of dynamic classes discovered at runtime. <a href="classOpenBabel_1_1OBPlugin.shtml#details">More...</a><br/></td></tr>
<tr><td colspan="2"><h2><a name="namespaces"></a>
Namespaces</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceOpenBabel.shtml">OpenBabel</a></td></tr>
<tr><td colspan="2"><h2><a name="define-members"></a>
Defines</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="group__plugins.shtml#gadd740e2a386b7db3e1f0e271cbe8a3f1">MAKE_PLUGIN</a>(BaseClass)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="group__plugins.shtml#gaac917d0f317751ba3ac45628fb467e58">OB_STATIC_PLUGIN</a>(className, instanceName)</td></tr>
<tr><td colspan="2"><h2><a name="func-members"></a>
Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">std::vector< std::string > </td><td class="memItemRight" valign="bottom"><a class="el" href="group__plugins.shtml#ga7a0297c3c2a3fa58bbd5e321b900ddec">EnableStaticPlugins</a> ()</td></tr>
</table>
<hr/><a name="details" id="details"></a><h2>Detailed Description</h2>
<div class="textblock"><p>Simplify 'plugin' classes to be discovered and/or loaded at runtime. </p>
<p>The code in this file makes it easy to make 'plugin' classes. These classes are derived from a base class, like OBFingerprint. The derived classes ('sub-types' like fingerprint2) usually have a single instance. Plugin classes are only discovered at runtime, so no existing code needs to be changed when adding a new derived class. In some builds the new code can be added or removed by just moving a DLL or so file. The plugin classes derived from any base class (including new ones) type can be listed from the commandline.</p>
<h2>Step-by-Step Instructions</h2>
<p>1) In the header file for YourBaseClass (which handles whatsits). Make sure to include the <a class="el" href="plugin_8h.shtml" title="Simplify 'plugin' classes to be discovered and/or loaded at runtime.">plugin.h</a> header , derive the class from OBPlugin and in its definition add the MAKE_PLUGIN macro and a function TypeID() containing a simple descriptor of the type </p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include <<a class="code" href="plugin_8h.shtml" title="Simplify 'plugin' classes to be discovered and/or loaded at runtime.">openbabel/plugin.h</a>></span>
<span class="keyword">class </span>YourBaseClass : <span class="keyword">public</span> OBPlugin
{
<a class="code" href="group__plugins.shtml#gadd740e2a386b7db3e1f0e271cbe8a3f1">MAKE_PLUGIN</a>(YourBaseClass)
const <span class="keywordtype">char</span>* TypeID()
{ <span class="keywordflow">return</span> <span class="stringliteral">"whatsits"</span>; };
...rest of implementation, probably involving <span class="keyword">virtual</span> functions redefined
in the sub-type classes
};
</pre></div><p> See below for what the macro contains.</p>
<p>2) Declare each sub-type in a class derived from the base class and give it a constructor which calls OBPlugin constructor as shown: </p>
<div class="fragment"><pre class="fragment"><span class="keyword">class </span>YourSubType1 : <span class="keyword">public</span> YourBaseClass
{
<span class="keyword">public</span>:
YourSubtype1(<span class="keyword">const</span> <span class="keywordtype">char</span>* ID, <span class="keywordtype">bool</span> IsDefault=<span class="keyword">false</span>)
: YourBaseClass(ID, IsDefault){}
<span class="keyword">virtual</span> <span class="keywordtype">string</span> Description()
{ <span class="keywordflow">return</span> <span class="stringliteral">"A description with one or more lines"</span>;};
...rest of implementation
};
</pre></div><p> Only the first line of the description is used when the subclasses are listed.</p>
<p>3) Declare a global instance of the sub-type class which specifies its ID. and, optionally, whether it is to be regarded as the default type of YourBaseClass. </p>
<div class="fragment"><pre class="fragment">YourSubType1 theType1(<span class="stringliteral">"whatsit2"</span>,<span class="keyword">true</span>);
</pre></div><p>4) The following functions are available:</p>
<p>YourBaseClass* YourBaseClass::FindType(const char* ID); This returns the default type when ID is NULL or empty.</p>
<p>To list the sub-types of any plugin class use the List which sends to cout by default (or any other ostream if specified). </p>
<div class="fragment"><pre class="fragment"> OBPlugin::List(<span class="stringliteral">"whatsits"</span>)
</pre></div><p> The ListAsString and ListAsVector functions are alternatives, usable with scripting.</p>
<p>It is also possible to iterate through each sub-type by the following code: </p>
<div class="fragment"><pre class="fragment"> OBPlugin::PluginIterator itr;
<span class="keywordflow">for</span>(itr=OBPlugin::Begin(<span class="stringliteral">"whatsits"</span>);itr!=OBPlugin::End(<span class="stringliteral">"whatsits"</span>);++itr)
{
itr is a std::map::const_iterator
itr->first is the ID of the subtype;
itr->second is The OBPlugin* which you will have to cast to your type
}
</pre></div><p> Since this is not the most beautiful code, it is probably better to use the List methods if possible.</p>
<p>YourBaseClass* MakeNewInstance();</p>
<h2>How it works</h2>
<p><a class="el" href="group__plugins.shtml#gadd740e2a386b7db3e1f0e271cbe8a3f1">MAKE_PLUGIN(YourBaseClass)</a> inserts the following code into YourBaseClass: </p>
<div class="fragment"><pre class="fragment"><span class="keyword">protected</span>:
<span class="comment">//The collection of sub-types is in a local static variable to avoid</span>
<span class="comment">//any difficulties with the order of initialization of static objects.</span>
<span class="keyword">static</span> PluginMapType& Map()
{
<span class="keyword">static</span> PluginMapType m;
<span class="keywordflow">return</span> m;
}
<span class="comment">//Making the map accessible to the base class (Cannot be used during construction)</span>
<span class="keyword">virtual</span> PluginMapType& GetMap()<span class="keyword">const</span>
<span class="keyword"> </span>{
<span class="keywordflow">return</span> Map();
}
<span class="keyword">public</span>:
<span class="keyword">static</span> YourBaseClass*& Default()
{
<span class="keyword">static</span> YourBaseClass* d;
<span class="keywordflow">return</span> d;
}
<span class="comment">//Constructor registers the sub-type</span>
YourBaseClass(<span class="keyword">const</span> <span class="keywordtype">char</span>* ID, <span class="keywordtype">bool</span> IsDefault=<span class="keyword">false</span>)
{
_id = ID;
<span class="keywordflow">if</span>(ID && *ID && *ID!=<span class="charliteral">' '</span>) <span class="comment">//do not register if ID is empty or starts with a space</span>
{
<span class="keywordflow">if</span>(IsDefault || Map().empty())
Default() = <span class="keyword">this</span>;
Map()[ID]=<span class="keyword">this</span>;
<span class="comment">//Ensure YourBaseClass is registered in OBPlugin so it can be accessed from the commandline</span>
PluginMap()[TypeID()] =<span class="keyword">this</span>;
}
}
<span class="keyword">static</span> YourBaseClass* FindType(<span class="keyword">const</span> <span class="keywordtype">char</span>* ID)
{
<span class="keywordflow">if</span>(!ID || *ID==0)
<span class="keywordflow">return</span> Default();
<span class="keywordflow">return</span> <span class="keyword">static_cast<</span>YourBaseClass*<span class="keyword">></span>(BaseFindType(Map(),ID));
}
</pre></div> </div></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Defines</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- end content -->
<!--#include file="footer.html" -->
<div id="footer">
<hr size="1">
<img src="http://openbabel.org/babel256.png" width="136" height="127" alt="" style="float: left;" />
<p>This file is part of the documentation for <a href="http://openbabel.org/wiki/">Open Babel</a>, version 2.3.</p>
<div class="bottom">
Documentation copyright © 1998-2007, the <a href="http://openbabel.org/wiki/THANKS">Open Babel Developers</a>.<br>
Open Babel is hosted by: <a href="http://sourceforge.net">
<img src="http://sourceforge.net/sflogo.php?group_id=40728"
width="88" height="31" border="0" alt="SourceForge Logo"></a><br>
Generated on Thu Oct 13 2011 16:08:08 by <a href="http://www.doxygen.org/"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.7.5.1.
</div>
</body>
</html>
|