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