Loading...
Searching...
No Matches
RDProps.h
Go to the documentation of this file.
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDGeneral/export.h>
#ifndef RDKIT_RDPROPS_H
#define RDKIT_RDPROPS_H
#include "Dict.h"
#include "types.h"
namespace RDKit {
class RDProps {
protected:
mutable Dict d_props;
// It is a quirk of history that this is mutable
// as the RDKit allows properties to be set
// on const objects.
public:
RDProps() : d_props() {}
RDProps(const RDProps &rhs) : d_props(rhs.d_props) {}
RDProps &operator=(const RDProps &rhs) {
if (this == &rhs) {
return *this;
}
d_props = rhs.d_props;
return *this;
}
RDProps(RDProps &&o) noexcept = default;
RDProps &operator=(RDProps &&rhs) noexcept = default;
void clear() { d_props.reset(); }
//! gets the underlying Dictionary
const Dict &getDict() const { return d_props; }
Dict &getDict() { return d_props; }
// ------------------------------------
// Local Property Dict functionality
// all setProp functions are const because they
// are not meant to change the atom chemically
// ------------------------------------
//! returns a list with the names of our \c properties
STR_VECT getPropList(bool includePrivate = true,
bool includeComputed = true) const {
const STR_VECT &tmp = d_props.keys();
STR_VECT res, computed;
if (!includeComputed &&
getPropIfPresent(RDKit::detail::computedPropName, computed)) {
computed.push_back(RDKit::detail::computedPropName);
}
auto pos = tmp.begin();
while (pos != tmp.end()) {
if ((includePrivate || (*pos)[0] != '_') &&
std::find(computed.begin(), computed.end(), *pos) == computed.end()) {
res.push_back(*pos);
}
pos++;
}
return res;
}
//! sets a \c property value
/*!
\param key the name under which the \c property should be stored.
If a \c property is already stored under this name, it will be
replaced.
\param val the value to be stored
\param computed (optional) allows the \c property to be flagged
\c computed.
*/
//! \overload
template <typename T>
void setProp(const std::string &key, T val, bool computed = false) const {
if (computed) {
STR_VECT compLst;
getPropIfPresent(RDKit::detail::computedPropName, compLst);
if (std::find(compLst.begin(), compLst.end(), key) == compLst.end()) {
compLst.push_back(key);
d_props.setVal(RDKit::detail::computedPropName, compLst);
}
}
d_props.setVal(key, val);
}
//! allows retrieval of a particular property value
/*!
\param key the name under which the \c property should be stored.
If a \c property is already stored under this name, it will be
replaced.
\param res a reference to the storage location for the value.
<b>Notes:</b>
- if no \c property with name \c key exists, a KeyErrorException will be
thrown.
- the \c boost::lexical_cast machinery is used to attempt type
conversions.
If this fails, a \c boost::bad_lexical_cast exception will be thrown.
*/
//! \overload
template <typename T>
void getProp(const std::string &key, T &res) const {
d_props.getVal(key, res);
}
//! \overload
template <typename T>
T getProp(const std::string &key) const {
return d_props.getVal<T>(key);
}
//! returns whether or not we have a \c property with name \c key
//! and assigns the value if we do
//! \overload
template <typename T>
bool getPropIfPresent(const std::string &key, T &res) const {
return d_props.getValIfPresent(key, res);
}
//! \overload
bool hasProp(const std::string &key) const { return d_props.hasVal(key); }
//! clears the value of a \c property
/*!
<b>Notes:</b>
- if no \c property with name \c key exists, this will be a no-op.
- if the \c property is marked as \c computed, it will also be removed
from our list of \c computedProperties
*/
//! \overload
void clearProp(const std::string &key) const {
STR_VECT compLst;
if (getPropIfPresent(RDKit::detail::computedPropName, compLst)) {
auto svi = std::find(compLst.begin(), compLst.end(), key);
if (svi != compLst.end()) {
compLst.erase(svi);
d_props.setVal(RDKit::detail::computedPropName, compLst);
}
}
d_props.clearVal(key);
}
//! clears all of our \c computed \c properties
void clearComputedProps() const {
STR_VECT compLst;
if (getPropIfPresent(RDKit::detail::computedPropName, compLst) &&
!compLst.empty()) {
for (const auto &sv : compLst) {
d_props.clearVal(sv);
}
compLst.clear();
d_props.setVal(RDKit::detail::computedPropName, compLst);
}
}
//! update the properties from another
/*
\param source Source to update the properties from
\param preserve Existing If true keep existing data, else override from
the source
*/
void updateProps(const RDProps &source, bool preserveExisting = false) {
d_props.update(source.getDict(), preserveExisting);
}
};
} // namespace RDKit
#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