RDKit
Open-source cheminformatics and machine learning.
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
35 #include <boost/any.hpp>
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 <iostream>
45 #include <sstream>
46 namespace RDKit {
47 
48 // RDValue does not dynamically create POD types (kind of like
49 // cdiggins::any) However, it doesn't use RTTI type info
50 // directly, it uses a companion short valued type
51 // to determine what to do.
52 // For unregistered types, it falls back to boost::any.
53 // The Size of an RDAny is (sizeof(double) + sizeof(short) == 10 bytes)
54 //
55 // For the sake of compatibility, errors throw boost::bad_any_cast
56 //
57 // Examples:
58 //
59 // RDAny v(2.);
60 // v = 1;
61 // std::vector<double> d;
62 // v == d;
63 // v.asDoubleVect().push_back(4.)
64 // rdany_cast<std::vector<double>(v).push_back(4.)
65 //
66 // Falls back to boost::any for non registered types
67 // v = boost::shared_ptr<ROMol>(new ROMol(m));
68 //
69 
70 // Safe container for RDValue -- cleans up memory and copy constructs
71 struct RDAny {
73 
74  RDAny() : m_value() {}
75  template <class T>
76  RDAny(const T &d) : m_value(d) {}
77  /*
78  explicit RDAny(bool v) : m_value(v) {}
79  template <class T>
80  explicit RDAny(std::vector<T> *v) : m_value(v) {}
81  template <class T>
82  explicit RDAny(const boost::shared_ptr<T> &v) : m_value(v) {}
83  */
84  RDAny(const RDAny &rhs) { copy_rdvalue(m_value, rhs.m_value); }
85 
87 
88  // For easy of use:
89  // RDAny v;
90  // v = 2.0;
91  // v = std::string("foo...");
92 
93  RDAny &operator=(const RDAny &rhs) {
95  return *this;
96  }
97 
98  RDAny &operator=(float d) {
100  m_value = RDValue(d);
101  return *this;
102  }
103 
104  RDAny &operator=(int d) {
106  m_value = RDValue(d);
107  return *this;
108  }
109 
110  RDAny &operator=(unsigned int d) {
112  m_value = RDValue(d);
113  return *this;
114  }
115 
116  RDAny &operator=(bool d) {
118  m_value = RDValue(d);
119  return *this;
120  }
121 
122  RDAny &operator=(const std::string &d) {
124  m_value = RDValue(d);
125  return *this;
126  }
127 
128  RDAny &operator=(const std::vector<double> &d) {
130  m_value = RDValue(d);
131  return *this;
132  }
133 
134  RDAny &operator=(const std::vector<float> &d) {
136  m_value = RDValue(d);
137  return *this;
138  }
139 
140  RDAny &operator=(const std::vector<int> &d) {
142  m_value = RDValue(d);
143  return *this;
144  }
145 
146  RDAny &operator=(const std::vector<unsigned int> &d) {
148  m_value = d;
149  return *this;
150  }
151 
152  RDAny &operator=(const std::vector<std::string> &d) {
154  m_value = RDValue(d);
155  return *this;
156  }
157 
158  RDAny &operator=(const boost::any &d) {
160  m_value = RDValue(d); // new boost::any(d);
161  return *this;
162  }
163 
164  template <class T>
165  RDAny &operator=(const T &d) {
167  boost::any *v = new boost::any(d);
168  m_value = RDValue(v);
169  return *this;
170  }
171 };
172 
173 ////////////////////////////////////////////////////////////////
174 // rdany_cast
175 ////////////////////////////////////////////////////////////////
176 
177 // Const Access
178 template <class T>
179 const T rdany_cast(const RDAny &d) {
180  return rdvalue_cast<T>(d.m_value);
181 }
182 
183 // Direct access
184 template <class T>
186  return rdvalue_cast<T>(d.m_value);
187 }
188 
189 template <class T>
190 typename boost::enable_if<boost::is_arithmetic<T>, T>::type from_rdany(
191  const RDAny &arg) {
192  T res;
193  if (arg.m_value.getTag() == RDTypeTag::StringTag) {
195  try {
196  res = rdany_cast<T>(arg);
197  } catch (const boost::bad_any_cast &exc) {
198  try {
199  res = boost::lexical_cast<T>(rdany_cast<std::string>(arg));
200  } catch (...) {
201  throw exc;
202  }
203  }
204  } else {
205  res = rdany_cast<T>(arg);
206  }
207  return res;
208 }
209 
210 template <class T>
211 typename boost::disable_if<boost::is_arithmetic<T>, T>::type from_rdany(
212  const RDAny &arg) {
213  return rdany_cast<T>(arg);
214 }
215 
216 } // namespace RDKit
217 #endif
RDKit::RDAny::~RDAny
~RDAny()
Definition: RDAny.h:86
RDKit::rdany_cast
const T rdany_cast(const RDAny &d)
Definition: RDAny.h:179
RDKit::RDAny::m_value
RDValue m_value
Definition: RDAny.h:72
BoostStartInclude.h
RDKit::RDAny::RDAny
RDAny(const RDAny &rhs)
Definition: RDAny.h:84
RDKit::RDValue
Definition: RDValue-doublemagic.h:167
RDKit::RDAny
Definition: RDAny.h:71
RDKit::RDAny::operator=
RDAny & operator=(unsigned int d)
Definition: RDAny.h:110
RDKit::RDAny::operator=
RDAny & operator=(const T &d)
Definition: RDAny.h:165
RDKit::RDValue::cleanup_rdvalue
static void cleanup_rdvalue(RDValue v)
Definition: RDValue-doublemagic.h:331
BoostEndInclude.h
RDKit::RDAny::operator=
RDAny & operator=(float d)
Definition: RDAny.h:98
RDKit::RDAny::operator=
RDAny & operator=(int d)
Definition: RDAny.h:104
RDKit::RDAny::operator=
RDAny & operator=(bool d)
Definition: RDAny.h:116
RDKit::RDAny::RDAny
RDAny()
Definition: RDAny.h:74
RDKit::Utils::LocaleSwitcher
Definition: LocaleSwitcher.h:44
LocaleSwitcher.h
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< double > &d)
Definition: RDAny.h:128
RDKit::RDTypeTag::StringTag
static const boost::uint64_t StringTag
Definition: RDValue-doublemagic.h:104
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::RDAny::operator=
RDAny & operator=(const RDAny &rhs)
Definition: RDAny.h:93
RDKit::RDValue::getTag
boost::uint64_t getTag() const
Definition: RDValue-doublemagic.h:282
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< unsigned int > &d)
Definition: RDAny.h:146
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< int > &d)
Definition: RDAny.h:140
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< float > &d)
Definition: RDAny.h:134
RDKit::RDAny::operator=
RDAny & operator=(const std::vector< std::string > &d)
Definition: RDAny.h:152
RDKit::from_rdany
boost::enable_if< boost::is_arithmetic< T >, T >::type from_rdany(const RDAny &arg)
Definition: RDAny.h:190
RDValue.h
RDKit::RDAny::operator=
RDAny & operator=(const boost::any &d)
Definition: RDAny.h:158
RDKit::copy_rdvalue
void copy_rdvalue(RDValue &dest, const RDValue &src)
Definition: RDValue-doublemagic.h:339
RDKit::RDAny::operator=
RDAny & operator=(const std::string &d)
Definition: RDAny.h:122
RDKit::RDAny::RDAny
RDAny(const T &d)
Definition: RDAny.h:76
export.h