RDKit
Open-source cheminformatics and machine learning.
Toggle main menu visibility
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>
35
#include <
RDGeneral/BoostStartInclude.h
>
36
#include <boost/utility.hpp>
37
#include <boost/lexical_cast.hpp>
38
#include <
RDGeneral/BoostEndInclude.h
>
39
40
#include "
LocaleSwitcher.h
"
41
#include "
RDValue.h
"
42
#include <string>
43
#include <vector>
44
#include <sstream>
45
namespace
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
70
struct
RDAny
{
71
RDValue
m_value
;
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
85
~RDAny
() {
RDValue::cleanup_rdvalue
(
m_value
); }
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) {
93
copy_rdvalue
(
m_value
, rhs.
m_value
);
94
return
*
this
;
95
}
96
97
RDAny
&
operator=
(
float
d) {
98
RDValue::cleanup_rdvalue
(
m_value
);
99
m_value
=
RDValue
(d);
100
return
*
this
;
101
}
102
103
RDAny
&
operator=
(
int
d) {
104
RDValue::cleanup_rdvalue
(
m_value
);
105
m_value
=
RDValue
(d);
106
return
*
this
;
107
}
108
109
RDAny
&
operator=
(
unsigned
int
d) {
110
RDValue::cleanup_rdvalue
(
m_value
);
111
m_value
=
RDValue
(d);
112
return
*
this
;
113
}
114
115
RDAny
&
operator=
(
bool
d) {
116
RDValue::cleanup_rdvalue
(
m_value
);
117
m_value
=
RDValue
(d);
118
return
*
this
;
119
}
120
121
RDAny
&
operator=
(
const
std::string &d) {
122
RDValue::cleanup_rdvalue
(
m_value
);
123
m_value
=
RDValue
(d);
124
return
*
this
;
125
}
126
127
RDAny
&
operator=
(
const
std::vector<double> &d) {
128
RDValue::cleanup_rdvalue
(
m_value
);
129
m_value
=
RDValue
(d);
130
return
*
this
;
131
}
132
133
RDAny
&
operator=
(
const
std::vector<float> &d) {
134
RDValue::cleanup_rdvalue
(
m_value
);
135
m_value
=
RDValue
(d);
136
return
*
this
;
137
}
138
139
RDAny
&
operator=
(
const
std::vector<int> &d) {
140
RDValue::cleanup_rdvalue
(
m_value
);
141
m_value
=
RDValue
(d);
142
return
*
this
;
143
}
144
145
RDAny
&
operator=
(
const
std::vector<unsigned int> &d) {
146
RDValue::cleanup_rdvalue
(
m_value
);
147
m_value
= d;
148
return
*
this
;
149
}
150
151
RDAny
&
operator=
(
const
std::vector<std::string> &d) {
152
RDValue::cleanup_rdvalue
(
m_value
);
153
m_value
=
RDValue
(d);
154
return
*
this
;
155
}
156
157
RDAny
&
operator=
(
const
std::any &d) {
158
RDValue::cleanup_rdvalue
(
m_value
);
159
m_value
=
RDValue
(d);
// new std::any(d);
160
return
*
this
;
161
}
162
163
template
<
class
T>
164
RDAny
&
operator=
(
const
T &d) {
165
RDValue::cleanup_rdvalue
(
m_value
);
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
177
template
<
class
T>
178
const
T
rdany_cast
(
const
RDAny
&d) {
179
return
rdvalue_cast<T>
(d.
m_value
);
180
}
181
182
// Direct access
183
template
<
class
T>
184
T
rdany_cast
(
RDAny
&d) {
185
return
rdvalue_cast<T>
(d.
m_value
);
186
}
187
188
template
<
class
T>
189
typename
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
) {
193
Utils::LocaleSwitcher
ls;
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
209
template
<
class
T>
210
typename
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
BoostEndInclude.h
BoostStartInclude.h
LocaleSwitcher.h
RDValue.h
RDKit::Utils::LocaleSwitcher
Definition
LocaleSwitcher.h:44
export.h
RDKit::RDTypeTag::StringTag
static const std::uint64_t StringTag
Definition
RDValue-doublemagic.h:102
RDKit
Std stuff.
Definition
Abbreviations.h:19
RDKit::copy_rdvalue
void copy_rdvalue(RDValue &dest, const RDValue &src)
Definition
RDValue-doublemagic.h:338
RDKit::rdany_cast
const T rdany_cast(const RDAny &d)
Definition
RDAny.h:178
RDKit::rdvalue_cast
T rdvalue_cast(RDValue_cast_t v)
Definition
RDValue-doublemagic.h:419
RDKit::from_rdany
boost::enable_if< boost::is_arithmetic< T >, T >::type from_rdany(const RDAny &arg)
Definition
RDAny.h:189
RDKit::RDAny
Definition
RDAny.h:70
RDKit::RDAny::m_value
RDValue m_value
Definition
RDAny.h:71
RDKit::RDAny::operator=
RDAny & operator=(const RDAny &rhs)
Definition
RDAny.h:92
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< std::string > &d)
Definition
RDAny.h:151
RDKit::RDAny::operator=
RDAny & operator=(const std::string &d)
Definition
RDAny.h:121
RDKit::RDAny::RDAny
RDAny(const T &d)
Definition
RDAny.h:75
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< double > &d)
Definition
RDAny.h:127
RDKit::RDAny::~RDAny
~RDAny()
Definition
RDAny.h:85
RDKit::RDAny::RDAny
RDAny(const RDAny &rhs)
Definition
RDAny.h:83
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< float > &d)
Definition
RDAny.h:133
RDKit::RDAny::operator=
RDAny & operator=(const std::any &d)
Definition
RDAny.h:157
RDKit::RDAny::operator=
RDAny & operator=(int d)
Definition
RDAny.h:103
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< unsigned int > &d)
Definition
RDAny.h:145
RDKit::RDAny::RDAny
RDAny()
Definition
RDAny.h:73
RDKit::RDAny::operator=
RDAny & operator=(const T &d)
Definition
RDAny.h:164
RDKit::RDAny::operator=
RDAny & operator=(float d)
Definition
RDAny.h:97
RDKit::RDAny::operator=
RDAny & operator=(unsigned int d)
Definition
RDAny.h:109
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< int > &d)
Definition
RDAny.h:139
RDKit::RDAny::operator=
RDAny & operator=(bool d)
Definition
RDAny.h:115
RDKit::RDValue
Definition
RDValue-doublemagic.h:165
RDKit::RDValue::getTag
std::uint64_t getTag() const
Definition
RDValue-doublemagic.h:281
RDKit::RDValue::cleanup_rdvalue
static void cleanup_rdvalue(RDValue v)
Definition
RDValue-doublemagic.h:330
RDGeneral
RDAny.h
Generated on
for RDKit by
1.17.0