Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
keyValueFile.hpp
00001 #ifndef KERNEL_KEY_VALUE_FILE_HPP
00002 #define KERNEL_KEY_VALUE_FILE_HPP
00003 
00004 #include <typeinfo>
00005 #include <string>
00006 #include <sstream>
00007 #include <map>
00008 
00009 #include "kernel/kernelException.hpp"
00010 
00011 namespace jafar {
00012   namespace kernel {
00013 
00014 #ifndef SWIG // swig ignores KeyValueFile class
00015 
00020     class KeyValueFile {
00021 
00022     private:
00023 
00025       std::string keyValueSeparator;
00026       char commentPrefix;
00027 
00028       typedef std::map<std::string, std::string> KeyValueMap;
00029 
00030       KeyValueMap keyValue;
00031 
00032     public:
00033 
00034       KeyValueFile(std::string const& keyValueSeparator_ = ":", char commentPrefix_ = '#');
00035 
00037       void readFile(std::string const& filename);
00038 
00046       void writeFile(std::string const& filename);
00047       
00048       bool hasKey(std::string const& key) const;
00049 
00051       template<class T>
00052       void getItem(std::string const& key, T& value) const {
00053 
00054   KeyValueMap::const_iterator it = keyValue.find(key);
00055 
00056   JFR_PRED_ERROR(it != keyValue.end(),
00057            KernelException,
00058            KernelException::KEYVALUEFILE_UNKNOWN_KEY,
00059            "KeyValueFile:getItem: unknown key: " << key);
00060 
00061   std::istringstream ss(it->second);
00062   ss >> value;
00063 
00064   JFR_IO_STREAM(ss, 
00065           "KeyValueFile::getItem: invalid value parsing:" << std::endl << 
00066           "key: " << key << std::endl <<
00067           "value: " << it->second << std::endl <<
00068           "value-type: " << typeid(T).name());
00069       }
00070 
00072       template<class T>
00073       void getItem(std::string const& key, T& value, std::string default_value) const
00074       {
00075         KeyValueMap::const_iterator it = keyValue.find(key);
00076 
00077         std::istringstream ss(it == keyValue.end() ? default_value : it->second);
00078         ss >> value;
00079 
00080         JFR_IO_STREAM(ss,
00081                 "KeyValueFile::getItem: invalid value parsing:" << std::endl <<
00082                 "key: " << key << std::endl <<
00083                 "value: " << it->second << std::endl <<
00084                 "value-type: " << typeid(T).name());
00085       }
00086 
00087 
00089       template<class T>
00090       void setItem(std::string const& key, const T& value) {
00091   std::ostringstream ss;
00092   ss << value;
00093   keyValue[key] = ss.str();
00094       }
00095 
00096     }; // class KeyValueFile
00097 
00098 #endif // SWIG
00099 
00105     class KeyValueFileLoad {
00106 
00107     public:
00108 
00114       void load(std::string const& filename,
00115     std::string const& keyValueSeparator_ = ":", char commentPrefix_ = '#');
00116 
00117     protected:
00118 
00119       virtual ~KeyValueFileLoad() {};
00120 
00124       virtual void loadKeyValueFile(jafar::kernel::KeyValueFile const& keyValueFile) = 0;
00125       
00126     }; // class KeyValueFileLoad
00127 
00128 
00134     class KeyValueFileSave {
00135 
00136     public:
00137 
00143       void save(std::string const& filename,
00144     std::string const& keyValueSeparator_ = ":", char commentPrefix_ = '#');
00145 
00146     protected:
00147 
00148       virtual ~KeyValueFileSave() {};
00149 
00153       virtual void saveKeyValueFile(jafar::kernel::KeyValueFile& keyValueFile) = 0;
00154 
00155     }; // class KeyValueFileSave
00156 
00162     class KeyValueFileSaveLoad : public KeyValueFileLoad, public KeyValueFileSave {
00163 
00164     }; // class KeyValueFileSaveLoad
00165 
00166   } // namespace kernel
00167 } // namespace jafar
00168 
00169 #endif // KERNEL_KEY_VALUE_FILE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS