<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Visual C++ Tips &#187; Preprocessors</title>
	<atom:link href="http://weseetips.com/category/preprocessors/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Stringizing Operator, Charizing Operator and Token pasting operator.</title>
		<link>http://weseetips.com/2008/05/29/stringizing-operator-charizing-operator-and-token-pasting-operator/</link>
		<comments>http://weseetips.com/2008/05/29/stringizing-operator-charizing-operator-and-token-pasting-operator/#comments</comments>
		<pubDate>Thu, 29 May 2008 18:50:12 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Preprocessors]]></category>
		<category><![CDATA[#]]></category>
		<category><![CDATA[##]]></category>
		<category><![CDATA[#@]]></category>
		<category><![CDATA[Charizing Operator]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[Stringizing Operator]]></category>
		<category><![CDATA[Token Pasting Operator]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=140</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
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.</p>
<p>For instance, the MFC message map itself. The message map support is provided by three macros &#8211; <span style="color:#0000ff;">DECLARE_MESSAGE_MAP()</span>, <span style="color:#0000ff;">BEGIN_MESSAGE_MAP(</span>) and <span style="color:#0000ff;">END_MESSAGE_MAP()</span>. 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 .</p>
<p>There are some helpful operators which helps you to write powerful marcos. They are #, #@ and ##.</p>
<p><img class="alignnone size-medium wp-image-15" src="http://weseetips.files.wordpress.com/2008/03/icon_underthehood.jpg?w=208" alt="" width="208" height="32" /><br />
<span style="text-decoration:underline;"><strong>Stringizing Operator( # )<br />
</strong></span>As name says &#8211; 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 &#8211; <span style="color:#0000ff;">CDialogDlg::OnButton</span> will get expanded to <span style="color:#0000ff;">&#8220;CDialogDlg::OnButton&#8221;</span></p>
<pre>MESSAGE_BOX( CDialogDlg::OnButton );
...
// The macro which shows Message.
#define MESSAGE_BOX( Message ) \
{\
    AfxMessageBox( _T( #Message )); \
}</pre>
<p>if you want to see a real world example, check the following line from DAOCORE.CPP of MFC. A DAO trace macro.</p>
<pre>DAO_TRACE(m_pDAODatabase-&gt;Close());
...
#define DAO_TRACE(f) AfxDaoTrace(f, #f, THIS_FILE, __LINE__)</pre>
<p><span style="text-decoration:underline;"><strong>Charizing Operator( #@ )<br />
</strong></span>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.</p>
<pre>// Same as a = 'b';
char a = MAKECHAR( b );
...
#define MAKECHAR( x )  #@x</pre>
<p><span style="text-decoration:underline;"><strong>Token-Pasting Operator (##)</strong></span><br />
Its also known as &#8220;Merging operator&#8221;. If its prefixed in macro parameter, that token just get pasted for each occurance. The best example is _T() macro. See its definition below.</p>
<pre>// 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</pre>
<p>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.</p>
<pre>#define DECLARE_DYNAMIC(class_name) \
protected: \
    static CRuntimeClass* PASCAL _GetBaseClass(); \
public: \
    static const AFX_DATA CRuntimeClass <span style="color:#0000ff;">class##class_name</span>; \
    virtual CRuntimeClass* GetRuntimeClass() const; \</pre>
<p><img class="alignnone size-medium wp-image-18" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
But still keep in mind that macros are twin side sharp swords. If not properly used, &#8230; <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="alignnone size-medium wp-image-53" src="http://weseetips.files.wordpress.com/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audience &#8211; Intermediate.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/05/29/stringizing-operator-charizing-operator-and-token-pasting-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual C++ Preprocessor null directive</title>
		<link>http://weseetips.com/2008/03/30/some-problem-with-visual-c-preprocessor/</link>
		<comments>http://weseetips.com/2008/03/30/some-problem-with-visual-c-preprocessor/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 17:14:59 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Preprocessors]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[null directive]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Visual C++]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=47</guid>
		<description><![CDATA[Well, add an empty # symbol to your #include list and compile the project. For e.g. see the following #include list. #include &#8220;stdafx.h&#8221; #include &#8220;Dialog.h&#8221; # #include &#8220;DialogDlg.h&#8221; It will compile without showing any errors. Weird? The single &#8220;#&#8221; in a line is called null directive. It has no effect. Indeed, its not a bug. [...]]]></description>
			<content:encoded><![CDATA[<p><a title="icon_underthehood.jpg" href="http://siteground205.com/~weseetip/wp-content/uploads/2008/03/icon_underthehood.jpg"></a><img src="http://siteground205.com/~weseetip/wp-content/uploads/2008/03/icon_description.jpg" alt="Icon Description" /><br />
Well, add an empty # symbol to your #include list and compile the project. For e.g. see the following #include list.</p>
<p><span style="color:#0000ff;">#include &#8220;stdafx.h&#8221;<br />
#include &#8220;Dialog.h&#8221; </span><span style="color:#0000ff;"><br />
</span><span style="color:#0000ff;"><span style="color:#ff0000;"># </span></span><span style="color:#0000ff;"><br />
#include &#8220;DialogDlg.h&#8221;</span></p>
<p>It will compile without showing any errors. Weird?</p>
<p><img src="http://siteground205.com/~weseetip/wp-content/uploads/2008/03/icon_underthehood.jpg" alt="icon_underthehood.jpg" /><br />
The single &#8220;#&#8221; in a line is called null directive. It has no effect. Indeed, its not a bug. <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
But i can&#8217;t find the answer for question&#8230; What is the purpose?</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/03/30/some-problem-with-visual-c-preprocessor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Log path informations to eventlog, Even better.</title>
		<link>http://weseetips.com/2008/03/23/log-path-informations-to-eventlog-even-better/</link>
		<comments>http://weseetips.com/2008/03/23/log-path-informations-to-eventlog-even-better/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 19:10:41 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Preprocessors]]></category>
		<category><![CDATA[#line]]></category>
		<category><![CDATA[Eventlog]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[__FILE__]]></category>
		<category><![CDATA[__LINE__]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=19</guid>
		<description><![CDATA[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 &#8211; &#8220;C:\Jijo\Build\MyProduct&#8221;, the __FILE__ macro will include [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://weseetips.wordpress.com/files/2008/03/icon_description.jpg" alt="Icon Description" /><br />
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.</p>
<p>But the file macros expands to the fullpath. for e.g. If i am building the delivery in my personal folder &#8211; &#8220;C:\Jijo\Build\MyProduct&#8221;, 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. <img src='http://weseetips.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg" alt="Icon How Can I Do It?" /><br />
You can use #line directive to modify <span style="color:#0000ff;">__LINE__</span> and <span style="color:#0000ff;">__FILENAME__</span>. The syntax is as follows,<br />
<span style="color:#0000ff;">#line lineno &#8220;FileName&#8221;</span></p>
<p>Please see an example below.</p>
<pre><span style="color:#000000;">#line __LINE__ "MyProduct\Sources\JobDll\Job.cpp"</span></pre>
<p><span style="color:#0000ff;"><img src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg" alt="Icon Note" /><span style="color:#0000ff;"><br />
</span></span>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/03/23/log-path-informations-to-eventlog-even-better/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

