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