I am trying have generic "catch certain signal" function, that would handle the signal in specific manner.
The functions are in different flavours; some with no return (void) some with returns (bool) and some with parameters (actually only on).
I got it to compile with three variants of the same function:
int64_t run_inside_abort_check_v( std::function<void(Device*)> fun_ptr );
template<typename Arg>
int64_t run_inside_abort_check_p( std::function<void(Device*, Arg )> fun_ptr, Arg arg );
int64_t run_inside_abort_check_r( bool* ret_value, std::function<bool (Device*)> fun_ptr );
But this would require slightly different three implementations -- that seems stupid.
How could i combine these three into single function?
As an example, the sample version for the single arg version:
template<typename Arg>
int64_t Class::run_inside_abort_check( std::function<void(Device*, Arg)> fun_ptr, Arg args )
{
try
{
if ( flag_abort )
throw SignalAborted();
fun_ptr( this->device, arg ); // Call the wanted function that might thrown SignalToCatch
}
catch ( const SignalToCatch& sig )
{
device->handle_trouble_case();
return (int64_t)ERROR_TROUBLE;
}
return 0x00;
}
As @VTT pointed out the cases (1) and (2) are similar, other is with empty args: When i try such the compiling fails:
#include <iostream>
#include <functional>
class Device
{
public:
void foo1() { std::cout << "foo called\n"; };
void foo2( int bar ) { std::cout << "foo2 called:" << bar << "\n"; };
};
template<typename Arg>
int64_t run_inside_abort_check_p( std::function<void(Device*, Arg )> fun_ptr, Arg arg );
template<typename ... Arg>
int64_t run_inside_abort_check_va( std::function<void(Device*, Arg... )> fun_ptr, Arg ... arg );
int main()
{
int foo;
run_inside_abort_check_p<int>( &Device::foo2, foo ); // works fine!
run_inside_abort_check_va<int>( &Device::foo2, foo );
}
Produces:
error: no matching function for call to ‘run_inside_abort_check_va<int>(void (Device::*)(int), int&)’
silly.cpp:18:9: note: template argument deduction/substitution failed:
silly.cpp:23:56: note: mismatched types ‘std::function<void(Device*, Arg ...)>’ and ‘void (Device::*)(int)’
run_inside_abort_check_va<int>( &Device::foo2, foo );