Skip to content

Commit 1cdcf86

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 1cdcf86

File tree

2 files changed

+77
-92
lines changed

2 files changed

+77
-92
lines changed

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: 69 additions & 77 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,126 @@ 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
{
177178
// head recursion to print path from root to target
178179

179-
if(! setting.isRoot())
180+
if(!setting.isRoot())
180181
{
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() << ']';
182+
__writeSettingPath(setting.getParent(), o);
183+
o << '.';
190184
}
185+
const char *name = setting.getName();
186+
187+
if(name)
188+
o << name;
189+
else
190+
o << '[' << setting.getIndex() << ']';
191191
}
192192

193193
// ---------------------------------------------------------------------------
194194

195-
static std::string __makeSettingExceptionErrorString(char const *path,
196-
char const *derivedType)
195+
static std::string __constructSettingPath(const Setting &setting)
197196
{
198-
std::stringstream sstr;
199-
sstr << derivedType << ": " << path;
200-
return sstr.str();
197+
std::stringstream ss;
198+
__writeSettingPath(setting, ss);
199+
return ss.str();
201200
}
202201

203202
// ---------------------------------------------------------------------------
204203

205-
SettingException::SettingException(char const *derivedType,
206-
const Setting &setting)
204+
static std::string __constructSettingPath(const Setting &setting, int idx)
207205
{
208-
std::stringstream sstr;
209-
__constructPath(setting, sstr);
206+
std::stringstream ss;
207+
__writeSettingPath(setting, ss);
208+
ss << ".[" << idx << ']';
209+
return ss.str();
210+
}
211+
212+
// ---------------------------------------------------------------------------
210213

211-
_path = ::strdup(sstr.str().c_str());
212-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
214+
static std::string __constructSettingPath(const Setting &setting, const char *name)
215+
{
216+
std::stringstream ss;
217+
__writeSettingPath(setting, ss);
218+
ss << ".[" << name << ']';
219+
return ss.str();
213220
}
214221

215222
// ---------------------------------------------------------------------------
216223

217-
SettingException::SettingException(char const *derivedType,
218-
const Setting &setting,
219-
int idx)
224+
static std::string __constructErrorMessage(char const *derivedType, std::string const & path)
220225
{
221-
std::stringstream sstr;
222-
__constructPath(setting, sstr);
223-
sstr << ".[" << idx << "]";
226+
std::stringstream ss;
227+
ss << derivedType << ": " << path;
228+
return ss.str();
229+
}
230+
231+
// ---------------------------------------------------------------------------
224232

225-
_path = ::strdup(sstr.str().c_str());
226-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
233+
SettingException::SettingException(char const *derivedType, std::string path)
234+
: ConfigException(__constructErrorMessage(derivedType, path))
235+
, _path(std::move(path))
236+
{
227237
}
228238

229239
// ---------------------------------------------------------------------------
230240

231241
SettingException::SettingException(char const *derivedType,
232-
const Setting &setting,
233-
const char *name)
242+
const Setting &setting)
243+
: SettingException(derivedType, __constructSettingPath(setting))
234244
{
235-
std::stringstream sstr;
236-
__constructPath(setting, sstr);
237-
sstr << '.' << name;
245+
}
246+
247+
// ---------------------------------------------------------------------------
238248

239-
_path = ::strdup(sstr.str().c_str());
240-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
249+
SettingException::SettingException(char const *derivedType,
250+
const Setting &setting,
251+
int idx)
252+
: SettingException(derivedType, __constructSettingPath(setting, idx))
253+
{
241254
}
242255

243256
// ---------------------------------------------------------------------------
244257

245258
SettingException::SettingException(char const *derivedType,
246-
const char *path)
259+
const Setting &setting,
260+
const char *name)
261+
: SettingException(derivedType, __constructSettingPath(setting, name))
247262
{
248-
_path = ::strdup(path);
249-
_errorMessage = __makeSettingExceptionErrorString(_path, derivedType);
250263
}
251264

252265
// ---------------------------------------------------------------------------
253266

254267
SettingException::SettingException(const Setting &setting)
268+
: SettingException("setting exception", setting)
255269
{
256-
std::stringstream sstr;
257-
__constructPath(setting, sstr);
258-
259-
_path = ::strdup(sstr.str().c_str());
260-
_errorMessage =
261-
__makeSettingExceptionErrorString(_path, "setting exception");
262270
}
263271

264272
// ---------------------------------------------------------------------------
265273

266274
SettingException::SettingException(const Setting &setting, int idx)
275+
: SettingException("setting exception", setting, idx)
267276
{
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");
275277
}
276278

277279
// ---------------------------------------------------------------------------
278280

279281
SettingException::SettingException(const Setting &setting, const char *name)
282+
: SettingException("setting exception", setting, name)
280283
{
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");
288284
}
289285

290286
// ---------------------------------------------------------------------------
291287

292288
SettingException::SettingException(const char *path)
289+
: SettingException("setting exception", path)
293290
{
294-
_path = ::strdup(path);
295-
_errorMessage =
296-
__makeSettingExceptionErrorString(_path, "setting exception");
297291
}
298292

299293
// ---------------------------------------------------------------------------
300294

301-
const char *SettingException::getPath() const
295+
std::string const & SettingException::getPath() const
302296
{
303297
return(_path);
304298
}
@@ -307,17 +301,16 @@ const char *SettingException::getPath() const
307301

308302
SettingException::SettingException(const SettingException &other)
309303
: ConfigException(other)
304+
, _path(other._path)
310305
{
311-
_path = ::strdup(other._path);
312306
}
313307

314308
// ---------------------------------------------------------------------------
315309

316310
SettingException &SettingException::operator=(const SettingException &other)
317311
{
318312
ConfigException::operator=(other);
319-
::free(_path);
320-
_path = ::strdup(other._path);
313+
_path = other._path;
321314

322315
return(*this);
323316
}
@@ -326,7 +319,6 @@ SettingException &SettingException::operator=(const SettingException &other)
326319

327320
SettingException::~SettingException() throw()
328321
{
329-
::free(_path);
330322
}
331323

332324
// ---------------------------------------------------------------------------
@@ -384,9 +376,9 @@ SettingNameException::SettingNameException(const Setting &setting,
384376

385377
// ---------------------------------------------------------------------------
386378

387-
const char *FileIOException::what() const throw()
379+
FileIOException::FileIOException()
380+
: ConfigException("FileIOException")
388381
{
389-
return("FileIOException");
390382
}
391383

392384
// ---------------------------------------------------------------------------
@@ -1111,7 +1103,7 @@ std::string Setting::getPath() const
11111103
{
11121104
std::stringstream path;
11131105

1114-
__constructPath(*this, path);
1106+
__writeSettingPath(*this, path);
11151107

11161108
return(path.str());
11171109
}

0 commit comments

Comments
 (0)