C++ Define With Function Call in C#

manala

New member
Joined
Jun 8, 2022
Messages
3
Programming Experience
10+
C#:
#define CALL_FUNCTION(_function, _checkFor) \
{ \
    writeln("Calling %s",  #_call); \
    int res = _function; \
    if(res != (_checkFor)) \
    { \
        std::stringstream msg; \
        writeln("%s returned error %d", #_function, res); \
    } \
}

Not that each function can have different arguments. So I think delegates don't work. Also note that there is actually more going on in the #define but the goal is to do one-liners instead of repeating the same code again and again.
 
Last edited by a moderator:
In the specific case above, delegates will work. Even more specifically, the Func<int> delegate will be a perfect fit for the functional part of calling and checking the return value. What won't be a good fit will be the stringification of the passed in function call for tracing purposes.

Anyway what you are trying to do above is the C way of doing things. In the C# (and most other object oriented languages) way of doing things is by defining interfaces. Then using the decorator pattern, you have one class implement the actual code behind the methods, and in another decorator class, it's methods are just thin wrappers that call the decorated class and does any error checks and logging.

Also, in more object oriented languages, errors are actually thrown as exceptions, not returned as error codes. It looks like you have an impedance mismatch between how you are trying to do or implement things, and the language and the normal principles used in the language.
 
Last edited:
Back
Top Bottom