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() { dp_props.reset(); }
24  //! gets the underlying Dictionary
25  const Dict &getDict() const { return dp_props; }
26  Dict &getDict() { return dp_props; }
27 
28  // ------------------------------------
29  // Local Property Dict functionality
30  // all setProp functions are const because they
31  // are not meant to change the atom chemically
32  // ------------------------------------
33  //! returns a list with the names of our \c properties
34  STR_VECT getPropList(bool includePrivate = true,
35  bool includeComputed = true) const {
36  const STR_VECT &tmp = dp_props.keys();
37  STR_VECT res, computed;
38  if (!includeComputed &&
40  computed.push_back(RDKit::detail::computedPropName);
41  }
42 
43  STR_VECT::const_iterator pos = tmp.begin();
44  while (pos != tmp.end()) {
45  if ((includePrivate || (*pos)[0] != '_') &&
46  std::find(computed.begin(), computed.end(), *pos) == computed.end()) {
47  res.push_back(*pos);
48  }
49  pos++;
50  }
51  return res;
52  }
53 
54  //! sets a \c property value
55  /*!
56  \param key the name under which the \c property should be stored.
57  If a \c property is already stored under this name, it will be
58  replaced.
59  \param val the value to be stored
60  \param computed (optional) allows the \c property to be flagged
61  \c computed.
62  */
63 
64  //! \overload
65  template <typename T>
66  void setProp(const std::string &key, T val, bool computed = false) const {
67  if (computed) {
68  STR_VECT compLst;
70  if (std::find(compLst.begin(), compLst.end(), key) == compLst.end()) {
71  compLst.push_back(key);
72  dp_props.setVal(RDKit::detail::computedPropName, compLst);
73  }
74  }
75  // setProp(key.c_str(),val);
76  dp_props.setVal(key, val);
77  }
78 
79  //! allows retrieval of a particular property value
80  /*!
81 
82  \param key the name under which the \c property should be stored.
83  If a \c property is already stored under this name, it will be
84  replaced.
85  \param res a reference to the storage location for the value.
86 
87  <b>Notes:</b>
88  - if no \c property with name \c key exists, a KeyErrorException will be
89  thrown.
90  - the \c boost::lexical_cast machinery is used to attempt type
91  conversions.
92  If this fails, a \c boost::bad_lexical_cast exception will be thrown.
93 
94  */
95  //! \overload
96  template <typename T>
97  void getProp(const std::string &key, T &res) const {
98  dp_props.getVal(key, res);
99  }
100 
101  //! \overload
102  template <typename T>
103  T getProp(const std::string &key) const {
104  return dp_props.getVal<T>(key);
105  }
106 
107  //! returns whether or not we have a \c property with name \c key
108  //! and assigns the value if we do
109  //! \overload
110  template <typename T>
111  bool getPropIfPresent(const std::string &key, T &res) const {
112  return dp_props.getValIfPresent(key, res);
113  }
114 
115  //! \overload
116  bool hasProp(const std::string &key) const { return dp_props.hasVal(key); };
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:90
bool getPropIfPresent(const std::string &key, T &res) const
Definition: RDProps.h:111
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:186
void getProp(const std::string &key, T &res) const
allows retrieval of a particular property value
Definition: RDProps.h:97
STR_VECT keys() const
Returns the set of keys in the dictionary.
Definition: Dict.h:102
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:158
Defines the Dict class.
RDProps & operator=(const RDProps &rhs)
Definition: RDProps.h:19
Dict & getDict()
Definition: RDProps.h:26
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
Definition: Dict.h:248
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
Definition: Dict.h:125
STR_VECT getPropList(bool includePrivate=true, bool includeComputed=true) const
returns a list with the names of our properties
Definition: RDProps.h:34
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:66
const Dict & getDict() const
gets the underlying Dictionary
Definition: RDProps.h:25
void clear()
Definition: RDProps.h:23
bool hasProp(const std::string &key) const
Definition: RDProps.h:116
std::vector< std::string >::iterator STR_VECT_I
Definition: types.h:212
void reset()
Clears all keys (and values) from the dictionary.
Definition: Dict.h:264
const std::string computedPropName
The Dict class can be used to store objects of arbitrary type keyed by strings.
Definition: Dict.h:35
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:28
T getProp(const std::string &key) const
Definition: RDProps.h:103