RDKit
Open-source cheminformatics and machine learning.
Dict.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2003-2008 Greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 /*! \file Dict.h
11 
12  \brief Defines the Dict class
13 
14 */
15 #ifndef __RD_DICT_H__
16 #define __RD_DICT_H__
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 #include <boost/any.hpp>
22 #include <RDGeneral/Exceptions.h>
23 #include <boost/lexical_cast.hpp>
24 
25 namespace RDKit {
26 typedef std::vector<std::string> STR_VECT;
27 
28 //! \brief The \c Dict class can be used to store objects of arbitrary
29 //! type keyed by \c strings.
30 //!
31 //! The actual storage is done using \c boost::any objects.
32 //!
33 class Dict {
34  public:
35  typedef std::map<std::string, boost::any> DataType;
36  Dict() { _data.clear(); };
37 
38  Dict(const Dict &other) : _data(other._data){};
39 
40  Dict &operator=(const Dict &other) {
41  _data = other._data;
42  return *this;
43  };
44 
45  //----------------------------------------------------------
46  //! \brief Returns whether or not the dictionary contains a particular
47  //! key.
48  bool hasVal(const char *what) const {
49  std::string key(what);
50  return hasVal(key);
51  };
52  bool hasVal(const std::string &what) const {
53  return _data.find(what) != _data.end();
54  };
55 
56  //----------------------------------------------------------
57  //! Returns the set of keys in the dictionary
58  /*!
59  \return a \c STR_VECT
60  */
61  STR_VECT keys() const {
62  STR_VECT res;
63  DataType::const_iterator item;
64  for (item = _data.begin(); item != _data.end(); item++) {
65  res.push_back(item->first);
66  }
67  return res;
68  }
69 
70  //----------------------------------------------------------
71  //! \brief Gets the value associated with a particular key
72  /*!
73  \param what the key to lookup
74  \param res a reference used to return the result
75 
76  <B>Notes:</b>
77  - If \c res is a \c std::string, every effort will be made
78  to convert the specified element to a string using the
79  \c boost::lexical_cast machinery.
80  - If the dictionary does not contain the key \c what,
81  a KeyErrorException will be thrown.
82  */
83  template <typename T>
84  void getVal(const std::string &what, T &res) const {
85  res = getVal<T>(what);
86  };
87  //! \overload
88  template <typename T>
89  T getVal(const std::string &what) const {
90  DataType::const_iterator pos = _data.find(what);
91  if (pos == _data.end()) throw KeyErrorException(what);
92  const boost::any &val = pos->second;
93  return fromany<T>(val);
94  }
95 
96  //! \overload
97  template <typename T>
98  T getVal(const char *what,
99  T &res) const { // FIX: it doesn't really fit that this returns T
100  std::string key(what);
101  res = getVal<T>(key);
102  return res;
103  };
104  //! \overload
105  template <typename T>
106  T getVal(const char *what) const {
107  std::string key(what);
108  return getVal<T>(key);
109  };
110 
111  //! \overload
112  void getVal(const std::string &what, std::string &res) const;
113  //! \overload
114  void getVal(const char *what, std::string &res) const {
115  getVal(std::string(what), res);
116  };
117 
118  //----------------------------------------------------------
119  //! \brief Potentially gets the value associated with a particular key
120  //! returns true on success/false on failure.
121  /*!
122  \param what the key to lookup
123  \param res a reference used to return the result
124 
125  <B>Notes:</b>
126  - If \c res is a \c std::string, every effort will be made
127  to convert the specified element to a string using the
128  \c boost::lexical_cast machinery.
129  - If the dictionary does not contain the key \c what,
130  a KeyErrorException will be thrown.
131  */
132 
133  template <typename T>
134  bool getValIfPresent(const std::string &what, T &res) const {
135  DataType::const_iterator pos = _data.find(what);
136  if (pos == _data.end()) return false;
137  const boost::any &val = pos->second;
138  res = fromany<T>(val);
139  return true;
140  };
141 
142  template <typename T>
143  bool getValIfPresent(const char *what, T &res) const {
144  std::string key(what);
145  return getValIfPresent<T>(key, res);
146  };
147 
148  //! \overload
149  bool getValIfPresent(const std::string &what, std::string &res) const;
150  //! \overload
151  bool getValIfPresent(const char *what, std::string &res) const {
152  return getValIfPresent(std::string(what), res);
153  };
154 
155  //----------------------------------------------------------
156  //! \brief Sets the value associated with a key
157  /*!
158 
159  \param what the key to set
160  \param val the value to store
161 
162  <b>Notes:</b>
163  - If \c val is a <tt>const char *</tt>, it will be converted
164  to a \c std::string for storage.
165  - If the dictionary already contains the key \c what,
166  the value will be replaced.
167  */
168  template <typename T>
169  void setVal(const std::string &what, T &val) {
170  _data[what] = toany(val);
171  };
172  //! \overload
173  template <typename T>
174  void setVal(const char *what, T &val) {
175  std::string key = what;
176  setVal(key, val);
177  };
178  //! \overload
179  void setVal(const std::string &what, const char *val) {
180  std::string h(val);
181  setVal(what, h);
182  }
183 
184  //----------------------------------------------------------
185  //! \brief Clears the value associated with a particular key,
186  //! removing the key from the dictionary.
187  /*!
188 
189  \param what the key to clear
190 
191  <b>Notes:</b>
192  - If the dictionary does not contain the key \c what,
193  a KeyErrorException will be thrown.
194  */
195  void clearVal(const std::string &what) {
196  if (!this->hasVal(what)) throw KeyErrorException(what);
197  _data.erase(what);
198  };
199 
200  //! \overload
201  void clearVal(const char *what) {
202  std::string key = what;
203  clearVal(key);
204  };
205 
206  //----------------------------------------------------------
207  //! \brief Clears all keys (and values) from the dictionary.
208  //!
209  void reset() { _data.clear(); };
210 
211  //----------------------------------------------------------
212  //! Converts a \c boost::any to type \c T
213  /*!
214  \param arg a \c boost::any reference
215 
216  \returns the converted object of type \c T
217  */
218  template <typename T>
219  T fromany(const boost::any &arg) const;
220 
221  //----------------------------------------------------------
222  //! Converts an instance of type \c T to \c boost::any
223  /*!
224  \param arg the object to be converted
225 
226  \returns a \c boost::any instance
227  */
228  template <typename T>
229  boost::any toany(T arg) const;
230 
231  private:
232  DataType _data; //!< the actual dictionary
233 };
234 }
235 #endif
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
Definition: Dict.h:169
void getVal(const char *what, std::string &res) const
Definition: Dict.h:114
bool hasVal(const char *what) const
Returns whether or not the dictionary contains a particular key.
Definition: Dict.h:48
Dict()
Definition: Dict.h:36
void setVal(const char *what, T &val)
Definition: Dict.h:174
std::map< std::string, boost::any > DataType
Definition: Dict.h:35
void setVal(const std::string &what, const char *val)
Definition: Dict.h:179
bool getValIfPresent(const std::string &what, T &res) const
Potentially gets the value associated with a particular key returns true on success/false on failure...
Definition: Dict.h:134
bool hasVal(const std::string &what) const
Definition: Dict.h:52
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
Definition: Dict.h:84
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
Definition: Dict.h:195
Dict(const Dict &other)
Definition: Dict.h:38
void clearVal(const char *what)
Definition: Dict.h:201
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
boost::any toany(T arg) const
Converts an instance of type T to boost::any.
STR_VECT keys() const
Returns the set of keys in the dictionary.
Definition: Dict.h:61
T getVal(const char *what) const
Definition: Dict.h:106
T fromany(const boost::any &arg) const
Converts a boost::any to type T.
Dict & operator=(const Dict &other)
Definition: Dict.h:40
T getVal(const std::string &what) const
Definition: Dict.h:89
void reset()
Clears all keys (and values) from the dictionary.
Definition: Dict.h:209
bool getValIfPresent(const char *what, std::string &res) const
Definition: Dict.h:151
T getVal(const char *what, T &res) const
Definition: Dict.h:98
The Dict class can be used to store objects of arbitrary type keyed by strings.
Definition: Dict.h:33
std::vector< std::string > STR_VECT
Definition: Dict.h:26
Class to allow us to throw a KeyError from C++ and have it make it back to Python.
Definition: Exceptions.h:48
bool getValIfPresent(const char *what, T &res) const
Definition: Dict.h:143