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(){
37  _data.clear();
38  };
39 
40  Dict(const Dict &other) : _data(other._data) {};
41 
42  Dict &operator=(const Dict &other) {
43  _data = other._data;
44  return *this;
45  };
46 
47  //----------------------------------------------------------
48  //! \brief Returns whether or not the dictionary contains a particular
49  //! key.
50  bool hasVal(const char *what) const{
51  std::string key(what);
52  return hasVal(key);
53  };
54  bool hasVal(const std::string &what) const {
55  return _data.find(what)!=_data.end();
56  };
57 
58  //----------------------------------------------------------
59  //! Returns the set of keys in the dictionary
60  /*!
61  \return a \c STR_VECT
62  */
63  STR_VECT keys() const {
64  STR_VECT res;
65  DataType::const_iterator item;
66  for (item = _data.begin(); item != _data.end(); item++) {
67  res.push_back(item->first);
68  }
69  return res;
70  }
71 
72  //----------------------------------------------------------
73  //! \brief Gets the value associated with a particular key
74  /*!
75  \param what the key to lookup
76  \param res a reference used to return the result
77 
78  <B>Notes:</b>
79  - If \c res is a \c std::string, every effort will be made
80  to convert the specified element to a string using the
81  \c boost::lexical_cast machinery.
82  - If the dictionary does not contain the key \c what,
83  a KeyErrorException will be thrown.
84  */
85  template <typename T>
86  void getVal(const std::string &what,T &res) const {
87  res = getVal<T>(what);
88  };
89  //! \overload
90  template <typename T>
91  T getVal(const std::string &what) const {
92  DataType::const_iterator pos=_data.find(what);
93  if(pos==_data.end())
94  throw KeyErrorException(what);
95  const boost::any &val = pos->second;
96  return fromany<T>(val);
97  }
98 
99  //! \overload
100  template <typename T>
101  T getVal(const char *what,T &res) const { // FIX: it doesn't really fit that this returns T
102  std::string key(what);
103  res = getVal<T>(key);
104  return res;
105  };
106  //! \overload
107  template <typename T>
108  T getVal(const char *what) const {
109  std::string key(what);
110  return getVal<T>(key);
111  };
112 
113  //! \overload
114  void getVal(const std::string &what, std::string &res) const;
115  //! \overload
116  void getVal(const char *what, std::string &res) const { getVal(std::string(what),res); };
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())
137  return false;
138  const boost::any &val = pos->second;
139  res = fromany<T>(val);
140  return true;
141  };
142 
143  template <typename T>
144  bool getValIfPresent(const char *what,T &res) const {
145  std::string key(what);
146  return getValIfPresent<T>(key, res);
147  };
148 
149  //! \overload
150  bool getValIfPresent(const std::string &what, std::string &res) const;
151  //! \overload
152  bool getValIfPresent(const char *what, std::string &res) const {
153  return getValIfPresent(std::string(what),res);
154  };
155 
156  //----------------------------------------------------------
157  //! \brief Sets the value associated with a key
158  /*!
159 
160  \param what the key to set
161  \param val the value to store
162 
163  <b>Notes:</b>
164  - If \c val is a <tt>const char *</tt>, it will be converted
165  to a \c std::string for storage.
166  - If the dictionary already contains the key \c what,
167  the value will be replaced.
168  */
169  template <typename T>
170  void setVal(const std::string &what, T &val){
171  _data[what] = toany(val);
172  };
173  //! \overload
174  template <typename T>
175  void setVal(const char *what, T &val){
176  std::string key = what;
177  setVal(key,val);
178  };
179  //! \overload
180  void setVal(const std::string &what, const char *val){
181  std::string h(val);
182  setVal(what,h);
183  }
184 
185 
186 
187  //----------------------------------------------------------
188  //! \brief Clears the value associated with a particular key,
189  //! removing the key from the dictionary.
190  /*!
191 
192  \param what the key to clear
193 
194  <b>Notes:</b>
195  - If the dictionary does not contain the key \c what,
196  a KeyErrorException will be thrown.
197  */
198  void clearVal(const std::string &what) {
199  if(! this->hasVal(what) ) throw KeyErrorException(what);
200  _data.erase(what);
201  };
202 
203  //! \overload
204  void clearVal(const char *what) {
205  std::string key=what;
206  clearVal(key);
207  };
208 
209  //----------------------------------------------------------
210  //! \brief Clears all keys (and values) from the dictionary.
211  //!
212  void reset(){
213  _data.clear();
214  };
215 
216  //----------------------------------------------------------
217  //! Converts a \c boost::any to type \c T
218  /*!
219  \param arg a \c boost::any reference
220 
221  \returns the converted object of type \c T
222  */
223  template <typename T>
224  T fromany(const boost::any &arg) const;
225 
226 
227  //----------------------------------------------------------
228  //! Converts an instance of type \c T to \c boost::any
229  /*!
230  \param arg the object to be converted
231 
232  \returns a \c boost::any instance
233  */
234  template <typename T>
235  boost::any toany(T arg) const;
236 
237  private:
238  DataType _data; //!< the actual dictionary
239  };
240 }
241 #endif
bool hasVal(const std::string &what) const
Definition: Dict.h:54
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
Definition: Dict.h:170
T getVal(const std::string &what) const
Definition: Dict.h:91
bool getValIfPresent(const char *what, std::string &res) const
Definition: Dict.h:152
STR_VECT keys() const
Returns the set of keys in the dictionary.
Definition: Dict.h:63
Dict()
Definition: Dict.h:36
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
void setVal(const char *what, T &val)
Definition: Dict.h:175
bool getValIfPresent(const char *what, T &res) const
Definition: Dict.h:144
std::map< std::string, boost::any > DataType
Definition: Dict.h:35
void setVal(const std::string &what, const char *val)
Definition: Dict.h:180
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
Definition: Dict.h:198
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
Definition: Dict.h:86
T fromany(const boost::any &arg) const
Converts a boost::any to type T.
void getVal(const char *what, std::string &res) const
Definition: Dict.h:116
Dict(const Dict &other)
Definition: Dict.h:40
void clearVal(const char *what)
Definition: Dict.h:204
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.
bool hasVal(const char *what) const
Returns whether or not the dictionary contains a particular key.
Definition: Dict.h:50
T getVal(const char *what) const
Definition: Dict.h:108
Dict & operator=(const Dict &other)
Definition: Dict.h:42
T getVal(const char *what, T &res) const
Definition: Dict.h:101
void reset()
Clears all keys (and values) from the dictionary.
Definition: Dict.h:212
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:46