<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
>

<channel>
	<title>Wazoo Enterprises<title>&#187; misc</title>
</title>
	<atom:link href="http://www.wazooinc.com/category/misc/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wazooinc.com</link>
	<description>making software fun again</description>
	<lastBuildDate>Thu, 09 Sep 2010 06:04:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<!-- podcast_generator="Blubrry PowerPress/1.0.5" mode="simple" entry="normal" -->
	<itunes:summary>making software fun again</itunes:summary>
	<itunes:author>Wazoo Enterprises</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.wazooinc.com/wp-content/plugins/powerpress/itunes_default.jpg" />
	<itunes:owner>
		<itunes:name>Wazoo Enterprises</itunes:name>
		<itunes:email>erikyuzwa@gmail.com</itunes:email>
	</itunes:owner>
	<managingEditor>erikyuzwa@gmail.com (Wazoo Enterprises)</managingEditor>
	<copyright>Copyright &#xA9; Wazoo Enterprises 2010</copyright>
	<itunes:subtitle>making software fun again</itunes:subtitle>
	<image>
		<title>Wazoo Enterprises&lt;title&gt;&#187; misc&lt;/title&gt;
</title>
		<url>http://www.wazooinc.com/wp-content/plugins/powerpress/rss_default.jpg</url>
		<link>http://www.wazooinc.com/category/misc</link>
	</image>
