RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
RDValue-taggedunion.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_RDVALUE_TAGGED_UNION_H
33#define RDKIT_RDVALUE_TAGGED_UNION_H
34
35#include <cassert>
36#include "Invariant.h"
37#include <iomanip>
38#include <sstream>
39#include <vector>
40#include <cstdint>
42#include <cstdint>
43#include <any>
44#include <boost/utility.hpp>
45#include <boost/lexical_cast.hpp>
46#include <boost/numeric/conversion/cast.hpp>
47#include <boost/type_traits/is_floating_point.hpp>
49#include "LocaleSwitcher.h"
50
51#define RDVALUE_HASBOOL
52
53namespace RDKit {
54
55// RDValue does not dynamically create POD types (kind of like
56// cdiggins::any) However, it doesn't use RTTI type info
57// directly, it uses a companion short valued type
58// to determine what to do.
59// For unregistered types, it falls back to std::any.
60// The Size of an RDAny is (sizeof(double) + sizeof(short) == 10 bytes
61// (aligned to actually 16 so hard to pass as value type)
62//
63// For the sake of compatibility, errors throw std::bad_any_cast
64//
65// Examples:
66//
67// RDAny v(2.);
68// v = 1;
69// std::vector<double> d;
70// v == d;
71// v.asDoubleVect().push_back(4.)
72// rdany_cast<std::vector<double>(v).push_back(4.)
73//
74// Falls back to std::any for non registered types
75// v = boost::shared_ptr<ROMol>(new ROMol(m));
76//
77
78// RDValue does not manange memory of non-pod data
79// this must be done externally (string, Any, vector...)
80// Tagged union
81
82namespace RDTypeTag {
83const short EmptyTag = 0;
84const short IntTag = 1;
85const short DoubleTag = 2;
86const short StringTag = 3;
87const short FloatTag = 4;
88const short BoolTag = 5;
89const short UnsignedIntTag = 6;
90const short AnyTag = 7;
91const short VecDoubleTag = 8;
92const short VecFloatTag = 9;
93const short VecIntTag = 10;
94const short VecUnsignedIntTag = 11;
95const short VecStringTag = 12;
96template <class T>
97inline short GetTag() {
98 return AnyTag;
99}
100template <>
101inline short GetTag<double>() {
102 return DoubleTag;
103}
104template <>
105inline short GetTag<float>() {
106 return FloatTag;
107}
108template <>
109inline short GetTag<int>() {
110 return IntTag;
111}
112template <>
113inline short GetTag<unsigned int>() {
114 return UnsignedIntTag;
115}
116template <>
117inline short GetTag<bool>() {
118 return BoolTag;
119}
120template <>
121inline short GetTag<std::string>() {
122 return StringTag;
123}
124template <>
126 return VecDoubleTag;
127}
128template <>
130 return VecFloatTag;
131}
132template <>
133inline short GetTag<std::vector<int>>() {
134 return VecIntTag;
135}
136template <>
138 return VecUnsignedIntTag;
139}
140template <>
142 return VecStringTag;
143}
144template <>
145inline short GetTag<std::any>() {
146 return AnyTag;
147}
148
149namespace detail {
150union Value {
151 double d;
152 float f;
153 int i;
154 unsigned u;
155 bool b;
156 std::string *s;
157 std::any *a;
158 std::vector<double> *vd;
159 std::vector<float> *vf;
160 std::vector<int> *vi;
161 std::vector<unsigned int> *vu;
162 std::vector<std::string> *vs;
163
164 inline Value() {}
165 inline Value(double v) : d(v) {}
166 inline Value(float v) : f(v) {}
167 inline Value(int v) : i(v) {}
168 inline Value(unsigned int v) : u(v) {}
169 inline Value(bool v) : b(v) {}
170 inline Value(std::string *v) : s(v) {}
171 inline Value(std::any *v) : a(v) {}
172 inline Value(std::vector<double> *v) : vd(v) {}
173 inline Value(std::vector<float> *v) : vf(v) {}
174 inline Value(std::vector<int> *v) : vi(v) {}
175 inline Value(std::vector<unsigned int> *v) : vu(v) {}
176 inline Value(std::vector<std::string> *v) : vs(v) {}
177};
178
179template <class T>
180inline T *valuePtrCast(Value value) {
181 return std::any_cast<T *>(*value.a);
182}
183template <>
184inline std::any *valuePtrCast<std::any>(Value value) {
185 return value.a;
186}
187
188template <>
189inline std::string *valuePtrCast<std::string>(Value value) {
190 return value.s;
191}
192template <>
193inline std::vector<double> *valuePtrCast<std::vector<double>>(Value value) {
194 return value.vd;
195}
196template <>
197inline std::vector<float> *valuePtrCast<std::vector<float>>(Value value) {
198 return value.vf;
199}
200template <>
201inline std::vector<int> *valuePtrCast<std::vector<int>>(Value value) {
202 return value.vi;
203}
204template <>
205inline std::vector<unsigned int> *valuePtrCast<std::vector<unsigned int>>(
206 Value value) {
207 return value.vu;
208}
209template <>
210inline std::vector<std::string> *valuePtrCast<std::vector<std::string>>(
211 Value value) {
212 return value.vs;
213}
214} // namespace detail
215} // namespace RDTypeTag
216
217struct RDValue {
219 short type;
220 short reserved_tag = 0; // 16 bit alignment
221
222 inline RDValue() : value(0.0), type(RDTypeTag::EmptyTag) {}
223 // Pod Style (Direct storage)
224 inline RDValue(double v) : value(v), type(RDTypeTag::DoubleTag) {}
225 inline RDValue(float v) : value(v), type(RDTypeTag::FloatTag) {}
226 inline RDValue(int v) : value(v), type(RDTypeTag::IntTag) {}
227 inline RDValue(unsigned v) : value(v), type(RDTypeTag::UnsignedIntTag) {}
228 inline RDValue(bool v) : value(v), type(RDTypeTag::BoolTag) {}
229
230 inline RDValue(std::any *v) : value(v), type(RDTypeTag::AnyTag) {}
231
232 // Copies passed in pointers
233 inline RDValue(const std::any &v)
234 : value(new std::any(v)), type(RDTypeTag::AnyTag) {}
235 inline RDValue(const std::string &v)
236 : value(new std::string(v)), type(RDTypeTag::StringTag) {}
237 template <class T>
238 inline RDValue(const T &v)
239 : value(new std::any(v)), type(RDTypeTag::AnyTag) {}
240
241 inline RDValue(const std::vector<double> &v)
242 : value(new std::vector<double>(v)), type(RDTypeTag::VecDoubleTag) {}
243 inline RDValue(const std::vector<float> &v)
244 : value(new std::vector<float>(v)), type(RDTypeTag::VecFloatTag) {}
245 inline RDValue(const std::vector<int> &v)
246 : value(new std::vector<int>(v)), type(RDTypeTag::VecIntTag) {}
247 inline RDValue(const std::vector<unsigned int> &v)
248 : value(new std::vector<unsigned int>(v)),
249 type(RDTypeTag::VecUnsignedIntTag) {}
250 inline RDValue(const std::vector<std::string> &v)
251 : value(new std::vector<std::string>(v)), type(RDTypeTag::VecStringTag) {}
252
253 short getTag() const { return type; }
254
255 // ptrCast - unsafe, use rdvalue_cast instead.
256 template <class T>
257 inline T *ptrCast() const {
259 }
260
261 // RDValue doesn't have an explicit destructor, it must
262 // be wrapped in a container.
263 // The idea is that POD types don't need to be destroyed
264 // and this allows the container optimization possibilities.
265 bool needsCleanup() const {
266 switch (type) {
274 return true;
275 default:
276 return false;
277 }
278 }
279
280 // Keep in sync with needsCleanup() above.
281 void destroy() {
282 switch (type) {
284 delete value.s;
285 break;
287 delete value.a;
288 break;
290 delete value.vd;
291 break;
293 delete value.vf;
294 break;
296 delete value.vi;
297 break;
299 delete value.vu;
300 break;
302 delete value.vs;
303 break;
304 default:
305 break;
306 }
308 }
309
310 static // Given a type and an RDAnyValue - delete the appropriate structure
311 inline void
313 rdvalue.destroy();
314 }
315};
316
317/////////////////////////////////////////////////////////////////////////////////////
318// Given two RDValue::Values - copy the appropriate structure
319// RDValue doesn't have a copy constructor, the default
320// copy act's like a move for better value semantics.
321// Containers may need to copy though.
322inline void copy_rdvalue(RDValue &dest, const RDValue &src) {
323 if (&dest == &src) { // don't copy over yourself
324 return;
325 }
326 dest.destroy();
327 dest.type = src.type;
328 switch (src.type) {
330 dest.value.s = new std::string(*src.value.s);
331 break;
333 dest.value.a = new std::any(*src.value.a);
334 break;
336 dest.value.vd = new std::vector<double>(*src.value.vd);
337 break;
339 dest.value.vf = new std::vector<float>(*src.value.vf);
340 break;
342 dest.value.vi = new std::vector<int>(*src.value.vi);
343 break;
345 dest.value.vu = new std::vector<unsigned int>(*src.value.vu);
346 break;
348 dest.value.vs = new std::vector<std::string>(*src.value.vs);
349 break;
350 default:
351 dest = src;
352 }
353}
354
355#ifdef RDK_32BIT_BUILD
356// avoid register pressure and spilling on 32 bit systems
357typedef const RDValue &RDValue_cast_t;
358#else
359typedef RDValue RDValue_cast_t;
360#endif
361
362/////////////////////////////////////////////////////////////////////////////////////
363// rdvalue_is<T>
364
365template <class T>
366inline bool rdvalue_is(RDValue_cast_t v) {
367 const short tag =
369
370 // If we are an Any tag, check the any type info
371 // see the template specialization below if we are
372 // looking for a boost any directly
373 if (v.getTag() == RDTypeTag::AnyTag) {
374 return v.value.a->type() == typeid(T);
375 }
376
377 if (v.getTag() == tag) {
378 return true;
379 }
380
381 return false;
382}
383
384template <>
386 // If we are explicitly looking for a std::any
387 // then just check the top level tag
388 const short tag = RDTypeTag::GetTag<std::any>();
389 return v.getTag() == tag;
390}
391/////////////////////////////////////////////////////////////////////////////////////
392// rdvalue_cast<T>
393//
394// POD types do not support reference semantics. Other types do.
395// rdvalue_cast<const std::vector<double> &>(RDValue); // ok
396// rdvalue_cast<const float &>(RDValue); // bad_any_cast
397
398// Get stuff stored in boost any
399template <class T>
400inline T rdvalue_cast(RDValue_cast_t v) {
401 // Disable reference and pointer casts to POD data.
402 BOOST_STATIC_ASSERT(!(
403 (boost::is_pointer<T>::value &&
404 (boost::is_integral<typename boost::remove_pointer<T>::type>::value ||
405 boost::is_floating_point<
406 typename boost::remove_pointer<T>::type>::value)) ||
407 (boost::is_reference<T>::value &&
408 (boost::is_integral<typename boost::remove_reference<T>::type>::value ||
409 boost::is_floating_point<
410 typename boost::remove_reference<T>::type>::value))));
411
412 if (rdvalue_is<std::any>(v)) {
413 return std::any_cast<T>(*v.ptrCast<std::any>());
414 }
415 throw std::bad_any_cast();
416}
417
418// POD casts
419template <>
421 if (rdvalue_is<double>(v)) {
422 return v.value.d;
423 }
424 if (rdvalue_is<float>(v)) {
425 return v.value.f;
426 }
427 throw std::bad_any_cast();
428}
429
430template <>
432 if (rdvalue_is<float>(v)) {
433 return v.value.f;
434 }
435 if (rdvalue_is<double>(v)) {
436 return boost::numeric_cast<float>(v.value.d);
437 }
438 throw std::bad_any_cast();
439}
440
441template <>
443 if (rdvalue_is<int>(v)) {
444 return v.value.i;
445 }
447 return boost::numeric_cast<int>(v.value.u);
448 }
449 throw std::bad_any_cast();
450}
451
452template <>
454 if (rdvalue_is<int>(v)) {
455 return boost::numeric_cast<std::int8_t>(v.value.i);
456 }
458 return boost::numeric_cast<std::int8_t>(v.value.u);
459 }
460 throw std::bad_any_cast();
461}
462
463template <>
465 if (rdvalue_is<int>(v)) {
466 return boost::numeric_cast<std::int16_t>(v.value.i);
467 }
469 return boost::numeric_cast<std::int16_t>(v.value.u);
470 }
471 throw std::bad_any_cast();
472}
473
474template <>
476 if (rdvalue_is<int>(v)) {
477 return static_cast<std::int64_t>(v.value.i);
478 }
480 return static_cast<std::int64_t>(v.value.u);
481 }
482 if (rdvalue_is<std::any>(v)) {
483 return std::any_cast<std::int64_t>(*v.ptrCast<std::any>());
484 }
485 throw std::bad_any_cast();
486}
487
488template <>
491 return v.value.u;
492 }
493 if (rdvalue_is<int>(v)) {
494 return boost::numeric_cast<unsigned int>(v.value.i);
495 }
496 throw std::bad_any_cast();
497}
498
499template <>
501 if (rdvalue_is<int>(v)) {
502 return boost::numeric_cast<std::uint8_t>(v.value.i);
503 }
505 return boost::numeric_cast<std::uint8_t>(v.value.u);
506 }
507 throw std::bad_any_cast();
508}
509
510template <>
512 if (rdvalue_is<int>(v)) {
513 return boost::numeric_cast<std::uint16_t>(v.value.i);
514 }
516 return boost::numeric_cast<std::uint16_t>(v.value.u);
517 }
518 throw std::bad_any_cast();
519}
520
521template <>
524 return static_cast<std::uint64_t>(v.value.u);
525 }
526 if (rdvalue_is<int>(v)) {
527 return boost::numeric_cast<std::uint64_t>(v.value.i);
528 }
529 if (rdvalue_is<std::any>(v)) {
530 return std::any_cast<std::uint64_t>(*v.ptrCast<std::any>());
531 }
532 throw std::bad_any_cast();
533}
534
535template <>
537 if (rdvalue_is<bool>(v)) {
538 return v.value.b;
539 }
540 throw std::bad_any_cast();
541}
542
543} // namespace RDKit
544#endif
std::any * valuePtrCast< std::any >(Value value)
std::string * valuePtrCast< std::string >(Value value)
boost::uint64_t GetTag< int >()
static const boost::uint64_t UnsignedIntTag
static const boost::uint64_t StringTag
boost::uint64_t GetTag< std::string >()
boost::uint64_t GetTag< bool >()
boost::uint64_t GetTag< unsigned int >()
boost::uint64_t GetTag()
static const boost::uint64_t VecStringTag
boost::uint64_t GetTag< std::any >()
static const boost::uint64_t VecIntTag
static const boost::uint64_t FloatTag
static const boost::uint64_t VecUnsignedIntTag
static const boost::uint64_t DoubleTag
boost::uint64_t GetTag< double >()
static const boost::uint64_t IntTag
boost::uint64_t GetTag< float >()
static const boost::uint64_t AnyTag
static const boost::uint64_t VecFloatTag
static const boost::uint64_t VecDoubleTag
static const boost::uint64_t BoolTag
Std stuff.
std::int16_t rdvalue_cast< std::int16_t >(RDValue_cast_t v)
int rdvalue_cast< int >(RDValue_cast_t v)
unsigned int rdvalue_cast< unsigned int >(RDValue_cast_t v)
std::uint8_t rdvalue_cast< std::uint8_t >(RDValue_cast_t v)
bool rdvalue_is(const RDValue_cast_t)
std::int64_t rdvalue_cast< std::int64_t >(RDValue_cast_t v)
std::uint64_t rdvalue_cast< std::uint64_t >(RDValue_cast_t v)
double rdvalue_cast< double >(RDValue_cast_t v)
void copy_rdvalue(RDValue &dest, const RDValue &src)
T rdvalue_cast(RDValue_cast_t v)
std::int8_t rdvalue_cast< std::int8_t >(RDValue_cast_t v)
std::uint16_t rdvalue_cast< std::uint16_t >(RDValue_cast_t v)
RDValue RDValue_cast_t
bool rdvalue_cast< bool >(RDValue_cast_t v)
float rdvalue_cast< float >(RDValue_cast_t v)
bool rdvalue_is< std::any >(RDValue_cast_t v)
RDValue(const std::vector< int > &v)
RDValue(const std::vector< std::string > &v)
RDValue(const std::vector< double > &v)
RDValue(const std::vector< unsigned int > &v)
bool needsCleanup() const
boost::uint64_t getTag() const
RDTypeTag::detail::Value value
RDValue(const std::string &v)
RDValue(const std::vector< float > &v)
RDValue(const std::any &v)
static void cleanup_rdvalue(RDValue &rdvalue)
Value(std::vector< std::string > *v)
std::vector< std::string > * vs
Value(std::vector< unsigned int > *v)
Value(std::vector< double > *v)
std::vector< unsigned int > * vu