#ifndef AASPI_DICT_H #define AASPI_DICT_H #include #include #include #include #include "aaspi_types.h" //! \file //! \brief Dictionary used for holding parameters using namespace std; //! \brief Dictionary used to hold parameters //! //! The internal representation of this dictionary is an STL //! map of string to string. Conversion to other types is class AASPI_Dictionary { public: AASPI_Dictionary() { cur_it=dict.end(); } AASPI_Dictionary(const AASPI_Dictionary& ad) : dict(ad.dict) { reset_keylist(); } ~AASPI_Dictionary() {} const AASPI_Dictionary& operator=(const AASPI_Dictionary &ad) { dict=ad.dict; reset_keylist(); return *this; } //! Check to see if key is defined //! inline bool is_defined(const string &tag) const { return dict.find(tag) != dict.end(); } //! Get associated key value as a string //! //! The default parameter is returned if the key is not found. inline string get_string(const string &tag, const string &defval="") const { string ret=defval; map::const_iterator it=dict.find(tag); if (it != dict.end()) { ret=(*it).second; } return ret; } //! Get associated key value as a 32 bit integer //! //! The default parameter is returned if the key is not found. inline int get_int(const string &tag, int defval=0) const { int ret=defval; map::const_iterator it=dict.find(tag); if (it != dict.end()) { istringstream iss((*it).second); iss >> ret; } return ret; } //! Get associated key value as a 64 bit integer //! //! The default parameter is returned if the key is not found. inline aaspi_int64 get_int64(const string &tag, aaspi_int64 defval=0) const { aaspi_int64 ret=defval; map::const_iterator it=dict.find(tag); if (it != dict.end()) { istringstream iss((*it).second); iss >> ret; } return ret; } //! Get associated key value as a 32 bit real //! //! The default parameter is returned if the key is not found. inline float get_float(const string &tag, float defval=0.f) const { float ret=defval; map::const_iterator it=dict.find(tag); if (it != dict.end()) { istringstream iss((*it).second); iss >> ret; } return ret; } //! Get associated key value as a 64 bit real //! //! The default parameter is returned if the key is not found. inline double get_double(const string &tag, double defval=0.) const { double ret=defval; map::const_iterator it=dict.find(tag); if (it != dict.end()) { istringstream iss((*it).second); iss >> ret; } return ret; } //! Get associated key value as a boolean //! //! The default parameter is returned if the key is not found. inline bool get_boolean(const string &tag, bool defval=false) const { bool ret=defval; map::const_iterator it=dict.find(tag); // if string doesn't match the correct responses // it will return the default. if (it != dict.end()) { string val=(*it).second; if (!val.empty()) { if (val[0] == 'Y') ret=true; else if (val[0] == 'y') ret=true; else if (val[0] == '1') ret=true; else if (val[0] == 'N') ret=false; else if (val[0] == 'n') ret=false; else if (val[0] == '0') ret=false; } } return ret; } //! Set keys associated value //! inline void set_string(const string &tag, const string &val) { dict[tag]=val; cur_it=dict.begin(); // Reset iterator } //! Reset current value iterator //! //! This method is supplied for accessing the contents of the entire //! dictionary. //! /see get_next_key inline void reset_keylist() { cur_it=dict.begin(); } //! Get next value in dictionary //! //! This method is supplied for accessing the contents of the entire //! dictionary. //! /see reset_keylist inline bool get_next_key(string &rstr) { if (cur_it == dict.end()) return false; rstr=(*cur_it).first; ++cur_it; return true; } //! Add the contents of an another dictionary to this one //! inline void append(const AASPI_Dictionary &idict) { map::const_iterator it; for (it=idict.dict.begin(); it != idict.dict.end(); ++it) { dict[(*it).first]=(*it).second; } } //! Overloaded convenience function for accessing dictionary elements //! inline void get(const string &par, string &val) const { val=get_string(par); } //! Overloaded convenience function for accessing dictionary elements //! inline void get(const string &par, aaspi_int32 &val) const { val=get_int(par); } //! Overloaded convenience function for accessing dictionary elements //! inline void get(const string &par, aaspi_int64 &val) const { val=get_int64(par); } //! Overloaded convenience function for accessing dictionary elements //! inline void get(const string &par, aaspi_real32 &val) const { val=get_float(par); } //! Overloaded convenience function for accessing dictionary elements //! inline void get(const string &par, aaspi_real64 &val) const { val=get_double(par); } //! Overloaded convenience function for accessing dictionary elements //! inline void get(const string &par, bool &val) const { val=get_boolean(par); } private: map dict; map::iterator cur_it; }; #endif