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