Chemical Data Processing Library C++ API - Version 1.4.0
BasicReaction.hpp
Go to the documentation of this file.
1 /*
2  * BasicReaction.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_CHEM_BASICREACTION_HPP
30 #define CDPL_CHEM_BASICREACTION_HPP
31 
32 #include <vector>
33 #include <memory>
34 
35 #include <boost/iterator/indirect_iterator.hpp>
36 
37 #include "CDPL/Chem/APIPrefix.hpp"
38 #include "CDPL/Chem/Reaction.hpp"
40 #include "CDPL/Util/ObjectPool.hpp"
41 
42 
43 namespace CDPL
44 {
45 
46  namespace Chem
47  {
48 
56  {
57 
59  typedef ComponentCache::SharedObjectPointer ComponentPtr;
60  typedef std::vector<ComponentPtr> ComponentList;
61 
62  public:
66  typedef std::shared_ptr<BasicReaction> SharedPointer;
67 
71  typedef boost::indirect_iterator<ComponentList::iterator, BasicMolecule> ComponentIterator;
72 
76  typedef boost::indirect_iterator<ComponentList::const_iterator, const BasicMolecule> ConstComponentIterator;
77 
82 
88 
93  BasicReaction(const Reaction& rxn);
94 
101 
102  unsigned int getComponentRole(const Molecule& mol) const;
103 
104  std::size_t getComponentIndex(const Molecule& mol) const;
105 
106  bool containsComponent(const Molecule& mol) const;
107 
108  void clear();
109 
110  std::size_t getNumComponents() const;
111 
112  std::size_t getNumComponents(unsigned int role) const;
113 
119 
125 
131 
137 
147  ConstComponentIterator getComponentsBegin(unsigned int role) const;
148 
159 
169  ConstComponentIterator getComponentsEnd(unsigned int role) const;
170 
181 
182  const BasicMolecule& getComponent(std::size_t idx) const;
183 
184  BasicMolecule& getComponent(std::size_t idx);
185 
186  const BasicMolecule& getComponent(std::size_t idx, unsigned int role) const;
187 
188  BasicMolecule& getComponent(std::size_t idx, unsigned int role);
189 
190  BasicMolecule& addComponent(unsigned int role);
191 
201  BasicMolecule& addComponent(unsigned int role, const Molecule& mol);
202 
203  void swapComponentRoles(unsigned int role1, unsigned int role2);
204 
205  void removeComponent(std::size_t idx);
206 
207  void removeComponent(std::size_t idx, unsigned int role);
208 
215 
216  void removeComponents(unsigned int role);
217 
219 
230 
231  using Reaction::operator=;
232 
238  void copy(const BasicReaction& rxn);
239 
240  void copy(const Reaction& rxn);
241 
242  private:
243  ConstComponentIterator getReactantsBegin() const;
244  ComponentIterator getReactantsBegin();
245 
246  ConstComponentIterator getReactantsEnd() const;
247  ComponentIterator getReactantsEnd();
248 
249  ConstComponentIterator getAgentsBegin() const;
250  ComponentIterator getAgentsBegin();
251 
252  ConstComponentIterator getAgentsEnd() const;
253  ComponentIterator getAgentsEnd();
254 
255  ConstComponentIterator getProductsBegin() const;
256  ComponentIterator getProductsBegin();
257 
258  ConstComponentIterator getProductsEnd() const;
259  ComponentIterator getProductsEnd();
260 
261  /*
262  * \brief Returns a \c const reference to the reactant at index \a idx.
263  * \param idx The zero-based reactant index.
264  * \return A \c const reference to the reactant molecule.
265  * \throw Base::IndexError if \a idx is not less than getNumReactants().
266  */
267  const BasicMolecule& getReactant(std::size_t idx) const;
268 
269  /*
270  * \brief Returns a mutable reference to the reactant at index \a idx.
271  * \param idx The zero-based reactant index.
272  * \return A mutable reference to the reactant molecule.
273  * \throw Base::IndexError if \a idx is not less than getNumReactants().
274  */
275  BasicMolecule& getReactant(std::size_t idx);
276 
277  /*
278  * \brief Returns a \c const reference to the agent at index \a idx.
279  * \param idx The zero-based agent index.
280  * \return A \c const reference to the agent molecule.
281  * \throw Base::IndexError if \a idx is not less than getNumAgents().
282  */
283  const BasicMolecule& getAgent(std::size_t idx) const;
284 
285  /*
286  * \brief Returns a mutable reference to the agent at index \a idx.
287  * \param idx The zero-based agent index.
288  * \return A mutable reference to the agent molecule.
289  * \throw Base::IndexError if \a idx is not less than getNumAgents().
290  */
291  BasicMolecule& getAgent(std::size_t idx);
292 
293  /*
294  * \brief Returns a \c const reference to the product at index \a idx.
295  * \param idx The zero-based product index.
296  * \return A \c const reference to the product molecule.
297  * \throw Base::IndexError if \a idx is not less than getNumProducts().
298  */
299  const BasicMolecule& getProduct(std::size_t idx) const;
300 
301  /*
302  * \brief Returns a mutable reference to the product at index \a idx.
303  * \param idx The zero-based product index.
304  * \return A mutable reference to the product molecule.
305  * \throw Base::IndexError if \a idx is not less than getNumProducts().
306  */
307  BasicMolecule& getProduct(std::size_t idx);
308 
309  /*
310  * \brief Removes the reactant at index \a idx.
311  * \param idx The zero-based reactant index.
312  * \throw Base::IndexError if \a idx is not less than getNumReactants().
313  */
314  void removeReactant(std::size_t idx);
315 
316  /*
317  * \brief Removes the agent at index \a idx.
318  * \param idx The zero-based agent index.
319  * \throw Base::IndexError if \a idx is not less than getNumAgents().
320  */
321  void removeAgent(std::size_t idx);
322 
323  /*
324  * \brief Removes the product at index \a idx.
325  * \param idx The zero-based product index.
326  * \throw Base::IndexError if \a idx is not less than getNumProducts().
327  */
328  void removeProduct(std::size_t idx);
329 
330  /*
331  * \brief Removes all reactants from the reaction.
332  */
333  void removeReactants();
334 
335  /*
336  * \brief Removes all agents from the reaction.
337  */
338  void removeAgents();
339 
340  /*
341  * \brief Removes all products from the reaction.
342  */
343  void removeProducts();
344 
345  /*
346  * \brief Creates a new reaction component with the supplied \a role, optionally copying the contents of \a mol.
347  * \param role The Chem::ReactionRole flag indicating where the new component is inserted (REACTANT, AGENT, or PRODUCT).
348  * \param mol The molecule whose contents are copied into the new component, or \c nullptr to create an empty component.
349  * \return A reference to the newly added component.
350  */
351  BasicMolecule& addComponent(unsigned int role, const Molecule* mol);
352 
353  /*
354  * \brief Creates a new reactant component, optionally copying the contents of \a mol.
355  * \param mol The molecule whose contents are copied into the new reactant, or \c nullptr to create an empty reactant.
356  * \return A reference to the newly added reactant.
357  */
358  BasicMolecule& addReactant(const Molecule* mol);
359 
360  /*
361  * \brief Creates a new agent component, optionally copying the contents of \a mol.
362  * \param mol The molecule whose contents are copied into the new agent, or \c nullptr to create an empty agent.
363  * \return A reference to the newly added agent.
364  */
365  BasicMolecule& addAgent(const Molecule* mol);
366 
367  /*
368  * \brief Creates a new product component, optionally copying the contents of \a mol.
369  * \param mol The molecule whose contents are copied into the new product, or \c nullptr to create an empty product.
370  * \return A reference to the newly added product.
371  */
372  BasicMolecule& addProduct(const Molecule* mol);
373 
374  /*
375  * \brief Removes all reactant, agent, and product components from the reaction.
376  */
377  void clearComponents();
378 
379  /*
380  * \brief Replaces the current reactants, agents, and products by deep copies of those in \a rxn.
381  * \param rxn The source reaction.
382  */
383  void copyComponents(const BasicReaction& rxn);
384 
385  /*
386  * \brief Replaces the current reactants, agents, and products by deep copies of those in \a rxn.
387  * \param rxn The source reaction.
388  */
389  void copyComponents(const Reaction& rxn);
390 
391  ComponentPtr allocComponent(const Molecule* mol);
392 
393  ComponentCache compCache;
394  ComponentList components;
395  std::size_t agentsStartIdx;
396  std::size_t productsStartIdx;
397  };
398  } // namespace Chem
399 } // namespace CDPL
400 
401 #endif // CDPL_CHEM_BASICREACTION_HPP
Definition of class CDPL::Chem::BasicMolecule.
Definition of the preprocessor macro CDPL_CHEM_API.
#define CDPL_CHEM_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of class CDPL::Util::ObjectPool.
Definition of class CDPL::Chem::Reaction.
Default implementation of the Chem::Molecule interface.
Definition: BasicMolecule.hpp:58
Default implementation of the Chem::Reaction interface.
Definition: BasicReaction.hpp:56
BasicReaction(const BasicReaction &rxn)
Constructs a BasicReaction instance that is a copy of the reaction rxn.
boost::indirect_iterator< ComponentList::const_iterator, const BasicMolecule > ConstComponentIterator
A constant random access iterator used to iterate over the stored const Chem::BasicMolecule reaction ...
Definition: BasicReaction.hpp:76
ComponentIterator removeComponent(const ComponentIterator &it)
Removes the component referenced by the given iterator.
void copy(const BasicReaction &rxn)
Replaces the current set of reaction components and properties by a copy of the components and proper...
boost::indirect_iterator< ComponentList::iterator, BasicMolecule > ComponentIterator
A mutable random access iterator used to iterate over the stored Chem::BasicMolecule reaction compone...
Definition: BasicReaction.hpp:71
ConstComponentIterator getComponentsEnd(unsigned int role) const
Returns a constant iterator pointing to the end of the reaction components with the specified role.
ComponentIterator getComponentsEnd(unsigned int role)
Returns a mutable iterator pointing to the end of the reaction components with the specified role.
ComponentIterator getComponentsEnd()
Returns a mutable iterator pointing to the end of the reaction components.
std::size_t getNumComponents() const
Returns the total number of reaction components.
void removeComponents(unsigned int role)
Removes all components with the specified role.
BasicReaction & operator=(const BasicReaction &rxn)
Replaces the current set of reaction components and properties by a copy of the components and proper...
ConstComponentIterator getComponentsBegin(unsigned int role) const
Returns a constant iterator pointing to the beginning of the reaction components with the specified r...
BasicReaction()
Constructs an empty BasicReaction instance.
std::size_t getNumComponents(unsigned int role) const
Returns the number of reaction components with the specified role.
ComponentIterator getComponentsBegin(unsigned int role)
Returns a mutable iterator pointing to the beginning of the reaction components with the specified ro...
bool containsComponent(const Molecule &mol) const
Tells whether the specified molecule is a component of this reaction.
std::shared_ptr< BasicReaction > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated BasicReaction instances.
Definition: BasicReaction.hpp:66
BasicMolecule & addComponent(unsigned int role, const Molecule &mol)
Creates a new reaction component with the specified role that is a copy of the molecule mol.
void removeComponent(std::size_t idx, unsigned int role)
Removes the reaction component at index idx in the list of components with the specified role.
unsigned int getComponentRole(const Molecule &mol) const
Returns the reaction role of the specified component.
void swapComponentRoles(unsigned int role1, unsigned int role2)
Swaps the reaction roles of the component sets specified by role1 and role2.
void clear()
Removes all components and clears all properties of the reaction.
BasicReaction(const Reaction &rxn)
Constructs a BasicReaction instance that is a copy of the reaction rxn.
Reaction::SharedPointer clone() const
Creates a deep copy of the current reaction state.
ConstComponentIterator getComponentsBegin() const
Returns a constant iterator pointing to the beginning of the reaction components.
void copy(const Reaction &rxn)
Replaces the current set of reaction components and properties by a copy of the components and proper...
void removeComponent(std::size_t idx)
Removes the reaction component at the specified index.
ConstComponentIterator getComponentsEnd() const
Returns a constant iterator pointing to the end of the reaction components.
BasicMolecule & getComponent(std::size_t idx)
Returns a non-const reference to the reaction component at index idx.
const BasicMolecule & getComponent(std::size_t idx) const
Returns a const reference to the reaction component at index idx.
BasicMolecule & addComponent(unsigned int role)
Creates a new reaction component with the specified role.
ComponentIterator getComponentsBegin()
Returns a mutable iterator pointing to the beginning of the reaction components.
BasicMolecule & getComponent(std::size_t idx, unsigned int role)
Returns a non-const reference to the reaction component at index idx in the list of components with t...
const BasicMolecule & getComponent(std::size_t idx, unsigned int role) const
Returns a const reference to the reaction component at index idx in the list of components with the s...
std::size_t getComponentIndex(const Molecule &mol) const
Returns the index of the specified reaction component.
Abstract base class representing a mutable molecular graph that owns and manages its atoms and bonds.
Definition: Molecule.hpp:53
Abstract base class for chemical reactions composed of role-tagged reaction components of type Chem::...
Definition: Reaction.hpp:59
std::shared_ptr< Reaction > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Reaction instances.
Definition: Reaction.hpp:62
std::shared_ptr< ObjectType > SharedObjectPointer
A smart pointer to a borrowed object that returns the object to the pool on destruction.
Definition: ObjectPool.hpp:71
CDPL_CHEM_API void clearComponents(MolecularGraph &molgraph)
Clears the value of the Chem::MolecularGraphProperty::COMPONENTS property of the molecular graph molg...
The namespace of the Chemical Data Processing Library.