Skip to content

Commit e29ad28

Browse files
committed
Inherit exceptions from std::runtime_error
This includes refactoring some of the error message and path construction functions.
1 parent 68f6159 commit e29ad28

2 files changed

Lines changed: 72 additions & 94 deletions

File tree

lib/libconfig.h++

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define __libconfig_hpp
2525

2626
#include <stdio.h>
27-
#include <exception>
27+
#include <stdexcept>
2828
#include <string>
2929

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

4949
namespace libconfig {
5050

51-
struct LIBCONFIGXX_API ConfigException : public std::exception
51+
struct LIBCONFIGXX_API ConfigException : public std::runtime_error
5252
{
5353
ConfigException();
5454
ConfigException(std::string const &message);
5555

5656
ConfigException(ConfigException const &other);
57+
ConfigException& operator=(ConfigException const &other);
5758

5859
virtual ~ConfigException() throw();
59-
60-
virtual const char * what() const throw();
61-
62-
protected:
63-
64-
std::string _errorMessage;
6560
};
6661

6762
class Setting; // fwd decl
@@ -76,7 +71,7 @@ class LIBCONFIGXX_API SettingException : public ConfigException
7671
SettingException(char const *derivedType, const Setting &setting);
7772
SettingException(char const *derivedType, const Setting &setting, int idx);
7873
SettingException(char const *derivedType, const Setting &setting, const char *name);
79-
SettingException(char const *derivedType, const char *path);
74+
SettingException(char const *derivedType, std::string path);
8075

8176
public:
8277

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

9186
virtual ~SettingException() throw();
9287

93-
const char *getPath() const;
88+
std::string const & getPath() const;
9489

9590
private:
9691

97-
char *_path;
92+
std::string _path;
9893
};
9994

10095
class LIBCONFIGXX_API SettingTypeException : public SettingException
@@ -122,11 +117,9 @@ class LIBCONFIGXX_API SettingNameException : public SettingException
122117
SettingNameException(const Setting &setting, const char *name);
123118
};
124119

125-
class LIBCONFIGXX_API FileIOException : public ConfigException
120+
struct LIBCONFIGXX_API FileIOException : public ConfigException
126121
{
127-
public:
128-
129-
virtual const char *what() const throw();
122+
FileIOException();
130123
};
131124

132125
class LIBCONFIGXX_API ParseException : public ConfigException

lib/libconfigcpp.c++

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,36 @@ static const char **__include_func(config_t *config,
4949
// ---------------------------------------------------------------------------
5050

5151
ConfigException::ConfigException()
52+
: std::runtime_error("")
5253
{
5354
}
5455

5556
// ---------------------------------------------------------------------------
5657

5758
ConfigException::ConfigException(std::string const &errorMessage)
58-
: _errorMessage(errorMessage)
59+
: std::runtime_error(errorMessage)
5960
{
6061
}
6162

6263
// ---------------------------------------------------------------------------
6364

6465
ConfigException::ConfigException(ConfigException const &other)
65-
: _errorMessage(other._errorMessage)
66+
: std::runtime_error(other.what())
6667
{
6768
}
6869

6970
// ---------------------------------------------------------------------------
7071

71-
ConfigException::~ConfigException() throw()
72+
ConfigException& ConfigException::operator=(ConfigException const &other)
7273
{
74+
std::runtime_error::operator=(other);
75+
return(*this);
7376
}
7477

7578
// ---------------------------------------------------------------------------
7679

77-
const char * ConfigException::what() const throw()
80+
ConfigException::~ConfigException() throw()
7881
{
79-
return _errorMessage.c_str();
8082
}
8183

8284
// ---------------------------------------------------------------------------
@@ -171,134 +173,119 @@ static int __toTypeCode(Setting::Type type)
171173

172174
// ---------------------------------------------------------------------------
173175

174-
static void __constructPath(const Setting &setting,
175-
std::stringstream &path)
176+
static void __writeSettingPath(const Setting &setting, std::ostream &o)
176177
{
177-
// head recursion to print path from root to target
178-
179-
if(! setting.isRoot())
178+
if (!setting.isRoot())
180179
{
181-
__constructPath(setting.getParent(), path);
182-
if(path.tellp() > 0)
183-
path << '.';
184-
185-
const char *name = setting.getName();
186-
if(name)
187-
path << name;
188-
else
189-
path << '[' << setting.getIndex() << ']';
180+
__writeSettingPath(setting.getParent(), o);
181+
o << '.';
190182
}
183+
o << setting.getName();
191184
}
192185

193186
// ---------------------------------------------------------------------------
194187

195-
static std::string __makeSettingExceptionErrorString(char const *path,
196-
char const *derivedType)
188+
static std::string __constructSettingPath(const Setting &setting)
197189
{
198-
std::stringstream sstr;
199-
sstr << derivedType << ": " << path;
200-
return sstr.str();
190+
std::stringstream ss;
191+
__writeSettingPath(setting, ss);
192+
return ss.str();
201193
}
202194

203195
// ---------------------------------------------------------------------------
204196

205-
SettingException::SettingException(char const *derivedType,
206-
const Setting &setting)
197+
static std::string __constructSettingPath(const Setting &setting, int idx)
207198
{
208-
std::stringstream sstr;
209-
__constructPath(setting, sstr);
199+
std::stringstream ss;
200+
__writeSettingPath(setting, ss);
201+
ss << ".[" << idx << ']';
202+
return ss.str();
203+
}
210204

211-
_path = ::strdup(sstr.str().c_str());
212-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
205+
// ---------------------------------------------------------------------------
206+
207+
static std::string __constructSettingPath(const Setting &setting, const char *name)
208+
{
209+
std::stringstream ss;
210+
__writeSettingPath(setting, ss);
211+
ss << ".[" << name << ']';
212+
return ss.str();
213213
}
214214

215215
// ---------------------------------------------------------------------------
216216

217-
SettingException::SettingException(char const *derivedType,
218-
const Setting &setting,
219-
int idx)
217+
static std::string __constructErrorMessage(char const *derivedType, std::string const & path)
220218
{
221-
std::stringstream sstr;
222-
__constructPath(setting, sstr);
223-
sstr << ".[" << idx << "]";
219+
std::stringstream ss;
220+
ss << derivedType << ": " << path;
221+
return ss.str();
222+
}
223+
224+
// ---------------------------------------------------------------------------
224225

225-
_path = ::strdup(sstr.str().c_str());
226-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
226+
SettingException::SettingException(char const *derivedType, std::string path)
227+
: ConfigException(__constructErrorMessage(derivedType, path))
228+
, _path(std::move(path))
229+
{
227230
}
228231

229232
// ---------------------------------------------------------------------------
230233

231234
SettingException::SettingException(char const *derivedType,
232-
const Setting &setting,
233-
const char *name)
235+
const Setting &setting)
236+
: SettingException(derivedType, __constructSettingPath(setting))
234237
{
235-
std::stringstream sstr;
236-
__constructPath(setting, sstr);
237-
sstr << '.' << name;
238+
}
239+
240+
// ---------------------------------------------------------------------------
238241

