A silly macro technique

A

Alf P. Steinbach

Replace "debugger api" with "whatever" in this


<code>
#define USE_DEBUGGER_API // Or not

extern void useDebuggerApi();

#define CALL_IF_EXPANDED( doIt, f ) \
struct S_##f { \
struct Size2 { char x[2]; }; \
char foo( ... ) { return 0; } \
Size2 foo##doIt( int ) { return Size2(); } \
}; \
(sizeof( S_##f().foo(0) ) > 1 ? f() : (void)0)

#define CALL_IF( doIt, f ) CALL_IF_EXPANDED( doIt, f )

int main()
{
CALL_IF( USE_DEBUGGER_API, useDebuggerApi );
}
</code>


And why is it silly?

Well, if only routine is called in any particular build, then there could just
be a macro FUNCNAME, say, defined as the name of the routine to call.

But hey!

Actually I found a use for it.

I think (not sure yet).


Cheers,

- Alf
 
N

Noah Roberts

Replace "debugger api" with "whatever" in this


<code>
#define USE_DEBUGGER_API // Or not

extern void useDebuggerApi();

#define CALL_IF_EXPANDED( doIt, f ) \
struct S_##f { \
struct Size2 { char x[2]; }; \
char foo( ... ) { return 0; } \
Size2 foo##doIt( int ) { return Size2(); } \
}; \
(sizeof( S_##f().foo(0) ) > 1 ? f() : (void)0)

#define CALL_IF( doIt, f ) CALL_IF_EXPANDED( doIt, f )

int main()
{
CALL_IF( USE_DEBUGGER_API, useDebuggerApi );
}
</code>


And why is it silly?

Well, if only routine is called in any particular build, then there could just
be a macro FUNCNAME, say, defined as the name of the routine to call.

But hey!

Actually I found a use for it.

Neat trick but I'd be interested to see anything that couldn't be done
in an easier way.
 
K

Kaz Kylheku

Replace "debugger api" with "whatever" in this


<code>
#define USE_DEBUGGER_API // Or not

extern void useDebuggerApi();

#define CALL_IF_EXPANDED( doIt, f ) \
struct S_##f { \
struct Size2 { char x[2]; }; \
char foo( ... ) { return 0; } \
Size2 foo##doIt( int ) { return Size2(); } \
}; \
(sizeof( S_##f().foo(0) ) > 1 ? f() : (void)0)

#define CALL_IF( doIt, f ) CALL_IF_EXPANDED( doIt, f )

int main()
{
CALL_IF( USE_DEBUGGER_API, useDebuggerApi );
}
</code>


And why is it silly?

It's also silly because USE_DEBUGGER_API could just be a preprocessor
symbol that is always defined, and expands to 0 or 1.

Then CALL_IF is simply

#define CALL_IF(FLAG, FUNC) ((FLAG) ? FUNC() : (void) 0)

And it doesn't save you typing:

if (flag) func();

CALL_IF(flag, func);

Oops!
 
A

Alan Woodland

Noah said:
Replace "debugger api" with "whatever" in this


<code>
#define USE_DEBUGGER_API // Or not

extern void useDebuggerApi();

#define CALL_IF_EXPANDED( doIt, f ) \
struct S_##f { \
struct Size2 { char x[2]; }; \
char foo( ... ) { return 0; } \
Size2 foo##doIt( int ) { return Size2(); } \
}; \
(sizeof( S_##f().foo(0) ) > 1 ? f() : (void)0)

#define CALL_IF( doIt, f ) CALL_IF_EXPANDED( doIt, f )

int main()
{
CALL_IF( USE_DEBUGGER_API, useDebuggerApi );
}
</code>


And why is it silly?

Well, if only routine is called in any particular build, then there could just
be a macro FUNCNAME, say, defined as the name of the routine to call.

But hey!

Actually I found a use for it.

Neat trick but I'd be interested to see anything that couldn't be done
in an easier way.

Similar idea, different approach. I wanted to be able to pass things to
the function in question too if it got called. Doing anything sane with
a return value is impossible of course given that one may/may not even
call it in the end.

Mostly just an excuse to try the variadic template arguments out really.

I wanted the function pointers to be template arguments themselves, but
couldn't see a way to make that work and keep the type deduction going.

Totally pointless learning exercise for me, probably wouldn't ever
deploy this in production :)

Alan

#include <cstdlib>
#include <ctime>
#include <iostream>

template <int test>
class CallIfTrue {
template <typename Ret, typename... Args>
struct dispatcher {
typedef Ret (*F_ptr)(Args...);
const F_ptr f;
dispatcher(const F_ptr& f) : f(f) {}
Ret operator()(const Args&... args) const {
return f(args...);
}
};
public:
// Figure out how to use the dispatcher if we need args
template<typename T, typename... Args>
static dispatcher<T,Args...> dispatch(T(*F)(Args...)) {
return dispatcher<T,Args...>(F);
}

// Simple if no function args
template<typename T>
static T dispatch( T(F)()) {
return F();
}
};

template<>
class CallIfTrue<0> {
// Lets this work for both param and non-param cases
struct eater {
void operator()(...) const {
}
};
public:
// Don't care about anything if we don't call it
static eater dispatch(...) {
return eater();
}
};

#define CALL_IF_TRUE(x,y) CallIfTrue<(x)>::dispatch(y)

void foo(const int& a) { std::cout << "Foo(" << a << ")" << std::endl; }
void bar() { std::cout << "Bar()" << std::endl; }

int main() {
std::cout << "Should get called:" << std::endl;
CallIfTrue<1>::dispatch(foo)(1);
std::cout << "Shouldn't get called:" << std::endl;
CALL_IF_TRUE(0,foo)(1);
std::cout << "Should get called:" << std::endl;
CallIfTrue<1>::dispatch(bar);
std::cout << "Shouldn't get called:" << std::endl;
CALL_IF_TRUE(0,bar);
std::cout << "Should get called:" << std::endl;
CALL_IF_TRUE(1,foo)(100);
// This will fail to compile if we tried to use the retun value when
// it wasn't true
std::cout << "Time is: " << CALL_IF_TRUE(-1,time)(NULL) << std::endl;

return 0;
}
 
A

Alf P. Steinbach

* Kaz Kylheku:
Replace "debugger api" with "whatever" in this


<code>
#define USE_DEBUGGER_API // Or not

extern void useDebuggerApi();

#define CALL_IF_EXPANDED( doIt, f ) \
struct S_##f { \
struct Size2 { char x[2]; }; \
char foo( ... ) { return 0; } \
Size2 foo##doIt( int ) { return Size2(); } \
}; \
(sizeof( S_##f().foo(0) ) > 1 ? f() : (void)0)

#define CALL_IF( doIt, f ) CALL_IF_EXPANDED( doIt, f )

int main()
{
CALL_IF( USE_DEBUGGER_API, useDebuggerApi );
}
</code>


And why is it silly?

It's also silly because USE_DEBUGGER_API could just be a preprocessor
symbol that is always defined, and expands to 0 or 1.

Then CALL_IF is simply

#define CALL_IF(FLAG, FUNC) ((FLAG) ? FUNC() : (void) 0)

And it doesn't save you typing:

if (flag) func();

CALL_IF(flag, func);

Oops!

I wouldn't say that that is a silly thing. After all, trying to use a large
coffee cup as a bathtub is rather silly, but it's not a property of the cup:
it's a property of the particular usage it's put to... For the above to be
reasonable one must, I think, imagine a number of macros like USE_DEBUGGER_API,
perhaps defined in the compiler invocation.

Cheers,

- Alf
 
A

Alf P. Steinbach

* Alf P. Steinbach:
Replace "debugger api" with "whatever" in this


<code>
#define USE_DEBUGGER_API // Or not

extern void useDebuggerApi();

#define CALL_IF_EXPANDED( doIt, f ) \
struct S_##f { \
struct Size2 { char x[2]; }; \
char foo( ... ) { return 0; } \
Size2 foo##doIt( int ) { return Size2(); } \
}; \
(sizeof( S_##f().foo(0) ) > 1 ? f() : (void)0)

#define CALL_IF( doIt, f ) CALL_IF_EXPANDED( doIt, f )

int main()
{
CALL_IF( USE_DEBUGGER_API, useDebuggerApi );
}
</code>


And why is it silly?

Well, if only routine is called in any particular build, then there
could just be a macro FUNCNAME, say, defined as the name of the routine
to call.

But hey!

Actually I found a use for it.

I think (not sure yet).

I would be interested in improvements.

For those who wonder (and I guess that would be every reader, yes?) the use case
is where a possibly large number of macro symbols can potentially be defined,
and where it's impractical to define them all, so the code should cope well with
any symbol not being defined.

The remaining problem with this is as noted in the comment:


<code>
// f is called if preprocessor symbol 'symbol' is defined.
// If 'symbol' is defined it must be defined as nothing or as something
// that's valid at the end of a C++ name, e.g. TEST_BLAH defined as 1.
//
// Quirk: f will be called if e.g. TEST_BLAH is defined as TEST_BLAH.

#define TESTING_CALL_IFDEF_( symbol, literal_symbol, f, args ) \
do { \
struct S_##f { \
struct Size2 { char x[2]; }; \
char foo##literal_symbol( ... ) { return 0; } \
Size2 foo_##symbol( int ) { return Size2(); } \
}; \
if( \
sizeof( S_##f().foo##literal_symbol(0) ) == 1 \
) \
{ f args; } \
} while( false )

#define TESTING_CALL_IFDEF( symbol, f, args ) \
TESTING_CALL_IFDEF_( symbol, _##symbol, f, args )
</code>


Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top