diff options
author | Jonathan McCrohan <jmccrohan@gmail.com> | 2015-07-08 23:43:03 +0100 |
---|---|---|
committer | Jonathan McCrohan <jmccrohan@gmail.com> | 2015-07-08 23:53:24 +0100 |
commit | 6c7ac8dc769435ea0231db550799d73cb479a773 (patch) | |
tree | e5de3e6642d6669a17b2afd8341ea5b82655461c /lib/libconfig.h++ | |
parent | 695702e323759d158366d33c24c1ff47141056e9 (diff) | |
download | libconfig-6c7ac8dc769435ea0231db550799d73cb479a773.tar.gz |
Imported Upstream version 1.5upstream/1.5
Diffstat (limited to '')
-rw-r--r-- | lib/libconfig.h++ | 468 |
1 files changed, 275 insertions, 193 deletions
diff --git a/lib/libconfig.h++ b/lib/libconfig.h++ index 04be7a9..a43672c 100644 --- a/lib/libconfig.h++ +++ b/lib/libconfig.h++ @@ -1,6 +1,6 @@ /* ---------------------------------------------------------------------------- libconfig - A library for processing structured configuration files - Copyright (C) 2005-2010 Mark A Lindner + Copyright (C) 2005-2014 Mark A Lindner This file is part of libconfig. @@ -40,8 +40,8 @@ #endif /* WIN32 */ #define LIBCONFIGXX_VER_MAJOR 1 -#define LIBCONFIGXX_VER_MINOR 4 -#define LIBCONFIGXX_VER_REVISION 9 +#define LIBCONFIGXX_VER_MINOR 5 +#define LIBCONFIGXX_VER_REVISION 0 struct config_t; // fwd decl struct config_setting_t; // fwd decl @@ -51,13 +51,18 @@ namespace libconfig { class LIBCONFIGXX_API ConfigException : public std::exception { }; class Setting; // fwd decl +class SettingIterator; +class SettingConstIterator; class LIBCONFIGXX_API SettingException : public ConfigException { - friend class Config; - public: + SettingException(const Setting &setting); + SettingException(const Setting &setting, int idx); + SettingException(const Setting &setting, const char *name); + SettingException(const char *path); + SettingException(const SettingException &other); SettingException& operator=(const SettingException &other); @@ -67,13 +72,6 @@ class LIBCONFIGXX_API SettingException : public ConfigException virtual const char *what() const throw(); - protected: - - SettingException(const Setting &setting); - SettingException(const Setting &setting, int idx); - SettingException(const Setting &setting, const char *name); - SettingException(const char *path); - private: char *_path; @@ -81,48 +79,33 @@ class LIBCONFIGXX_API SettingException : public ConfigException class LIBCONFIGXX_API SettingTypeException : public SettingException { - friend class Config; - friend class Setting; - public: - virtual const char *what() const throw(); - - private: - SettingTypeException(const Setting &setting); SettingTypeException(const Setting &setting, int idx); SettingTypeException(const Setting &setting, const char *name); + + virtual const char *what() const throw(); }; class LIBCONFIGXX_API SettingNotFoundException : public SettingException { - friend class Config; - friend class Setting; - public: - virtual const char *what() const throw(); - - private: - + SettingNotFoundException(const char *path); SettingNotFoundException(const Setting &setting, int idx); SettingNotFoundException(const Setting &setting, const char *name); - SettingNotFoundException(const char *path); + + virtual const char *what() const throw(); }; class LIBCONFIGXX_API SettingNameException : public SettingException { - friend class Config; - friend class Setting; - public: - virtual const char *what() const throw(); - - private: - SettingNameException(const Setting &setting, const char *name); + + virtual const char *what() const throw(); }; class LIBCONFIGXX_API FileIOException : public ConfigException @@ -134,29 +117,27 @@ class LIBCONFIGXX_API FileIOException : public ConfigException class LIBCONFIGXX_API ParseException : public ConfigException { - friend class Config; - public: + ParseException(const char *file, int line, const char *error); + ParseException(const ParseException &other); virtual ~ParseException() throw(); - inline const char *getFile() const throw() + inline const char *getFile() const { return(_file); } - inline int getLine() const throw() + inline int getLine() const { return(_line); } - inline const char *getError() const throw() + inline const char *getError() const { return(_error); } virtual const char *what() const throw(); private: - ParseException(const char *file, int line, const char *error); - const char *_file; int _line; const char *_error; @@ -189,182 +170,288 @@ class LIBCONFIGXX_API Setting FormatHex = 1 }; - private: + enum Option + { + OptionNone = 0, + OptionAutoConvert = 0x01, + OptionSemicolonSeparators = 0x02, + OptionColonAssignmentForGroups = 0x04, + OptionColonAssignmentForNonGroups = 0x08, + OptionOpenBraceOnSeparateLine = 0x10 + }; - config_setting_t *_setting; - Type _type; - Format _format; + typedef SettingIterator iterator; + typedef SettingConstIterator const_iterator; - Setting(config_setting_t *setting); + public: - void assertType(Type type) const throw(SettingTypeException); - static Setting & wrapSetting(config_setting_t *setting); + virtual ~Setting(); + + inline Type getType() const { return(_type); } + + inline Format getFormat() const { return(_format); } + void setFormat(Format format); + + operator bool() const; + operator int() const; + operator unsigned int() const; + operator long() const; + operator unsigned long() const; + operator long long() const; + operator unsigned long long() const; + operator double() const; + operator float() const; + operator const char *() const; + operator std::string() const; + + inline const char *c_str() const + { return operator const char *(); } + + Setting & operator=(bool value); + Setting & operator=(int value); + Setting & operator=(long value); + Setting & operator=(const long long &value); + Setting & operator=(const double &value); + Setting & operator=(float value); + Setting & operator=(const char *value); + Setting & operator=(const std::string &value); + + Setting & lookup(const char *path) const; + inline Setting & lookup(const std::string &path) const + { return(lookup(path.c_str())); } - Setting(const Setting& other); // not supported - Setting& operator=(const Setting& other); // not supported + Setting & operator[](const char *name) const; + Setting & operator[](int index) const; - public: + bool lookupValue(const char *name, bool &value) const; + bool lookupValue(const char *name, int &value) const; + bool lookupValue(const char *name, unsigned int &value) const; + bool lookupValue(const char *name, long long &value) const; + bool lookupValue(const char *name, unsigned long long &value) const; + bool lookupValue(const char *name, double &value) const; + bool lookupValue(const char *name, float &value) const; + bool lookupValue(const char *name, const char *&value) const; + bool lookupValue(const char *name, std::string &value) const; - virtual ~Setting() throw(); - - inline Type getType() const throw() { return(_type); } - - inline Format getFormat() const throw() { return(_format); } - void setFormat(Format format) throw(); - - operator bool() const throw(SettingTypeException); - operator int() const throw(SettingTypeException); - operator unsigned int() const throw(SettingTypeException); - operator long() const throw(SettingTypeException); - operator unsigned long() const throw(SettingTypeException); - operator long long() const throw(SettingTypeException); - operator unsigned long long() const throw(SettingTypeException); - operator double() const throw(SettingTypeException); - operator float() const throw(SettingTypeException); - operator const char *() const throw(SettingTypeException); - operator std::string() const throw(SettingTypeException); - - inline const char *c_str() const throw(SettingTypeException) - { return operator const char*(); } - - Setting & operator=(bool value) throw(SettingTypeException); - Setting & operator=(int value) throw(SettingTypeException); - Setting & operator=(long value) throw(SettingTypeException); - Setting & operator=(const long long &value) throw(SettingTypeException); - Setting & operator=(const double &value) throw(SettingTypeException); - Setting & operator=(float value) throw(SettingTypeException); - Setting & operator=(const char *value) throw(SettingTypeException); - Setting & operator=(const std::string &value) throw(SettingTypeException); - - Setting & operator[](const char *key) const - throw(SettingTypeException, SettingNotFoundException); - - inline Setting & operator[](const std::string &key) const - throw(SettingTypeException, SettingNotFoundException) - { return(operator[](key.c_str())); } - - Setting & operator[](int index) const - throw(SettingTypeException, SettingNotFoundException); - - bool lookupValue(const char *name, bool &value) const throw(); - bool lookupValue(const char *name, int &value) const throw(); - bool lookupValue(const char *name, unsigned int &value) const throw(); - bool lookupValue(const char *name, long long &value) const throw(); - bool lookupValue(const char *name, unsigned long long &value) - const throw(); - bool lookupValue(const char *name, double &value) const throw(); - bool lookupValue(const char *name, float &value) const throw(); - bool lookupValue(const char *name, const char *&value) const throw(); - bool lookupValue(const char *name, std::string &value) const throw(); - - inline bool lookupValue(const std::string &name, bool &value) - const throw() + inline bool lookupValue(const std::string &name, bool &value) const { return(lookupValue(name.c_str(), value)); } - inline bool lookupValue(const std::string &name, int &value) - const throw() + inline bool lookupValue(const std::string &name, int &value) const { return(lookupValue(name.c_str(), value)); } - inline bool lookupValue(const std::string &name, unsigned int &value) - const throw() + inline bool lookupValue(const std::string &name, unsigned int &value) const { return(lookupValue(name.c_str(), value)); } - inline bool lookupValue(const std::string &name, long long &value) - const throw() + inline bool lookupValue(const std::string &name, long long &value) const { return(lookupValue(name.c_str(), value)); } inline bool lookupValue(const std::string &name, - unsigned long long &value) const throw() + unsigned long long &value) const { return(lookupValue(name.c_str(), value)); } inline bool lookupValue(const std::string &name, double &value) const - throw() { return(lookupValue(name.c_str(), value)); } inline bool lookupValue(const std::string &name, float &value) const - throw() { return(lookupValue(name.c_str(), value)); } inline bool lookupValue(const std::string &name, const char *&value) const - throw() { return(lookupValue(name.c_str(), value)); } inline bool lookupValue(const std::string &name, std::string &value) const - throw() { return(lookupValue(name.c_str(), value)); } - void remove(const char *name) - throw(SettingTypeException, SettingNotFoundException); + void remove(const char *name); - inline void remove(const std::string & name) - throw(SettingTypeException, SettingNotFoundException) + inline void remove(const std::string &name) { remove(name.c_str()); } - void remove(unsigned int idx) - throw(SettingTypeException, SettingNotFoundException); + void remove(unsigned int idx); + + Setting & add(const char *name, Type type); - inline Setting & add(const std::string & name, Type type) - throw(SettingNameException, SettingTypeException) + inline Setting & add(const std::string &name, Type type) { return(add(name.c_str(), type)); } - Setting & add(const char *name, Type type) - throw(SettingNameException, SettingTypeException); + Setting & add(Type type); - Setting & add(Type type) throw(SettingTypeException); + bool exists(const char *name) const; - inline bool exists(const std::string &name) const throw() + inline bool exists(const std::string &name) const { return(exists(name.c_str())); } - bool exists(const char *name) const throw(); - - int getLength() const throw(); - const char *getName() const throw(); + int getLength() const; + const char *getName() const; std::string getPath() const; - int getIndex() const throw(); + int getIndex() const; - const Setting & getParent() const throw(SettingNotFoundException); - Setting & getParent() throw(SettingNotFoundException); + const Setting & getParent() const; + Setting & getParent(); - bool isRoot() const throw(); + bool isRoot() const; - inline bool isGroup() const throw() + inline bool isGroup() const { return(_type == TypeGroup); } - inline bool isArray() const throw() + inline bool isArray() const { return(_type == TypeArray); } - inline bool isList() const throw() + inline bool isList() const { return(_type == TypeList); } - inline bool isAggregate() const throw() + inline bool isAggregate() const { return(_type >= TypeGroup); } - inline bool isScalar() const throw() + inline bool isScalar() const { return((_type > TypeNone) && (_type < TypeGroup)); } - inline bool isNumber() const throw() - { return((_type == TypeInt) || (_type == TypeInt64) - || (_type == TypeFloat)); } + inline bool isNumber() const + { + return((_type == TypeInt) || (_type == TypeInt64) || (_type == TypeFloat)); + } + + unsigned int getSourceLine() const; + const char *getSourceFile() const; + + iterator begin(); + iterator end(); - unsigned int getSourceLine() const throw(); - const char *getSourceFile() const throw(); + const_iterator begin() const; + const_iterator end() const; + + private: + + config_setting_t *_setting; + Type _type; + Format _format; + + Setting(config_setting_t *setting); + + void assertType(Type type) const; + static Setting & wrapSetting(config_setting_t *setting); + + Setting(const Setting& other); // not supported + Setting& operator=(const Setting& other); // not supported }; -class LIBCONFIGXX_API Config + +class LIBCONFIGXX_API SettingIterator { + public: + + SettingIterator(Setting &setting, bool endIterator = false); + SettingIterator(const SettingIterator &other); + SettingIterator& operator=(const SettingIterator &other); + + // Equality comparison. + inline bool operator==(SettingIterator const &other) const + { return((_setting == other._setting) && (_idx == other._idx)); } + + inline bool operator!=(SettingIterator const &other) const + { return(!operator==(other)); } + + bool operator<(SettingIterator const &other) const; + + // Dereference operators. + inline Setting & operator*() + { return((*_setting)[_idx]); } + + inline Setting * operator->() + { return(&(*_setting)[_idx]); } + + inline const Setting & operator*() const + { return(*_setting)[_idx]; } + inline const Setting * operator->() const + { return(&(*_setting)[_idx]); } + + // Increment and decrement operators. + SettingIterator & operator++(); + SettingIterator operator++(int); + + SettingIterator & operator--(); + SettingIterator operator--(int); + + // Arithmetic operators. + SettingIterator operator+(int offset) const; + SettingIterator & operator+=(int offset); + + SettingIterator operator-(int offset) const; + SettingIterator & operator-=(int offset); + + int operator-(const SettingIterator &other) const; + private: - config_t *_config; + Setting *_setting; - static void ConfigDestructor(void *arg); - Config(const Config& other); // not supported - Config& operator=(const Config& other); // not supported + int _count; + int _idx; +}; + +SettingIterator operator+(int offset, const SettingIterator &si); +class LIBCONFIGXX_API SettingConstIterator +{ + public: + + SettingConstIterator(const Setting &setting, bool endIterator = false); + SettingConstIterator(const SettingConstIterator &rhs); + SettingConstIterator& operator=(const SettingConstIterator &rhs); + + // Equality comparison. + bool operator==(SettingConstIterator const &other) const + { return((_setting == other._setting) && (_idx == other._idx)); } + + inline bool operator!=(SettingConstIterator const &other) const + { return(!operator==(other)); } + + // Dereference operators. + inline Setting const & operator*() + { return((*_setting)[_idx]); } + inline Setting const * operator->() + { return(&(*_setting)[_idx]); } + + inline const Setting& operator*() const + { return((*_setting)[_idx]); } + inline const Setting * operator->() const + { return(&(*_setting)[_idx]); } + + // Increment and decrement operators. + SettingConstIterator & operator++(); + SettingConstIterator operator++(int); + + SettingConstIterator & operator--(); + SettingConstIterator operator--(int); + + // Arithmetic operators. + SettingConstIterator operator+(int offset) const; + SettingConstIterator & operator+=(int offset); + + SettingConstIterator operator-(int offset) const; + SettingConstIterator & operator-=(int offset); + + int operator-(const SettingConstIterator &other) const; + + private: + + const Setting *_setting; + + int _count; + int _idx; +}; + +SettingConstIterator operator+(int offset, const SettingConstIterator &si); + +class LIBCONFIGXX_API Config +{ public: Config(); virtual ~Config(); + void setOptions(int options); + int getOptions() const; + void setAutoConvert(bool flag); bool getAutoConvert() const; @@ -372,85 +459,80 @@ class LIBCONFIGXX_API Config inline Setting::Format getDefaultFormat() const { return(_defaultFormat); } - void setTabWidth(unsigned short width) throw(); - unsigned short getTabWidth() const throw(); + void setTabWidth(unsigned short width); + unsigned short getTabWidth() const; - void setIncludeDir(const char *includeDir) throw(); - const char *getIncludeDir() const throw(); + void setIncludeDir(const char *includeDir); + const char *getIncludeDir() const; - void read(FILE *stream) throw(ParseException); + void read(FILE *stream); void write(FILE *stream) const; - void readString(const char *str) throw(ParseException); - inline void readString(const std::string &str) throw(ParseException) + void readString(const char *str); + inline void readString(const std::string &str) { return(readString(str.c_str())); } - void readFile(const char *filename) throw(FileIOException, ParseException); - void writeFile(const char *filename) throw(FileIOException); + void readFile(const char *filename); + void writeFile(const char *filename); + Setting & lookup(const char *path) const; inline Setting & lookup(const std::string &path) const - throw(SettingNotFoundException) { return(lookup(path.c_str())); } - Setting & lookup(const char *path) const throw(SettingNotFoundException); - - inline bool exists(const std::string & path) const throw() + bool exists(const char *path) const; + inline bool exists(const std::string &path) const { return(exists(path.c_str())); } - bool exists(const char *path) const throw(); - - bool lookupValue(const char *path, bool &value) const throw(); - bool lookupValue(const char *path, int &value) const throw(); - bool lookupValue(const char *path, unsigned int &value) const throw(); - bool lookupValue(const char *path, long long &value) const throw(); - bool lookupValue(const char *path, unsigned long long &value) - const throw(); - bool lookupValue(const char *path, double &value) const throw(); - bool lookupValue(const char *path, float &value) const throw(); - bool lookupValue(const char *path, const char *&value) const throw(); - bool lookupValue(const char *path, std::string &value) const throw(); - - inline bool lookupValue(const std::string &path, bool &value) const throw() + bool lookupValue(const char *path, bool &value) const; + bool lookupValue(const char *path, int &value) const; + bool lookupValue(const char *path, unsigned int &value) const; + bool lookupValue(const char *path, long long &value) const; + bool lookupValue(const char *path, unsigned long long &value) const; + bool lookupValue(const char *path, double &value) const; + bool lookupValue(const char *path, float &value) const; + bool lookupValue(const char *path, const char *&value) const; + bool lookupValue(const char *path, std::string &value) const; + + inline bool lookupValue(const std::string &path, bool &value) const { return(lookupValue(path.c_str(), value)); } - inline bool lookupValue(const std::string &path, int &value) const throw() + inline bool lookupValue(const std::string &path, int &value) const { return(lookupValue(path.c_str(), value)); } - inline bool lookupValue(const std::string &path, unsigned int &value) - const throw() + inline bool lookupValue(const std::string &path, unsigned int &value) const { return(lookupValue(path.c_str(), value)); } - inline bool lookupValue(const std::string &path, long long &value) - const throw() + inline bool lookupValue(const std::string &path, long long &value) const { return(lookupValue(path.c_str(), value)); } inline bool lookupValue(const std::string &path, - unsigned long long &value) const throw() + unsigned long long &value) const { return(lookupValue(path.c_str(), value)); } - inline bool lookupValue(const std::string &path, double &value) - const throw() + inline bool lookupValue(const std::string &path, double &value) const { return(lookupValue(path.c_str(), value)); } - inline bool lookupValue(const std::string &path, float &value) - const throw() + inline bool lookupValue(const std::string &path, float &value) const { return(lookupValue(path.c_str(), value)); } - inline bool lookupValue(const std::string &path, const char *&value) - const throw() + inline bool lookupValue(const std::string &path, const char *&value) const { return(lookupValue(path.c_str(), value)); } - inline bool lookupValue(const std::string &path, std::string &value) - const throw() + inline bool lookupValue(const std::string &path, std::string &value) const { return(lookupValue(path.c_str(), value)); } Setting & getRoot() const; private: + static void ConfigDestructor(void *arg); + void handleError() const; + + config_t *_config; Setting::Format _defaultFormat; - void handleError() const; + Config(const Config& other); // not supported + Config& operator=(const Config& other); // not supported }; } // namespace libconfig |