239-
_path = ::strdup(sstr.str().c_str());
240-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
242+
SettingException::SettingException(char const *derivedType,
243+
const Setting &setting,
244+
int idx)
245+
: SettingException(derivedType, __constructSettingPath(setting, idx))
246+
{
241247
}
242248

243249
// ---------------------------------------------------------------------------
244250

245251
SettingException::SettingException(char const *derivedType,
246-
const char *path)
252+
const Setting &setting,
253+
const char *name)
254+
: SettingException(derivedType, __constructSettingPath(setting, name))
247255
{
248-
_path = ::strdup(path);
249-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
250256
}
251257

252258
// ---------------------------------------------------------------------------
253259

254260
SettingException::SettingException(const Setting &setting)
261+
: SettingException("setting exception", setting)
255262
{
256-
std::stringstream sstr;
257-
__constructPath(setting, sstr);
258-
259-
_path = ::strdup(sstr.str().c_str());
260-
_errorMessage =
261-
__makeSettingExceptionErrorString(_path, "setting exception");
262263
}
263264

264265
// ---------------------------------------------------------------------------
265266

266267
SettingException::SettingException(const Setting &setting, int idx)
268+
: SettingException("setting exception", setting, idx)
267269
{
268-
std::stringstream sstr;
269-
__constructPath(setting, sstr);
270-
sstr << ".[" << idx << "]";
271-
272-
_path = ::strdup(sstr.str().c_str());
273-
_errorMessage =
274-
__makeSettingExceptionErrorString(_path, "setting exception");
275270
}
276271

277272
// ---------------------------------------------------------------------------
278273

279274
SettingException::SettingException(const Setting &setting, const char *name)
275+
: SettingException("setting exception", setting, name)
280276
{
281-
std::stringstream sstr;
282-
__constructPath(setting, sstr);
283-
sstr << '.' << name;
284-
285-
_path = ::strdup(sstr.str().c_str());
286-
_errorMessage =
287-
__makeSettingExceptionErrorString(_path, "setting exception");
288277
}
289278

290279
// ---------------------------------------------------------------------------
291280

292281
SettingException::SettingException(const char *path)
282+
: SettingException("setting exception", path)
293283
{
294-
_path = ::strdup(path);
295-
_errorMessage =
296-
__makeSettingExceptionErrorString(_path, "setting exception");
297284
}
298285

299286
// ---------------------------------------------------------------------------
300287

301-
const char *SettingException::getPath() const
288+
std::string const & SettingException::getPath() const
302289
{
303290
return(_path);
304291
}
@@ -307,17 +294,16 @@ const char *SettingException::getPath() const
307294

308295
SettingException::SettingException(const SettingException &other)
309296
: ConfigException(other)
297+
, _path(other._path)
310298
{
311-
_path = ::strdup(other._path);
312299
}
313300

314301
// ---------------------------------------------------------------------------
315302

316303
SettingException &SettingException::operator=(const SettingException &other)
317304
{
318305
ConfigException::operator=(other);
319-
::free(_path);
320-
_path = ::strdup(other._path);
306+
_path = other._path;
321307

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

327313
SettingException::~SettingException() throw()
328314
{
329-
::free(_path);
330315
}
331316

332317
// ---------------------------------------------------------------------------
@@ -384,9 +369,9 @@ SettingNameException::SettingNameException(const Setting &setting,
384369

385370
// ---------------------------------------------------------------------------
386371

387-
const char *FileIOException::what() const throw()
372+
FileIOException::FileIOException()
373+
: ConfigException("FileIOException")
388374
{
389-
return("FileIOException");
390375
}
391376

392377
// ---------------------------------------------------------------------------
@@ -1111,7 +1096,7 @@ std::string Setting::getPath() const
11111096
{
11121097
std::stringstream path;
11131098

1114-
__constructPath(*this, path);
1099+
__writeSettingPath(*this, path);
11151100

11161101
return(path.str());
11171102
}

0 commit comments

Comments
 (0)