From 31e47daf84af1dd21fd2a7a28a339badc256cd61 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sun, 9 Mar 2014 00:12:07 +0400 Subject: [PATCH] Fixed fatal error handing (exceptions); MethodBinder moved into a separate file --- .../uavcan/global_data_type_registry.hpp | 4 +- .../uavcan/internal/lazy_constructor.hpp | 4 +- .../include/uavcan/internal/method_binder.hpp | 47 +++++++++++++++++++ libuavcan/include/uavcan/internal/util.hpp | 21 --------- libuavcan/test/scheduler.cpp | 1 + 5 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 libuavcan/include/uavcan/internal/method_binder.hpp diff --git a/libuavcan/include/uavcan/global_data_type_registry.hpp b/libuavcan/include/uavcan/global_data_type_registry.hpp index 3d3d60cceb..262fcd9cc9 100644 --- a/libuavcan/include/uavcan/global_data_type_registry.hpp +++ b/libuavcan/include/uavcan/global_data_type_registry.hpp @@ -5,12 +5,14 @@ #pragma once #include +#include #include #include #include #include #include #include +#include namespace uavcan { @@ -127,7 +129,7 @@ struct DefaultDataTypeRegistrator if (res != GlobalDataTypeRegistry::RegistResultOk) { #if UAVCAN_EXCEPTIONS - throw std::logic_error("Type registration failed"); + throw std::runtime_error("Type registration failed"); #else assert(0); std::abort(); diff --git a/libuavcan/include/uavcan/internal/lazy_constructor.hpp b/libuavcan/include/uavcan/internal/lazy_constructor.hpp index 7aeaa0f1bd..c17e41b465 100644 --- a/libuavcan/include/uavcan/internal/lazy_constructor.hpp +++ b/libuavcan/include/uavcan/internal/lazy_constructor.hpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include namespace uavcan { @@ -20,7 +22,7 @@ class LazyConstructor void failure() const { #if UAVCAN_EXCEPTIONS - throw std::logic_error(typeid(*this).name()); + throw std::runtime_error(typeid(*this).name()); #else assert(0); std::abort(); diff --git a/libuavcan/include/uavcan/internal/method_binder.hpp b/libuavcan/include/uavcan/internal/method_binder.hpp new file mode 100644 index 0000000000..768533d61a --- /dev/null +++ b/libuavcan/include/uavcan/internal/method_binder.hpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2014 Pavel Kirienko + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace uavcan +{ + +template +class MethodBinder +{ + ObjectPtr obj_; + MemFunPtr fun_; + +public: + MethodBinder(ObjectPtr o, MemFunPtr f) + : obj_(o) + , fun_(f) + { + if (!o || !f) + { +#if UAVCAN_EXCEPTIONS + throw std::runtime_error(typeid(*this).name()); +#else + assert(0); + std::abort(); +#endif + } + } + + void operator()() { (obj_->*fun_)(); } + + template + void operator()(Par1 p1) { (obj_->*fun_)(p1); } + + template + void operator()(Par1 p1, Par2 p2) { (obj_->*fun_)(p1, p2); } +}; + +} diff --git a/libuavcan/include/uavcan/internal/util.hpp b/libuavcan/include/uavcan/internal/util.hpp index 8e8b7aa3fd..15ab6e7bfa 100644 --- a/libuavcan/include/uavcan/internal/util.hpp +++ b/libuavcan/include/uavcan/internal/util.hpp @@ -74,27 +74,6 @@ template struct BooleanType { }; typedef BooleanType TrueType; typedef BooleanType FalseType; -/** - * Makeshift binder - */ -template -class MethodBinder -{ - ObjectPtr obj_; - MemFunPtr fun_; - -public: - MethodBinder(ObjectPtr o, MemFunPtr f) : obj_(o), fun_(f) { } - - void operator()() { (obj_->*fun_)(); } - - template - void operator()(Par1 p1) { (obj_->*fun_)(p1); } - - template - void operator()(Par1 p1, Par2 p2) { (obj_->*fun_)(p1, p2); } -}; - } /// Ensure that conditional comilation macros are present diff --git a/libuavcan/test/scheduler.cpp b/libuavcan/test/scheduler.cpp index 88d7bd4f67..8eb07079f3 100644 --- a/libuavcan/test/scheduler.cpp +++ b/libuavcan/test/scheduler.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "common.hpp" #include "transport/can/iface_mock.hpp"