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