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