2

Possible Duplicate:
Can I redefine a C++ macro then define it back?

I have an application that calls a function from a third party SDK lots of times along the app lifetime. This third party function checks for some errors with its _3RDPARTY_ASSERT (which is a wrapper around _ASSERT).

My problem is that in one of these calls, I sometimes expect an error (and handle it afterwards). I would like to disable the assert in this case, as it's quite annoying while debugging, but keep it in all the other cases.

I have tried to handle it with pragma push_macro/pop_macro but I haven't found a way. Is this possible?

I have the source of 3rdParty.cpp but would prefer not to touch it.

This would be a simplified version of the code:

mine.cpp:

#include "3rdparty.h"

HRESULT MyMethod(...)
{
    HRESULT hr;
    hr = _3rdParty(...);
    if (SUCCEEDED(hr))
        hr = _3rdParty(...);
    if (SUCCEEDED(hr))
        hr = _3rdParty(...);
    ...    
    if (SUCCEEDED(hr))
        hr = _3rdParty(...); // This call shouldn't throw the assertion, as I expect it to fail sometimes!
    if (FAILED(hr))
        doSomething();
    else
        doSomethingElse();
    ...
    if (SUCCEEDED(hr))
       hr = _3rdParty(...);
    return hr;    
}

3rdParty.cpp:

...
#define _3RDPARTY_ASSERT (_ASSERT)
...
HRESULT _3rdParty(...)
{
    HRESULT hr;
    hr = SomeFunction();
    _3RDPARTY_ASSERT(SUCCEEDED(hr));
    return hr;
}
Community
  • 1
  • 1
raven
  • 2,574
  • 2
  • 27
  • 49

2 Answers2

2

The problem here is that unless it is inlined, the function will be tokenized and compiled a single time. This means it doesn't matter if the macro is defined or not when you call the function, only when it is compiling the function itself.

Agentlien
  • 4,996
  • 1
  • 16
  • 27
  • I know that... I was hoping there was some kind of workaround :) – raven Dec 04 '12 at 10:06
  • Since you want to change how the function is defined, but only at one specific call, I don't really see how. Sorry. Is there no other way you can handle this first and then save the error info for processing later? – Agentlien Dec 04 '12 at 10:08
  • I don't think so. I could add a _3rdParty_NoAssert() with the exact same code but without the assert. I'm trying to avoid that, though. – raven Dec 04 '12 at 10:15
  • 1
    Them I'm afraid I don't see a solution to your problem. – Agentlien Dec 04 '12 at 10:17
1

If you have access to the definition of _3RDPARTY_ASSER, like

#define _3RDPARTY_ASSERT definition

Then you save that definition:

#define SAVE_ASSERT definition

Then in the place in your code when you don't want an assertion you could #undef _3RDPARTY_ASSERT or #define it to something else.

After that code you can re-enable the old definition by

#define _3RDPARTY_ASSERT SAVE_ASSERT

A second solution, which I prefer, is this: If you have access to the 3'd party code, you can create another version of _3rdParty(...) which doesn't assert, then use it as needed.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
  • 1
    Except he wants to disable it within a call, not where the macro is specified itself, so redefining the macro at that point won't make a difference. Your second solution, however, seems reasonable. – Agentlien Dec 04 '12 at 10:10
  • Thanks! As Agentlien stated, the first part is not useful for my specific problem. I fear I will have to create another version of the method... – raven Dec 04 '12 at 10:24