diff --git a/libuavcan/include/uavcan/global_data_type_registry.hpp b/libuavcan/include/uavcan/global_data_type_registry.hpp index d6be9cab3e..68a3d79c28 100644 --- a/libuavcan/include/uavcan/global_data_type_registry.hpp +++ b/libuavcan/include/uavcan/global_data_type_registry.hpp @@ -5,12 +5,11 @@ #pragma once #include -#include #include -#include #include #include #include +#include #include #include #if UAVCAN_DEBUG @@ -132,14 +131,7 @@ struct DefaultDataTypeRegistrator GlobalDataTypeRegistry::instance().regist(Type::DefaultDataTypeID); if (res != GlobalDataTypeRegistry::RegistResultOk) - { -#if UAVCAN_EXCEPTIONS - throw std::runtime_error("Type registration failed"); -#else - assert(0); - std::abort(); -#endif - } + handleFatalError("Type registration failed"); } }; diff --git a/libuavcan/include/uavcan/internal/fatal_error.hpp b/libuavcan/include/uavcan/internal/fatal_error.hpp new file mode 100644 index 0000000000..0f88a14328 --- /dev/null +++ b/libuavcan/include/uavcan/internal/fatal_error.hpp @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2014 Pavel Kirienko + */ + +#pragma once + +namespace uavcan +{ + +/** + * Fatal error handler. + * Throws std::runtime_error() if exceptions are available, otherwise calls assert(0) then std::abort(). + */ +#if __GNUC__ +__attribute__ ((noreturn)) +#endif +void handleFatalError(const char* msg); + +} diff --git a/libuavcan/include/uavcan/internal/lazy_constructor.hpp b/libuavcan/include/uavcan/internal/lazy_constructor.hpp index c17e41b465..1e4942ebf8 100644 --- a/libuavcan/include/uavcan/internal/lazy_constructor.hpp +++ b/libuavcan/include/uavcan/internal/lazy_constructor.hpp @@ -5,9 +5,7 @@ #pragma once #include -#include -#include -#include +#include #include namespace uavcan @@ -19,26 +17,16 @@ class LazyConstructor unsigned char data_[sizeof(T)] __attribute__((aligned(16))); // TODO: compiler-independent alignment T* ptr_; - void failure() const - { -#if UAVCAN_EXCEPTIONS - throw std::runtime_error(typeid(*this).name()); -#else - assert(0); - std::abort(); -#endif - } - void ensureConstructed() const { if (!ptr_) - failure(); + handleFatalError("LazyConstructor is not constructed"); } void ensureNotConstructed() const { if (ptr_) - failure(); + handleFatalError("LazyConstructor is already constructed"); } template struct ParamType { typedef const U& Type; }; diff --git a/libuavcan/include/uavcan/internal/marshal/array.hpp b/libuavcan/include/uavcan/internal/marshal/array.hpp index f441c14419..5ab2a5cb12 100644 --- a/libuavcan/include/uavcan/internal/marshal/array.hpp +++ b/libuavcan/include/uavcan/internal/marshal/array.hpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -37,7 +36,7 @@ protected: if (pos < Size) return pos; #if UAVCAN_EXCEPTIONS - throw std::out_of_range(typeid(*this).name()); + throw std::out_of_range("uavcan::Array"); #else assert(0); return Size - 1; // Ha ha @@ -66,7 +65,7 @@ protected: if (pos < size_) return pos; #if UAVCAN_EXCEPTIONS - throw std::out_of_range(typeid(*this).name()); + throw std::out_of_range("uavcan::Array"); #else assert(0); return (size_ == 0) ? 0 : (size_ - 1); diff --git a/libuavcan/include/uavcan/internal/method_binder.hpp b/libuavcan/include/uavcan/internal/method_binder.hpp index 4335edc859..94582ace65 100644 --- a/libuavcan/include/uavcan/internal/method_binder.hpp +++ b/libuavcan/include/uavcan/internal/method_binder.hpp @@ -4,10 +4,7 @@ #pragma once -#include -#include -#include -#include +#include #include #include @@ -23,14 +20,7 @@ class MethodBinder void validateBeforeCall() const { if (!operator bool()) - { -#if UAVCAN_EXCEPTIONS - throw std::runtime_error(typeid(*this).name()); -#else - assert(0); - std::abort(); -#endif - } + handleFatalError("Null method binder"); } public: diff --git a/libuavcan/src/fatal_error.cpp b/libuavcan/src/fatal_error.cpp new file mode 100644 index 0000000000..bf1cf3f177 --- /dev/null +++ b/libuavcan/src/fatal_error.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2014 Pavel Kirienko + */ + +#include +#include +#include +#include +#include + +namespace uavcan +{ + +void handleFatalError(const char* msg) +{ +#if UAVCAN_EXCEPTIONS + throw std::runtime_error(msg); +#else + (void)msg; + assert(0); + std::abort(); +#endif +} + +}