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 #ifndef RDKIT_RDANY_H
32 #define RDKIT_RDANY_H
34 #include <boost/any.hpp>
35 #include <boost/utility.hpp>
36 #include <boost/lexical_cast.hpp>
38 
39 #include "LocaleSwitcher.h"
40 #include "RDValue.h"
41 #include <string>
42 #include <vector>
43 #include <iostream>
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 boost::any.
52 // The Size of an RDAny is (sizeof(double) + sizeof(short) == 10 bytes)
53 //
54 // For the sake of compatibility, errors throw boost::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 boost::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 {
72 
73  RDAny() : m_value() {}
74  template <class T> RDAny(const T &d) : m_value(d) {}
75  /*
76  explicit RDAny(bool v) : m_value(v) {}
77  template <class T>
78  explicit RDAny(std::vector<T> *v) : m_value(v) {}
79  template <class T>
80  explicit RDAny(const boost::shared_ptr<T> &v) : m_value(v) {}
81  */
82  RDAny(const RDAny &rhs) {
83  copy_rdvalue(m_value, rhs.m_value);
84  }
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) {
94  copy_rdvalue(m_value, rhs.m_value);
95  return *this;
96  }
97 
98  RDAny &operator=(float d) {
99  RDValue::cleanup_rdvalue(m_value);
100  m_value = RDValue(d);
101  return *this;
102  }
103 
104  RDAny &operator=(int d) {
105  RDValue::cleanup_rdvalue(m_value);
106  m_value = RDValue(d);
107  return *this;
108  }
109 
110  RDAny &operator=(unsigned int d) {
111  RDValue::cleanup_rdvalue(m_value);
112  m_value = RDValue(d);
113  return *this;
114  }
115 
116  RDAny &operator=(bool d) {
117  RDValue::cleanup_rdvalue(m_value);
118  m_value = RDValue(d);
119  return *this;
120  }
121 
122  RDAny &operator=(const std::string &d) {
123  RDValue::cleanup_rdvalue(m_value);
124  m_value = RDValue(d);
125  return *this;
126  }
127 
128  RDAny &operator=(const std::vector<double> &d) {
129  RDValue::cleanup_rdvalue(m_value);
130  m_value = RDValue(d);
131  return *this;
132  }
133 
134  RDAny &operator=(const std::vector<float> &d) {
135  RDValue::cleanup_rdvalue(m_value);
136  m_value = RDValue(d);
137  return *this;
138  }
139 
140  RDAny &operator=(const std::vector<int> &d) {
141  RDValue::cleanup_rdvalue(m_value);
142  m_value = RDValue(d);
143  return *this;
144  }
145 
146  RDAny &operator=(const std::vector<unsigned int> &d) {
147  RDValue::cleanup_rdvalue(m_value);
148  m_value = d;
149  return *this;
150  }
151 
152  RDAny &operator=(const std::vector<std::string> &d) {
153  RDValue::cleanup_rdvalue(m_value);
154  m_value = RDValue(d);
155  return *this;
156  }
157 
158  RDAny &operator=(const boost::any &d) {
159  RDValue::cleanup_rdvalue(m_value);
160  m_value = RDValue(d);//new boost::any(d);
161  return *this;
162  }
163 
164  template<class T>
165  RDAny &operator=(const T &d) {
166  RDValue::cleanup_rdvalue(m_value);
167  boost::any *v = new boost::any(d);
168  m_value = RDValue(v);
169  return *this;
170  }
171 
172 };
173 
174 ////////////////////////////////////////////////////////////////
175 // rdany_cast
176 ////////////////////////////////////////////////////////////////
177 
178 
179 // Const Access
180 template <class T>
181 const T rdany_cast(const RDAny &d) {
182  return rdvalue_cast<T>(d.m_value);
183 }
184 
185 // Direct access
186 template <class T>
188  return rdvalue_cast<T>(d.m_value);
189 }
190 
191 template <class T>
192 typename boost::enable_if<boost::is_arithmetic<T>, T>::type from_rdany(
193  const RDAny &arg) {
194  T res;
195  if (arg.m_value.getTag() == RDTypeTag::StringTag) {
197  try {
198  res = rdany_cast<T>(arg);
199  } catch (const boost::bad_any_cast &exc) {
200  try {
201  res = boost::lexical_cast<T>(rdany_cast<std::string>(arg));
202  } catch (...) {
203  throw exc;
204  }
205  }
206  } else {
207  res = rdany_cast<T>(arg);
208  }
209  return res;
210 }
211 
212 template <class T>
213 typename boost::disable_if<boost::is_arithmetic<T>, T>::type from_rdany(
214  const RDAny &arg) {
215  return rdany_cast<T>(arg);
216 }
217 
218 }
219 #endif
~RDAny()
Definition: RDAny.h:86
void copy_rdvalue(RDValue &dest, const RDValue &src)
const T rdany_cast(const RDAny &d)
Definition: RDAny.h:181
T rdvalue_cast(RDValue v)
RDValue m_value
Definition: RDAny.h:71
static void cleanup_rdvalue(RDValue v)
RDAny & operator=(const RDAny &rhs)
Definition: RDAny.h:93
static const boost::uint64_t StringTag
RDAny(const RDAny &rhs)
Definition: RDAny.h:82
RDAny & operator=(const std::vector< float > &d)
Definition: RDAny.h:134
RDAny(const T &d)
Definition: RDAny.h:74
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
RDAny & operator=(const std::vector< unsigned int > &d)
Definition: RDAny.h:146
RDAny & operator=(unsigned int d)
Definition: RDAny.h:110
RDAny & operator=(const boost::any &d)
Definition: RDAny.h:158
RDAny & operator=(int d)
Definition: RDAny.h:104
RDAny & operator=(const std::string &d)
Definition: RDAny.h:122
RDAny & operator=(const T &d)
Definition: RDAny.h:165
RDAny & operator=(const std::vector< int > &d)
Definition: RDAny.h:140
RDAny & operator=(float d)
Definition: RDAny.h:98
boost::uint64_t getTag() const
RDAny & operator=(const std::vector< double > &d)
Definition: RDAny.h:128
boost::enable_if< boost::is_arithmetic< T >, T >::type from_rdany(const RDAny &arg)
Definition: RDAny.h:192
RDAny & operator=(bool d)
Definition: RDAny.h:116
RDAny & operator=(const std::vector< std::string > &d)
Definition: RDAny.h:152