<cloud domain='www.wazooinc.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>a list of pointers with Boost</title>
		<link>http://www.wazooinc.com/a-list-of-pointers-with-boost.html</link>
		<comments>http://www.wazooinc.com/a-list-of-pointers-with-boost.html#comments</comments>
		<pubDate>Wed, 06 May 2009 03:55:10 +0000</pubDate>
		<dc:creator>wazoo</dc:creator>
				<category><![CDATA[misc]]></category>
		<category><![CDATA[dynamic list]]></category>
		<category><![CDATA[STL]]></category>

		<guid isPermaLink="false">http://www.wazooinc.com/?p=12</guid>
		<description><![CDATA[One of the popular uses of the Standard Template Library (STL) in the C++ language is to manage a dynamic list of pointers. For example, if you have a rendering system for your game which works with a common base object that every drawable object is derived from. If you were using STL, then typically [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.wazooinc.com%2Fa-list-of-pointers-with-boost.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.wazooinc.com%2Fa-list-of-pointers-with-boost.html&amp;source=wazooinc&amp;style=normal&amp;service=bit.ly&amp;service_api=headmaster%3AR_9643738fb640817d9b7ff248d89f8c7e" height="61" width="50" /><br />
			</a>
		</div>
<p>One of the popular uses of the Standard Template Library (STL) in the C++ language is to manage a dynamic list of pointers. For example, if you have a rendering system for your game which works with a common base object that every drawable object is derived from.</p>
<p>If you were using <strong>STL</strong>, then typically you would create something like the following:</p>
<blockquote><p>
#include &lt;iostream&gt;<br />
#include &lt;list&gt;</p>
<p>using namespace std;</p>
<p>class IRenderObj<br />
{<br />
public:<br />
   float x;<br />
   float y;<br />
   float z;<br />
};</p>
<p>main()<br />
{<br />
   list&lt;IRenderObj*&gt; drawList;<br />
   list&lt;IRenderObj*&gt;::iterator drawListIter;</p>
<p>   IRenderObj *a= new IRenderObj;<br />
   IRenderObj *b= new IRenderObj;<br />
   IRenderObj *c= new IRenderObj;</p>
<p>   a->x = 0.0f; a->y = 0.0f; a->z = 0.0f;<br />
   b->x = 0.0f; b->y = 1.0f; b->z = 0.0f;<br />
   c->x = 1.0f; c->y = 0.0f; c->z = -10.0f;</p>
<p>   drawList.push_back(a);<br />
   drawList.push_back(b);<br />
   drawList.push_back(c);</p>
<p>   for (drawListIter = drawList.begin();<br />
        drawListIter != drawList.end();<br />
        drawListIter++)<br />
   {<br />
      //draw, translate each vector<br />
      glTranslatef( (*drawListIter)->x, (*drawListIter)->y, (*drawListIter)->z );</p>
<p>   }</p>
<p>   // Now Free pointers<br />
   for (drawListIter = drawList.begin();<br />
        drawListIter != drawList.end();<br />
        drawListIter++)<br />
   {<br />
      delete *drawListIter;<br />
   }</p>
<p>   drawList.clear(); // List is deleted.</p>
<p>}</p>
</blockquote>
<p><span id="more-12"></span></p>
<p>Not bad, not bad. It&#8217;s just a bit of a pain to remember to manually go through the list to delete each object. It&#8217;s manageable if you only have a few of these such lists, but even then you have to be supremely careful that you remember to do it. In short, there&#8217;s a chance of creating a memory leak.</p>
<h2>Using Boost::ptr_list</h2>
<p>Another approach is to use a component of the Boost C++ libraries, <strong>boost::ptr_list</strong> to manage your list of objects for you. With this object as your dynamic container, it will automatically clean things up for you.</p>
<blockquote><p>
#include &lt;boost/ptr_container/ptr_list.hpp&gt;<br />
#include &lt;iostream&gt;</p>
<p>using namespace std;</p>
<p>class IRenderObj<br />
{<br />
public:<br />
   float x;<br />
   float y;<br />
   float z;<br />
};</p>
<p>main()<br />
{<br />
   //notice the declaration doesn&#8217;t require you to use a pointer to IRenderObj<br />
   boost::ptr_list&lt;IRenderObj&gt; drawList;<br />
   boost::ptr_list&lt;IRenderObj&gt;::iterator drawListIter;</p>
<p>   IRenderObj *a= new IRenderObj;<br />
   IRenderObj *b= new IRenderObj;<br />
   IRenderObj *c= new IRenderObj;</p>
<p>   a->x = 0.0f; a->y = 0.0f; a->z = 0.0f;<br />
   b->x = 0.0f; b->y = 1.0f; b->z = 0.0f;<br />
   c->x = 1.0f; c->y = 0.0f; c->z = -10.0f;</p>
<p>   drawList.push_back(a);<br />
   drawList.push_back(b);<br />
   drawList.push_back(c);</p>
<p>   for (drawListIter = drawList.begin();<br />
        drawListIter != drawList.end();<br />
        drawListIter++)<br />
   {<br />
      //draw, translate each vector<br />
      glTranslatef( (*drawListIter)->x, (*drawListIter)->y, (*drawListIter)->z );</p>
<p>   }</p>
<p>   //so far so good, the same as our STL code</p>
<p>   //except the kicker&#8230;the clear method will clean up for us!<br />
   drawList.clear(); // All pointers held in list are deleted.</p>
<p>}
</p></blockquote>
<p>The choice is yours, but in this case I pretty much stick with Boost as it&#8217;s one less potential memory leak I have to worry about.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.wazooinc.com/a-list-of-pointers-with-boost.html/feed" rel="" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;title=a+list+of+pointers+with+Boost" rel="" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;title=a+list+of+pointers+with+Boost" rel="" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;title=a+list+of+pointers+with+Boost&amp;desc=One%20of%20the%20popular%20uses%20of%20the%20Standard%20Template%20Library%20%28STL%29%20in%20the%20C%2B%2B%20language%20is%20to%20manage%20a%20dynamic%20list%20of%20pointers.%20For%20example%2C%20if%20you%20have%20a%20rendering%20system%20for%20your%20game%20which%20works%20with%20a%20common%20base%20object%20that%20every%20drawable%20object%20is%20derived%20from.%0D%0A%0D%0AIf%20you%20were%20using%20STL%2C%20then%20typic" rel="" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;t=a+list+of+pointers+with+Boost" rel="" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;imageurl=" rel="" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;bm_description=a+list+of+pointers+with+Boost&amp;plugin=sexybookmarks" rel="" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;title=a+list+of+pointers+with+Boost" rel="" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;title=a+list+of+pointers+with+Boost" rel="" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.wazooinc.com/a-list-of-pointers-with-boost.html&amp;title=a+list+of+pointers+with+Boost" rel="" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://www.wazooinc.com/a-list-of-pointers-with-boost.html" rel="" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=a+list+of+pointers+with+Boost+-+http://b2l.me/ag2d6s&amp;source=shareaholic" rel="" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.wazooinc.com/a-list-of-pointers-with-boost.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>the singleton</title>
		<link>http://www.wazooinc.com/the-singleton.html</link>
		<comments>http://www.wazooinc.com/the-singleton.html#comments</comments>
		<pubDate>Wed, 06 May 2009 03:53:29 +0000</pubDate>
		<dc:creator>wazoo</dc:creator>
				<category><![CDATA[misc]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.wazooinc.com/?p=10</guid>
		<description><![CDATA[The Singleton In most cases for a software codebase there is the need to usually ensure that there is one single instantiation of an object. In other words, usually a fairly important object which would wreck havoc on the application if there were more than one. In the days of C, this was accomplished with [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.wazooinc.com%2Fthe-singleton.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.wazooinc.com%2Fthe-singleton.html&amp;source=wazooinc&amp;style=normal&amp;service=bit.ly&amp;service_api=headmaster%3AR_9643738fb640817d9b7ff248d89f8c7e" height="61" width="50" /><br />
			</a>
		</div>
<h1>The Singleton</h1>
<p>In most cases for a software codebase there is the need to usually ensure that there<br />
is one single instantiation of an object. In other words, usually a fairly important object<br />
which would wreck havoc on the application if there were more than one.</p>
<p>In the days of <em>C</em>, this was accomplished with the use of a global variable. While<br />
not pretty, it does do the job it is meant for. One problem with using a global variable<br />
this way, is that the object is <em>always</em> consuming resources. For example, what<br />
if we have a class design in which the Singleton object need only be created when<br />
certain circumstances / conditions are met?</p>
<p>In the C++ language, we can create a small <em>Singleton</em> object that we can<br />
then use for future objects in our system. In other words we can create a base<br />
object to generalize any common handling of Singletons.</p>
<h2>Da Code!</h2>
<blockquote><pre>
/**
* Template class for creating single-instance global classes.
* The code in this file is taken from article 1.3 in the the book:
* Game Programming Gems from Charles River Media with the
* copyright notice going to Scott Bilas.
*/
template <typename T> class ISingleton
{
protected:

	/** The static member object */
	static T* ms_Singleton;

public:
/**
* Constructor
*/
ISingleton( void )
{
    assert( !ms_Singleton );
    ms_Singleton = static_cast< T* >( this );
}

/**
* Destructor
*/
~ISingleton( void )
    {  assert( ms_Singleton );  ms_Singleton = 0;  }

	/**
	* This method just returns the internal member by
	* reference
	* @return T&#038; - reference to internal abstract Type
	*/
	static T&#038; getSingleton( void )
	{	assert( ms_Singleton );  return ( *ms_Singleton ); }

	/**
	* This method just returns the internal member by
	* a pointer
	* @return T* - pointer to the internal abstract Type
	*/
	static T* getSingletonPtr( void )
	{ return ms_Singleton; }
};
</pre>
</blockquote>
<p>That&#8217;s pretty much all we need defined for our lowest-level base class.<br />
To see this object in use, be sure to check out the other tutorials<br />
on this site. Most of them employ this <em>Singleton</em> object to<br />
work with objects such as a <em>FileLogger</em>.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.wazooinc.com/the-singleton.html/feed" rel="" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.wazooinc.com/the-singleton.html&amp;title=the+singleton" rel="" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.wazooinc.com/the-singleton.html&amp;title=the+singleton" rel="" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://www.wazooinc.com/the-singleton.html&amp;title=the+singleton&amp;desc=The%20Singleton%0D%0AIn%20most%20cases%20for%20a%20software%20codebase%20there%20is%20the%20need%20to%20usually%20ensure%20that%20there%0D%0Ais%20one%20single%20instantiation%20of%20an%20object.%20In%20other%20words%2C%20usually%20a%20fairly%20important%20object%0D%0Awhich%20would%20wreck%20havoc%20on%20the%20application%20if%20there%20were%20more%20than%20one.%0D%0A%0D%0AIn%20the%20days%20of%20C%2C%20this%20was%20acco" rel="" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.wazooinc.com/the-singleton.html&amp;t=the+singleton" rel="" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.wazooinc.com/the-singleton.html&amp;imageurl=" rel="" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://www.wazooinc.com/the-singleton.html&amp;bm_description=the+singleton&amp;plugin=sexybookmarks" rel="" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://www.wazooinc.com/the-singleton.html&amp;title=the+singleton" rel="" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://www.wazooinc.com/the-singleton.html&amp;title=the+singleton" rel="" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.wazooinc.com/the-singleton.html&amp;title=the+singleton" rel="" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://www.wazooinc.com/the-singleton.html" rel="" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=the+singleton+-+http://b2l.me/ag2d6r&amp;source=shareaholic" rel="" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.wazooinc.com/the-singleton.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
