RDCode: Dict.h Source File

# Dict.h

Go to the documentation of this file.

    00001 //
    00002 // Copyright (C) 2003-2008 Greg Landrum and Rational Discovery LLC
    00003 //
    00004 //  @@ All Rights Reserved @@
    00005 //
    00006 /*! \file Dict.h
    00007
    00008   \brief Defines the Dict class
    00009
    00010 */
    00011 #ifndef __RD_DICT_H__
    00012 #define __RD_DICT_H__
    00013
    00014 #include <map>
    00015 #include <string>
    00016 #include <vector>
    00017 #include <boost/any.hpp>
    00018 #include <RDBoost/Exceptions.h>
    00019 #include <boost/lexical_cast.hpp>
    00020
    00021 namespace RDKit{
    00022   typedef std::vector<std::string> STR_VECT;
    00023
    00024   //! \brief The \c Dict class can be used to store objects of arbitrary
    00025   //!        type keyed by \c strings.
    00026   //!
    00027   //!  The actual storage is done using \c boost::any objects.
    00028   //!
    00029   class Dict {
    00030     //! \brief this function is used solely to force the instantiation of particular
    00031     //!    types of the \c Dict.toAny() and \c Dict.fromAny() methods in order to
    00032     //!    avoid link errors.
    00033     friend void force_types();
    00034   public:
    00035     typedef std::map<const std::string, boost::any> DataType;
    00036     Dict(){
    00037       _data.clear();
    00038     };
    00039
    00040     Dict(const Dict &other) {
    00041       _data = other._data;
    00042     };
    00043
    00044     Dict &operator=(const Dict &other) {
    00045       _data = other._data;
    00046       return *this;
    00047     };
    00048
    00049     //----------------------------------------------------------
    00050     //! \brief Returns whether or not the dictionary contains a particular
    00051     //!        key.
    00052     bool hasVal(const char *what) const{
    00053       std::string key(what);
    00054       return hasVal(key);
    00055     };
    00056     bool hasVal(const std::string &what) const {
    00057       return _data.find(what)!=_data.end();
    00058     };
    00059
    00060     //----------------------------------------------------------
    00061     //! Returns the set of keys in the dictionary
    00062     /*!
    00063        \return  a \c STR_VECT
    00064     */
    00065     STR_VECT keys() const {
    00066       STR_VECT res;
    00067       DataType::const_iterator item;
    00068       for (item = _data.begin(); item != _data.end(); item++) {
    00069         res.push_back(item->first);
    00070       }
    00071       return res;
    00072     }
    00073
    00074     //----------------------------------------------------------
    00075     //! \brief Gets the value associated with a particular key
    00076     /*!
    00077        \param what  the key to lookup
    00078        \param res   a reference used to return the result
    00079
    00080        <B>Notes:</b>
    00081         - If \c res is a \c std::string, every effort will be made
    00082           to convert the specified element to a string using the
    00083           \c boost::lexical_cast machinery.
    00084         - If the dictionary does not contain the key \c what,
    00085           a KeyErrorException will be thrown.
    00086     */
    00087     template <typename T>
    00088     void getVal(const std::string &what,T &res) const {
    00089       DataType::const_iterator pos=_data.find(what);
    00090       if(pos==_data.end())
    00091         throw KeyErrorException(what);
    00092       const boost::any &val = pos->second;
    00093       res = fromany<T>(val);
    00094     };
    00095     //! \overload
    00096     template <typename T>
    00097     T getVal(const std::string &what) const {
    00098       T res;
    00099       getVal(what,res);
    00100       return res;
    00101     }
    00102
    00103     //! \overload
    00104     template <typename T>
    00105     T getVal(const char *what,T &res) const {
    00106       std::string key(what);
    00107       getVal(key, res);
    00108       return res;
    00109     };
    00110     //! \overload
    00111     template <typename T>
    00112     T getVal(const char *what) const {
    00113       std::string key(what);
    00114       return getVal<T>(key);
    00115     };
    00116
    00117     //! \overload
    00118     void getVal(const std::string &what, std::string &res) const;
    00119
    00120     //----------------------------------------------------------
    00121     //! \brief Sets the value associated with a key
    00122     /*!
    00123
    00124        \param what the key to set
    00125        \param val  the value to store
    00126
    00127        <b>Notes:</b>
    00128           - If \c val is a <tt>const char *</tt>, it will be converted
    00129              to a \c std::string for storage.
    00130           - If the dictionary already contains the key \c what,
    00131             the value will be replaced.
    00132     */
    00133     template <typename T>
    00134     void setVal(const std::string &what, T &val){
    00135       std::string key = what;
    00136       _data[key] = toany(val);
    00137     };
    00138     //! \overload
    00139     template <typename T>
    00140     void setVal(const char *what, T &val){
    00141       std::string key = what;
    00142       setVal(key,val);
    00143     };
    00144     //! \overload
    00145     void setVal(const std::string &what, const char *val){
    00146       std::string h(val);
    00147       setVal(what,h);
    00148     }
    00149
    00150
    00151
    00152     //----------------------------------------------------------
    00153     //! \brief Clears the value associated with a particular key,
    00154     //!     removing the key from the dictionary.
    00155     /*!
    00156
    00157        \param what the key to clear
    00158
    00159      <b>Notes:</b>
    00160         - If the dictionary does not contain the key \c what,
    00161           a KeyErrorException will be thrown.
    00162     */
    00163     void clearVal(const std::string &what) {
    00164       if(! this->hasVal(what) ) throw KeyErrorException(what);
    00165       _data.erase(what);
    00166     };
    00167
    00168     //! \overload
    00169     void clearVal(const char *what) {
    00170       std::string key=what;
    00171       clearVal(key);
    00172     };
    00173
    00174     //----------------------------------------------------------
    00175     //! \brief Clears all keys (and values) from the dictionary.
    00176     //!
    00177     void reset(){
    00178       _data.clear();
    00179     };
    00180
    00181     //----------------------------------------------------------
    00182     //! Converts a \c boost::any to type \c T
    00183     /*!
    00184        \param arg a \c boost::any reference
    00185
    00186        \returns the converted object of type \c T
    00187     */
    00188     template <typename T>
    00189       T fromany(const boost::any &arg) const;
    00190
    00191
    00192     //----------------------------------------------------------
    00193     //! Converts an instance of type \c T to \c boost::any
    00194     /*!
    00195        \param arg the object to be converted
    00196
    00197        \returns a \c boost::any instance
    00198     */
    00199     template <typename T>
    00200       boost::any toany(T arg) const;
    00201
    00202   private:
    00203     DataType _data; //!< the actual dictionary
    00204   };
    00205 }
    00206 #endif