From 97f1f049710da905bbd1338923eb6d25125b7f15 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Nov 2015 20:34:20 +0000 Subject: Add exception helper --- libadhocutil/exception.h | 28 +++++++++ libadhocutil/unittests/Jamfile.jam | 10 ++++ libadhocutil/unittests/testException.cpp | 97 ++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 libadhocutil/exception.h create mode 100644 libadhocutil/unittests/testException.cpp diff --git a/libadhocutil/exception.h b/libadhocutil/exception.h new file mode 100644 index 0000000..a63439b --- /dev/null +++ b/libadhocutil/exception.h @@ -0,0 +1,28 @@ +#ifndef ADHOCUTIL_EXCEPTION_H +#define ADHOCUTIL_EXCEPTION_H + +#include +#include +#include + +namespace AdHoc { + template + class Exception : public BaseException { + public: + inline const char * what() const throw() override + { + if (!msg) { + msg = message(); + } + return msg->c_str(); + } + + private: + virtual std::string message() const throw() = 0; + mutable boost::optional msg; + }; + typedef Exception StdException; +} + +#endif + diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index b4ec60a..0961f31 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -164,3 +164,13 @@ run testFactory ; +run + testException.cpp + : : : + BOOST_TEST_DYN_LINK + ..//adhocutil + boost_utf + : + testException + ; + diff --git a/libadhocutil/unittests/testException.cpp b/libadhocutil/unittests/testException.cpp new file mode 100644 index 0000000..5794468 --- /dev/null +++ b/libadhocutil/unittests/testException.cpp @@ -0,0 +1,97 @@ +#define BOOST_TEST_MODULE Excetion +#include + +#include +#include + +using namespace AdHoc; + +class Ex1 : public AdHoc::StdException { + public: + Ex1(const char * f, int l) : file(f), line(l) { } + + private: + std::string message() const throw() override + { + return stringf("Something something at %s:%d", file, line); + } + + const char * file; + const int line; +}; + +class OtherBaseException : public std::exception { }; + +class Ex2 : public AdHoc::Exception { + public: + Ex2(const char * f, int l) : file(f), line(l) { } + + private: + std::string message() const throw() override + { + return stringf("Something other something at %s:%d", file, line); + } + + const char * file; + const int line; +}; + +class Ex3 : public AdHoc::StdException { + private: + // LCOV_EXCL_START + std::string message() const throw() override + { + // Never called + std::abort(); + } + // LCOV_EXCL_STOP +}; + +void failing1() +{ + throw Ex1(__PRETTY_FUNCTION__, 1); +} + +void failing2() +{ + throw Ex2(__PRETTY_FUNCTION__, 2); +} + +void failing3() +{ + throw Ex3(); +} + +BOOST_AUTO_TEST_CASE( throwCatch ) +{ + BOOST_REQUIRE_THROW(failing1(), Ex1); + BOOST_REQUIRE_THROW(failing1(), std::exception); + + BOOST_REQUIRE_THROW(failing2(), Ex2); + BOOST_REQUIRE_THROW(failing2(), OtherBaseException); + BOOST_REQUIRE_THROW(failing2(), std::exception); + + BOOST_REQUIRE_THROW(failing3(), Ex3); + BOOST_REQUIRE_THROW(failing3(), std::exception); +} + +BOOST_AUTO_TEST_CASE( message1 ) +{ + try { + failing1(); + } + catch (const std::exception & ex) { + BOOST_REQUIRE_EQUAL("Something something at void failing1():1", ex.what()); + } +} + +BOOST_AUTO_TEST_CASE( message2 ) +{ + try { + failing2(); + } + catch (const std::exception & ex) { + BOOST_REQUIRE_EQUAL("Something other something at void failing2():2", ex.what()); + } +} + -- cgit v1.2.3