diff --git a/include/etl/exception.h b/include/etl/exception.h index 49e6adca..4323df78 100644 --- a/include/etl/exception.h +++ b/include/etl/exception.h @@ -33,6 +33,14 @@ SOFTWARE. #include "platform.h" +#if ETL_USING_STD_EXCEPTION +#include + + #define ETL_EXCEPTION_CONSTEXPR +#else + #define ETL_EXCEPTION_CONSTEXPR ETL_CONSTEXPR +#endif + ///\defgroup exception exception /// The base class for all ETL exceptions. ///\ingroup utilities @@ -44,6 +52,9 @@ namespace etl /// A low overhead exception base class. //*************************************************************************** class exception +#if ETL_USING_STD_EXCEPTION + : public std::exception +#endif { public: @@ -54,7 +65,7 @@ namespace etl //************************************************************************* /// Constructor. //************************************************************************* - ETL_CONSTEXPR + ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type file_, numeric_type line_) : reason_text(reason_), file_text(file_), @@ -65,7 +76,7 @@ namespace etl //************************************************************************* /// Constructor. //************************************************************************* - ETL_CONSTEXPR + ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type /*file_*/, numeric_type line_) : reason_text(reason_), line(line_) @@ -77,8 +88,11 @@ namespace etl /// Gets the reason for the exception. /// \return const char* to the reason. //*************************************************************************** - ETL_CONSTEXPR - string_type what() const + ETL_EXCEPTION_CONSTEXPR + string_type what() const ETL_NOEXCEPT +#if ETL_USING_STD_EXCEPTION + override +#endif { return reason_text; } diff --git a/include/etl/platform.h b/include/etl/platform.h index 89fafeef..62a5da60 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -293,6 +293,17 @@ SOFTWARE. #define ETL_HAS_VIRTUAL_MESSAGES 1 #endif +//************************************* +// Indicate if etl::exception is to be derived from std::exception. +#if defined(ETL_USE_STD_EXCEPTION) +#if ETL_NOT_USING_STL + #error "Requested std base for etl::exception, but STL is not used" +#endif + #define ETL_USING_STD_EXCEPTION 1 +#else + #define ETL_USING_STD_EXCEPTION 0 +#endif + //************************************* // Indicate if etl::literals::chrono_literals uses ETL verbose style. #if defined(ETL_USE_VERBOSE_CHRONO_LITERALS) && ETL_USING_CPP11 @@ -631,6 +642,7 @@ namespace etl static ETL_CONSTANT bool using_legacy_bitset = (ETL_USING_LEGACY_BITSET == 1); static ETL_CONSTANT bool using_exceptions = (ETL_USING_EXCEPTIONS == 1); static ETL_CONSTANT bool using_libc_wchar_h = (ETL_USING_LIBC_WCHAR_H == 1); + static ETL_CONSTANT bool using_std_exception = (ETL_USING_STD_EXCEPTION == 1); // Has... static ETL_CONSTANT bool has_initializer_list = (ETL_HAS_INITIALIZER_LIST == 1); diff --git a/test/etl_profile.h b/test/etl_profile.h index d29a1093..b6089a40 100644 --- a/test/etl_profile.h +++ b/test/etl_profile.h @@ -44,6 +44,9 @@ SOFTWARE. #define ETL_IN_UNIT_TEST //#define ETL_DEBUG_COUNT #define ETL_ARRAY_VIEW_IS_MUTABLE +#if !defined(ETL_NO_STL) +#define ETL_USE_STD_EXCEPTION +#endif #define ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK #define ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK diff --git a/test/test_exception.cpp b/test/test_exception.cpp index 0a57804f..474eee96 100644 --- a/test/test_exception.cpp +++ b/test/test_exception.cpp @@ -61,5 +61,21 @@ namespace CHECK_EQUAL(123, c.line_number()); } } - } + +#if ETL_USING_STD_EXCEPTION + TEST(test_exception_std_base) + { + etl::exception e("An exception", "Some file", 123); + + try + { + throw e; + } + catch (std::exception& c) + { + CHECK_EQUAL(std::string("An exception"), std::string(c.what())); + } + } +#endif + }; }