<?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>EntBlog &#187; CodeGems</title>
	<atom:link href="http://entland.homelinux.com/blog/category/codegems/feed/" rel="self" type="application/rss+xml" />
	<link>http://entland.homelinux.com/blog</link>
	<description>Code, 3D, Games, Linux and much more...</description>
	<lastBuildDate>Mon, 08 Feb 2010 09:51:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Compile-Time Strings</title>
		<link>http://entland.homelinux.com/blog/2009/04/28/compile-time-strings/</link>
		<comments>http://entland.homelinux.com/blog/2009/04/28/compile-time-strings/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 22:19:40 +0000</pubDate>
		<dc:creator>ent</dc:creator>
				<category><![CDATA[CodeGems]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://entland.homelinux.com/blog/?p=346</guid>
		<description><![CDATA[It would be nice if we had such a feature in the C language, wouldn&#8217;t it? The term &#8216;compile-time string&#8217; is referred here as strings that are converted to unique integer identifiers at compile time. At run-time those identifiers are simple integers that can be compared and hashed very fast.  In other languages, like [...]]]></description>
			<content:encoded><![CDATA[<p>It would be nice if we had such a feature in the C language, wouldn&#8217;t it? The term &#8216;compile-time string&#8217; is referred here as strings that are converted to unique integer identifiers at compile time. At run-time those identifiers are simple integers that can be compared and hashed very fast.  In other languages, like for example Smalltalk, the concept of Symbol implements a similar idea. The following post describes a possible implementation of this feature in C/C++.</p>
<p><span id="more-346"></span></p>
<p>Imagine, for example, a generic object factory where object instances are created using unique identifiers. The classical solution here is having a shared-by-all-code header where all the identifiers are declared in a C enumeration. This solution, apart from creating a serious physical dependency where adding a new identifier to the enumeration forces a recompilation for all the project, is unfeasible in modular architectures where modules are isolated. In those architectures having a global header is not an option.</p>
<p>One viable solution may be using strings as identifiers. But strings are heavy objects, hard to compare and prone to typing errors because miswritten symbols would be detected at run-time instead of compile-time. Other equally insufficient solutions to this problem include <a href="http://en.wikipedia.org/wiki/FourCC">FourCC</a> and esoteric template tricks for generating a hash at compile time (desist from it, it is not possible to solve this 100% with templates because strings cannot be used as template parameters and anyway hashing a string is not collision-free. More information in <a href="http://www.usenet.com/newsgroups/comp.lang.c++.moderated/msg05807.html">this usenet thread</a>). Mick West proposes more solutions in his <a href="http://cowboyprogramming.com/2007/01/04/practical-hash-ids/">Practical Hash IDs</a> article.</p>
<p>What follows is an implementation that has been working nicely for me and that satisfactorily fits the requirements for simulating compile-time strings. First let me show you two examples of the usage:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">namespace</span>
<span style="color: #008000;">&#123;</span>
DECLARE_SYMBOL<span style="color: #008000;">&#40;</span>CubeMesh<span style="color: #008000;">&#41;</span>
DECLARE_SYMBOL<span style="color: #008000;">&#40;</span>SphereMesh<span style="color: #008000;">&#41;</span>
DECLARE_SYMBOL<span style="color: #008000;">&#40;</span>DuckMesh<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> CollectNodes<span style="color: #008000;">&#40;</span>Ptr<span style="color: #000080;">&lt;</span>Node<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> node<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Ptr<span style="color: #000080;">&lt;</span>Mesh<span style="color: #000080;">&gt;</span> mesh0 <span style="color: #000080;">=</span> CreateObject<span style="color: #008000;">&#40;</span>S<span style="color: #008000;">&#40;</span>CubeMesh<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Add<span style="color: #008000;">&#40;</span>mesh0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    Ptr<span style="color: #000080;">&lt;</span>Mesh<span style="color: #000080;">&gt;</span> mesh1 <span style="color: #000080;">=</span> CreateObject<span style="color: #008000;">&#40;</span>S<span style="color: #008000;">&#40;</span>SphereMesh<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Add<span style="color: #008000;">&#40;</span>mesh1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    Ptr<span style="color: #000080;">&lt;</span>Mesh<span style="color: #000080;">&gt;</span> mesh2 <span style="color: #000080;">=</span> CreateObject<span style="color: #008000;">&#40;</span>S<span style="color: #008000;">&#40;</span>DuckMesh<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Add<span style="color: #008000;">&#40;</span>mesh2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">namespace</span>
<span style="color: #008000;">&#123;</span>
DECLARE_SYMBOL<span style="color: #008000;">&#40;</span>FirstMessage<span style="color: #008000;">&#41;</span>
DECLARE_SYMBOL<span style="color: #008000;">&#40;</span>SecondMessage<span style="color: #008000;">&#41;</span>
DECLARE_SYMBOL<span style="color: #008000;">&#40;</span>ThirdMessage<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> ProcessMessage<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Message<span style="color: #000040;">&amp;</span> msg<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>msg.<span style="color: #007788;">id</span> <span style="color: #000080;">==</span> S<span style="color: #008000;">&#40;</span>FirstMessage<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       <span style="color: #666666;">/// ...</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>msg.<span style="color: #007788;">id</span> <span style="color: #000080;">==</span> S<span style="color: #008000;">&#40;</span>SecondMessage<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       <span style="color: #666666;">/// ...</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>msg.<span style="color: #007788;">id</span><span style="color: #000080;">==</span> S<span style="color: #008000;">&#40;</span>ThirdMessage<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       <span style="color: #666666;">/// ...</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>A symbol represents a compile-time string. They must be declared before being used. The macro for declaring a symbol is hiding an inline function with a static inside itself:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define DECLARE_SYMBOL(id)\
    inline Symbol __GetSymbol##id() throw()\
    {\
        static size_t sym;\
        if (sym == 0)\
        {\
            sym = GetIdFromString(#id);\
        }\
        return Symbol(sym);\
    }</span></pre></div></div>

<p>The function GetIdFromString() hashes the string, stores it in an internal table and returns the table position for that string (the Symbol class is a simple wrapper around the identifier). This is done only the first time the symbol is requested. For future requests the static ID is returned. This adds a little overhead against using simple integers as symbols. Beware of local static initializations: they are not thread-safe. That is the reason of the manual comparison against 0. GetIdFromString() must be thread-safe for this code to work.</p>
<p>The S macro simply invokes the local function previously generated:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define S(id) __GetSymbol##id()</span></pre></div></div>

<p>And there you have it. Compile-time strings with negligible (in case you are doing anything more that simply comparing symbols) overhead. In case you need 100% efficient code you could pre-generate a table with the symbols being used by your project (searching for all DECLARE_SYMBOL blocks) and substitute each S() with a really unique identifier generated at compile-time. And that would be so easy if the preprocessor could be extended in a standard way&#8230;</p>
<p>Hope this makes sense. Thank you for reading.</p>
<p>&nbsp;</p>
<div class="hr"></div>
<ol>
<li>
<a href="http://cowboyprogramming.com/2007/01/04/practical-hash-ids/">Practical Hash IDs</a>
</li>
<li>
<a href="http://en.wikipedia.org/wiki/FourCC">FourCC</a>
</li>
<li>
<a href="http://www.usenet.com/newsgroups/comp.lang.c++.moderated/msg05807.html">Compile-time string hash generator</a>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://entland.homelinux.com/blog/2009/04/28/compile-time-strings/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Stripping comments from Shader bytecodes</title>
		<link>http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/</link>
		<comments>http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 20:37:39 +0000</pubDate>
		<dc:creator>ent</dc:creator>
				<category><![CDATA[CodeGems]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://entland.homelinux.com/blog/?p=281</guid>
		<description><![CDATA[In DirectX, when compiling a shader with D3DXCompileShader() a buffer containing the shader bytecodes is received. Apart from the bytecodes, extra content like debug and symbol table information is embedded. That extra information is added in form of comments that probably can be eliminated because you are already processing it at compile-time and it is [...]]]></description>
			<content:encoded><![CDATA[<p>In DirectX, when compiling a shader with <strong>D3DXCompileShader()</strong> a buffer containing the shader bytecodes is received. Apart from the bytecodes, extra content like debug and symbol table information is embedded. That extra information is added in form of comments that probably can be eliminated because you are already processing it at compile-time and it is not needed at run-time when loading the shader.</p>
<p>If you can do without that information the following code will help you to save a few bytes, even halving the size of the byte-code in the best cases.</p>
<p>Although not documented in the DirectX SDK, this CodeGem is not an undocumented hack. The Direct3D shader code format is documented in the <a href="http://msdn.microsoft.com/en-us/library/ms800355.aspx">MSDN</a>, so it probaly won&#8217;t change in future revision of DirectX v9.0 (if there is going to be any more&#8230;)</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">D3DPtr<span style="color: #000080;">&lt;</span>ID3DXBuffer<span style="color: #000080;">&gt;</span> StripComments<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> D3DPtr<span style="color: #000080;">&lt;</span>ID3DXBuffer<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> code<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Calculates the new size (without comments)</span>
    <span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> codeData <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>code<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetBufferPointer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> sizeInWords <span style="color: #000080;">=</span> code<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetBufferSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> strippedSizeInWords <span style="color: #000080;">=</span> sizeInWords<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> sizeInWords<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>codeData<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">&amp;</span> <span style="color: #208080;">0xffff</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> D3DSIO_COMMENT<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">int</span> commentSize <span style="color: #000080;">=</span> codeData<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;&gt;</span> <span style="color: #0000dd;">16</span><span style="color: #008080;">;</span>
            strippedSizeInWords <span style="color: #000040;">-</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000040;">+</span> commentSize<span style="color: #008080;">;</span>
            i <span style="color: #000040;">+</span><span style="color: #000080;">=</span> commentSize<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// Creates a new buffer with the original code but omitting the comments</span>
    D3DPtr<span style="color: #000080;">&lt;</span>ID3DXBuffer<span style="color: #000080;">&gt;</span> strippedCode<span style="color: #008080;">;</span>
    V<span style="color: #008000;">&#40;</span>D3DXCreateBuffer<span style="color: #008000;">&#40;</span>strippedSizeInWords <span style="color: #000040;">*</span> <span style="color: #0000dd;">4</span>, strippedCode.<span style="color: #007788;">GetPtrForInit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> strippedCodeData <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>strippedCode<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetBufferPointer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">size_t</span> offset <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> sizeInWords<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>codeData<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">&amp;</span> <span style="color: #208080;">0xffff</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> D3DSIO_COMMENT<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">int</span> commentSize <span style="color: #000080;">=</span> codeData<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;&gt;</span> <span style="color: #0000dd;">16</span><span style="color: #008080;">;</span>
            i <span style="color: #000040;">+</span><span style="color: #000080;">=</span> commentSize<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span>
        <span style="color: #008000;">&#123;</span>
            strippedCodeData<span style="color: #008000;">&#91;</span>offset<span style="color: #000040;">++</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> codeData<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> strippedCode<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Capturing OutputDebugString</title>
		<link>http://entland.homelinux.com/blog/2007/11/09/capturing-outputdebugstring/</link>
		<comments>http://entland.homelinux.com/blog/2007/11/09/capturing-outputdebugstring/#comments</comments>
		<pubDate>Fri, 09 Nov 2007 01:20:51 +0000</pubDate>
		<dc:creator>ent</dc:creator>
				<category><![CDATA[CodeGems]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://entland.homelinux.com/blog/2007/11/09/capturing-outputdebugstring/</guid>
		<description><![CDATA[Using OutputDebugString for tracing your programs is probably not a good idea, at least if you have lots of traces being generated (OutputDebugString raises an exception and causes a kernel mode transition). So you will probably end up implementing your own tracing/logging system. Parts of code that are not under your control may be still [...]]]></description>
			<content:encoded><![CDATA[<p>Using <strong>OutputDebugString</strong> for tracing your programs is probably not a good idea, at least if you have lots of traces being generated (<strong>OutputDebugString</strong> raises an exception and causes a kernel mode transition). So you will probably end up implementing your own tracing/logging system. Parts of code that are not under your control may be still using <strong>OutputDebugString</strong> (like Debugging Tools for Windows, DirectX, etc). The code listed below allows capturing <strong>OutputDebugString</strong> calls generated inside your own process (in fact, it is capturing all the OSD calls generated by all the active processes). </p>
<p>You will need to put this piece of code in a separate thread, but those details are omitted for better clarity.</p>
<p><strong>NOTE</strong>: If the process is being debugged, the ODS calls will be intercepted by debugger.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> DbWinBuffer
<span style="color: #008000;">&#123;</span>
    DWORD dwProcessId<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span> data<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4096</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
DbWinBuffer<span style="color: #000040;">*</span> dbBuffer<span style="color: #008080;">;</span>
&nbsp;
HANDLE hAckEvent<span style="color: #008080;">;</span>
HANDLE hEvent<span style="color: #008080;">;</span>
HANDLE hSharedFile<span style="color: #008080;">;</span>
&nbsp;
SECURITY_DESCRIPTOR sd<span style="color: #008080;">;</span>
SECURITY_ATTRIBUTES sa<span style="color: #008080;">;</span>
<span style="color: #666666;">/////////////////////////////////////////////////////////////////////////////</span>
&nbsp;
sa.<span style="color: #007788;">nLength</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>SECURITY_ATTRIBUTES<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
sa.<span style="color: #007788;">bInheritHandle</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
sa.<span style="color: #007788;">lpSecurityDescriptor</span> <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>sd<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>InitializeSecurityDescriptor<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>sd, SECURITY_DESCRIPTOR_REVISION<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”ERROR<span style="color: #008080;">:</span> InitializeSecurityDescriptor\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>SetSecurityDescriptorDacl<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>sd, <span style="color: #0000ff;">true</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”ERROR<span style="color: #008080;">:</span> SetSecurityDescriptorDacl\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
hAckEvent <span style="color: #000080;">=</span> CreateEvent<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>sa, <span style="color: #0000ff;">false</span>, <span style="color: #0000ff;">false</span>, L”DBWIN_BUFFER_READY”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>hAckEvent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”ERROR<span style="color: #008080;">:</span> CreateEvent<span style="color: #008000;">&#40;</span>\”DBWIN_BUFFER_READY\”<span style="color: #008000;">&#41;</span>\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
hEvent <span style="color: #000080;">=</span> CreateEvent<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>sa, <span style="color: #0000ff;">false</span>, <span style="color: #0000ff;">false</span>, L”DBWIN_DATA_READY”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>hEvent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”ERROR<span style="color: #008080;">:</span> CreateEvent<span style="color: #008000;">&#40;</span>\”DBWIN_DATA_READY\”<span style="color: #008000;">&#41;</span>\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
hSharedFile <span style="color: #000080;">=</span> CreateFileMapping<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>HANDLE<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span>, <span style="color: #000040;">&amp;</span>sa, PAGE_READWRITE, <span style="color: #0000dd;">0</span>, 
    L”DBWIN_BUFFER”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>hSharedFile<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”ERROR<span style="color: #008080;">:</span> CreateFileMapping<span style="color: #008000;">&#40;</span>\”DBWIN_BUFFER\”<span style="color: #008000;">&#41;</span>\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
dbBuffer <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>DbWinBuffer<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>MapViewOfFile<span style="color: #008000;">&#40;</span>hSharedFile, FILE_MAP_READ, <span style="color: #0000dd;">0</span>,
    <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">4096</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>dbBuffer<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”ERROR<span style="color: #008080;">:</span> MapViewOfFile\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
SetEvent<span style="color: #008000;">&#40;</span>hAckEvent<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
DWORD pid <span style="color: #000080;">=</span> GetCurrentProcessId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”Tracing PID<span style="color: #008080;">:</span> <span style="color: #000040;">%</span>d\n\n”, pid<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #008080;">;;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    DWORD ret <span style="color: #000080;">=</span> WaitForSingleObject<span style="color: #008000;">&#40;</span>hEvent, INFINITE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>ret <span style="color: #000080;">==</span> WAIT_FAILED<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”ERROR<span style="color: #008080;">:</span> WaitForSingleObject\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>dbBuffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dwProcessId <span style="color: #000080;">==</span> pid<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>”<span style="color: #000040;">%</span>s”, dbBuffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    SetEvent<span style="color: #008000;">&#40;</span>hAckEvent<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://entland.homelinux.com/blog/2007/11/09/capturing-outputdebugstring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Efficient update of stl containers</title>
		<link>http://entland.homelinux.com/blog/2006/05/17/efficient-update-of-stl-containers/</link>
		<comments>http://entland.homelinux.com/blog/2006/05/17/efficient-update-of-stl-containers/#comments</comments>
		<pubDate>Wed, 17 May 2006 21:08:20 +0000</pubDate>
		<dc:creator>ent</dc:creator>
				<category><![CDATA[CodeGems]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://entland.homelinux.com/blog/2006/05/17/efficient-update-of-stl-containers/</guid>
		<description><![CDATA[Update: Fixed some bugs in the sample code.
Update: Note added in the third option about the hint.
I have seen dozens of times code like this to update an entry in a map:

typedef std::map&#60;std::string, int&#62; Items;
Items items;
&#160;
Items::iterator it = items.find&#40;&#34;counter&#34;&#41;;
&#160;
if &#40;it == items.end&#40;&#41;&#41;
&#123;
   items.insert&#40;std::make_pair&#40;&#34;counter&#34;, 1&#41;&#41;;
&#125;
else
&#123;
   it-&#62;second++;
&#125;

The problem with this code is that [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> Fixed some bugs in the sample code.<br />
<strong>Update:</strong> Note added in the third option about the hint.</p>
<p>I have seen dozens of times code like this to update an entry in a map:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, <span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> Items<span style="color: #008080;">;</span>
Items items<span style="color: #008080;">;</span>
&nbsp;
Items<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it <span style="color: #000080;">=</span> items.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;counter&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>it <span style="color: #000080;">==</span> items.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   items.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">make_pair</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;counter&quot;</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">else</span>
<span style="color: #008000;">&#123;</span>
   it<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>second<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The problem with this code is that you are traversing the map two times if the item is not found in the map, one for the first find and one more for the insert.</p>
<p>You can improve the code with something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">pair</span><span style="color: #000080;">&lt;</span>items<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span>, <span style="color: #0000ff;">bool</span><span style="color: #000080;">&gt;</span> res <span style="color: #000080;">=</span> items.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">make_pair</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;counter&quot;</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>res.<span style="color: #007788;">second</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>res.<span style="color: #007788;">first</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">second</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This is notably better. You only traverse the map one time. But there is still a problem with this code: you are creating the pair even when you do not need it. In this case the pair is simple and its creation is not a problem, but imagine a pair with more complex objects hard to create. You can avoid the creation with a code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Items<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> lowerBound <span style="color: #000080;">=</span> items.<span style="color: #007788;">lower_bound</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;counter&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>lowerBound <span style="color: #000040;">!</span><span style="color: #000080;">=</span> items.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #000040;">&amp;&amp;</span> <span style="color: #000040;">!</span><span style="color: #008000;">&#40;</span>items.<span style="color: #007788;">key_comp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;counter&quot;</span>, lowerBound<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   lowerBound<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>second<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">else</span>
<span style="color: #008000;">&#123;</span>
   items.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>lowerBound, std<span style="color: #008080;">::</span><span style="color: #007788;">make_pair</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;counter&quot;</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Although being more efficient, this code is more obscure, so in case you do not need those nanoseconds you can live with the more clear second option. Apart from being more obscure, the hint is only that, a hint. The implementation can ignore it. As noted by Steven in the comments below, Visual Studio ignores it for the hash_map and hast_set containers.</p>
]]></content:encoded>
			<wfw:commentRss>http://entland.homelinux.com/blog/2006/05/17/efficient-update-of-stl-containers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
