Stringizing Operator, Charizing Operator and Token pasting operator.

29 05 2008


What ever C++ best practices says, its a bit tough to eliminate macros. Especially in extensible frameworks. The best example is MFC. Indeed macros have drawbacks, but its the best wepon to inject code inside client classes which uses the framework.

For instance, the MFC message map itself. The message map support is provided by three macros – DECLARE_MESSAGE_MAP(), BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP(). These macros declare static message map array and some utility functions to class. Indeed, the user is free from all those complications and everything are buried deep inside these macros .

There are some helpful operators which helps you to write powerful marcos. They are #, #@ and ##.


Stringizing Operator( # )
As name says – Stringizing Operator converts the macro parameter to a string. if # is prefixed before the parameter name, and used in the macro body, that parameter will get enclosed in double quotes. For instance see the following macro used for showing messagebox. The message – CDialogDlg::OnButton will get expanded to “CDialogDlg::OnButton”

MESSAGE_BOX( CDialogDlg::OnButton );
...
// The macro which shows Message.
#define MESSAGE_BOX( Message ) \
{\
    AfxMessageBox( _T( #Message )); \
}

if you want to see a real world example, check the following line from DAOCORE.CPP of MFC. A DAO trace macro.

DAO_TRACE(m_pDAODatabase->Close());
...
#define DAO_TRACE(f) AfxDaoTrace(f, #f, THIS_FILE, __LINE__)

Charizing Operator( #@ )
The Charizing operator is used to convert a macro parameter to a charector constant. If its prefixed in macro parameter, the parameter get enclosed in single quotes to form a char constant. For instance see the e.g. below. Its taken from MSDN.

// Same as a = 'b';
char a = MAKECHAR( b );
...
#define MAKECHAR( x )  #@x

Token-Pasting Operator (##)
Its also known as “Merging operator”. If its prefixed in macro parameter, that token just get pasted for each occurance. The best example is _T() macro. See its definition below.

// Assume unicode is defined.
// This will expand to csString = L"Hello";
CString csString = _T( "Hello" );
...
#define _T(x)       __T(x)
#define __T(x)      L ## x

Here the ##x pasts the token directly there. See one more example to see the power of token pasting operator. It can also be used to give unique names to the member variables in the injected code. See the definition of DECLARE_DYNAMIC() macro. Here the class name is pasted to generate unique name for the member variable injected.

#define DECLARE_DYNAMIC(class_name) \
protected: \
    static CRuntimeClass* PASCAL _GetBaseClass(); \
public: \
    static const AFX_DATA CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; \


But still keep in mind that macros are twin side sharp swords. If not properly used, … πŸ˜‰


Targeted Audience – Intermediate.

Advertisements




Visual C++ Preprocessor null directive

30 03 2008

Icon Description
Well, add an empty # symbol to your #include list and compile the project. For e.g. see the following #include list.

#include “stdafx.h”
#include “Dialog.h”

#
#include “DialogDlg.h”

It will compile without showing any errors. Weird?

icon_underthehood.jpg
The single “#” in a line is called null directive. It has no effect. Indeed, its not a bug. πŸ˜‰
But i can’t find the answer for question… What is the purpose?





Log path informations to eventlog, Even better.

23 03 2008

Icon Description
In most of our projects, when some error occurs, it’s being logged to the evenlog with line number and filename. Its done by using __FILE__ and __LINE__ macro.

But the file macros expands to the fullpath. for e.g. If i am building the delivery in my personal folder – “C:\Jijo\Build\MyProduct”, the __FILE__ macro will include this full path and finally in eventlog will contain funny paths which the end-user might see. You can see some 3ed party eventlogs in event viewer which contains authors name in path. πŸ™‚

Icon How Can I Do It?
You can use #line directive to modify __LINE__ and __FILENAME__. The syntax is as follows,
#line lineno “FileName”

Please see an example below.

#line __LINE__ "MyProduct\Sources\JobDll\Job.cpp"

Icon Note
While experimenting I’ve found that – Its safe to use #line after all #includes. If its used at the top of file, It can be reinitialized by the include files. At first it was not working for me. Latter works. Anyway take care.