Skip to content

Commit

Permalink
Inherit exceptions from std::runtime_error
Browse files Browse the repository at this point in the history
This includes refactoring some of the error message and path
construction functions.
  • Loading branch information
muesli4 committed Sep 5, 2019
1 parent 68f6159 commit e29ad28
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 94 deletions.
23 changes: 8 additions & 15 deletions lib/libconfig.h++
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define __libconfig_hpp

#include <stdio.h>
#include <exception>
#include <stdexcept>
#include <string>

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
Expand All @@ -48,20 +48,15 @@ struct config_setting_t; // fwd decl

namespace libconfig {

struct LIBCONFIGXX_API ConfigException : public std::exception
struct LIBCONFIGXX_API ConfigException : public std::runtime_error
{
ConfigException();
ConfigException(std::string const &message);

ConfigException(ConfigException const &other);
ConfigException& operator=(ConfigException const &other);

virtual ~ConfigException() throw();

virtual const char * what() const throw();

protected:

std::string _errorMessage;
};

class Setting; // fwd decl
Expand All @@ -76,7 +71,7 @@ class LIBCONFIGXX_API SettingException : public ConfigException
SettingException(char const *derivedType, const Setting &setting);
SettingException(char const *derivedType, const Setting &setting, int idx);
SettingException(char const *derivedType, const Setting &setting, const char *name);
SettingException(char const *derivedType, const char *path);
SettingException(char const *derivedType, std::string path);

public:

Expand All @@ -90,11 +85,11 @@ class LIBCONFIGXX_API SettingException : public ConfigException

virtual ~SettingException() throw();

const char *getPath() const;
std::string const & getPath() const;

private:

char *_path;
std::string _path;
};

class LIBCONFIGXX_API SettingTypeException : public SettingException
Expand Down Expand Up @@ -122,11 +117,9 @@ class LIBCONFIGXX_API SettingNameException : public SettingException
SettingNameException(const Setting &setting, const char *name);
};

class LIBCONFIGXX_API FileIOException : public ConfigException
struct LIBCONFIGXX_API FileIOException : public ConfigException
{
public:

virtual const char *what() const throw();
FileIOException();
};

class LIBCONFIGXX_API ParseException : public ConfigException
Expand Down
143 changes: 64 additions & 79 deletions lib/libconfigcpp.c++
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,36 @@ static const char **__include_func(config_t *config,
// ---------------------------------------------------------------------------

ConfigException::ConfigException()
: std::runtime_error("")
{
}

// ---------------------------------------------------------------------------

ConfigException::ConfigException(std::string const &errorMessage)
: _errorMessage(errorMessage)
: std::runtime_error(errorMessage)
{
}

// ---------------------------------------------------------------------------

ConfigException::ConfigException(ConfigException const &other)
: _errorMessage(other._errorMessage)
: std::runtime_error(other.what())
{
}

// ---------------------------------------------------------------------------

ConfigException::~ConfigException() throw()
ConfigException& ConfigException::operator=(ConfigException const &other)
{
std::runtime_error::operator=(other);
return(*this);
}

// ---------------------------------------------------------------------------

const char * ConfigException::what() const throw()
ConfigException::~ConfigException() throw()
{
return _errorMessage.c_str();
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -171,134 +173,119 @@ static int __toTypeCode(Setting::Type type)

// ---------------------------------------------------------------------------

static void __constructPath(const Setting &setting,
std::stringstream &path)
static void __writeSettingPath(const Setting &setting, std::ostream &o)
{
// head recursion to print path from root to target

if(! setting.isRoot())
if (!setting.isRoot())
{
__constructPath(setting.getParent(), path);
if(path.tellp() > 0)
path << '.';

const char *name = setting.getName();
if(name)
path << name;
else
path << '[' << setting.getIndex() << ']';
__writeSettingPath(setting.getParent(), o);
o << '.';
}
o << setting.getName();
}

// ---------------------------------------------------------------------------

static std::string __makeSettingExceptionErrorString(char const *path,
char const *derivedType)
static std::string __constructSettingPath(const Setting &setting)
{
std::stringstream sstr;
sstr << derivedType << ": " << path;
return sstr.str();
std::stringstream ss;
__writeSettingPath(setting, ss);
return ss.str();
}

// ---------------------------------------------------------------------------

SettingException::SettingException(char const *derivedType,
const Setting &setting)
static std::string __constructSettingPath(const Setting &setting, int idx)
{
std::stringstream sstr;
__constructPath(setting, sstr);
std::stringstream ss;
__writeSettingPath(setting, ss);
ss << ".[" << idx << ']';
return ss.str();
}

_path = ::strdup(sstr.str().c_str());
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
// ---------------------------------------------------------------------------

static std::string __constructSettingPath(const Setting &setting, const char *name)
{
std::stringstream ss;
__writeSettingPath(setting, ss);
ss << ".[" << name << ']';
return ss.str();
}

// ---------------------------------------------------------------------------

SettingException::SettingException(char const *derivedType,
const Setting &setting,
int idx)
static std::string __constructErrorMessage(char const *derivedType, std::string const & path)
{
std::stringstream sstr;
__constructPath(setting, sstr);
sstr << ".[" << idx << "]";
std::stringstream ss;
ss << derivedType << ": " << path;
return ss.str();
}

// ---------------------------------------------------------------------------

_path = ::strdup(sstr.str().c_str());
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
SettingException::SettingException(char const *derivedType, std::string path)
: ConfigException(__constructErrorMessage(derivedType, path))
, _path(std::move(path))
{
}

// ---------------------------------------------------------------------------

SettingException::SettingException(char const *derivedType,
const Setting &setting,
const char *name)
const Setting &setting)
: SettingException(derivedType, __constructSettingPath(setting))
{
std::stringstream sstr;
__constructPath(setting, sstr);
sstr << '.' << name;
}

// ---------------------------------------------------------------------------

_path = ::strdup(sstr.str().c_str());
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
SettingException::SettingException(char const *derivedType,
const Setting &setting,
int idx)
: SettingException(derivedType, __constructSettingPath(setting, idx))
{
}

// ---------------------------------------------------------------------------

SettingException::SettingException(char const *derivedType,
const char *path)
const Setting &setting,
const char *name)
: SettingException(derivedType, __constructSettingPath(setting, name))
{
_path = ::strdup(path);
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
}

