Centralized fatal error handling via handleFatalError(msg)

This commit is contained in:
Pavel Kirienko
2014-03-09 20:48:06 +04:00
parent 1b7efa45f5
commit 3dc5183317
6 changed files with 53 additions and 40 deletions
@@ -5,12 +5,11 @@
#pragma once
#include <cassert>
#include <typeinfo>
#include <bitset>
#include <stdexcept>
#include <stdint.h>
#include <algorithm>
#include <uavcan/data_type.hpp>
#include <uavcan/internal/fatal_error.hpp>
#include <uavcan/internal/linked_list.hpp>
#include <uavcan/internal/impl_constants.hpp>
#if UAVCAN_DEBUG
@@ -132,14 +131,7 @@ struct DefaultDataTypeRegistrator
GlobalDataTypeRegistry::instance().regist<Type>(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");
}
};
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#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);
}
@@ -5,9 +5,7 @@
#pragma once
#include <cstdlib>
#include <cassert>
#include <stdexcept>
#include <typeinfo>
#include <uavcan/internal/fatal_error.hpp>
#include <uavcan/internal/impl_constants.hpp>
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<T> is not constructed");
}
void ensureNotConstructed() const
{
if (ptr_)
failure();
handleFatalError("LazyConstructor<T> is already constructed");
}
template <typename U> struct ParamType { typedef const U& Type; };
@@ -7,7 +7,6 @@
#include <bitset>
#include <algorithm>
#include <stdexcept>
#include <typeinfo>
#include <cstring>
#include <uavcan/internal/impl_constants.hpp>
#include <uavcan/internal/util.hpp>
@@ -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);
@@ -4,10 +4,7 @@
#pragma once
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#include <typeinfo>
#include <uavcan/internal/fatal_error.hpp>
#include <uavcan/internal/impl_constants.hpp>
#include <uavcan/internal/util.hpp>
@@ -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:
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#include <uavcan/internal/fatal_error.hpp>
#include <uavcan/internal/impl_constants.hpp>
namespace uavcan
{
void handleFatalError(const char* msg)
{
#if UAVCAN_EXCEPTIONS
throw std::runtime_error(msg);
#else
(void)msg;
assert(0);
std::abort();
#endif
}
}