Reputation: 1398
I have a helper class defined like this:
DataGuard.h
template<typename T, typename D>
class DataGuard {
public:
typedef MY_STATUS( *FunctionType )( const T*, D* );
private:
bool _isSuccess;
D& _data;
const FunctionType _function;
public:
explicit DataGuard( D& data, FunctionType& function, const T* ptr );
~DataGuard();
bool isSuccess() const;
};
#define DATAGUARD( Type ) const DataGuard<Type, Type##Data>
#include "DataGuard.inl"
DataGuard.inl
#define GET_FUNCTION_NAME( functionName ) #functionName
template<typename T, typename D>
DataGuard<T,D>::DataGuard( D& data, FunctionType& function, const T* ptr ) :
_data( data ),
_function( function )
{
auto iReturn = function( ptr, &data );
_isSuccess = iReturn == MY_SUCCESS;
if( !_isSuccess ) {
Utility::logErrorMessage( GET_FUNCTION_NAME( _function ), iReturn );
}
}
template<typename T, typename D>
DataGuard<T,D>::~DataGuard() {
if( _isSuccess ) {
_function( NULL, &_data );
}
}
template<typename T, typename D>
bool DataGuard<T,D>::isSuccess() const {
return _isSuccess;
}
This helper class is being called like this:
DATAGUARD( MyObject ) guard( data, MyObjectGet, ptr );
The problem is that if there is an error in the DataGuard constructor, the error message that I print is showing _function rather than the desired MyObjectGet function name.
Any ideas why the GET_FUNCTION_NAME macro is failing me?
Upvotes: 1
Views: 196
Reputation: 264471
You can't do it from the inside the function.
You can do externally:
DATAGUARD( MyObject ) guard( data, MyObjectGet, ptr );
I would change this:
#define DG(data, func, ptr) data, func, ptr, #func
// usage is now:
DATAGUARD( MyObject ) guard(DG(guard, data, MyObjectGet, ptr));
You will notice that we pass the function pointer and the name of the function pointer as a quoted string as the last parameter. You can use this string as part of your error message.
Upvotes: 1