// ---------------------------------------------------------------------------

SettingException::SettingException(const Setting &setting)
: SettingException("setting exception", setting)
{
std::stringstream sstr;
__constructPath(setting, sstr);

_path = ::strdup(sstr.str().c_str());
_errorMessage =
__makeSettingExceptionErrorString(_path, "setting exception");
}

// ---------------------------------------------------------------------------

SettingException::SettingException(const Setting &setting, int idx)
: SettingException("setting exception", setting, idx)
{
std::stringstream sstr;
__constructPath(setting, sstr);
sstr << ".[" << idx << "]";

_path = ::strdup(sstr.str().c_str());
_errorMessage =
__makeSettingExceptionErrorString(_path, "setting exception");
}

// ---------------------------------------------------------------------------

SettingException::SettingException(const Setting &setting, const char *name)
: SettingException("setting exception", setting, name)
{
std::stringstream sstr;
__constructPath(setting, sstr);
sstr << '.' << name;

_path = ::strdup(sstr.str().c_str());
_errorMessage =
__makeSettingExceptionErrorString(_path, "setting exception");
}

// ---------------------------------------------------------------------------

SettingException::SettingException(const char *path)
: SettingException("setting exception", path)
{
_path = ::strdup(path);
_errorMessage =
__makeSettingExceptionErrorString(_path, "setting exception");
}

// ---------------------------------------------------------------------------

const char *SettingException::getPath() const
std::string const & SettingException::getPath() const
{
return(_path);
}
Expand All @@ -307,17 +294,16 @@ const char *SettingException::getPath() const

SettingException::SettingException(const SettingException &other)
: ConfigException(other)
, _path(other._path)
{
_path = ::strdup(other._path);
}

// ---------------------------------------------------------------------------

SettingException &SettingException::operator=(const SettingException &other)
{
ConfigException::operator=(other);
::free(_path);
_path = ::strdup(other._path);
_path = other._path;

return(*this);
}
Expand All @@ -326,7 +312,6 @@ SettingException &SettingException::operator=(const SettingException &other)

SettingException::~SettingException() throw()
{
::free(_path);
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -384,9 +369,9 @@ SettingNameException::SettingNameException(const Setting &setting,

// ---------------------------------------------------------------------------

const char *FileIOException::what() const throw()
FileIOException::FileIOException()
: ConfigException("FileIOException")
{
return("FileIOException");
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1111,7 +1096,7 @@ std::string Setting::getPath() const
{
std::stringstream path;

__constructPath(*this, path);
__writeSettingPath(*this, path);

return(path.str());
}
Expand Down

0 comments on commit e29ad28

Please sign in to comment.