RDKit: RDProps.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 &&
39 getPropIfPresent(RDKit::detail::computedPropName, computed)) {
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;
69 getPropIfPresent(RDKit::detail::computedPropName, 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;
129 if (getPropIfPresent(RDKit::detail::computedPropName, 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;
142 if (getPropIfPresent(RDKit::detail::computedPropName, 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 //! update the properties from another
150 /*
151 \param source Source to update the properties from
152 \param preserve Existing If true keep existing data, else override from the source
153 */
154 void updateProps(const RDProps &source, bool preserveExisting=false) {
155 dp_props.update(source.getDict(), preserveExisting);
156 }
157
158 };
159
160
161
162 }
163 #endif
RDKit::Dict::hasVal
bool hasVal(const std::string &what) const
Returns whether or not the dictionary contains a particular key.
Definition: Dict.h:117
types.h
RDKit::RDProps::getPropIfPresent
bool getPropIfPresent(const std::string &key, T &res) const
Definition: RDProps.h:111
RDKit::RDProps::dp_props
Dict dp_props
Definition: RDProps.h:11
RDKit::RDProps::clearComputedProps
void clearComputedProps() const
clears all of our computed properties
Definition: RDProps.h:140
RDKit::Dict::setVal
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
Definition: Dict.h:212
RDKit::RDProps::getProp
void getProp(const std::string &key, T &res) const
allows retrieval of a particular property value
Definition: RDProps.h:97
RDKit::Dict::keys
STR_VECT keys() const
Returns the set of keys in the dictionary.
Definition: Dict.h:129
RDKit::RDProps::updateProps
void updateProps(const RDProps &source, bool preserveExisting=false)
update the properties from another
Definition: RDProps.h:154
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:185
Dict.h
Defines the Dict class.
RDKit::RDProps
Definition: RDProps.h:9
RDKit::RDProps::operator=
RDProps & operator=(const RDProps &rhs)
Definition: RDProps.h:19
RDKit::RDProps::getDict
Dict & getDict()
Definition: RDProps.h:26
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:266
RDKit::Dict::getVal
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
Definition: Dict.h:152
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:34
RDKit::RDProps::RDProps
RDProps(const RDProps &rhs)
Definition: RDProps.h:18
RDKit
Std stuff.
Definition: Atom.h:29
RDKit::RDProps::setProp
void setProp(const std::string &key, T val, bool computed=false) const
sets a property value
Definition: RDProps.h:66
RDKit::RDProps::RDProps
RDProps()
Definition: RDProps.h:17
RDKit::RDProps::getDict
const Dict & getDict() const
gets the underlying Dictionary
Definition: RDProps.h:25
RDKit::RDProps::clear
void clear()
Definition: RDProps.h:23
RDKit::Dict::update
void update(const Dict &other, bool preserveExisting=false)
Definition: Dict.h:66
RDKit::RDProps::hasProp
bool hasProp(const std::string &key) const
Definition: RDProps.h:116
RDKit::STR_VECT_I
std::vector< std::string >::iterator STR_VECT_I
Definition: types.h:213
RDKit::Dict::reset
void reset()
Clears all keys (and values) from the dictionary.
Definition: Dict.h:282
RDKit::detail::computedPropName
const std::string computedPropName
RDKit::Dict
The Dict class can be used to store objects of arbitrary type keyed by strings.
Definition: Dict.h:35
RDKit::RDProps::clearProp
void clearProp(const std::string &key) const
clears the value of a property
Definition: RDProps.h:127
RDKit::STR_VECT
std::vector< std::string > STR_VECT
Definition: Dict.h:28
RDKit::RDProps::getProp
T getProp(const std::string &key) const
Definition: RDProps.h:103