RDKit
Open-source cheminformatics and machine learning.
RDProps.h
Go to the documentation of this file.
1 #ifndef RDKIT_RDPROPS_H
2 #define RDKIT_RDPROPS_H
3 #include "Dict.h"
4 #include "types.h"
5 #include <boost/foreach.hpp>
6 
7 namespace RDKit {
8 
9 class RDProps {
10 protected:
11  mutable Dict dp_props;
12  // It is a quirk of history that this is mutable
13  // as the RDKit allows properties to be set
14  // on const obects.
15 
16  public:
17  RDProps() : dp_props() {}
18  RDProps(const RDProps &rhs) : dp_props(rhs.dp_props) {}
19  RDProps& operator=(const RDProps &rhs) {
20  dp_props = rhs.dp_props;
21  return *this;
22  }
23  void clear() {
24  dp_props.reset();
25  }
26  // ------------------------------------
27  // Local Property Dict functionality
28  // all setProp functions are const because they
29  // are not meant to change the atom chemically
30  // ------------------------------------
31  //! returns a list with the names of our \c properties
32  STR_VECT getPropList(bool includePrivate = true,
33  bool includeComputed = true) const {
34  const STR_VECT &tmp = dp_props.keys();
35  STR_VECT res, computed;
36  if (!includeComputed &&
38  computed.push_back(RDKit::detail::computedPropName);
39  }
40 
41  STR_VECT::const_iterator pos = tmp.begin();
42  while (pos != tmp.end()) {
43  if ((includePrivate || (*pos)[0] != '_') &&
44  std::find(computed.begin(), computed.end(), *pos) == computed.end()) {
45  res.push_back(*pos);
46  }
47  pos++;
48  }
49  return res;
50  }
51 
52  //! sets a \c property value
53  /*!
54  \param key the name under which the \c property should be stored.
55  If a \c property is already stored under this name, it will be
56  replaced.
57  \param val the value to be stored
58  \param computed (optional) allows the \c property to be flagged
59  \c computed.
60  */
61 
62  //! \overload
63  template <typename T>
64  void setProp(const std::string &key, T val, bool computed = false) const {
65  if (computed) {
66  STR_VECT compLst;
68  if (std::find(compLst.begin(), compLst.end(), key) == compLst.end()) {
69  compLst.push_back(key);
70  dp_props.setVal(RDKit::detail::computedPropName, compLst);
71  }
72  }
73  // setProp(key.c_str(),val);
74  dp_props.setVal(key, val);
75  }
76 
77  //! allows retrieval of a particular property value
78  /*!
79 
80  \param key the name under which the \c property should be stored.
81  If a \c property is already stored under this name, it will be
82  replaced.
83  \param res a reference to the storage location for the value.
84 
85  <b>Notes:</b>
86  - if no \c property with name \c key exists, a KeyErrorException will be
87  thrown.
88  - the \c boost::lexical_cast machinery is used to attempt type
89  conversions.
90  If this fails, a \c boost::bad_lexical_cast exception will be thrown.
91 
92  */
93  //! \overload
94  template <typename T>
95  void getProp(const std::string &key, T &res) const {
96  dp_props.getVal(key, res);
97  }
98 
99  //! \overload
100  template <typename T>
101  T getProp(const std::string &key) const {
102  return dp_props.getVal<T>(key);
103  }
104 
105  //! returns whether or not we have a \c property with name \c key
106  //! and assigns the value if we do
107  //! \overload
108  template <typename T>
109  bool getPropIfPresent(const std::string &key, T &res) const {
110  return dp_props.getValIfPresent(key, res);
111  }
112 
113  //! \overload
114  bool hasProp(const std::string &key) const {
115  return dp_props.hasVal(key);
116  };
117 
118  //! clears the value of a \c property
119  /*!
120  <b>Notes:</b>
121  - if no \c property with name \c key exists, a KeyErrorException
122  will be thrown.
123  - if the \c property is marked as \c computed, it will also be removed
124  from our list of \c computedProperties
125  */
126  //! \overload
127  void clearProp(const std::string &key) const {
128  STR_VECT compLst;
130  STR_VECT_I svi = std::find(compLst.begin(), compLst.end(), key);
131  if (svi != compLst.end()) {
132  compLst.erase(svi);
133  dp_props.setVal(RDKit::detail::computedPropName, compLst);
134  }
135  }
136  dp_props.clearVal(key);
137  };
138 
139  //! clears all of our \c computed \c properties
140  void clearComputedProps() const {
141  STR_VECT compLst;
143  BOOST_FOREACH (const std::string &sv, compLst) { dp_props.clearVal(sv); }
144  compLst.clear();
145  dp_props.setVal(RDKit::detail::computedPropName, compLst);
146  }
147  }
148 };
149 }
150 #endif
bool hasVal(const std::string &what) const
Returns whether or not the dictionary contains a particular key.
Definition: Dict.h:81
bool getPropIfPresent(const std::string &key, T &res) const
Definition: RDProps.h:109
Dict dp_props
Definition: RDProps.h:11
void clearComputedProps() const
clears all of our computed properties
Definition: RDProps.h:140
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
Definition: Dict.h:177
void getProp(const std::string &key, T &res) const
allows retrieval of a particular property value
Definition: RDProps.h:95
STR_VECT keys() const
Returns the set of keys in the dictionary.
Definition: Dict.h:93
bool getValIfPresent(const std::string &what, T &res) const
Potentially gets the value associated with a particular key returns true on success/false on failure...
Definition: Dict.h:149
Defines the Dict class.
RDProps & operator=(const RDProps &rhs)
Definition: RDProps.h:19
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
Definition: Dict.h:239
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
Definition: Dict.h:116
STR_VECT getPropList(bool includePrivate=true, bool includeComputed=true) const
returns a list with the names of our properties
Definition: RDProps.h:32
RDProps(const RDProps &rhs)
Definition: RDProps.h:18
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
void setProp(const std::string &key, T val, bool computed=false) const
sets a property value
Definition: RDProps.h:64
void clear()
Definition: RDProps.h:23
bool hasProp(const std::string &key) const
Definition: RDProps.h:114
std::vector< std::string >::iterator STR_VECT_I
Definition: types.h:210
void reset()
Clears all keys (and values) from the dictionary.
Definition: Dict.h:255
const std::string computedPropName
The Dict class can be used to store objects of arbitrary type keyed by strings.
Definition: Dict.h:33
void clearProp(const std::string &key) const
clears the value of a property
Definition: RDProps.h:127
std::vector< std::string > STR_VECT
Definition: Dict.h:26
T getProp(const std::string &key) const
Definition: RDProps.h:101