RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
RDAny.h
Go to the documentation of this file.
1// Copyright (c) 2015, Novartis Institutes for BioMedical Research Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following
12// disclaimer in the documentation and/or other materials provided
13// with the distribution.
14// * Neither the name of Novartis Institutes for BioMedical Research Inc.
15// nor the names of its contributors may be used to endorse or promote
16// products derived from this software without specific prior written
17// permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30//
31#include <RDGeneral/export.h>
32#ifndef RDKIT_RDANY_H
33#define RDKIT_RDANY_H
34#include <any>
36#include <boost/utility.hpp>
37#include <boost/lexical_cast.hpp>
39
40#include "LocaleSwitcher.h"
41#include "RDValue.h"
42#include <string>
43#include <vector>
44#include <sstream>
45namespace RDKit {
46
47// RDValue does not dynamically create POD types (kind of like
48// cdiggins::any) However, it doesn't use RTTI type info
49// directly, it uses a companion short valued type
50// to determine what to do.
51// For unregistered types, it falls back to std::any.
52// The Size of an RDAny is (sizeof(double) + sizeof(short) == 10 bytes)
53//
54// For the sake of compatibility, errors throw std::bad_any_cast
55//
56// Examples:
57//
58// RDAny v(2.);
59// v = 1;
60// std::vector<double> d;
61// v == d;
62// v.asDoubleVect().push_back(4.)
63// rdany_cast<std::vector<double>(v).push_back(4.)
64//
65// Falls back to std::any for non registered types
66// v = boost::shared_ptr<ROMol>(new ROMol(m));
67//
68
69// Safe container for RDValue -- cleans up memory and copy constructs
70struct RDAny {
72
73 RDAny() : m_value() {}
74 template <class T>
75 RDAny(const T &d) : m_value(d) {}
76 /*
77 explicit RDAny(bool v) : m_value(v) {}
78 template <class T>
79 explicit RDAny(std::vector<T> *v) : m_value(v) {}
80 template <class T>
81 explicit RDAny(const boost::shared_ptr<T> &v) : m_value(v) {}
82 */
83 RDAny(const RDAny &rhs) { copy_rdvalue(m_value, rhs.m_value); }
84
86
87 // For easy of use:
88 // RDAny v;
89 // v = 2.0;
90 // v = std::string("foo...");
91
92 RDAny &operator=(const RDAny &rhs) {
94 return *this;
95 }
96
97 RDAny &operator=(float d) {
99 m_value = RDValue(d);
100 return *this;
101 }
102
103 RDAny &operator=(int d) {
105 m_value = RDValue(d);
106 return *this;
107 }
108
109 RDAny &operator=(unsigned int d) {
111 m_value = RDValue(d);
112 return *this;
113 }
114
115 RDAny &operator=(bool d) {
117 m_value = RDValue(d);
118 return *this;
119 }
120
121 RDAny &operator=(const std::string &d) {
123 m_value = RDValue(d);
124 return *this;
125 }
126
127 RDAny &operator=(const std::vector<double> &d) {
129 m_value = RDValue(d);
130 return *this;
131 }
132
133 RDAny &operator=(const std::vector<float> &d) {
135 m_value = RDValue(d);
136 return *this;
137 }
138
139 RDAny &operator=(const std::vector<int> &d) {
141 m_value = RDValue(d);
142 return *this;
143 }
144
145 RDAny &operator=(const std::vector<unsigned int> &d) {
147 m_value = d;
148 return *this;
149 }
150
151 RDAny &operator=(const std::vector<std::string> &d) {
153 m_value = RDValue(d);
154 return *this;
155 }
156
157 RDAny &operator=(const std::any &d) {
159 m_value = RDValue(d); // new std::any(d);
160 return *this;
161 }
162
163 template <class T>
164 RDAny &operator=(const T &d) {
166 std::any *v = new std::any(d);
167 m_value = RDValue(v);
168 return *this;
169 }
170};
171
172////////////////////////////////////////////////////////////////
173// rdany_cast
174////////////////////////////////////////////////////////////////
175
176// Const Access
177template <class T>
178const T rdany_cast(const RDAny &d) {
179 return rdvalue_cast<T>(d.m_value);
180}
181
182// Direct access
183template <class T>
185 return rdvalue_cast<T>(d.m_value);
186}
187
188template <class T>
189typename boost::enable_if<boost::is_arithmetic<T>, T>::type from_rdany(
190 const RDAny &arg) {
191 T res;
192 if (arg.m_value.getTag() == RDTypeTag::StringTag) {
194 try {
195 res = rdany_cast<T>(arg);
196 } catch (const std::bad_any_cast &exc) {
197 try {
198 res = boost::lexical_cast<T>(rdany_cast<std::string>(arg));
199 } catch (...) {
200 throw exc;
201 }
202 }
203 } else {
204 res = rdany_cast<T>(arg);
205 }
206 return res;
207}
208
209template <class T>
210typename boost::disable_if<boost::is_arithmetic<T>, T>::type from_rdany(
211 const RDAny &arg) {
212 return rdany_cast<T>(arg);
213}
214
215} // namespace RDKit
216#endif
static const boost::uint64_t StringTag
Std stuff.
void copy_rdvalue(RDValue &dest, const RDValue &src)
const T rdany_cast(const RDAny &d)
Definition RDAny.h:178
T rdvalue_cast(RDValue_cast_t v)
boost::enable_if< boost::is_arithmetic< T >, T >::type from_rdany(const RDAny &arg)
Definition RDAny.h:189
RDValue m_value
Definition RDAny.h:71
RDAny & operator=(const RDAny &rhs)
Definition RDAny.h:92
RDAny & operator=(const std::vector< std::string > &d)
Definition RDAny.h:151
RDAny & operator=(const std::string &d)
Definition RDAny.h:121
RDAny(const T &d)
Definition RDAny.h:75
RDAny & operator=(const std::vector< double > &d)
Definition RDAny.h:127
RDAny(const RDAny &rhs)
Definition RDAny.h:83
RDAny & operator=(const std::vector< float > &d)
Definition RDAny.h:133
RDAny & operator=(const std::any &d)
Definition RDAny.h:157
RDAny & operator=(int d)
Definition RDAny.h:103
RDAny & operator=(const std::vector< unsigned int > &d)
Definition RDAny.h:145
RDAny & operator=(const T &d)
Definition RDAny.h:164
RDAny & operator=(float d)
Definition RDAny.h:97
RDAny & operator=(unsigned int d)
Definition RDAny.h:109
RDAny & operator=(const std::vector< int > &d)
Definition RDAny.h:139
RDAny & operator=(bool d)
Definition RDAny.h:115
boost::uint64_t getTag() const
static void cleanup_rdvalue(RDValue v)