<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Game Development Student Journal</title>
	<atom:link href="http://gamedevstudent.com/journal/feed/" rel="self" type="application/rss+xml" />
	<link>http://gamedevstudent.com/journal</link>
	<description>Ramblings of Sean, Student of Game Development</description>
	<lastBuildDate>Fri, 17 Feb 2012 21:45:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C++ Method Type Deduction Tricks</title>
		<link>http://gamedevstudent.com/journal/2012/02/c-method-type-deduction-tricks/</link>
		<comments>http://gamedevstudent.com/journal/2012/02/c-method-type-deduction-tricks/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 01:24:23 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=297</guid>
		<description><![CDATA[Working on the next part of the C++ Metadata series, I decided that I didn&#8217;t like the way I was handling setters and getters for object properties. I wanted something even faster and with even less overhead while retaining the extreme simplicity of my API. I discovered some tricks (only tested in Visual Studio 2010 [...]]]></description>
			<content:encoded><![CDATA[<p>Working on the next part of the C++ Metadata series, I decided that I didn&#8217;t like the way I was handling setters and getters for object properties.  I wanted something even faster and with even less overhead while retaining the extreme simplicity of my API.  I discovered some tricks (only tested in Visual Studio 2010 so far) that do exactly what I need.  I&#8217;m afraid I&#8217;m a little too swamped to write anything about how these tricks work just now, but I wanted to get the code out there.<br />
<span id="more-297"></span></p>
<p>I may update the article with explanations of what&#8217;s going on and why this all works later if time permits.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;type_traits&gt;</span><br />
<br />
<span style="color: #666666;">// debugging macro</span><br />
<span style="color: #339900;">#define assert(expr) do{ if (!(expr)) __debugbreak(); }while(0)</span><br />
<br />
<span style="color: #666666;">// getter/setter typedefs</span><br />
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">void</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>Getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> object, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> out_value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">void</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>Setter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> object, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// deduce if a getter is const or not</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
std<span style="color: #008080;">::</span><span style="color: #007788;">true_type</span> deduce_constness<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
std<span style="color: #008080;">::</span><span style="color: #007788;">false_type</span> deduce_constness<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// deduce the object type of a getter/setter pointer-to-method</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
ObjectType deduce_object<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
ObjectType deduce_object<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType, <span style="color: #0000ff;">typename</span> SetType<span style="color: #000080;">&gt;</span><br />
ObjectType deduce_object<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>SetType<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// deduce the return type of a getter/setter pointer-to-method</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
ReturnType deduce_return_val<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
ReturnType deduce_return_val<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType, <span style="color: #0000ff;">typename</span> SetType<span style="color: #000080;">&gt;</span><br />
ReturnType deduce_return_val<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>SetType<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// deduce the parameter type of a setter pointer-to-method</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType, <span style="color: #0000ff;">typename</span> SetType<span style="color: #000080;">&gt;</span><br />
SetType deduce_first_param<span style="color: #008000;">&#40;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>setter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>SetType value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// wrapper for a const getter</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">bool</span> Const, <span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> wrap_getter<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>Getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> thunk<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> object, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> out_value<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">const</span> ObjectType<span style="color: #000040;">*</span> typed_object <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> ObjectType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>object<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; ReturnType<span style="color: #000040;">*</span> typed_value <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>ReturnType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>out_value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #000040;">*</span>typed_value <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>typed_object<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #000040;">*</span>Getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// wrapper for a non-const getter</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> wrap_getter<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">false</span>, ObjectType, ReturnType<span style="color: #000080;">&gt;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>Getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> thunk<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> object, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> out_value<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; ObjectType<span style="color: #000040;">*</span> typed_object <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>ObjectType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>object<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; ReturnType<span style="color: #000040;">*</span> typed_value <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>ReturnType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>out_value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #000040;">*</span>typed_value <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>typed_object<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #000040;">*</span>Getter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// wrapper for a setter</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ObjectType, <span style="color: #0000ff;">typename</span> ReturnType, <span style="color: #0000ff;">typename</span> SetType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> wrap_setter<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span>ReturnType<span style="color: #008000;">&#40;</span>ObjectType<span style="color: #008080;">::</span><span style="color: #000040;">*</span>Setter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>SetType<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> thunk<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> object, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> value<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; ObjectType<span style="color: #000040;">*</span> typed_object <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>ObjectType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>object<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">const</span> SetType<span style="color: #000040;">*</span> typed_value <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> SetType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#40;</span>typed_object<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #000040;">*</span>Setter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>typed_value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// construction macros</span><br />
<span style="color: #339900;">#define GETTER(method) \<br />
&nbsp; (&amp;wrap_getter&lt;std::is_same&lt;decltype(deduce_constness(method)), std::true_type&gt;::value, decltype(deduce_object(method)), decltype(deduce_return_val(method))&gt;::thunk&lt;method&gt;)</span><br />
<br />
<span style="color: #339900;">#define SETTER(method) \<br />
&nbsp; (&amp;wrap_setter&lt;decltype(deduce_object(method)), decltype(deduce_return_val(method)), decltype(deduce_first_param(method))&gt;::thunk&lt;method&gt;)</span><br />
<br />
<span style="color: #666666;">// test object</span><br />
<span style="color: #0000ff;">class</span> TestObject<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; TestObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span> i<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span>, f<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span>.<span style="color: #007788;">f</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">int</span> getInt<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">float</span> getFloat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> f<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">void</span> setInt<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i_<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> i <span style="color: #000080;">=</span> i_<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">float</span> setFloat<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> f_<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> f <span style="color: #000080;">=</span> f_<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">float</span> f<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// test code</span><br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; Getter call_i <span style="color: #000080;">=</span> GETTER<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>TestObject<span style="color: #008080;">::</span><span style="color: #007788;">getInt</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; Getter call_f <span style="color: #000080;">=</span> GETTER<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>TestObject<span style="color: #008080;">::</span><span style="color: #007788;">getFloat</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; Setter set_i <span style="color: #000080;">=</span> SETTER<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>TestObject<span style="color: #008080;">::</span><span style="color: #007788;">setInt</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; Setter set_f <span style="color: #000080;">=</span> SETTER<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>TestObject<span style="color: #008080;">::</span><span style="color: #007788;">setFloat</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <br />
&nbsp; TestObject o<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">float</span> f<span style="color: #008080;">;</span><br />
&nbsp; <br />
&nbsp; call_i<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>o, <span style="color: #000040;">&amp;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; call_f<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>o, <span style="color: #000040;">&amp;</span>f<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>i <span style="color: #000080;">==</span> o.<span style="color: #007788;">i</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>f <span style="color: #000080;">==</span> o.<span style="color: #007788;">f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; i <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span><br />
&nbsp; f <span style="color: #000080;">=</span> <span style="color: #0000dd;">7</span>.<span style="color: #007788;">f</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; set_i<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>o, <span style="color: #000040;">&amp;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; set_f<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>o, <span style="color: #000040;">&amp;</span>f<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>o.<span style="color: #007788;">i</span> <span style="color: #000080;">==</span> i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>o.<span style="color: #007788;">f</span> <span style="color: #000080;">==</span> f<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2012/02/c-method-type-deduction-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Metadata – Part II, Inheritance, Dynamic Casting, and Allocation</title>
		<link>http://gamedevstudent.com/journal/2012/01/c-metadata-part-ii-inheritance-dynamic-casting-and-allocation/</link>
		<comments>http://gamedevstudent.com/journal/2012/01/c-metadata-part-ii-inheritance-dynamic-casting-and-allocation/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 10:03:33 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=289</guid>
		<description><![CDATA[In Part I in the series on C++ Metadata, I explored how to build the basics of a runtime metadata system for C++ types, including custom classes, third-party libraries&#8217; classes, and even C++ primitives. The article detailed building the low-level facilities to have metadata, but did not cover any real-world uses of metadata. Today&#8217;s article [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://gamedevstudent.com/journal/2012/01/c-metadata-par…ons-and-lookup/">Part I</a> in the series on C++ Metadata, I explored how to build the basics of a runtime metadata system for C++ types, including custom classes, third-party libraries&#8217; classes, and even C++ primitives.  The article detailed building the low-level facilities to have metadata, but did not cover any real-world uses of metadata.  Today&#8217;s article is going to cover adding inheritance information to the metadata, using that information to construct a dynamic_cast<> work-alike, and adding a simple allocation helper.<br />
<span id="more-289"></span></p>
<span id="Quick_Review"><h2>Quick Review</h2></span>
<p>I&#8217;m going to jump in and start with a relisting of the Metadata class I built last time.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> Metadata<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; Metadata<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name, <span style="color: #0000ff;">size_t</span> size<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Name<span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span>, m_Size<span style="color: #008000;">&#40;</span>size<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Name<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Size<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> m_Name<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> m_Size<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>The Metadata class has just enough information for some interesting demos but not much else.  The m_Size member might be enough to support a memory allocation method for applications that never need to worry about extended alignment, but that might not be enough for many games that use extended-alignment for vectors and matrices that are intended to be optimized with SSE, NEON, or AltiVec.</p>
<span id="Inheritance_Metadata"><h2>Inheritance Metadata</h2></span>
<p>Inheritance information is one of the easier things to add to our Metadata class.  Especially if we limit ourselves to single-inheritance patterns, which is what I do.  Multiple-inheritance has a lot of valid use cases, but the complexity for our implementation is high and the benefits of supporting multiple-inheritance with dynamic type casting in an engine that makes heavy use of <a href="http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/">component-based design</a> are relatively limited.</p>
<p>The first step then is to extend Metadata with a pointer to a parent Metadata, representing the inheritance.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> Metadata<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; Metadata<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name, <span style="color: #0000ff;">size_t</span> size, <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> parent<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Name<span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span>, m_Size<span style="color: #008000;">&#40;</span>size<span style="color: #008000;">&#41;</span>, m_Parent<span style="color: #008000;">&#40;</span>parent<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Name<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Size<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> parent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Parent<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">bool</span> isa<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> base<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> m_Name<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> m_Size<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> m_Parent<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Quite straight forward.  You may have noticed that I also added an isa() method to Metadata.  The method simply checks if the parameter is in the chain of parent pointers for the Metadata instance.  It will be used for dynamic type casting/checking later on.</p>
<p>Actually setting the pointer require a small addition to the macros we created in the last article.  The macros made use of templates and a special MetaSingleton<> class, which comes in very handy now.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#define DEFINE_META(metatype, parent) Metadata MetaSingleton&lt;metatype&gt;::s_Meta(#metatype, sizeof(metatype), parent);</span><br />
<span style="color: #339900;">#define META_TYPE(metatype) (MetaSingleton&lt;metatype&gt;::get())</span></div></div>
<p>Using the macros to define metadata for types with and without parents is simple:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">struct</span> Foo <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">struct</span> Bar <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Foo <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
DEFINE_META<span style="color: #008000;">&#40;</span>Foo, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
DEFINE_META<span style="color: #008000;">&#40;</span>Bar, META_TYPE<span style="color: #008000;">&#40;</span>Foo<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></div>
<p>It&#8217;s entirely reasonable to just create two different DEFINE_META macros; one for types with parents and one for types without, with the former removing the need to explicitly invoke META_TYPE when specifying the parent type.  In a later article in the series we&#8217;ll cover some advanced tricks for making the definition of metadata exceedingly easy and readable (with some caveats, of course).  For now, the simple macros get the trick done.</p>
<p>Extending the code to support multiple inheritance is not necessarily difficult, but it does require some thought.  Maintaining a list or other data structure of all possible parents for a type without needing dynamic memory allocation will require a small bit of care.  Initializing that list with clean, readable DEFINE_META-style macros requires even more care.  All doable, but I&#8217;d recommend just skipping that unless a real need for it arises.</p>
<span id="Dynamic_Type_Casting"><h2>Dynamic Type Casting</h2></span>
<p>The dynamic_cast<> operator built-in to C++ works about as efficiently as possible and supports every conceivable valid type cast C++&#8217;s type system allows.  Replacing it does not make a lot of sense unless there is some value to doing so.  One reason may well be that the default RTTI system in C++ has been disabled because of its redundant data; even without a replacement metadata system, many games have been known to disable RTTI, especially those targeting resource-constrained mobile gaming platforms.  A simpler, lighter replacement for dynamic_cast<> that supports exactly the features needed and no more can be valuable in these cases.</p>
<p>Another huge value of a replacement is that dynamic_cast<> does not support specifying the target type at runtime.  That is, the target type is a compile-time template parameter to dynamic_cast<>.  Actually casting to a runtime-specified type makes little sense in C++, but being able to query whether such a cast would be valid has plenty of uses.</p>
<p>A third reason to replace dynamic_cast<> is the exception-based failure mode for dynamic_cast<>.  If exceptions are disabled, or if failures are expected to be common for some particular use, it is important to use a purely pointer-based implementation that can simply return NULL on failure.</p>
<p>Dynamic type casting via C++&#8217;s built-in dynamic_cast<> is a fairly complicated affair.  At the simplest level, C++ needs to use the RTTI&#8217;s chain of inheritance, much as our system will do.  At the most complicated level, C++ supports virtual inheritance, which can require a lot of black magic to do the proper pointer offset arithmetic.  We&#8217;re not going to even consider supporting virtual bases in this article series, so we can ignore all that complexity.</p>
<p>The first thing we&#8217;re going to need is an implementation for the Metadata::isa() method I added to the class definition previously.  All it does is walk a linked list, looking for a match.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">bool</span> Metadata<span style="color: #008080;">::</span><span style="color: #007788;">isa</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> base<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> meta <span style="color: #000080;">=</span> <span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>meta <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>meta <span style="color: #000080;">==</span> base<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span> <span style="color: #666666;">// found a match</span><br />
&nbsp; &nbsp; &nbsp;meta <span style="color: #000080;">=</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>parent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> <span style="color: #666666;">// no match found</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Implementing a cast operator is now quite trivial, using a little bit of template magic and our MetaLookup<> template from the previous article.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> TargetType, <span style="color: #0000ff;">typename</span> InputType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">static</span> TargetType<span style="color: #000040;">*</span> MetaCast<span style="color: #008000;">&#40;</span>InputType<span style="color: #000040;">*</span> input<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> meta <span style="color: #000080;">=</span> MetaLookup<span style="color: #000080;">&lt;</span>InputType<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> target <span style="color: #000080;">=</span> MetaSingleton<span style="color: #000080;">&lt;</span>TargetType<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">return</span> meta <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000040;">&amp;&amp;</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>isa<span style="color: #008000;">&#40;</span>target<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>TargetType<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> TargetType, <span style="color: #0000ff;">typename</span> InputType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> TargetType<span style="color: #000040;">*</span> MetaCast<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> InputType<span style="color: #000040;">*</span> input<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> meta <span style="color: #000080;">=</span> MetaLookup<span style="color: #000080;">&lt;</span>InputType<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> target <span style="color: #000080;">=</span> MetaSingleton<span style="color: #000080;">&lt;</span>TargetType<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">return</span> meta <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000040;">&amp;&amp;</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>isa<span style="color: #008000;">&#40;</span>target<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> TargetType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Usage of the new &#8220;operator&#8221; is more or less identical to the dynamic_cast<> operator, except that only pointers are supported, and failure to perform the cast is signaled by a NULL return value.</p>
<p>Extending the operator to support multiple inheritance requires no changes to the implementation of MetaCast<>() itself, as all of the real work is performed by Metadata::isa().</p>
<span id="Memory_Allocation"><h2>Memory Allocation</h2></span>
<p>A final real-world use for metadata that I&#8217;ll cover in this part of the metadata series is the creation of a memory allocation helper.  We already have enough information for a basic implementation, as we have the size of the type stored in the Metadata class.</p>
<p>For the sake of many game engines, however, it can be quite handy to have alignment support as well.  This can be a teeny bit tricky to do in an automatic way as not all compilers support the new C++11 alignof operator, including both GCC and Visual C++.  Thankfully, GCC does provide a nearly identical proprietary operator, and Visual C++ has an operator that can be cajoled into doing the work we need.  We can wrap them up in a macro easily enough.  The VC++ version has some&#8230; magic involved to work around a compiler crash bug I ran into.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#if __cplusplus &gt;= 201103L</span><br />
&nbsp; <span style="color: #ff0000; font-style: italic;">/* do nothing, alignof() should be supported, or else the compiler is broken and a liar */</span><br />
<span style="color: #339900;">#elif defined(_MSC_VER)</span><br />
<span style="color: #339900;"># &nbsp;define alignof(x) __alignof(decltype(*static_cast&lt;std::remove_reference&lt;std::remove_pointer&lt;(x)&gt;::type&gt;::type*&gt;(0)))</span><br />
<span style="color: #339900;">#elif defined(__GNUC__)</span><br />
<span style="color: #339900;"># &nbsp;define alignof(x) __alignof__((x))</span><br />
<span style="color: #339900;">#else</span><br />
<span style="color: #339900;"># &nbsp;error &quot;Unsupported compiler: missing C++11 alignof() operator or known work-around&quot;</span><br />
<span style="color: #339900;">#endif</span></div></div>
<p>The Visual C++ version makes use of the proprietary __alignof operator.  However, I found that it will cause a compiler crash for some cases, like references to abstract classes, which we certainly want to be able to create Metadata objects for.  The mess up above was something I found that worked around the bug.  The GCC version uses the proprietary __alignof__ operator, which has no bugs I could detect and which works enough like the C++11 alignof that it just works for all tests I&#8217;ve tried.  Clang 3.0 should support alignof, but I don&#8217;t know if it defined __cplusplus to the appropriate version yet.  I don&#8217;t have a build of Clang ready locally to test with, but the code may need a simple additional check for Clang to whitelist it to using the alignof operator.  Please let me know if you test and find changes are necessary.  I don&#8217;t use (or recommend) any other compilers, so I do not know if any need workarounds or what those might be.</p>
<p>That out of the way, the Metadata class can now be extended with alignment information and an allocate() method.  We also can update the DEFINE_META macro to automatically generate the alignment data.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> Metadata<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; Metadata<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name, <span style="color: #0000ff;">size_t</span> size, <span style="color: #0000ff;">size_t</span> alignment, <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> parent<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span><br />
&nbsp; &nbsp; m_Name<span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span>, m_Size<span style="color: #008000;">&#40;</span>size<span style="color: #008000;">&#41;</span>, m_Alignment<span style="color: #008000;">&#40;</span>alignment<span style="color: #008000;">&#41;</span>, m_Parent<span style="color: #008000;">&#40;</span>parent<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Name<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Size<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> alignment<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Alignment<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> parent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Parent<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">bool</span> isa<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> base<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> allocate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">void</span> deallocate<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> m_Name<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> m_Size<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> m_Alignment<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> m_Parent<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #339900;">#define DEFINE_META(metatype, parent) Metadata MetaSingleton&lt;metatype&gt;::s_Meta( \<br />
&nbsp; #metatype, sizeof(metatype), alignof(metatype), parent);</span></div></div>
<p>The allocate() method could almost just be a simple wrapper around operator new if it weren&#8217;t for C++&#8217;s embarrassing lack of alignment support in operator new (even in the new standard).  We have to rely on platform-specific memory allocation routines.  Unfortunately, on some platforms these also require platform-specific deallocation routines as well, hence the deallocate() method.</p>
<p>Implementing them is straight forward.  I provide Win32/POSIX implementations here.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> Metadata<span style="color: #008080;">::</span><span style="color: #007788;">allocate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #339900;">#if defined(_WIN32)</span><br />
&nbsp; <span style="color: #0000ff;">return</span> _aligned_malloc<span style="color: #008000;">&#40;</span>m_Size, m_Alignment<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #339900;">#else</span><br />
&nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> mem<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">int</span> rs <span style="color: #000080;">=</span> posix_memalign<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>mem, m_Alignment, m_Size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">return</span> rs <span style="color: #000080;">==</span> EOK <span style="color: #008080;">?</span> mem <span style="color: #008080;">:</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
<span style="color: #339900;">#endif</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> Metadata<span style="color: #008080;">::</span><span style="color: #007788;">deallocate</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> mem<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #339900;">#if defined(_WIN32)</span><br />
&nbsp; _aligned_free<span style="color: #008000;">&#40;</span>mem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #339900;">#else</span><br />
&nbsp; <span style="color: #0000dd;">free</span><span style="color: #008000;">&#40;</span>mem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #339900;">#endif</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>A few improvements and extensions can easily be made.  Supporting array allocation and deallocation is quite trivial, for starts.  Adding a bit more platform-specific logic can avoid the call to the aligned allocation functions (which may be slower than the regular allocation functions) if the type&#8217;s alignment is already less than or equal to the platform&#8217;s default alignment (8 on most PC platforms; 16 on OS X and Windowx x64).</p>
<p>Most importantly, it is quite possible to add support for custom allocator interfaces.  My engine completely bans the use of default operator new or malloc(), for instance, and requires all allocations to go through a slightly more sophisticated memory management API.  This API is used for custom allocator support (e.g., scratch allocators, pool allocators, etc.), handles hierarchical per-arena accounting, and ensures we have a decent general purpose allocation algorithm on platforms like Win XP which have pretty awful default allocators.  Extending the allocate() API here is as easy as giving it a new parameter for the allocator and to replace the implementation with calls to that allocator.</p>
<p>Allocation and deallocation are not nearly enough, naturally.  Objects need to be constructed and destructed.  That requires a bit more work to accomplish, and will be part of the next article in the series.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2012/01/c-metadata-part-ii-inheritance-dynamic-casting-and-allocation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[C++ Metadata]]></series:name>
	</item>
		<item>
		<title>C++ Metadata &#8211; Part I, Singletons and Lookup</title>
		<link>http://gamedevstudent.com/journal/2012/01/c-metadata-part-i-singletons-and-lookup/</link>
		<comments>http://gamedevstudent.com/journal/2012/01/c-metadata-part-i-singletons-and-lookup/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 00:48:49 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=267</guid>
		<description><![CDATA[Class metadata systems allow C++ applications to have a sizable fraction of the runtime reflection and introspection available in other high level languages, such as C#, Python, or Java. While C++ does not offer any true metadata system itself (excluding the nearly useless typeinfo/RTTI system, which is barely enough to handle dynamic_cast&#8216;s needs), it&#8217;s certainly [...]]]></description>
			<content:encoded><![CDATA[<p>Class metadata systems allow C++ applications to have a sizable fraction of the runtime reflection and introspection available in other high level languages, such as C#, Python, or Java.  While C++ does not offer any true metadata system itself (excluding the nearly useless typeinfo/RTTI system, which is barely enough to handle dynamic_cast<>&#8216;s needs), it&#8217;s certainly possible to build a system that gets the job done and maintains sufficient levels of performance and ease of use.  Given what a huge topic this is, I&#8217;m going to break this article up into a series.  Today I&#8217;m going to talk about the singleton patterns most useful for a metadata system, as well as go over some introductory topics and alternatives.<br />
<span id="more-267"></span></p>
<span id="Why_Metadata"><h2>Why Metadata</h2></span>
<p>A question I&#8217;ve been asked several times is, &#8220;why would anyone even want a system like this?&#8221;  Right after that is usually, &#8220;won&#8217;t this be really slow, or waste a lot of memory, or just be more costly than a less flexible solution?&#8221;  I often equate these questions to the old, &#8220;aren&#8217;t virtual functions too slow for games?&#8221; and the somewhat old, &#8220;aren&#8217;t components and data-driven design too inefficient to make up for their added flexibility?&#8221;</p>
<p>The Why of metadata is simply this: metadata allows for runtime data-driven systems in a game engine to work with a minimum of extra maintenance or bugs.  Metadata systems allow for the creation of simple factories with almost no extra code.  They make serialization of game objects trivial.  They allow for property editing in a game editor window or debug toolbar to be very simple.  Metadata allows for enhanced debugging output.  Metadata helps with documenting objects.  Metadata can even be used for script binding.</p>
<p>This all stems from the basic notion of what metadata is in a programming language.  Metadata is simply the addition of information to the base type system.  As a very simple example, take the name of a class.  In C++, at runtime, there is normally no way to ask a question like, &#8220;what is the name of the class instance referenced by my Foo* variable?&#8221;  (Yes, C++&#8217;s RTTI and its typeid and typeinfo provide a name but it&#8217;s not usually the name you&#8217;d expect or want.)  If it weren&#8217;t for dynamic polymorphism, the answer might well just be &#8220;Foo&#8221; but it&#8217;s just not that simple for us.  When we start using templates and metaprogramming, we likewise will not know the real type of an object by simply looking at the declaration for said object in the code.</p>
<p>With a proper metadata system, we have the ability to get an object that represents a class (or even a primitive type).  We can have something similar to <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Type_traits_for_metaprogramming">metaprogramming type traits</a> except with even more information and which is available at runtime.  We&#8217;ll be able to write code like the following:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// very simplistic deserialization of game object components</span><br />
IComponent<span style="color: #000040;">*</span> deserialize_component<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">istream</span><span style="color: #000040;">&amp;</span> input<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008080;">;</span><br />
<br />
&nbsp; input <span style="color: #000080;">&gt;&gt;</span> name<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> meta <span style="color: #000080;">=</span> MetaManager<span style="color: #008080;">::</span><span style="color: #007788;">lookup</span><span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; IComponent<span style="color: #000040;">*</span> component <span style="color: #000080;">=</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>createInstance<span style="color: #008000;">&#40;</span>g_ComponentAllocator<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; input <span style="color: #000080;">&gt;&gt;</span> name<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">const</span> Property<span style="color: #000040;">*</span> prop <span style="color: #000080;">=</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getProperty<span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; prop<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>deserialize<span style="color: #008000;">&#40;</span>component, input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">return</span> component<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>As per the example, by far the biggest use I&#8217;m getting out of metadata in my current project is the data-driven runtime composition of game objects, which allows us to highly customize game entities in very intricate ways with just a simple text file.  We&#8217;re also using it for our configuration system, some great debugging macros, and our editor is starting to make use of it for property editing as well.</p>
<p>Touching briefly on the questions above about performance, it&#8217;s worth noting that the system I&#8217;ll be describing is highly efficient in terms of both CPU time and memory.  It&#8217;s possible to get even more efficiency at the cost of some API cleanliness.  I&#8217;m going to describe the easier to use approach, but I&#8217;ll note the areas where things could be changed to maximize efficiency.  In my projects I lean towards a middle of the road approach, which has an API slightly more cumbersome than what I describe here, but which has a bit more efficiency.</p>
<span id="Metadata_Class"><h2>Metadata Class</h2></span>
<p>The first thing we&#8217;ll need is an actual class for our metadata objects.  (Yes, by the end of this, our metadata system will be able to describe itself.)  Nothing particularly complicated is needed, but we&#8217;re going to keep things extra simple for now.  Our metadata system will have just enough information to print out the type and size of an object.  Further articles in the series will expand its capabilities.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> Metadata<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; Metadata<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name, <span style="color: #0000ff;">size_t</span> size<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Name<span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span>, m_Size<span style="color: #008000;">&#40;</span>size<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Name<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Size<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> m_Name<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">size_t</span> m_Size<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<span id="Metadata_Registry"><h2>Metadata Registry</h2></span>
<p>One of the core features of a metadata system is ability to find the metadata for a class by name without needing an instance of a class first.  This is useful for factory systems, for example.</p>
<p>The idea then is that there should be another global system that manages all Metadata objects.  There are a lot of ways to make this work, ranging from the simple but inefficient to the highly complex but (practically) zero-cost.  I&#8217;m going to go over the simple approach.</p>
<p>At the most basic level, I just need to make a class to manage Metadata objects, and then have Metadata objects register themselves with that class.  That class is going to only use static methods and members (and hence could just be a few C-style functions and globals, if you wish) in order to avoid order-of-initialization problems.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> MetaManager<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #666666;">// add a new Metadata instance to the manager</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> registerMeta<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> meta<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MetaMap<span style="color: #000040;">&amp;</span> metas <span style="color: #000080;">=</span> getMetas<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; metas<span style="color: #008000;">&#91;</span>meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> meta<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666;">// find an instance of a Metadata object by name</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> get<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">const</span> MetaMap<span style="color: #000040;">&amp;</span> metas <span style="color: #000080;">=</span> getMetas<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; MetaMap<span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> meta <span style="color: #000080;">=</span> metas.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> meta <span style="color: #000080;">==</span> metas.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000ff;">NULL</span> <span style="color: #008080;">:</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>second<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <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;">const</span> Metadata<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> MetaMap<span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// safe and easy singleton for our std::map of Metadata objects</span><br />
&nbsp; <span style="color: #0000ff;">static</span> MetaMap<span style="color: #000040;">&amp;</span> getMetas<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">static</span> MetaMap metas<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> metas<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>There&#8217;s not much more that would ever be needed than this.  However, it&#8217;s not all that efficient to use std::string or std::map for this system.  Especially for memory-constrained environments, it can be quite worthwhile to avoid these.  </p>
<p>Avoiding the use of std::string is the easiest.  There&#8217;s no reason to make a copy of the Metadata&#8217;s name rather than just using the C-style character pointer.  It will be necessary to specify a custom comparator for the std::map instance, but that should hopefully be a trivial exercise.</p>
<p>Avoiding the std::map altogether is a bit trickier.  We want to keep efficient lookup of Metadata objects (no worse than O(logN), which means we&#8217;re going to want to keep them in a well-behaved tree.  The approach I&#8217;ve used is to actually write my own tree where the Metadata objects are also the nodes themselves; that is, the Metadata class has the child pointers and such necessary to implement a binary tree (or a red-black tree, or so on).  The consequence is a lot of extra code since std::map can&#8217;t just be reused, but there are absolutely zero runtime memory allocations required for the metadata system, the memory cost of metadata is as good as or better than std::map, and the runtime complexity is the same.</p>
<p>If <a href="http://gamedevstudent.com/journal/2011/05/compile-time-string-hashing-in-c0x/">compile-time string hashing</a> was feasible in Visual C++, it would also be an option to use a fixed-size hash table with our choice of &#8220;perfect&#8221; hash, but sadly that won&#8217;t be an option until at least Visual Studio 11 SP1 or so (whenever Microsoft finally gets us constexpr support like GCC and Clang already have).</p>
<p>If you do implement your own tree, I recommend doing it as a template mixin, as it will be useful later on for attaching properties to metadata, which has the same set of problems (and which is just another std::map member in the Metadata class if you go the simpler route).</p>
<p>To finish off the registration, the constructor for Metadata should call MetaManager::registerMeta(this), to register itself with the manager.</p>
<span id="Name-Based_Singletons"><h2>Name-Based Singletons</h2></span>
<p>Now that we have a Metadata class, the question becomes, &#8220;how do I create an instance of the Metadata object for each class?&#8221;  There&#8217;s a lot of ways to do this, I&#8217;ve used them all and seen most of them used in other engines, and they each have advantages and disadvantages.  Let&#8217;s start with the simpler methods and build up to the one I feel is best.</p>
<p>The first approach is to simply make a global variable for each class.  For example, if I had a class named GameObject, then I might have a global variable named g_MetaGameObject which is an instance of Metadata.  That simple.  A couple macros make it easy to declare, define, and reference these globals.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#define DECLARE_META(metatype) extern Metadata g_Meta ## metatype;</span><br />
<span style="color: #339900;">#define DEFINE_META(metatype) Metadata g_Meta ## metatype(#metatype, sieof(metatype));</span><br />
<span style="color: #339900;">#define META_TYPE(metatype) (&amp;g_Meta ## metatype)</span><br />
<span style="color: #339900;">#define META(object) ((object)-&gt;getMetadata())</span></div></div>
<p>Pretty darned simple.  Except for that last macro.  That one is a problem, and is one of the bigger reasons why I don&#8217;t recommend this approach.  It turns out that to actually get the metadata for a particular object, that object is going to have to have a method like getMetadata().  Required and mandatory.  There is no need for a common base class, just that every class using metadata has a method named getMetadata which requires a pointer to the Metadata for the class.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> Foo<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> getMetadata<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> META_TYPE<span style="color: #008000;">&#40;</span>Foo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>This can be simplified ever so slightly with the addition of another macro.  While this method does not need to be virtual for classes that won&#8217;t be derived from, it will need to be virtual for classes that have children so that the META macro works as expected when called on a base pointer.  I&#8217;ll keep it simple and just show the version of the macro for dynamic classes.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#define DYNAMIC_META(metatype) public: const Metadata* getMetadata() const { return META_TYPE(metatype); }</span><br />
<br />
<span style="color: #0000ff;">class</span> Foo <span style="color: #008000;">&#123;</span><br />
&nbsp; DYNAMIC_META<span style="color: #008000;">&#40;</span>Foo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>There are three big problems with this approach.  First, there is the requirement for the getMetadata() method.  While the other approaches require that for dynamically polymorphic objects (there&#8217;s only one workaround there, but it has a cost), it should not be necessary to modify or extend a class just to participate in metadata.  In particular, it&#8217;s impossible to add metadata to a class that cannot be modified, such as a third party library, as well as to primitive types.</p>
<p>The second big problem is that this approach does not work inside templates, as the compile-time metadata lookup requires knowledge of the type&#8217;s name (in order to construct the name of the appropriate metadata global) which the template does not have.</p>
<p>The third big problem is that types with namespaces or template parameters cannot have metadata, as there&#8217;s no way to construct a name for these objects.  Passing a class identifier like MyNamespace::MyClass to any of the above macros will produce invalid code.  The same happens if trying to pass a type like MyTemplate<MyClass>.</p>
<span id="Templated-Based_Singletons"><h2>Templated-Based Singletons</h2></span>
<p>The second and final approach I&#8217;ll cover is to use templates to define and find metadata.  The idea here is that a templated type can have static methods and even static members.  By the same rules that all other static members and static local variables operate, a statically allocated object in a template will exist once and once only in the problem (the One Definition Rule guarantees this).</p>
<p>It&#8217;s thus possible to create a new simple templated type and a new set of macros for creating metadata.  This template removes several of the disadvantages of the previous approach.  First, it allows to lookup the metadata for any type based on the compiler&#8217;s knowledge of the type rather than the type&#8217;s name, so it works in templates.  Second, as there is no need to construct a valid global identifier for each class, it trivially supports classes in namespaces or with template parameters, and it can be used without needing to extend a class, and it can even be used with primitive types like int or float.  Finally, a bit of template metaprogramming we&#8217;ll go over lately will make the requirement of a getMetadata() method on objects go away in every case where dynamic polymorphism isn&#8217;t an issue.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MetaSingleton<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> get<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>s_Meta<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">static</span> Metadata s_Meta<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #339900;">#define DEFINE_META(metatype) Metadata MetaSingleton&lt;metatype&gt;::s_Meta(#metatype, sizeof(metatype));</span><br />
<span style="color: #339900;">#define META_TYPE(metatype) (MetaSingleton&lt;metatype&gt;::get())</span><br />
<span style="color: #339900;">#define META(object) (MetaSingleton&lt;decltype(object)&gt;::get())</span></div></div>
<p>That last macro probably requires some explanation.  If we have an object, there is a new C++11 feature called decltype that allows us to get the type of that object.  This may sound redundant (if we have an object, we must have declared its type somewhere, after all, so clearly we already know what it is), but it&#8217;s far more convenient to just pass in the object&#8217;s name to a macro than to duplicate the type, especially for some of those long and ugly templated names.</p>
<p>There is one downside to this implementation of the META macro: it does not work with dynamic polymorphism at all.  That is, if we have a pointer to MyBase but the instance it references is actually MyDerived, the Metadata instance returned by META will be that of MyBase.  That&#8217;s no good.  We&#8217;ll fix this later on in the article.</p>
<p>You may have noticed that I implemented the singleton Metadata in MetaSingleton differently than I did in MetaManager.  There are two reasons for that.  Static local variables as found in the MetaManager::getMetas() method are usually implemented internally by having a hidden global boolean for each static local and checking if that boolean has been toggled to true or not ever time the function is called.  Globals of primitive types like bool are always zero initialized before any code runs, so that hidden boolean is always guaranteed to be false the first time the getMetas() method is called, even if it&#8217;s called from some random constructor in a global object.  In that first call, because the boolean is false the object is initialized and then the boolean is set to true, thereby guaranteeing that the static local object is initialized only once and initialized only when first used.  However, that boolean is checked every time the method is called.  While metadata objects are not generally accessed in any performance-sensitive part of a game, I did want to illustrate that raw bare-metal efficiency is possible even with a complex metadata system for the non-believers.</p>
<p>The second reason to use the static member is that it allows (requires, in fact) the initialization of that member to be explicit and in a .cpp file.  Right now we need a macro during initialization in order to set the class name in the Metadata object.  We&#8217;re also going to want that explicit initialization for a later article when I show how to add properties to objects and do other advanced runtime initialization.</p>
<span id="Robust_Compile-Time_Lookup_via_Partial_Template_Specialization"><h2>Robust Compile-Time Lookup via Partial Template Specialization</h2></span>
<p>We&#8217;ve got a major problem in our template approach right not.  Whenever we access MetaSingleton<>, we&#8217;re passing in some qualified type name.  Because of how templates work, MetaSingleton<MyFoo> and MetaSingleton<const MyFoo> and MetaSingleton<MyFoo*> are all different, and hence will have their own static Metadata objects.  We really don&#8217;t want that.</p>
<p>There&#8217;s two approaches to fixing this.  The first is to make use of special metaprogramming templates like std::remove_const<>, std::remove_reference<>, and so on.  They&#8217;re pretty easy to use, but they&#8217;re difficult to use just right in a case as complex as this one.  If we have some qualified type like &#8220;MyType const* const&#038;&#8221; then we&#8217;re going to have to nest those std::remove* uses pretty deeply to make sure it gets simplified down to the unqualified &#8220;MyType&#8221;.  Thankfully, partial template specialization is much easier to use and solves all our problems here.</p>
<p>Partial template specialization allows us to make alternative versions of MetaSingleton that just reference the unqualified version.  The end result is that no matter what we pass in as the template parameter, only the unqualified version&#8217;s static Metadata instance will ever be referenced.  This isn&#8217;t hard to do, it just requires some mostly redundant typing.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MetaSingleton<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> MetaType<span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000040;">&amp;</span><span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MetaSingleton<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> MetaType<span style="color: #000040;">&amp;</span><span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000040;">&amp;&amp;</span><span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MetaSingleton<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> MetaType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Note that the const versions of reference and pointer specializations are required.</p>
<p>Also, as a quiz for the reader: for completeness&#8217; sake, is a specialization for &#8220;volatile&#8221; necessary?  What about &#8220;static&#8221; and &#8220;register&#8221;?  (Hint: what is the difference in C++ between a type qualifier and a storage class, and which keywords fall into those categories, and why?)</p>
<span id="Supporting_Polymorphic_Classes"><h2>Supporting Polymorphic Classes</h2></span>
<p>Our META macro still does not support dynamic polymorphism.  We need some way to get the Metadata instance for the type of actual object we&#8217;re interested in, independent of whether or not we&#8217;re accessing it by a pointer or reference to a base type.</p>
<p>This unfortunately goes back to needing a virtual getMetadata() method on our objects.  Fortunately, with the template approach, this method is only needed when dynamic polymorphism is even an issue.  Also, the template approach allows the use of a template mixin for creating the virtual method, which may be considered easier or prettier than the macro we used before.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType, <span style="color: #0000ff;">typename</span> BaseType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> MetaMixin <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> BaseType<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> getMetadata<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> MetaSingleton<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Using this is just a simple application of CRTP (the Curiously Recursive Template Pattern).</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> DerivedType <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaMixin<span style="color: #000080;">&lt;</span>MyClass, BaseType<span style="color: #000080;">&gt;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">/// ...</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>It is arguable if this is prettier or not.  I&#8217;m not particularly fond of needing to make the &#8220;real&#8221; base type be a template parameter to the mixin template, but there&#8217;s no way around it.  Conversely, the macro variant is:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#define DYNAMIC_META(metatype) \<br />
&nbsp; public: virtual const Metadata* getMetadata() const \<br />
&nbsp; { return MetaSingleton&lt;metatype&gt;::get(); }</span><br />
<br />
<span style="color: #0000ff;">class</span> DerivedType <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> BaseType<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; DERIVED_META<span style="color: #008000;">&#40;</span>DerivedType<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">/// ...</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Either way works perfectly fine (and should generate identical class layouts and runtime code), so use whatever you feel looks best or is easier to understand.  If you make use of C++&#8217;s builtin RTTI, the template mixin approach may bloat your RTTI tables a bit, so that may be a deciding factor to push you towards the macro approach.</p>
<p>Simply having this method where it&#8217;s needed is only part of the solution.  We can now safely call this method where it needs to be called, but wouldn&#8217;t it be nice if we could just keep using that META macro, and have it automatically call getMetadata() if it exists or use the decltype trick otherwise?</p>
<span id="Template_Metaprogramming_for_Metadata_Lookup"><h2>Template Metaprogramming for Metadata Lookup</h2></span>
<p>Template metaprogramming offers us the ability to selectively call the getMetadata() method.  It&#8217;s an ugly, horrific, mind-bending trick, but a working one none-the-less.  Another article in the series is going to be making heavy use of metaprogramming to extend the capabilities of the Metadata object without requiring excessive manual intervention, so we&#8217;re going to have to cover this topic no matter what.  Time to rip off the bandaid.  In fact, let&#8217;s just look at the code.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> MetaIsDynamic<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">struct</span> no_return <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> U<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">char</span> check<span style="color: #008000;">&#40;</span>decltype<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>U<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getMetadata<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> U<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">static</span> no_return check<span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">bool</span> value <span style="color: #000080;">=</span> <span style="color: #000040;">!</span>std<span style="color: #008080;">::</span><span style="color: #007788;">is_same</span><span style="color: #000080;">&lt;</span>no_return, decltype<span style="color: #008000;">&#40;</span>check<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">value</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Yeah, that&#8217;s pretty ugly.  It&#8217;s not even particularly obvious.  There&#8217;s an even uglier way to do this, and there&#8217;s a lot of ways that one would think should work but won&#8217;t (with Visual Studio, at least).</p>
<p>I&#8217;m not going to explain metaprogramming&#8217;s core tricks here.  If you&#8217;re unfamiliar with template metaprogramming in general, I recommend the book <a href="http://erdani.com/index.php/books/modern-c-design/">Modern C++ Design</a>.  The topic is big enough that I simply can&#8217;t do it justice here, at least not without giving it an entire large article all to itself.</p>
<p>Let&#8217;s look back to MetaIsDynamic.  We have a boolean result, defined as:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">bool</span> value <span style="color: #000080;">=</span> <span style="color: #000040;">!</span>std<span style="color: #008080;">::</span><span style="color: #007788;">is_same</span><span style="color: #000080;">&lt;</span>no_return, decltype<span style="color: #008000;">&#40;</span>check<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">value</span><span style="color: #008080;">;</span></div></div>
<p>The tricky part is all the mess with std::is_same, no_return, decltype, and check<MetaType>(0).  Like I said above, we&#8217;re using type comparisons internally.  That use of the check<MetaType>(0) will evaluate to a function call which will have one of two different return types; if MetaType does NOT have a getMetadata() method, then the return type of the check<>() function will be the struct no_return defined in MetaIsDynamic, and if MetaType DOES have a getMetadata() method, then the return will be something other than struct no_return.  I&#8217;m checking the return type by using decltype and the std::is_same template.  The whole line can be read as &#8220;boolean &#8216;value&#8217; is false if no_return is the same type as the return value of &#8216;check<MetaType>(0)&#8217;, and true otherwise.&#8221;</p>
<p>The big question then is how the return type of check<>() is chosen.  This is done using the template behavior called Substitution Failure Is Not An Error (SFINAE).  We can thus arrange for there to be two templated versions of a function that are non-ambiguous (because that would be an error, SFINAE or no) and where one will be invalid on some condition we specify (such as whether the templated type has a specific method or not).</p>
<p>To use that trick, the MetaIsDynamic struct has two versions of the check<>() function.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> U<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">char</span> check<span style="color: #008000;">&#40;</span>decltype<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>U<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getMetadata<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> U<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">static</span> no_return check<span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></div>
<p>Let&#8217;s look at the first version of check<>().  It returns a char (our &#8220;true&#8221; value) and takes&#8230; some kind of mess.  The parameter type is definitely a beast, but it&#8217;s not too bad at all once it&#8217;s broken down to its component parts.  The parameter is a pointer, as can be seen by the asterisk at the end.  It&#8217;s a pointer to decltype of an expression, which means that it&#8217;s a pointer to whatever type that expression evaluates to.  That expression is the result of calling getMetadata() on some pointer cast, so the decltype evaluates to whatever the return type of getMetadata() is.  The pointer cast is a cast from 0 (a.k.a. NULL) to the template type U, which is just a cheap way of constructing a &#8220;random&#8221; pointer to call a method on, because we need an expression using a method and that requires an object.  Since decltype only looks at the type of an expression and never actually evaluates the expression, the pointer we use does not need to be a valid object, so casting NULL to our desired type is sufficient.  Thus the whole parameter can be read as &#8220;a pointer to the return type of U::getMetadata()&#8221;.</p>
<p>One might ask &#8220;but what if type U doesn&#8217;t have a getMetadata() method?&#8221;  Well, that&#8217;s the exact question that the compiler has to answer when it evaluates this declaration of check<>().  Because that expression is used to construct the parameter type, and since the parameter type must be a real valid type or else there is a substitution failure, this is where SFINAE kicks in.  If typename U (which is always the same type as MetaType) does not have a getMetadata() method then this declaration of function check<>() is simply thrown away and ignored by the SFINAE rules, with no errors.  Which is exactly what we want.</p>
<p>The second version of check<>() is simply the fallback.  Its parameter is an ellipses because of the overload resolution precedence in C++, allowing it to accept a pointer argument but also ensuring that the first version of check<>() is always unambiguously chosen if it exists.</p>
<p>The one fault of the code as presented is that it does not in any way guarantee that the getMetadata() on the type actually returns pointer to Metadata.  While it&#8217;s possible with a bit more effort to check that as well, I&#8217;d argue that there is little need; just don&#8217;t do anything silly like adding methods called &#8220;getMetadata&#8221; that don&#8217;t get Metadata and there won&#8217;t be a problem.</p>
<p>Actually using this new MetaIsDynamic metaprogramming struct takes just a bit more work.  A new set of template specializations is needed, then it&#8217;s all done.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> MetaLookup<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> U<span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">typename</span> std<span style="color: #008080;">::</span><span style="color: #007788;">enable_if</span><span style="color: #000080;">&lt;</span>MetaIsDynamic<span style="color: #000080;">&lt;</span>U<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">value</span>, <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">type</span> resolve<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> U<span style="color: #000040;">&amp;</span> obj<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> obj.<span style="color: #007788;">getMetadata</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> U<span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">typename</span> std<span style="color: #008080;">::</span><span style="color: #007788;">enable_if</span><span style="color: #000080;">&lt;</span><span style="color: #000040;">!</span>MetaIsDynamic<span style="color: #000080;">&lt;</span>U<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">value</span>, <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">type</span> resolve<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> U<span style="color: #000040;">&amp;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> MetaSingleton<span style="color: #000080;">&lt;</span>U<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> get<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MetaType<span style="color: #000040;">&amp;</span> obj<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> resolve<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>obj<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> MetaLookup<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> get<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MetaType<span style="color: #000040;">*</span> obj<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> MetaLookup<span style="color: #000080;">&lt;</span>MetaLookup<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>obj<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> MetaType<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> MetaLookup<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> MetaType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MetaLookup<span style="color: #000080;">&lt;</span>MetaType<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>There&#8217;s a tad bit more metaprogramming in there via the use of std::enable_if.  That is another neat little trick relying on SFINAE, which allows for a version of a function to exist based on some boolean constant.  A boolean constant like the one produced by our MetaIsDynamic.  One version of the resolve<>() function will invoke the getMetadata() method and the other just uses MetaSingleton directly.</p>
<p>Now we just need a new version of the META macro to make it all nice and easy to use.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#define META(obj) (MetaLookup&lt;decltype(obj)&gt;::get((obj)))</span></div></div>
<p>Note again that decltype does not actually evaluate its arguments, so the repeated use of the &#8216;obj&#8217; macro parameter does not run afoul of the usual double expansion problems of preprocessor macros.</p>
<p>It&#8217;s worth noting that when compiled with optimizations on, all of this metadata lookup code will compile away to either a trivial address lookup of a global object or (for classes that have a virtual getMetadata() method) a single virtual call to a function that simply returns the address of a global object.  Stepping through the code in debug builds can be a little scary, though, as it may seem like there are hobajillion recursive calls (especially when those lookup templates get hit), but don&#8217;t be fooled by how dumb a compiler is with all of its optimizations turned off.</p>
<span id="Usage_Examples"><h2>Usage Examples</h2></span>
<p>Keeping in mind that the Metadata class so far doesn&#8217;t do much, here&#8217;s a few examples of how the system can be used.  There was a lot of innards and implementation details, but at the end of the day it&#8217;s nice to see what all that work can do.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> MyClass <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* ... */</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
DEFINE_META<span style="color: #008000;">&#40;</span>MyClass<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
DEFINE_META<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
DEFINE_META<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
DEFINE_META<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> print_size_of<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> class_name<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> meta <span style="color: #000080;">=</span> MetaManager<span style="color: #008080;">::</span><span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>class_name<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>meta <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Size of &quot;</span> <span style="color: #000080;">&lt;&lt;</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is &quot;</span> <span style="color: #000080;">&lt;&lt;</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Size of &quot;</span> <span style="color: #000080;">&lt;&lt;</span> class_name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is unknown&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">void</span> print_name_of<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> object<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Metadata<span style="color: #000040;">*</span> meta <span style="color: #000080;">=</span> META<span style="color: #008000;">&#40;</span>object<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Object is a &quot;</span> <span style="color: #000080;">&lt;&lt;</span> meta<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; MyClass foo<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">int</span> bar<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">float</span> baz<span style="color: #008080;">;</span><br />
<br />
&nbsp; print_size_of<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;MyClass&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Size of MyClass is 1</span><br />
&nbsp; print_size_of<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;std::string&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Size of std::string is 24</span><br />
&nbsp; print_size_of<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;bool&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Size of bool is unknown</span><br />
&nbsp; print_name_of<span style="color: #008000;">&#40;</span>foo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Object is a MyClass</span><br />
&nbsp; print_name_of<span style="color: #008000;">&#40;</span>bar<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Object is a int</span><br />
&nbsp; print_name_of<span style="color: #008000;">&#40;</span>baz<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Object is a float</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Note you may get different sizes output depending on your compiler and standard library vendor.</p>
<span id="The_Future"><h2>The Future</h2></span>
<p>In following articles in the series, I&#8217;ll go over making the Metadata class more useful.  In particular, we&#8217;ll be adding factory methods including cloning, simple inheritance checks (think dynamic_cast<> that doesn&#8217;t require instances of objects), additional class attributes like alignment or an is_abstract flag, and named/typed class properties with full getter/setter support.  I may even go over method binding for script language binding generation if I have the time (it&#8217;s a big topic).</p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2012/01/c-metadata-part-i-singletons-and-lookup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[C++ Metadata]]></series:name>
	</item>
		<item>
		<title>Custom Containers in C++11</title>
		<link>http://gamedevstudent.com/journal/2012/01/custom-containers-in-c11/</link>
		<comments>http://gamedevstudent.com/journal/2012/01/custom-containers-in-c11/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 03:09:46 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=254</guid>
		<description><![CDATA[C++11 has brought us a few significant language and library improvements which can make for greatly increased API ease of use and performance. Several of these features have a huge impact on how we write custom containers. Since I haven&#8217;t seen any in-depth articles on this topic, and honestly haven&#8217;t seen any truly in-depth articles [...]]]></description>
			<content:encoded><![CDATA[<p>C++11 has brought us a few significant language and library improvements which can make for greatly increased API ease of use and performance.  Several of these features have a huge impact on how we write custom containers.  Since I haven&#8217;t seen any in-depth articles on this topic, and honestly haven&#8217;t seen any truly in-depth articles on writing custom containers even for older versions of C++, I decided it was time to share some of my recent experience working on a custom high-performance, C++11-friendly container library for my current game project.</p>
<p>Be warned: this is a particularly long article and delves into a fair bit of C++ magic.</p>
<p><span id="more-254"></span></p>
<span id="Introduction"><h2>Introduction</h2></span>
<p>By far the most important new feature of C++11 for container writers is rvalue references.  This feature allows containers to efficiently move around objects that might otherwise be very expensive to copy.  It&#8217;s also mandatory for supporting smart pointer classes like std::unique_ptr<>, and containers need to be made aware of rvalue references to be able to hold copies of those smart pointer types.</p>
<p>While on the topic of avoiding copies, there&#8217;s a few template tricks that very few people seem to be directly aware of that are important for making efficient containers.  The primary trick I&#8217;m going to cover is making sure that objects passed in to methods like find() do not require the creation of unnecessary temporaries or copies.</p>
<span id="Simple_Container"><h2>Simple Container</h2></span>
<p>Let&#8217;s go ahead and start things out with a container skeleton that we have all probably written at some point.  I&#8217;m going to ignore the actual data structure of the container, as that&#8217;s honestly not too important for this discussion; it could be a vector, a hash table, a tree, or whatever is needed.  For now, I&#8217;ll use set operations for the examples.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MyContainer<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #666666;">// constructors</span><br />
&nbsp; MyContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; MyContainer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MyContainer<span style="color: #000080;">&lt;</span>Type<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; ~MyContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// assignment operator</span><br />
&nbsp; MyContainer<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MyContainer<span style="color: #000080;">&lt;</span>Type<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// basic set operations</span><br />
&nbsp; <span style="color: #0000ff;">void</span> insert<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> element<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">void</span> <span style="color: #0000dd;">remove</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> element<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">bool</span> has<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> element<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Pretty basic, and gets the job done.</p>
<p>Even in the C++98 era, however, there are a couple problems with this implementation.  I&#8217;m going to cover those now, since the above is how almost all examples on custom containers start and end.  The problem is that the operations like remove() and has() may create unnecessary temporaries for their parameters if a conversion is required.</p>
<p>Take the following code example:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> do_stuff<span style="color: #008000;">&#40;</span>MyContainer<span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> string_set<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>string_set.<span style="color: #007788;">has</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;foobar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666;">// ...</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>What&#8217;s the problem?  The problem is that the has() method I created on MyContainer takes a const reference to a std::string, but in the code snippet I passed a string literal (a const char[]) to the has() method.  This means that the C++ compiler must construct a temporary std::string, which entails a memory allocation, copy, and eventual deallocation, all just to pass that string in to compare with the std::string contents.  Since std::string can be compared with a regular string literal just fine, this copy is unnecessary.</p>
<span id="Avoiding_Conversions_with_Templated_Methods"><h2>Avoiding Conversions with Templated Methods</h2></span>
<p>The solution is fairly simple, if a little non-obvious.  I&#8217;m going to declare the has() method as a templated method.  That is, the method has its own template parameter, in addition to the template parameters of the container class itself.  While I&#8217;m at it, I&#8217;m also going to do this for remove() since it has a similar problem.  I&#8217;ll handle insert()&#8217;s performance issue later when I discuss rvalue references.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MyContainer<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #666666;">// constructors</span><br />
&nbsp; MyContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; MyContainer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MyContainer<span style="color: #000080;">&lt;</span>type<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; ~MyContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// assignment operator</span><br />
&nbsp; MyContainer<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MyContainer<span style="color: #000080;">&lt;</span>Type<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// basic set operations</span><br />
&nbsp; <span style="color: #0000ff;">void</span> insert<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> element<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ParamType<span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">void</span> <span style="color: #0000dd;">remove</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> ParamType<span style="color: #000040;">&amp;</span> element<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ParamType<span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">bool</span> has<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> ParamType<span style="color: #000040;">&amp;</span> element<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>That&#8217;s all it takes!  Now the object given as a parameter to has() or remove() will be used without any additional conversion operators.  Assuming that the parameter can actually be compared with the type stored in the container, this will work perfectly.</p>
<span id="Avoiding_Conversions_with_Good_API_Design"><h2>Avoiding Conversions with Good API Design</h2></span>
<p>Note that there is still room for problems.  In particular, if the type passed in and the type stored in the container required an expensive conversion for the comparison operators, this version will actually be worse, not better, because the conversion will be performed once for each comparison!  This is exactly the behavior found with std::find(), so it&#8217;s something to be aware of in general anyway.</p>
<p>There are two easy rules to follow to avoid this problem.  First, only define the cheapest possible comparison operators.  Comparisons between two std::string&#8217;s or two int&#8217;s have simple, obvious, maximally efficient implementations.  A comparison operator between std::string and int, however, does not.  It either requires converting the string to an integer or the integer to a string, and then calling one of the original same-type comparison operators.  Comparison operators between two very similar types with no conversion overhead makes sense (like the built-in comparison operator support for int vs short), but avoid anything else.</p>
<p>The second rule is to avoid conversion constructors and conversion operators as much as possible; and when not possible, using the explicit keyword when declaring the conversions.  There is absolutely no implicit conversion operator or constructor for turning a float into a std::string, for example, nor for turning a std::vector into a std::list.  If it is ever necessary for such a conversion, the programmer can do so manually using library calls, but the language will never do sneaky and expensive things behind the programmer&#8217;s back.</p>
<p>As a side note, the STL and C++ in general does break the rule above in a few places.  For instance, C-style strings are implicitly convertible to std::string, which is not particularly nice.  In fact, the original problematic example I listed above would have been a lot more obvious if C++ did not allow for this automatic conversion.  In games, we frequently write our own string libraries as well as container libraries, so I heartily recommend that any and all custom string classes where explicit conversion from string literals and C-style strings into C++ string classes if any memory allocation or copies are required.</p>
<p>All that said, it may seem that I&#8217;m being a contradictory, and the fact that STL implementations don&#8217;t use the performance advice above may seem a bit damning.  After all, if the library has a minimal set of cheap comparison operators and conversions, the example above would never have even compiled, much less implicitly inserted inefficient code.  The problem comes up from those few instances where one does have comparison operators between different types.</p>
<p>For instance, my string library has several types of string classes, with different use cases and performance characteristics.  One type of string is for string pooling, and creating an instance of that type requires inserting the string into the string pool.  Another type is intended for large or one-off strings that need to stick around for a while, and is basically just a reimplementation of std::string (without all the silliness found in most STL implementations).  Yet another type is the string range, which is an efficient and safe way to pass around temporary string data without unnecessary, including slices of other strings.  It is equally cheap and efficient to compare all these different string types, just like it&#8217;s equally cheap and efficient to compare as std::string to a char* as it is to another std::string.  Therefor it&#8217;s quite reasonable even under these rules to have a container of one string type and using methods like has() or find() or remove() with a different string type as a parameter, where a conversion would be required without the above trick.</p>
<p>Furthermore, moving into rvalue reference territory, and the concept of &#8220;perfect forwarding&#8221; (which is one of the many great features that rvalue references unlock), it&#8217;s important for my container that I pass in parameters to the underlying comparison operators with no imposed conversions or other shenanigans.</p>
<span id="Explicit_Constructors"><h2>Explicit Constructors</h2></span>
<p>There&#8217;s one example I&#8217;d like to go over, as it seems to be poorly understood.  Here are three difference ways of creating an initializing an object:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MyType foo1<span style="color: #008000;">&#40;</span>initial_value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
MyType foo2 <span style="color: #000080;">=</span> initial_value<span style="color: #008080;">;</span><br />
<br />
MyType foo3<span style="color: #008080;">;</span><br />
foo3 <span style="color: #000080;">=</span> initial_value<span style="color: #008080;">;</span></div></div>
<p>Novice C++ programmers may not be aware that there&#8217;s any difference at all between the three, but even experienced C++ programmers may be unaware of some of the differences.  Particularly, the first two have some non-obvious behavior.  The example with foo1 will allocated storage on the stack and then invoke the the appropriate constructor.  The example involving foo3 will allocate storage on the stack, invoke the default constructor, and then invoke the appropriate assignment operator.  The example with foo2 is the most tricky.</p>
<p>In the case of foo2, there is no invocation of the assignment operator.  When a variable is declared and assignment to in the same statement, the copy/conversion constructor is invoked rather than the default constructor.  However, unlike the example with foo1, the explicit constructors will not be invoked!  This is particularly important when writing containers that respect the programmer&#8217;s explicit constructors.  If the container needs to move around its contents, such as when a std::vector is resized, then it should allocate the necessary space and then the constructor should be explicitly invoked.  If the container is simply copying data passed in with a template like I showed above, it should avoid explicitly calling the copy constructor.</p>
<span id="Introducing_Rvalue_References"><h2>Introducing Rvalue References</h2></span>
<p>Moving on, I now have a container that requires no unnecessary copies for searching or removing items, but there&#8217;s still a lot of wastage when I call insert().  There are two cases where I will make potentially expensive conversions that I don&#8217;t want to.  The first case is identical to the conversion problem with has() or remove().  An example:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MyContainer<span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span> strings<span style="color: #008080;">;</span><br />
<br />
strings.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;foobar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></div>
<p>Once again, a temporary std::string is created for the parameter.  This temporary object is not the object that is actually stored in the container.  A second copy will be made and inserted into the actual container.  This means that I&#8217;ll get two copies for a single insert when ideally I should only need one.</p>
<p>This might not seem too bad with some &#8220;expensive&#8221; types like std::string, especially when GCC&#8217;s libstdc++ using reference counting and VC++&#8217;s DinkumWare-based STL uses small string optimization.  These don&#8217;t apply to other types, like containers (it&#8217;s not uncommon to have a container of containers, after all), and honestly both of those string optimizations are really pessimizations in many uses (and reference counted std::string implementations are no longer allowed by C++11 in any case).</p>
<p>The second case of unnecessary copies during insert is when an already constructed temporary object is inserted.  This is of course the golden example of rvalue reference.  Most sites covering rvalue references list only that one use case, in fact.  An example:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> some_api_call<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> my_function<span style="color: #008000;">&#40;</span>MyContainer<span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> strings<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;strings.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>some_api_call<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>The temporary object returned by some_api_call() is called an rvalue, as compared to lvalue.  For those wondering, an lvalue is an expression that can be assigned to, while an rvalue is any expression that cannot be assigned to.  In other words, the expression &#8220;some_api_call()&#8221; can be on the right-hand side of an assignment, but not the left-hand side.  Notably, rvalues only exist for a single expression: if I call some_api_call() twice, even if inside the same expression, I&#8217;m going to get two separate std::string temporary objects returned from each call.  The problem with the above code is that I&#8217;m going to copy this temporary object into my container, and then throw away the temporary, which is wasteful.  Since the compiler knows that nobody else can use that std::string (its an rvalue, and cannot be used or accessed in any way after the call to insert() because it has no name), the compiler should be able to just move that temporary object into the container with no unnecessary copying.</p>
<p>Rvalue references facilitate this by providing new syntax, allowing methods to be written that &#8220;know&#8221; whether their parameter is an rvalue or an lvalue, and which will only bind to non-const rvalues.  The naive approach then for my container is to make two versions of insert; one for rvalue references that performs a move and another that makes copies.  There&#8217;s a small trick to that, so an example is in order:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MyContainer<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666;">// ... rest of class</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666;">// copy version of insert</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ParamType<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">void</span> insert<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> ParamType<span style="color: #000040;">&amp;</span> value<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;m_SomeDataStructure<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addNode<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> Node<span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666;">// move version of insert</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ParamType<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">void</span> insert<span style="color: #008000;">&#40;</span>ParamType<span style="color: #000040;">&amp;&amp;</span> value<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;m_DataStructure<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addNode<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> Node<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>The copy version looks normal enough.  The rvalue reference version has two significant changes.  The first is the change to the parameter type.  Rvalue references are denoted by using two ampersands &#038;&#038; instead of a single ampersand.  Also, it makes no sense to declare an rvalue reference as const (the whole point of an rvalue reference is to be able to destructively move the contents out of the object), and only normal references can be declared as const.</p>
<p>The second important note in the rvalue reference version of insert() is the use of std::move().  The std::move() function converts any non-const lvalue into an rvalue.  At first that may seem redundant, since value was declared as an rvalue reference in the function prototype.  However, it&#8217;s important to understand the precise meaning of the syntax.</p>
<p>When a function parameter is declared as an rvalue reference, that means that parameter will accept an rvalue.  However, the parameter inside the function itself clearly has a name, and hence it is an lvalue!  The parameter type declaration simply said what the function will accept, not how the function will actually use the value once it has it.</p>
<p>Note that if the function took a non-const reference, it would not be legal to pass an rvalue to the function.  That is, this code does not compile:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> my_func<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> string<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
my_func<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;foo&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></div>
<p>The reason is that a regular non-const reference can only bind to an lvalue, and the temporary std::string object that must be created to convert the string literal is an rvalue (as are all temporaries).  If my_func has been declared as taking an rvalue reference, or if there was an overloaded version taking an rvalue reference, the code would be valid.</p>
<p>Of course, in the presence of overloads, rvalues will bind to the overload taking an rvalue reference while, non-const lvalues will bind to the overload taking a non-const reference, and const rvalues and any lvalue will bind to the overload taking a const reference.</p>
<p>Examples of what works and what doesn&#8217;t below.  Note that the variable one is an lvalue (it can be assigned to), the variable two is considered a constant (technically an rvalue, but it will not bind to rvalue reference), and that the expression one+two creates a new temporary integer which is an rvalue and not constant (conceptually the value 3 is absolutely constant, but remember that the result of 1 + 2 in C++ is a temporary object that simply contains the value 3).</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> take_rvalue_ref<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;&amp;</span> value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">void</span> take_lvalue_ref<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span> value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">void</span> take_const_ref<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span> value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">int</span> one <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> two <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span><br />
<br />
take_rvalue_ref<span style="color: #008000;">&#40;</span>one<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// illegal</span><br />
take_rvalue_ref<span style="color: #008000;">&#40;</span>two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// illegal</span><br />
take_rvalue_ref<span style="color: #008000;">&#40;</span>one <span style="color: #000040;">+</span> two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// legal</span><br />
<br />
take_lvalue_ref<span style="color: #008000;">&#40;</span>one<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// legal</span><br />
take_lvalue_ref<span style="color: #008000;">&#40;</span>two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// illegal</span><br />
take_lvalue_ref<span style="color: #008000;">&#40;</span>one <span style="color: #000040;">+</span> two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// illegal</span><br />
<br />
take_const_ref<span style="color: #008000;">&#40;</span>one<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// legal</span><br />
take_const_ref<span style="color: #008000;">&#40;</span>two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// legal</span><br />
take_const_ref<span style="color: #008000;">&#40;</span>one <span style="color: #000040;">+</span> two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// legal</span></div></div>
<p>One more example, showing overloads:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> take_ref<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;&amp;</span> str<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> str <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is an rvalue reference&quot;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #0000ff;">void</span> take_ref<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> str<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> str <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is an lvalue reference&quot;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #0000ff;">void</span> take_ref<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> str<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> str <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is a const reference&quot;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> foo <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;foo&quot;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> bar <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;bar&quot;</span><br />
<br />
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> baz<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;baz()&quot;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> qux<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;qux()&quot;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
take_ref<span style="color: #008000;">&#40;</span>foo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// prints &quot;foo is an lvalue reference&quot;</span><br />
take_ref<span style="color: #008000;">&#40;</span>bar<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// prints &quot;bar is a const reference&quot;;</span><br />
take_ref<span style="color: #008000;">&#40;</span>baz<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: #666666;">// prints &quot;baz() is an rvalue reference&quot;;</span><br />
take_ref<span style="color: #008000;">&#40;</span>qux<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: #666666;">// prints &quot;qux() is a const reference&quot;;</span></div></div>
<p>Hopefully that explains all the reference semantics clearly enough.  It&#8217;s pretty confusing at first, but clears up a bit with examples.  If the above aren&#8217;t enough, just try writing some more on your own, and it should start making sense soon enough.  I recommend getting a grasp on this now before continuing, as the next part can be particularly tricky and requires a good understanding of the different types of references.</p>
<span id="Rvalue_References_in_Templates"><h2>Rvalue References in Templates</h2></span>
<p>Up above when I added rvalue references to the MyContainer class, I said that was a naive approach.  The approach I took was to write two different versions of insert(), once that makes copies and that that does not.  The problem is that required a lot of duplicate code (or would, in a real implementation at least), and the only part that is actually different inside the implementation is the use of std::move() in the moving version of the method.  All that duplication leads to maintenance overhead and large binary code size, neither of which is desirable.</p>
<p>Additionally, rvalue references have another important use, which is that of perfect forwarding.  The idea of perfect forwarding is that sometimes programmers write functions or methods that are just wrap other functions, and take some or all of their parameters and pass them on to the wrapper functions.  In fact, with my container, I have exactly this situation: the value I insert is actually passed on to the constructor for the instance of the object created inside the container.  Now consider a wrapped function (or constructor) that takes multiple parameters, such as node type stored inside a key-value container like a map.  What happens if this type&#8217;s constructor accepts an rvalue reference for its first parameter but not its second, or for its second parameter but not its first?  Or both?  Or neither?  I&#8217;d have to write four overloaded versions of insert() for each possible combination.  For other kinds of functions that may take even more parameters, the number of overloads grows exponentially.  This is not good.</p>
<p>Thankfully the C++ committee took care of this problem, with a little bit of special magic for rvalue references.  In fact, the example above is already using part of that trick, but not all of it.</p>
<p>The trick is&#8230; tricky.  It is this: if a templated parameter is an rvalue reference, the actual type of the parameter is an rvalue reference if the template type is an rvalue, and a regular reference otherwise.  That needs an example.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">void</span> my_func<span style="color: #008000;">&#40;</span>Type<span style="color: #000040;">&amp;&amp;</span> param<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">int</span> one <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">const</span> two <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span><br />
<br />
my_func<span style="color: #008000;">&#40;</span>one<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// type of param will be: int&amp;</span><br />
my_func<span style="color: #008000;">&#40;</span>two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// type param will be: const int&amp;</span><br />
my_func<span style="color: #008000;">&#40;</span>one <span style="color: #000040;">+</span> two<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// type of param will be: int&amp;&amp;</span></div></div>
<p>It&#8217;s magic.  Normally, I hate magic.  It makes code hard to read and maintain.  Programmer luminaries such as Linus Torvalds famously despise C++ for the amount of magic it facilities, and its not hard to see their reasoning with features such as these.  The feature does facilitate something useful, however, and mostly just works without much special effort.</p>
<p>It&#8217;s important to remember that this works only when the type is a template parameter.  There&#8217;s some relatively straight forward rules explaining how it all works, but it&#8217;s definitely easier to understand with examples like the one above than it is to try to grok an explanation of the rules.</p>
<p>Alright, so now I can get rid of the copy version of my insert().  Well, almost.  There&#8217;s the little problem of the std::move() call.  If I try to pass a const reference to std::move(), the compiler will complain.  With good reason, too.  If the contained type has a move constructor, I only want that called when I&#8217;m actually passing an rvalue to the insert() method.  What is needed then is a way to select which overloaded constructor to invoke based on the type passed into the insert(), and whether it was an rvalue reference or not.</p>
<p>That facility is the std::forward<>() function.  It works very similarly to std::move().  However, its return type is an rvalue reference if and only if the input parameter is an rvalue or is an lvalue declared as an rvalue reference.  Additionally, due to implementation details of how the template magic works, std::foward<>() requires that the type of the value be specified explicitly, unlike std::move() which can infer the template parameter type.</p>
<span id="Correct_Use_of_Rvalue_References_in_Containers"><h2>Correct Use of Rvalue References in Containers</h2></span>
<p>All that explanation and exposition and now we&#8217;re finally ready for the big reveal: the version of insert() I can use in my container:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> MyContainer<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// ...</span><br />
<br />
&nbsp; <span style="color: #666666;">// one and only perfect insert function</span><br />
&nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> InsertType<span style="color: #000080;">&gt;</span><br />
&nbsp; <span style="color: #0000ff;">void</span> insert<span style="color: #008000;">&#40;</span>InsertType<span style="color: #000040;">&amp;&amp;</span> value<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;m_SomeDataStructure<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addNode<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> Node<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">forward</span><span style="color: #000080;">&lt;</span>InsertType<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>There it is.  Only one insert() method is needed.  This version will bind to rvalue references as well as anything else, and will forward that type perfectly to the Node constructor.  If the value passed to insert() is an rvalue, and Node has an rvalue reference constructor, then the value will be moved into the new Node with no additional copies.</p>
<p>To summarize what happens, I&#8217;ve got another example:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MyContainer<span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span> strings<span style="color: #008080;">;</span><br />
<br />
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> foo <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;foo&quot;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">strinf</span> bar <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;bar&quot;</span><span style="color: #008080;">;</span><br />
<br />
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> baz<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;baz&quot;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
strings.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>foo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// copied</span><br />
strings.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>bar<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// copied</span><br />
strings.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>baz<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: #666666;">// moved; no copies</span><br />
strings.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;qux&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// copied</span></div></div>
<p>The second two do possibly require some qualification.  Technically, baz() makes a copy of a string literal and returns that, so one copy is made.  This copy is made by baz() itself and not by the container or its insert() method.  It&#8217;s a bit of a poor example only because it&#8217;s such a trivial function.  Likewise, the fourth insert of the string literal &#8220;qux&#8221; does make a copy when the literal is constructed, but it&#8217;s constructed in-place in the container, and does not require two copies like the original version of the container class did.</p>
<p>A more reasonable example of where the performance benefit comes in is as follows:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> generate_data<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> param<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> result<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3000000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #ff0000; font-style: italic;">/* push a couple million calculation into result based on param */</span><br />
<br />
&nbsp; <span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
MyContainer<span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;&gt;</span> results<span style="color: #008080;">;</span><br />
<br />
results.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>generate_data<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">123</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
results.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>generate_data<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">456</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
results.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>generate_data<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">789</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></div>
<p>In C++89, or with a naive container, that code would allocate and copy each of the std::vector&#8217;s contents several times.  With my container and the power of C++11 and rvalue reference, the only allocations made are when the std::vector&#8217;s are initialize constructed, and the data is never copied.</p>
<span id="Using_Move_Semantics_Inside_Containers"><h2>Using Move Semantics Inside Containers</h2></span>
<p>I&#8217;ve got this great container class that allows me to move expensive objects into it without requiring extra copies.  Except, I haven&#8217;t actually implemented any real container, and it turns out there&#8217;s some things to know about how to do that.</p>
<p>Above I&#8217;ve been using a sort of set container skeleton as an example.  From now on, I&#8217;m going to use a std::vector type of container as the example, because it&#8217;s the easier to understand for what I&#8217;m going to do.  In particular, any container that has to move its elements around internally needs to do a few things specially.  This might also include various implementations of hash tables that need to grow, as just one other example besides a vector.</p>
<p>I&#8217;m going to look specifically at a simple version of the reserve() method on std::vector, which is the function that allocates more memory.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">void</span> MyVector<span style="color: #000080;">&lt;</span>Type<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">reserve</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> new_capacity<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>new_capacity <span style="color: #000080;">&gt;</span> m_Capacity<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Type<span style="color: #000040;">*</span> new_array <span style="color: #000080;">=</span> operator <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>Type<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> new_capacity<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> m_Size<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>new_chunk<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> Type<span style="color: #008000;">&#40;</span>m_Array<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; m_Array<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>~Type<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000dd;">delete</span> m_Array<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; m_Array <span style="color: #000080;">=</span> new_array<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; m_Capacity <span style="color: #000080;">=</span> capacity<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Pretty basic.  In quick review of std::vector&#8217;s behavior: reserve() will allocate another memory chunk of the requested size, copy the old data to the new memory area, delete the old memory chunk, and update our internal data.  Note that capacity is the number of items the vector can hold without needing to allocate more memory, while size is the number of elements currently in the vector.  Size is strictly less than or equal to capacity, and its completely legal for size to be zero while capacity is a large number.</p>
<p>Of course, that implementation will make full copies of its elements when it grows.  That means making copies of expensive objects like those 3,000,000 element vectors in the previous example.  It also means that objects of type std::unique_ptr<>, which do not have a copy constructor, cannot be used with that implementation.</p>
<p>The fix is ridiculously simple, and should hopefully be clear at this point.  Instead of calling the copy constructor normally inside the loop, the move constructor (if available) should be invoked, letting it optimize things by moving resources instead of copying them.  Move constructors are constructors are implemented using rvalue references, which are conceptually temporary objects that won&#8217;t be used anymore.  The std::move() function forces an lvalue to be an rvalue reference.  So the fix is to just update the actual copy construction line to use std::move(), resulting in the following loop:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> m_Size<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>new_chunk<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> Type<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">&#40;</span>m_Array<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; m_Array<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>~Type<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></div>
<p>Notice that I did not remove the call to the destructor.  This is important.  Whether or not an object is passed to a function as an rvalue reference, the destructor is always called for every object when it goes out of scope.  The implementation of move constructors and move asssignment operators in most classes will rely on this fact, as I&#8217;ll go over soon.  Short version: the destructor must always be called, always, period.  If it&#8217;s not clear why just yet, read on.</p>
<span id="Writing_Move-aware_Classes"><h2>Writing Move-aware Classes</h2></span>
<p>I&#8217;ve got containers that can take advantage of move semantics with rvalue references, but what I haven&#8217;t done is explain how to actually write types that have move constructors that actually work.  I&#8217;m coming up on the end of this long-winded article, so bear with me just a bit longer as I explain this final piece of the puzzle.</p>
<p>The idea of a move constructor or move assignment operator is that some class &#8220;own&#8221; other resources which are expensive to duplicate, and which shouldn&#8217;t be copied unnecessarily.  Additionally, some classes have resources which cannot be duplicated at all, and hence it is not legal to copy the object; these types lack copy constructors and assignment operators, but should have move constructors and move assignment operators.  A simple example of the former type is std::vector, which owns a pointer to a chunk of memory containing its elements.  A good example of the latter type is std::unique_ptr<>, which guarantees that there is just one single handle to an object.</p>
<p>A move constructor looks a lot like a copy constructor, except it takes an rvalue reference.  Likewise, a move assignment operator looks a lot like a regular assignment operator, but takes an rvalue reference.</p>
<p>I&#8217;m going to use a simplistic example class to illustrate things.  Behold the integer box:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> IntBox<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; IntBox<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> value <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> <span style="color: #0000ff;">int</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; IntBox<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> IntBox<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span> <span style="color: #000080;">=</span> source<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; ~IntBox<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">delete</span> m_Ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; IntBox<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> IntBox<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>source<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> m_Ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; m_Ptr <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">int</span><span style="color: #008000;">&#40;</span>source.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666;">// pre-condition: m_Ptr is never NULL, so this is always safe</span><br />
&nbsp; <span style="color: #0000ff;">int</span> get<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span>m_Ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> m_Ptr<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>I&#8217;ll clarify what&#8217;s going on with the copy constructor for anyone not familiar with the trick used.  The idea is that we can just default construct the object and then use the assignment operator and have the same result with less effort than if we duplicated the logic in both the copy constructor and assignment operator.  Once again, such a trivial piece of code makes for a slightly poor example, but it gets the point across.  Also note that while the class has a precondition that m_Ptr never be NULL, and the copy constructor initializes m_Ptr to NULL, it will always be given a real address by the time the constructor ends.</p>
<p>I recommend using that trick where possible.  I&#8217;ve seen a few people express dismay over the &#8220;performance problems&#8221; with the code, so I&#8217;d like to take a moment to dispel those worries.  Thing is, the compiler is very smart.  It going to see all the redundancies that I see, and it&#8217;s going to remove them.  But only when optimizations are turned on, and only when there&#8217;s actually redundancies.  Programmers who try to &#8220;test&#8221; the optimization by setting break points in a debug build (with no optimizations turned on, and hence every empty useless do-nothing redundant line of code is executed as written) are getting misled.  Likewise, trying to add output logging by definition makes the functions non-trivial and of course the logging calls are not going to be optimized out.  Obvious enough, I&#8217;d hope.</p>
<p>Getting on to the interesting bit, I&#8217;m going to get rid of the inefficiency that IntBox has when it&#8217;s used inside a std::vector.  I certainly don&#8217;t want the code to allocate a new chunk of memory and copy the value into it just to delete the old chunk of memory afterward.  That doesn&#8217;t accomplish much besides burning cycles.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> IntBox<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; IntBox<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> value <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> <span style="color: #0000ff;">int</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; IntBox<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> IntBox<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span> <span style="color: #000080;">=</span> source<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; IntBox<span style="color: #008000;">&#40;</span>IntBox<span style="color: #000040;">&amp;&amp;</span> source<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span> <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">&#40;</span>source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; ~IntBox<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">delete</span> m_Ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; IntBox<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> IntBox<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>source<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> m_Ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; m_Ptr <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">int</span><span style="color: #008000;">&#40;</span>source.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; IntBox<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span>IntBox<span style="color: #000040;">&amp;&amp;</span> source<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">swap</span><span style="color: #008000;">&#40;</span>m_Ptr, source.<span style="color: #007788;">m_Ptr</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666;">// pre-condition: m_Ptr is never NULL, so this is always safe</span><br />
&nbsp; <span style="color: #0000ff;">int</span> get<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span>m_Ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; <span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> m_Ptr<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Not a whole lot to it.  The move assignment operator is actually quite a bit simpler than the regular version, and the move constructor is almost identical to the copy constructor.  Again, the use of std::move() in the move constructor is because the source parameter is an lvalue (because it has a name and can be assigned to) despite being declared as an rvalue reference.</p>
<p>The move assignment operator is a bit more interesting.  I&#8217;m not deleting the old memory.  I&#8217;m not checking to make sure the source isn&#8217;t the same as the this pointer.  I&#8217;m just swapping the pointers around.  Surely there&#8217;s a bug or two in such a naive implementation.</p>
<p>First, another example, just so we&#8217;re on the same page:</p>
<p>Actually, it&#8217;s quite rock solid.  First, I&#8217;ll quickly address the lack of the identity check.  The check is almost rendered unnecessary by the semantics of rvalues in the first place.  If I&#8217;m moving from source, it must be an rvalue, and hence is an unnamed temporary object, and therefor it can&#8217;t possible exist on both sides of an assignment operator.  Of course, the existence of std::move() allows me to violate those semantics (but only by writing horrible, incorrect code).  That doesn&#8217;t matter, however, since std::swap() is safe to call with both parameters being the same object; it&#8217;s just a no-op in that case.  And of course, std::swap() is using rvalue references and move semantics internally, so even it&#8217;s efficient even for types like std::vector in C++11.</p>
<p>The important bit is why std::swap() works at all for the move constructor.  It&#8217;s pretty clear that it&#8217;ll take the pointer owned by the source object and move it into the destination object, which is what I want.  The question then is what happens to the memory previously owned by the destination object.  For the move constructor, this was just a NULL pointer anyway, but if the move assignment is called on a constructed object, m_Ptr will already point to allocated memory.</p>
<p>The answer goes back to what I mentioned above about how the destructor for an object must always be called, always.  Since the destructor of the source object is always called, and the destructor for IntBox deallocates the memory referenced by m_Ptr, then the old memory in the destination IntBox will be given the source IntBox, and will be freed when the source IntBox is destructed.</p>
<p>Likewise, for the move constructor, the source IntBox&#8217;s m_Ptr will be set to NULL, and delete operator will safely do nothing.  However, one might note that in that case, the source&#8217;s m_Ptr now violates the class&#8217;s precondition that m_Ptr never be NULL, and now calling get() on the source IntBox.</p>
<p>One final example, just showing what happens when each of the</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">IntBox foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">return</span> IntBox<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">42</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// conversion constructor</span><br />
&nbsp; IntBox one<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// copy constructor</span><br />
&nbsp; IntBox two<span style="color: #008000;">&#40;</span>one<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// move constructor;</span><br />
&nbsp; <span style="color: #666666;">// we promise not to use two again, but the compiler won't stop us</span><br />
&nbsp; IntBox too<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">&#40;</span>two<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// conversion constructor, then move assignment operator;</span><br />
&nbsp; <span style="color: #666666;">// memory from too's old valid is freed because the temporary returned by foo() is destructed</span><br />
&nbsp; too <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// move constructor</span><br />
&nbsp; IntBox three<span style="color: #008000;">&#40;</span>foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// move assignment operator;</span><br />
&nbsp; <span style="color: #666666;">// memory from one's old valid is because when the temporary returned by foo() is destructed</span><br />
&nbsp; one <span style="color: #000080;">=</span> foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// move assignment operator;</span><br />
&nbsp; <span style="color: #666666;">// we promise not to use three again, but the compiler won't stop us;</span><br />
&nbsp; <span style="color: #666666;">// memory from one's old valid is freed when three goes out of scope</span><br />
&nbsp; one <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">&#40;</span>three<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// copy constructor</span><br />
&nbsp; IntBox four<span style="color: #008000;">&#40;</span>one<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// assignment operator</span><br />
&nbsp; IntBox five <span style="color: #000080;">=</span> one<span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// prints 1211</span><br />
&nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> one <span style="color: #000080;">&lt;&lt;</span> too <span style="color: #000080;">&lt;&lt;</span> four <span style="color: #000080;">&lt;&lt;</span> five <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// this will probably crash;</span><br />
&nbsp; <span style="color: #666666;">// we did promise not to do this after using std::move on both of these</span><br />
&nbsp; <span style="color: #ff0000; font-style: italic;">/*std::cout &lt;&lt; two &lt;&lt; three &lt;&lt; std::endl;*/</span><br />
<br />
&nbsp; <span style="color: #666666;">// everything goes out of scope;</span><br />
&nbsp; <span style="color: #666666;">// memory from one, too, four, and five is freed;</span><br />
&nbsp; <span style="color: #666666;">// also, the memory in three, which is one's old memory, is freed;</span><br />
&nbsp; <span style="color: #666666;">// destructor for two is also called, which has a NULL pointer</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<span id="Default_Constructors_and_Methods"><h2>Default Constructors and Methods</h2></span>
<p>One very final note.  All classes by default will have a default copy constructor and assignment operator defined.  In the first version of IntBox, if I had omitted my overridden versions of the constructor and assignment operator, the m_Ptr member would be copied by value and the class would likely not work correctly.  However, in the second version that has the move constructor and move assignment operator, the default copy constructor and assignment operator would be suppressed.</p>
<p>The following class example, for instance, does not allow for copying, but does allow for moving.  This is how types like std::unique_ptr are implemented:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Type<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> Unique<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; Unique<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; Unique<span style="color: #008000;">&#40;</span>Type<span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; Unique<span style="color: #008000;">&#40;</span>Unique<span style="color: #000040;">&amp;&amp;</span> source<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_Ptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span> <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">&#40;</span>source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; ~Unique<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">delete</span> m_Ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; Unique<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span>Unique<span style="color: #000080;">&lt;</span>Type<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;&amp;</span> source<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">swap</span><span style="color: #008000;">&#40;</span>m_Ptr, source.<span style="color: #007788;">m_Ptr</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; Type<span style="color: #000040;">*</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #0000ff;">const</span> Type<span style="color: #000040;">*</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_Ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; Type<span style="color: #000040;">*</span> m_Ptr<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Note that while not complete, it does offer most of the basic features of std::unique_ptr.  This type can be put into any move-aware container and will guarantee that only one instance of its pointed-to object will exist at any time, and guarantees that its owned object will be deleted when the Unique object goes out of scope.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2012/01/custom-containers-in-c11/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Trivial C++ For-Each Loop for MSVC 2010</title>
		<link>http://gamedevstudent.com/journal/2011/09/c-foreach-in-msvc-2010/</link>
		<comments>http://gamedevstudent.com/journal/2011/09/c-foreach-in-msvc-2010/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 03:06:51 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=238</guid>
		<description><![CDATA[The new C++11 standard includes a nice new feature called range-based for. The syntax allows for simple and quick iteration over ranges, such as containers. Usage looks like so: for &#40;int&#38; x : my_container&#41; &#123; &#160; do_something_with&#40;i&#41;; &#125; Pretty handy. Unfortunately, this new feature is not supported in MSVC 2010. In fact, very few C++11 [...]]]></description>
			<content:encoded><![CDATA[<p>The new <a href="http://en.wikipedia.org/wiki/C%2B%2B11">C++11</a> standard includes a nice new feature called range-based for.  The syntax allows for simple and quick iteration over ranges, such as containers.  Usage looks like so:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span> x <span style="color: #008080;">:</span> my_container<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; do_something_with<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Pretty handy.  Unfortunately, this new feature is not supported in MSVC 2010.  In fact, very few C++11 features are supported in MSVC, while GCC supports a large majority of them.  One of the popular solutions to many C++ problems like this one is to make use of <a href="http://boost.org">Boost</a>, which does include a ForEach library that can roughly simulate the new C++11 syntax.  However, this facility is quite heavy-weight and like most Boost libraries it includes some very complicated templates which can have a very detrimental effect on compile times.</p>
<p>Users of MSVC (and any other recent C++ compiler) fortunately do have access to some other facilities of C++11 that make it significantly easier to implement a this loop construct with a minimal of fuss.  While Boost attempts to service every major C++ compiler still in reasonably popular use, those of us who are explicitly targeting MSVC 2010 can make use of one of these other C++11 features to make a very small and efficient FOR_EACH macro.  In fact, the implementation I&#8217;m presenting does not add any templates or other types at all; it&#8217;s just a simple four-line macro declaration that is free of side-effects and works with no surprising behavior in any enclosing context, and has absolutely no run-time overhead compared to a traditional iterator-based for-loop or any comparable hand-coded loop!</p>
<p>I must note that in my container and algorithm codebase I make use of ranges rather than STL-style iterators.  For more information on that topic (which I plan on writing about later), check out the Andrei Alexandrescu&#8217;s paper <a href="http://www.uop.edu.jo/download/PdfCourses/Cplus/iterators-must-go.pdf">Iterators Must Go</a>.  I&#8217;m going to assume for the rest of this article that the reader is familiar with the concept of ranges vs iterators and is using ranges in their own containers and algorithms, as my foreach implementation depends on them.  Some sample code at the end of this article uses STL idioms for users not using a custom container and algorithm library.</p>
<p>I&#8217;m just going to post the code I have for FOR_EACH and then go through explaining how it works.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#define FOR_EACH(VAR, RANGE) \<br />
&nbsp; for (auto _foreach_range = (RANGE); !_foreach_range.empty(); _foreach_range.pop_front()) \<br />
&nbsp; &nbsp; if (bool _foreach_inner = false) {} else \<br />
&nbsp; &nbsp; &nbsp; for (VAR = _foreach_range.front(); !_foreach_inner; _foreach_inner = true)</span></div></div>
<p>The one feature of C++11 that this code depends on is the new behavior for the auto keyword.  This allows us to create a copy of the given range so that the input is only evaluated once, which is important to ensure that the macro has no unintended side-effects.</p>
<p>The second line has that odd-looking IF statement in the code.  It exists to create a variable that we need for the second FOR statement.  However, we have to be tricky and put that &#8220;{} else&#8221; bit in the code to ensure that the IF statement we made doesn&#8217;t bind to any other ELSE clauses.  If we didn&#8217;t do that, the following code would not work correctly:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>do_loop<span style="color: #008000;">&#41;</span><br />
&nbsp; FOR_EACH<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i, my_int_vector<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">else</span><br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;no data<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></div>
<p>The next two lines are where things get tricky.  We want our macro to allow the user declare a variable to start the values being enumerated, including the variable type.  That is going to require creating a new variable in a new scope if we want to support references, which we do.  We can&#8217;t do that with an IF statement on its own because that requires the variable to be convertible to false, which is of course not true for arbitrary types (or even most types, whose bool conversion tends to have some meaning attached to it rather than always being false).  The only construct we have for creating a new variable for a following statement is to use a FOR statement.  Howevever, that is a looping statement, and we only want to execute the following statement once.  We&#8217;re going to have to construct a FOR statement that will run once and only once every time.  To do that, we need a second variable to control the loop.</p>
<p>Enter the IF statement in the FOR_EACH macro.  The _foreach_loop is a boolean value initialized to false.  We initialize to false to make sure the ELSE clause that actually holds our FOR statement is always run.  We then use that boolean in the following FOR statement to ensure it runs only once.  The boolean is toggled to true after the loop is run once and then the loop terminates due to the condition !_foreach_loop.</p>
<p>The second FOR statement creates our local user-specified variable initialized to the front value from the range (which for STL adaptors is the same as initializing it to the iterator&#8217;s dereferenced value).  The macro is complete; the body of the FOR statement is the statement the user entered after the FOR_EACH macro.  We now have a simple means of iterating over any range, like so:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">FOR_EACH<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">auto</span> i, container.<span style="color: #007788;">all</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; do_something<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>That&#8217;s all there is to it!</p>
<p>For those using the STL, it should be trivial to convert the code to operate on a pair of iterators instead of a range object.  Modify the above code to use a std::pair<> of iterators and update the empty(), pop_front(), and front() calls with equivalent code: first != second, ++first, and *first, respectively.  The code should look something like the following snippet.  As I haven&#8217;t tested this version, note that if it breaks the reader gets to keep the pieces.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#define FOR_EACH(VAR, BEGIN, END) \<br />
&nbsp; for (auto _foreach_range = std::make_pair((BEGIN), (END)); _foreach_range.first != _foreach_range.second; ++_foreach_range.first) \<br />
&nbsp; &nbsp; if (bool _foreach_inner = false) {} else \<br />
&nbsp; &nbsp; &nbsp; for (VAR = *_foreach_range.first; !_foreach_inner; _foreach_inner = true)</span><br />
<br />
FOR_EACH<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i, int_vector.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, int_vector.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>p.s. Forgive the use of printf rather than C++ iostreams; the blog is not liking the less-than signs used for the stream-out operators, and I&#8217;m not much in the mood to wrestle with WordPress right now.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2011/09/c-foreach-in-msvc-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SONAR Released</title>
		<link>http://gamedevstudent.com/journal/2011/09/sonar-released/</link>
		<comments>http://gamedevstudent.com/journal/2011/09/sonar-released/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 05:13:59 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=235</guid>
		<description><![CDATA[Our game SONAR is now available on the Chrome Web Store! SONAR is one of the first commercial WebGL games released (possibly the first one ever to be written from the ground up in pure JavaScript). The gameplay is very similar in spirit to Subsonic, our sophomore DigiPen project. Aside from snazzy new 3D visuals, [...]]]></description>
			<content:encoded><![CDATA[<p>Our game <a href="http://sonargame.com">SONAR</a> is now available on the Chrome Web Store!</p>
<p>SONAR is one of the first commercial WebGL games released (possibly the first one ever to be written from the ground up in pure JavaScript).  The gameplay is very similar in spirit to Subsonic, our sophomore DigiPen project.</p>
<p>Aside from snazzy new 3D visuals, SONAR features more gadgets and enemies than the original, a new sound track, new story, and more content.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2011/09/sonar-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ String Library Best Practices</title>
		<link>http://gamedevstudent.com/journal/2011/06/c-string-library-best-practices/</link>
		<comments>http://gamedevstudent.com/journal/2011/06/c-string-library-best-practices/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 10:44:44 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=227</guid>
		<description><![CDATA[String libraries have been a frequent topic of conversation lately. There&#8217;s a lot of things wrong with the C++ standard&#8217;s implementation, a lot of missing functionality, and a lot of discussion about the best ways to fix it or work around it. I&#8217;m going to go over a few suggestions that I and my fellow [...]]]></description>
			<content:encoded><![CDATA[<p>String libraries have been a frequent topic of conversation lately.  There&#8217;s a lot of things wrong with the C++ standard&#8217;s implementation, a lot of missing functionality, and a lot of discussion about the best ways to fix it or work around it.  I&#8217;m going to go over a few suggestions that I and my fellow colleagues, peers, and instructors all agree on.<br />
<span id="more-227"></span></p>
<span id="Immutable_Strings"><h2>Immutable Strings</h2></span>
<p>The first thing to note is that std::string is not immutable.  This means that any string references can have its contents changed at any time, which leads to a greater deal of copying being done to ensure these changes never happen.  Given that strings very rarely change &#8212; even in applications heavily focused on string/text editing &#8212; it makes sense to use immutable strings.</p>
<p>In practice, this basically just means writing a simple wrapper around C-style strings that copies its source string on initialization or assignment, and frees it in the destructor.  That might sound inefficient, but in practice this string wrapper will be rarely used.  We&#8217;re mostly interested in this custom class just to ensure that we aren&#8217;t suffering the penalties imposed by poor std::string implementations (for instance, MSVC&#8217;s std::string is 32 bytes in size on x86, not including any memory which may be dynamically allocated).</p>
<span id="String_Ranges"><h2>String Ranges</h2></span>
<p>The work horse of our API will be a string range class.  This is a pretty simple type which is just a wrapper around a start pointer and an end pointer.  This class has a number of neat benefits.  First, size of any string can be easily calculated by simply subtracting start from end.</p>
<p>Second, a string range can &#8220;look into&#8221; any character data buffer.  APIs that need to process string data and which take string ranges can easily accept data from string literals, string objects, string builders, memory mapped files, and so on without any additional copies or temporary allocation.</p>
<p>Third, string ranges are quite fast to pass and return, being only two pointers in size.  They require absolutely no dynamic allocation, can have trivial copy and move constructors, assignment operators, and destructors.</p>
<p>There&#8217;s one case where a string range may not be suitable, and that&#8217;s for use with any API that expects a regular C-style NUL-terminated string.  This unfortunately includes almost all OS APIs on just about every platform.  These calls are suitably rare however that makes temporary copies in these cases is not usually going to be a problem.  There&#8217;s also a trick that you can use to avoid extraneous copies, with some care.  This trick is to simple dereference the end pointer and check if the value is the NUL byte.  If it is then the string range already represents a NUL-terminated string.  This will usually be safe because your string ranges will be constructed from string literals, string objects, string builders, or other sources of string data where the buffer will always have a NUL byte at the end, so the end pointer of the string range will either be pointing at that NUL byte or at some other valid character inside the source string data.  However, it is possible that your application does not always do this; if you use memory mapped files then it&#8217;s possible for a string range&#8217;s end pointer to be pointing at one byte past the end of the memory mapped range, and dereferencing it is undefined.  If you use any string data sources that may not be NUL terminated, it&#8217;s important to keep this in mind if you ever plan on using the is-end-NUL trick.  One final suggestion is that the default constructor for a string range should set the start and end pointers to reference an empty string literal rather than NULL.  This means that your string range&#8217;s end pointer will never be NULL, making sure that even default constructed string ranges are safe to dereference and pass to C-style APIs.</p>
<span id="String_Builder"><h2>String Builder</h2></span>
<p>When it comes to formatting strings, printf and other unsafe C style APIs are best avoided.  C++ iostreams style APIs are also best avoided as they are very slow and use the standard C++ std::string API, which we&#8217;re trying to avoid.</p>
<p>Thankfully, building your own is rather easy.  A string builder class is basically a wrapper around a std::vector<char> (or an equivalent container of your own) that offers various methods for writing data into the buffer.  Simple character append, string append, and number append functions are relatively easy to write.  For floating point values, you can use the gvct function (called _gvct on Windows) to get correct and accurate string formatting.  This is generally the function used internally by printf implementations.</p>
<p>If you desire, you can add advanced iterator support for using generic algorithms on string buffer data.  Insert and cursor operations are also relatively easy to add, if you need them.</p>
<p>The API then simply offers a method that returns a string range, which can be copied into a string wrapper if necessary for longer term storage in memory.</p>
<p>Note that it&#8217;s best to ensure that the character buffer is always NUL terminated, making it easy to debug the data by calling OS print/debugoutput functions on the string buffer&#8217;s contents.  It also enables the string range NUL check mentioned above.  It&#8217;s also feasible to offer a method that returns an rvalue reference for creating string wrappers directly, particularly if you don&#8217;t mind the extra memory padding that the buffer will often have due to automatic buffer expansion algorithms.</p>
<span id="String_Concatenation"><h2>String Concatenation</h2></span>
<p>A specialized use case of a string builder is to concatenate two or more strings.  A simple + operator seems attractive, but remember that this only works for two operands.  If you try to use a + operator with three or more strings then multiple temporary strings (and hence allocations/deallocations) become necessary, even with rvalue references and move constructors.  However, using a string builder is also not the most efficient means of doing string concatenation.</p>
<p>First, the string builder is going to use an expansion algorithm that results in multiple buffer reallocations, and the final buffer will have extra space.  If you are concatenating N strings, you can easily calculate the exact necessary space necessary (the sum of the lengths of all the operands, plus one byte for the NUL character), and you should at most need N copies (from each source to the proper location in the destination).  This can be easily implemented as a single variadic function (using a count value or sentinel), a variadic template, or a series of overloaded functions supporting up to some pre-determined number of operands.  The most operands I&#8217;ve seen in common use is 5, used for constructing paths (a directory, a path separator, a file name, a period, and an extension).</p>
<p>Though rarely needed, it&#8217;s easy enough to implement that doing so is a no brainer.</p>
<span id="String_Interning"><h2>String Interning</h2></span>
<p>One further major consideration is string interning.  String interning is the process of ensuring that there is only a single allocated copy of any particular string.  When strings are loaded up or constructed dynamically, the string is searched for in a central table; if found, the existing instance is returned, and otherwise a new entry is made and returned.</p>
<p>One advantage is a reduction in memory usage for applications that make heavy use of frequently entered dynamic strings.  Another advantage is that comparing interned strings is O(1) rather than O(n) and requires no dereferencing of the string pointers (because if the pointers are not equal, they must point to different strings).</p>
<p>Note that it is not generally a great idea to intern all strings.  Large strings, temporary strings of dynamic nature, and strings that are highly likely to be unique gain no benefits from string interning.  Especially for temporary unique strings, interning can mean that the strings never get freed in many simpler and more efficient implementations (as the interned string table is never pruned and entries never removed from it).</p>
<p>It&#8217;s also possible to allow multiple interning tables, which may make sense when there are different sets of strings that are unlikely to have values in common.  An interned string handle class may in this case contain two pointers: one to the intern table and one to the table entry for the string.  Checking strings for equality with this type is still fairly fast; if the table pointer is identical, simply compare the entry pointers, and if the table pointers are different, call the usual string range comparison types.</p>
<p>One final advantage of string interning is that the table entries can include hashes for each string (and in fact usually will, as the best interning table implementation is most often a hash table).  This means that using interned strings as keys into other hash tables can be done without needing to rehash the key.</p>
<span id="Advanced_String_Literals"><h2>Advanced String Literals</h2></span>
<p>A final neat trick is to use a smart string literal class.  Classes like the string wrapper, string builder, and string range would not be constructible from a string literal but would instead take a special literal string class.  This class itself is the only constructor/method (other than OS functions and string library internals) that ever accepts a C-style string pointer.</p>
<p>The first advantage is that this lets you easily ban the use of char* in the application framework and safely search/grep for it in static analysis tools, as only the string library code will ever use it.</p>
<p>Second, the string literal can be implemented in such a way that it computes the string length at compile time (most compilers have a builtin version of strlen that does this).  Furthermore, on current GCC and future MSVC compilers, this class can do compile-time string hashing, so that use of the literal as a hash table key or for string interning doesn&#8217;t require runtime hashing.</p>
<p>Finally, a string literal like this can be passed to a string intern lookup and, if the lookup requires the creation of a new entry, the implementation can know that it&#8217;s safe to just store the reference (as a string range, perhaps) without needing to make a copy of the string data.</p>
<span id="String_Editing"><h2>String Editing</h2></span>
<p>One special use case for strings is editing.  This includes both simple text entry interfaces in a simple GUI toolkit in a game to full-blown text editors and word processors.</p>
<p>It&#8217;s worth mentioning that none of the string classes outlined above are particularly suitable for those cases.  A GUI text entry may be better off with a fixed-size character buffer class with cursor support (although a string builder with cursor support is also quite servicable).  For larger text editors, more advanced string data types are called for.  Usually there will be no benefit to interning, and you certainly don&#8217;t want to use a single large buffer that needs to resize frequently as text is entered or removed.</p>
<p>String ranges may not even be useful for this case as the more common large string data structures do not guarantee contiguous memory, which a string range requires.</p>
<span id="Miscellaneous"><h2>Miscellaneous</h2></span>
<p>The suggestions above are in many cases made based on performance claims.  As with will performance tips, profile and test your changes to make sure they are actual performance improvements for you application.</p>
<p>Also, keep in mind that some applications may have different needs than most others and some of the above tips may not be as beneficial for your application.</char></p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2011/06/c-string-library-best-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compile-time String Hashing in C++0x</title>
		<link>http://gamedevstudent.com/journal/2011/05/compile-time-string-hashing-in-c0x/</link>
		<comments>http://gamedevstudent.com/journal/2011/05/compile-time-string-hashing-in-c0x/#comments</comments>
		<pubDate>Sat, 28 May 2011 20:19:02 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=217</guid>
		<description><![CDATA[I have written a small demo (tested in GCC 4.6) showing how to get compile-time hashing of a string literal in C++0x. (Note that the blog is having a little bit of fun with the formatting of the code below; my apologies.) #include &#60;iostream&#62; // NOTE: hashing algorithm used is FNV-1a // FNV-1a constants static [...]]]></description>
			<content:encoded><![CDATA[<p>I have written a small demo (tested in GCC 4.6) showing how to get compile-time hashing of a string literal in C++0x.<br />
<span id="more-217"></span></p>
<p>(Note that the blog is having a little bit of fun with the formatting of the code below; my apologies.)</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;iostream&gt;</span><br />
<br />
<span style="color: #666666;">// NOTE: hashing algorithm used is FNV-1a</span><br />
<br />
<span style="color: #666666;">// FNV-1a constants</span><br />
<span style="color: #0000ff;">static</span> constexpr <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> basis <span style="color: #000080;">=</span> 14695981039346656037ULL<span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">static</span> constexpr <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> prime <span style="color: #000080;">=</span> 1099511628211ULL<span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// compile-time hash helper function</span><br />
constexpr <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> hash_one<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> c, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> remain, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> value<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> c <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">?</span> value <span style="color: #008080;">:</span> hash_one<span style="color: #008000;">&#40;</span>remain<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, remain <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span>, <span style="color: #008000;">&#40;</span>value <span style="color: #000040;">^</span> c<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> prime<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// compile-time hash</span><br />
constexpr <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> hash<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> str<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> hash_one<span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, str <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span>, basis<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// run-time hash</span><br />
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> hash_rt<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> str<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> hash <span style="color: #000080;">=</span> basis<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>str <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hash <span style="color: #000040;">^</span><span style="color: #000080;">=</span> str<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hash <span style="color: #000040;">*</span><span style="color: #000080;">=</span> prime<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000040;">++</span>str<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> hash<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// ensure that the constexpr value is really compile-time constant</span><br />
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> NUM<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">struct</span> test_const<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> value <span style="color: #000080;">=</span> NUM<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// test</span><br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; constexpr <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> value <span style="color: #000080;">=</span> test_const<span style="color: #000080;">&lt;</span>hash <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;hello, world!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">value</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> value_rt <span style="color: #000080;">=</span> hash_rt<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;hello, world!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> <span style="color: #FF0000;">&quot;hash = &nbsp; &nbsp;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> value <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;hash_rt = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> value_rt <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> value <span style="color: #000080;">==</span> value_rt <span style="color: #008080;">?</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">127</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>The code is available at <a href="https://github.com/elanthis/constexpr-hash-demo">GitHub</a> as well, as will be any future updates/tweaks I make for the demo.</hash></unsigned></iostream></code></p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2011/05/compile-time-string-hashing-in-c0x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subsonic LLC</title>
		<link>http://gamedevstudent.com/journal/2011/05/subsonic-llc/</link>
		<comments>http://gamedevstudent.com/journal/2011/05/subsonic-llc/#comments</comments>
		<pubDate>Tue, 17 May 2011 08:50:38 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Article Links]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=214</guid>
		<description><![CDATA[Monday was my first day working at our new startup, Subsonic LLC. We&#8217;re creating a new HTML5+WebGL game for the Chrome Web Store, based on the design our DigiPen game, Subsonic. We&#8217;ll be getting a blog put together soon so everyone can follow the development of the game over the next three months, with in-depth [...]]]></description>
			<content:encoded><![CDATA[<p>Monday was my first day working at our new startup, Subsonic LLC.  We&#8217;re creating a new HTML5+WebGL game for the Chrome Web Store, based on the design our DigiPen game, Subsonic.</p>
<p>We&#8217;ll be getting a blog put together soon so everyone can follow the development of the game over the next three months, with in-depth articles on technological solutions to problems, art design and creation for Web-based games, the business challenges for a small startup, as well as game design and publishing stories.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2011/05/subsonic-llc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenGL 5.0 Wishlist</title>
		<link>http://gamedevstudent.com/journal/2011/04/opengl-5-0-wishlist/</link>
		<comments>http://gamedevstudent.com/journal/2011/04/opengl-5-0-wishlist/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 07:52:03 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://gamedevstudent.com/journal/?p=210</guid>
		<description><![CDATA[There are some articles around the Net these days detailing wishlists for OpenGL 4.2. Two of the more informed ones can be found here and here. Between those two, the most interesting and common requests seem to come around DSA (direct state access) and better bind-by-semantic behavior for shaders. I&#8217;m going to lean a bit [...]]]></description>
			<content:encoded><![CDATA[<p>There are some articles around the Net these days detailing wishlists for OpenGL 4.2.  Two of the more informed ones can be found <a href="http://rastergrid.com/blog/2010/11/suggestions-for-opengl-4-2-and-beyond/">here</a> and <a href="http://www.g-truc.net/post-0330.html">here</a>.  Between those two, the most interesting and common requests seem to come around DSA (direct state access) and better bind-by-semantic behavior for shaders.  I&#8217;m going to lean a bit more radically and propose an improved version of both that goes a bit beyond what the current proposals detail but which is still quite within the realm of possibility.<br />
<span id="more-210"></span></p>
<p><strong>Cleaner Object API</p>
<p>My approach for DSA would go beyond a simple addition of functions and instead would revolve the addition of some new GL types and accessors.  Get rid of the GLuint names for objects and instead use opaque types.</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> glContext glContext<span style="color: #339933;">;</span><br />
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> glTexture glTexture<span style="color: #339933;">;</span><br />
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> glBuffer glBuffer<span style="color: #339933;">;</span><br />
<span style="color: #808080; font-style: italic;">/* etc. */</span></div></div>
<p>Rename functions as appropriate and provide a parallel API that focuses purely on these objects.</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glContext<span style="color: #339933;">*</span> ctx <span style="color: #339933;">=</span> current_context<span style="color: #339933;">;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* create a new texture from existing texture data */</span><br />
glTexture<span style="color: #339933;">*</span> tex <span style="color: #339933;">=</span> glTextireCreate2D<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> usage<span style="color: #339933;">,</span> format<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> levels<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glByte<span style="color: #339933;">*</span> map <span style="color: #339933;">=</span> glTextureMap<span style="color: #009900;">&#40;</span>tex<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066;">memcpy</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> image_data<span style="color: #339933;">,</span> width <span style="color: #339933;">*</span> height <span style="color: #339933;">*</span> bytes_per_texel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glTextureUnmap<span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p></strong><strong>Direct State Access</strong></p>
<p>Use simple and concise accessor functions.  The map example above is a good indicator of what I mean, but here&#8217;s a few more examples.</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* framebuffer creation and configuration */</span><br />
glFrameBuffer<span style="color: #339933;">*</span> fb <span style="color: #339933;">=</span> glFrameBufferCreate<span style="color: #009900;">&#40;</span>ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glFrameBufferAttachTexture<span style="color: #009900;">&#40;</span>fb<span style="color: #339933;">,</span> GL_COLOR_BUFFER_0<span style="color: #339933;">,</span> my_color_buffer_texture<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glFrameBufferAttachTexture<span style="color: #009900;">&#40;</span>fb<span style="color: #339933;">,</span> GL_DEPTH_STENCIL_BUFFER<span style="color: #339933;">,</span> glTextureCreate2D<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> GL_USAGE_RENDER_BUFFER<span style="color: #339933;">,</span> GL_FORMAT_D24S8<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* shader program creation */</span><br />
glShader<span style="color: #339933;">*</span> fragment_shader <span style="color: #339933;">=</span> glShaderCreateFromSource<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> GL_FRAGMENT_SHADER<span style="color: #339933;">,</span> source_string<span style="color: #339933;">,</span> file_name<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* last two parameters allow setting debugging source information for error messages generated by the GL */</span></div></div>
<p><strong>Shader Semantics, Layouts, Bindings</strong></p>
<p>The bind-by-name approach used by GLSL normally is cumbersome and slow.  Bind-by-semantic simply makes more sense from both a performance a usability perspective.</p>
<p>I&#8217;d take things a step further and provide a layout configuration for drawing as well.  Layouts are a continuation of the FrameBuffer Object support, and in fact can probably just supplant FBO&#8217;s entirely.</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glLayout<span style="color: #339933;">*</span> layout <span style="color: #339933;">=</span> glCreateLayout<span style="color: #009900;">&#40;</span>ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glLayoutAddAttributeByName<span style="color: #009900;">&#40;</span>layout<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;POSITION&quot;</span><span style="color: #339933;">,</span> my_buffer<span style="color: #339933;">,</span> GL_TYPE_VEC3_32F<span style="color: #339933;">,</span> pos_offset<span style="color: #339933;">,</span> buffer_stride<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glLayoutAddAttributeByName<span style="color: #009900;">&#40;</span>layout<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;TEXCOORD0&quot;</span><span style="color: #339933;">,</span> my_buffer<span style="color: #339933;">,</span> GL_TYPE_VEC2_32F<span style="color: #339933;">,</span> uv_offset<span style="color: #339933;">,</span> buffer_stride<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glLayoutAddAttributeByIndex<span style="color: #009900;">&#40;</span>layout<span style="color: #339933;">,</span> bumpmap_semantic_slot<span style="color: #339933;">,</span> my_buffer<span style="color: #339933;">,</span> GL_TYPE_VEC2_32F<span style="color: #339933;">,</span> bumpmap_offset<span style="color: #339933;">,</span> buffer_stride<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
glLayoutAddOutputByTarget<span style="color: #009900;">&#40;</span>layout<span style="color: #339933;">,</span> GL_COLOR_BUFFER_0<span style="color: #339933;">,</span> my_output_texture<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>Separate Contexts from OS Windows</strong></p>
<p>The platform GL context bindings such as WGL, AGL, and GLX all currently assume a single distinct context per window, which is similar to how Direct3D 9 and earlier worked as well.  Unfortunately, this is pretty inflexible.  It leads to a lot of pain on some platforms where simply resizing a window (or more likely, switching between a windowed display and a fullscreen display) requires destroying the platform window and hence destroying the GL context.</p>
<p>It also means that resources can&#8217;t easily be shared between different windows/displays without yet more platform-specific functionality and extensions.</p>
<p>GL platform bindings should all be updated to offer two distinct sets of functions that parallel the DXGI functions for creating &#8220;devices&#8221; and &#8220;swap chains.&#8221;  One set of functions creates an OpenGL context that deals with resource management and access.  A second set of functions allows binding to an OS window.</p>
<p>These could then contain a new set of objects, similar to how DXGI has separate SwapChain objects.</p>
<p>Alternatively, a context hierarchy could be used, where sub-contexts can be created from the main context with additional semantics, similar to how API&#8217;s like <a href="http://www.cairographics.org/">Cairo</a> implement their state objects.</p>
<p>As examples of both approaches, see the following:</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* distinct objects */</span><br />
glContext<span style="color: #339933;">*</span> ctx <span style="color: #339933;">=</span> glWin32CreateContext<span style="color: #009900;">&#40;</span>HAPP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
glDisplay<span style="color: #339933;">*</span> display <span style="color: #339933;">=</span> glWin32CreateDisplay<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> HWND<span style="color: #339933;">,</span> GL_BIT_DOUBLE_BUFFER<span style="color: #339933;">|</span>GL_BIT_FULLSCREEN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
glFrameBuffer<span style="color: #339933;">*</span> fbo <span style="color: #339933;">=</span> glDisplayGetFrameBuffer<span style="color: #009900;">&#40;</span>display<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* hierarchial contexts */</span><br />
glContext<span style="color: #339933;">*</span> ctx <span style="color: #339933;">=</span> glWin32ContextCreate<span style="color: #009900;">&#40;</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
glContext<span style="color: #339933;">*</span> display <span style="color: #339933;">=</span> glWind32ContextCreateForWindow<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> HWND<span style="color: #339933;">,</span> GL_BIT_DOUBLE_BUFFER<span style="color: #339933;">|</span>GL_BIT_FULLSCREEN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
glFrameBuffer<span style="color: #339933;">*</span> fbo <span style="color: #339933;">=</span> glContextGetFrameBuffer<span style="color: #009900;">&#40;</span>display<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* this would just return NULL */</span><br />
fbo <span style="color: #339933;">=</span> glContextGetFrameBuffer<span style="color: #009900;">&#40;</span>ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>Shader Assembly Revisited</strong></p>
<p>The assembly-language shader support has been deprecated in favor of forcing developers to always use GLSL.  This is troublesome as it means that the development of alternative shading languages is much more difficult and limited to the features added to the core GLSL language.  A simpler assembly language format alleviates this problem.</p>
<p>One of the problems with assembly languages is that they usually lack a lot of semantic information necessary to properly optimize code.  I propose that a &#8220;high level&#8221; assembly language be implemented.  Utilize simple three-address-form operators in place of C-like expressions, allow direct indexing into buffers and input sets, but still provide high level flow control and semantic hints for inputs and output.</p>
<p>Something like the TGSI format used by <a href="http://mesa3d.org">Mesa 3D</a> would be a good start, though it&#8217;s not a complete solution.</p>
<p>This would allow tools like Cg to more easily target GL and optimize shader inputs and outputs properly.  It would also open the doors to getting a high quality port of HLSL or even the development of an entirely new high-level shading language.  Which is actually the next point.</p>
<p><strong>GLSL++</strong></p>
<p>GLSL was one of the first high-level shading languages and was very revolutionary for its time, but it many of the assumptions and design considerations of GLSL simply haven&#8217;t panned out all that well.  The bind-by-name behavior is still one of the better examples.  The lack of a high level effect framework, the inability to combine multiple shaders into a single source file, and the lack of overloading by version/profile are all additional negative points.</p>
<p>I&#8217;ll admit that I&#8217;m largely in favor of simply adopting Cg as an official shading language at this pointer, as it solves most of the above problems and already has a large body of documentation, examples, tools, and experience built up behind it.  I&#8217;m not at all opposed to an entirely new language though so long as it does fix of all the above problems without introducing new ones.</p>
<p>I&#8217;m certainly in favor of retaining column-major, right-handed math.  That row-major left-handed stuff from D3D is just&#8230; odd.</p>
<p>Simply expanding GLSL wouldn&#8217;t be too terribly complex.  Shader semantics need to become first class syntax, rather than the nameless and chunky layout qualifiers introduced in GLSL 4.10.  The idea of a &#8220;main&#8221; function needs to be replaced with user-named entry points that are qualified with their pipeline stage.  Finally, add profile/version overloading to functions, so shaders can be written that fallback easily to older hardware profiles as the GPU scene continues to evolve.</p>
<p><strong>Improved Utility Library</strong></p>
<p>Give us at least a few convenience library functions that most developers need.  Some basic functions for loading textures and shaders from files, for instance.  Even if you can only load DDS, TGA, and PNG images, that will save a lot of developers a lot of time and headaches.  Almost everyone has to write their own, or use some non-standard add-on library.  Almost nobody even implements these correctly; the number of load_shader_from_file() functions on the Net that break all kinds of rules (fseek, ftell, fseek, fread patterns, for instance) is simply huge, for instance.</p>
<p>One wouldn&#8217;t be opposed to a simple TTF font rendering library, or an improved GLUT that at the very least offered timers so animations and simple game loops could be implemented without resorting to platform-specific API calls would be nice.</p>
<p><strong>Conclusion</strong></p>
<p>The interesting thing I think about the above proposals is that they&#8217;re all rather trivially implementable on existing drivers.  They mostly just come down to developing a new set of API entry points over the existing functionality found in the drivers today.  As both the NVIDIA and AMD drivers already expose EXT_direct_state_access, simply updating the entry points to use the new opaque types and naming is mostly just busywork rather than serious development.  The vendors&#8217; shader compilers are mostly all about the low-level optimization and code generation, and adding new frontends is relatively easy compared to the effort of adding a new hardware profile, for instance.</p>
<p>Such an API could <em>almost</em> be implemented as a wrapper around OpenGL 4.1, EXT_direct_state_access, EXT_Cg_shader.  It can <em>almost</em> be implemented as a wrapper around Direct3D 10/11.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamedevstudent.com/journal/2011/04/opengl-5-0-wishlist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

