RDKit: RDProps.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
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
12namespace 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 &&
50 getPropIfPresent(RDKit::detail::computedPropName, computed)) {
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;
80 getPropIfPresent(RDKit::detail::computedPropName, compLst);
81 if (std::find(compLst.begin(), compLst.end(), key) == compLst.end()) {
82 compLst.push_back(key);
83 d_props.setVal(RDKit::detail::computedPropName, compLst);
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, this will be a no-op.
132 - if the \c property is marked as \c computed, it will also be removed
133 from our list of \c computedProperties
134 */
135 //! \overload
136 void clearProp(const std::string &key) const {
137 STR_VECT compLst;
138 if (getPropIfPresent(RDKit::detail::computedPropName, compLst)) {
139 auto svi = std::find(compLst.begin(), compLst.end(), key);
140 if (svi != compLst.end()) {
141 compLst.erase(svi);
142 d_props.setVal(RDKit::detail::computedPropName, compLst);
143 }
144 }
145 d_props.clearVal(key);
146 }
147
148 //! clears all of our \c computed \c properties
149 void clearComputedProps() const {
150 STR_VECT compLst;
151 if (getPropIfPresent(RDKit::detail::computedPropName, compLst) &&
152 !compLst.empty()) {
153 for (const auto &sv : compLst) {
154 d_props.clearVal(sv);
155 }
156 compLst.clear();
157 d_props.setVal(RDKit::detail::computedPropName, compLst);
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
Dict.h
Defines the Dict class.
RDKit::Dict
The Dict class can be used to store objects of arbitrary type keyed by strings.
Definition Dict.h:36
RDKit::RDProps::getPropIfPresent
bool getPropIfPresent(const std::string &key, T &res) const
Definition RDProps.h:121
RDKit::RDProps::clearProp
void clearProp(const std::string &key) const
clears the value of a property
Definition RDProps.h:136
RDKit::RDProps::getDict
const Dict & getDict() const
gets the underlying Dictionary
Definition RDProps.h:36
RDKit::RDProps::operator=
RDProps & operator=(RDProps &&rhs) noexcept=default
RDKit::RDProps::clear
void clear()
Definition RDProps.h:34
RDKit::RDProps::operator=
RDProps & operator=(const RDProps &rhs)
Definition RDProps.h:24
RDKit::RDProps::getProp
void getProp(const std::string &key, T &res) const
allows retrieval of a particular property value
Definition RDProps.h:107
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:113
RDKit::RDProps::getDict
Dict & getDict()
Definition RDProps.h:37
RDKit::RDProps::updateProps
void updateProps(const RDProps &source, bool preserveExisting=false)
update the properties from another
Definition RDProps.h:167
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:126
RDKit::RDProps::d_props
Dict d_props
Definition RDProps.h:16
RDKit::RDProps::RDProps
RDProps()
Definition RDProps.h:22
RDKit::RDProps::setProp
void setProp(const std::string &key, T val, bool computed=false) const
sets a property value
Definition RDProps.h:77
RDKit::RDProps::RDProps
RDProps(const RDProps &rhs)
Definition RDProps.h:23
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:45
RDKit::RDProps::clearComputedProps
void clearComputedProps() const
clears all of our computed properties
Definition RDProps.h:149
RDKit::RDProps::RDProps
RDProps(RDProps &&o) noexcept=default
export.h
RDKit::detail::computedPropName
RDKIT_RDGENERAL_EXPORT const std::string computedPropName
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::STR_VECT
std::vector< std::string > STR_VECT
Definition Dict.h:29
types.h