Sign in to follow this  
Prozak

How to do this Macro ?

Recommended Posts

I have a class I'm using to assist with debugging, and it has a method that looks something like this: void add_label ( string sourcefile, string sourceline, string label ); So, in my code I have to do: class.add_label( __FILE__, __LINE__, "before mem alloc"); Using macros, is there any way to just do: class.add_label( "before mem alloc" ); ...and have the code work?

Share this post


Link to post
Share on other sites
I think the best you'll be able to expect is ADD_LABEL(class, "before mem alloc").

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
I think the best you'll be able to expect is ADD_LABEL(class, "before mem alloc").


ah, sorry, i don't actually need the code to be:
class.add_label( "before mem alloc" );

and "class" is a global, so the Macro should end up looking something like this:
Debug_AddLabel( "before mem alloc" );

Share this post


Link to post
Share on other sites
Quote:
Original post by raz0r
#define Debug_AddLabel(Data) class.add_label(__FILE__, __LINE__, Data) ?


Thank You, Rating++

Share this post


Link to post
Share on other sites
Macros are just textual replacements, take whatever text that you would type and put it into the macro, and the compiler will type it for you.


class Test
{
public:
void UseLine(unsigned int Line)
{
std::cout << "Line: " << Line << std::endl;
}
};
#define USELINE() UseLine(__LINE__)
void main()
{
Test t;
t.USELINE();
Pause();
}





Works fine in MSVC. Just don't make the macro and any other function have the same name.

Edit: I see that there was an entire exchange in the time I was testing out macros-as-member functions, well, have fun.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Some code that might help:


// preprocessor - turn parameter to string
#define PSYKR_STRINGIZE( text ) PSYKR_STRINGIZER( text )
#define PSYKR_STRINGIZER( text ) #text
// preprocessor - turn string into unicode
#define PSYKR_WIDEN( a ) PSYKR_WIDEN2( a )
#define PSYKR_WIDEN2( a ) L ## a
// preprocessor - turn parameter to wide string
#define PSYKR_WSTRINGIZE( x ) PSYKR_WIDEN( PSYKR_STRINGIZE( x ) )

// then you can just get a bunch of strings like so:
#define Debug_AddLabel( x ) class.add_label( PSYKR_WIDEN(__FILE__), PSYKR_WSTRINGIZE(__LINE__), x )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this