Coverage Report

Created: 2025-08-26 06:55

/src/openbabel/include/openbabel/op.h
Line
Count
Source (jump to first uncovered line)
1
/**********************************************************************
2
op.h - plugin options or operations
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_OP_H
20
#define OB_OP_H
21
22
#include <openbabel/babelconfig.h>
23
#include <string>
24
#include <map>
25
#include <openbabel/plugin.h>
26
27
namespace OpenBabel
28
{
29
  class OBConversion; //used only as a pointer
30
  class OBBase;
31
32
// Class introduction below
33
class OBAPI OBOp : public OBPlugin
34
{
35
  MAKE_PLUGIN(OBOp);
36
37
public:
38
  typedef const std::map<std::string, std::string> OpMap ;
39
40
  ///Provides the name of this kind of plugin. Use -L "ops" to list from commandline.
41
36
  const char* TypeID() override { return "ops"; }
42
43
  ///Required function that does the work. Normally return true, unless object is not to be output.
44
  //NOTE: the parameters were changed in r3532
45
  virtual bool Do(OBBase* pOb, const char* OptionText=nullptr, OpMap* pOptions=nullptr, OBConversion* pConv=nullptr)=0;
46
47
  /// \return true if this op is designed to work with the class of pOb, e.g. OBMol
48
  virtual bool WorksWith(OBBase* pOb)const=0;
49
50
  /// Do something with an array of objects. Used a a callback routine in OpSort, etc.
51
0
  virtual bool ProcessVec(std::vector<OBBase*>& /* vec */){ return false; }
52
53
  /// \return string describing options, for display with -H and to make checkboxes in GUI
54
  static std::string OpOptions(OBBase* pOb)
55
0
  {
56
0
    std::string s;
57
0
    OBPlugin::PluginIterator itr;
58
0
    for(itr=OBPlugin::Begin("ops");itr!=OBPlugin::End("ops");++itr)
59
0
    {
60
0
      OBOp* pOp = dynamic_cast<OBOp*>(itr->second);
61
       //ignore ops with IDs that begin with '_' or have "not displayed in GUI" in their first line of description
62
0
      if(*(itr->first)=='_'
63
0
        || OBPlugin::FirstLine(pOp->Description()).find("not displayed in GUI")!=std::string::npos)
64
0
        continue;
65
0
      if(pOp && pOp->WorksWith(pOb))
66
0
      {
67
0
        s += "--";
68
0
        s += itr->first; //ID
69
0
        s += ' ';
70
0
        s += OBPlugin::FirstLine(pOp->Description()) + '\n';
71
0
      }
72
0
    }
73
0
    s += '\n';
74
0
    return s;
75
0
  }
76
77
  ///Call Do() of all the OBOps whose ID is a key in the map.
78
  ///Called from DoTransformations(). The map has general options like -x or --multicharoption
79
  ///The key is the option name and the value, if any, is text which follows the option name.
80
  /// In some cases, there may be several parameters, space separated)
81
  /// \return false indicating object should not be output, if any Do() returns false
82
  static bool DoOps(OBBase* pOb, OpMap* pOptions, OBConversion* pConv)
83
0
  {
84
0
    OpMap::const_iterator itr;
85
0
    for(itr=pOptions->begin();itr!=pOptions->end();++itr)
86
0
    {
87
0
      OBOp* pOp = FindType(itr->first.c_str());
88
0
      if(pOp)
89
0
        if(!pOp->Do(pOb, itr->second.c_str(), pOptions, pConv))
90
0
          return false; //Op has decided molecule should not be output
91
0
    }
92
0
    return true;
93
0
  }
94
};
95
96
/** \class OBOp op.h <openbabel/op.h>
97
      \brief Operations to modify molecules before output
98
      \since version 2.2
99
100
Classes derived from OBOp implement options for the obabel program (for both
101
its commandline and GUI interfaces). It is intended for options that carry out some
102
modification on the molecule(or reaction) after it has been input, but before
103
it is output. An example is the --center option implemented in the OpCenter class
104
in ops.cpp, which is a duplicate of the built in -c option for centering coordinates.
105
106
The advantage of plugin classes is that no existing code has to be modified
107
when a new class is added. You can list those that are present by
108
obabel -L ops
109
or from a menu item in the GUI.
110
111
Any OBOp derived class has to have a constructor, a function returning a short description,
112
and a Do() function which does the work. It also needs a WorksWith() function
113
which is always the same when operating on OBMol objects. (It is not made a default
114
to reducecode dependencies.) A single global instance of the class needs to be
115
instantiated to define the ID, by which the class is subsequently accessed.
116
117
OBOp works by two of its static functions being called from code in transform.cpp:
118
 - OpOptions(OBBase* pOb, OpMap* pOptions) returns a string describing each of the
119
derivated classes relevant to objects of the class of the OBBase parameter,
120
for use in the help text and to set checkboxes in the GUI;
121
 - DoOps(OBBase* pOb) applies each option whose ID is listed in the  Opmap parameter
122
to the object (ususally an OBMol) in the OBBase parameter.
123
124
Options which need parameters are passed these (space delimited) in the text parameter
125
of the Do() function. They can also access other general options specified on the
126
command line by examining the the OpMap parameter.
127
128
To use an OBOp class from the API it is necessary to use an extra step in case it isn't
129
present. So to apply the OBOp class with ID gen3D to your mol
130
131
\code
132
OBOp* pOp = OBOp::FindType("gen3D");
133
if(!pOp)
134
  ...report error
135
pOp->Do(mol);
136
\endcode
137
138
  */
139
140
}//namespace
141
142
#endif
143
144
//! \file op.h
145
//! \brief Base plugin class for operations on molecules