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

<channel>
	<title>Last Ray of Hope &#187; C++</title>
	<atom:link href="http://www.lastrayofhope.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lastrayofhope.com</link>
	<description>Home of Kaluriel Hargrove</description>
	<lastBuildDate>Thu, 02 Feb 2012 00:21:18 +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>The International Obfuscated C Code Contest</title>
		<link>http://www.lastrayofhope.com/2012/01/05/the-international-obfuscated-c-code-contest/</link>
		<comments>http://www.lastrayofhope.com/2012/01/05/the-international-obfuscated-c-code-contest/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 21:02:20 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3984</guid>
		<description><![CDATA[I got told about this brilliant bit of code recently. It is a magic eye. Supposedly it will compile with a GCC compiler, I was unable to get it to in Visual Studio. More similar code is on the official IO CCC website. http://www.ioccc.org/ http://www.ioccc.org/years.html#2001_herrmann2]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2012/01/ioccc.png"><img class="alignleft size-thumbnail wp-image-3994" title="IO CCC" src="http://www.lastrayofhope.com/wp-content/uploads/2012/01/ioccc-150x150.png" alt="" width="150" height="150" /></a>I got told about this brilliant bit of code recently. It is a magic eye. Supposedly it will compile with a GCC compiler, I was unable to get it to in Visual Studio.</p>
<p>More similar code is on the official IO CCC website.</p>
<p><a href="http://www.ioccc.org/" target="_blank">http://www.ioccc.org/</a></p>
<p><a href="http://www.ioccc.org/years.html#2001_herrmann2" target="_blank">http://www.ioccc.org/years.html#2001_herrmann2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2012/01/05/the-international-obfuscated-c-code-contest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SprintfCat</title>
		<link>http://www.lastrayofhope.com/2011/11/02/sprintfcat/</link>
		<comments>http://www.lastrayofhope.com/2011/11/02/sprintfcat/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 15:17:23 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[safe]]></category>
		<category><![CDATA[sprintf]]></category>
		<category><![CDATA[stdlib]]></category>
		<category><![CDATA[strlen]]></category>
		<category><![CDATA[vsprintf]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3975</guid>
		<description><![CDATA[On the train to Cardiff yesterday, I was creating a fixed function shader generator for the Athena engine. I kept using strcat() with if statements, and in a lot of places I kept using sprintf() then copying that in with strcat(). So I decided to make SprintfCat(), all the wonderful joy of sprintf, with the [...]]]></description>
			<content:encoded><![CDATA[<p>On the train to Cardiff yesterday, I was creating a fixed function shader generator for the Athena engine. I kept using strcat() with if statements, and in a lot of places I kept using sprintf() then copying that in with strcat().</p>
<p>So I decided to make SprintfCat(), all the wonderful joy of sprintf, with the ability to concatenate with a already existant string like strcat.</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">size_t</span> ath<span class="sy4">::</span><span class="me2">SprintfCat</span><span class="br0">&#40;</span> <span class="kw4">char</span> <span class="sy2">*</span> inoutBuffer, <span class="kw4">const</span> <span class="kw4">size_t</span> inBufferSize, <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> inFormat, ... <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">const</span> <span class="kw4">size_t</span> offset <span class="sy1">=</span> StrLen<span class="br0">&#40;</span> inoutBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">size_t</span> ret <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
	<span class="kw4">va_list</span> arg<span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw3">va_start</span><span class="br0">&#40;</span> arg, inFormat <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#123;</span>
		ret <span class="sy1">=</span> VSprintf<span class="br0">&#40;</span> inoutBuffer <span class="sy2">+</span> offset, inBufferSize <span class="sy2">-</span> offset, inFormat, arg <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
	<span class="kw3">va_end</span><span class="br0">&#40;</span> arg <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">return</span> offset <span class="sy2">+</span> ret<span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>I had already created my own wrappers around strlen() and vsprintf(), the reason being the fault of Microsoft. With their implementation of the standard library, they decided to deprecate non-safe versions complain at you until you either used their own _s variants, or defined a certain preprocessor.</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">size_t</span> ath<span class="sy4">::</span><span class="me2">VSprintf</span><span class="br0">&#40;</span> <span class="kw4">char</span> <span class="sy2">*</span> outBuffer, <span class="kw4">const</span> <span class="kw4">size_t</span> inBufferSize, <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> inFormat, <span class="kw4">va_list</span> inArgs <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
<span class="co2">#ifndef ATH_PLATFORM_WINDOWS</span>
	<span class="kw1">return</span> vsnprintf<span class="br0">&#40;</span> outBuffer, inBufferSize, inFormat, inArgs <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="co2">#else</span>
	<span class="kw1">return</span> vsprintf_s<span class="br0">&#40;</span> outBuffer, inBufferSize, inFormat, inArgs <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="co2">#endif</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">size_t</span> ath<span class="sy4">::</span><span class="me2">StrLen</span><span class="br0">&#40;</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> inString <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw1">return</span> <span class="kw3">strlen</span><span class="br0">&#40;</span> inString <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>While I do prefer to have safe versions, their _s variants are not present in the standard and so it makes the code not portable.</p>
<p>I'm still not quite sure about my naming convention for a variable that is an input and an output. While I do like the <strong>in</strong> or <strong>out</strong> prefix, I don't think <strong>inout</strong> looks right, being slightly too long. I wasn't a fan of <strong>io</strong> either. I have however grown to like the truncation of Athena namespace to ath.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/11/02/sprintfcat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assert Pointer and Delegate</title>
		<link>http://www.lastrayofhope.com/2011/05/09/assert-pointer-and-delegate/</link>
		<comments>http://www.lastrayofhope.com/2011/05/09/assert-pointer-and-delegate/#comments</comments>
		<pubDate>Mon, 09 May 2011 23:35:59 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3373</guid>
		<description><![CDATA[While bored the other day, I made a template class that wraps around a pointer, and will assert when it is null. When declaring your pointer, use the pointer type as the template argument. For example, if you want a pointer to an integer, AssertPointer&#60;int&#62;, or a pointer to an integer pointer, AssertPointer&#60;int *&#62;. For [...]]]></description>
			<content:encoded><![CDATA[<p>While bored the other day, I made a template class that wraps around a pointer, and will assert when it is null.</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="co2">#include &lt;assert.h&gt;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw2">template</span><span class="sy1">&lt;</span><span class="kw2">typename</span> T<span class="sy1">&gt;</span>
<span class="kw4">struct</span> AssertPointer
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	AssertPointer<span class="br0">&#40;</span><span class="br0">&#41;</span>
		<span class="sy4">:</span>
		mPointer<span class="br0">&#40;</span> <span class="kw2">NULL</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	AssertPointer<span class="br0">&#40;</span> T <span class="sy2">*</span> inPointer <span class="br0">&#41;</span>
		<span class="sy4">:</span>
		mPointer<span class="br0">&#40;</span> inPointer <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	operator T <span class="sy2">*</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw3">assert</span><span class="br0">&#40;</span> mPointer <span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="kw1">return</span> mPointer<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	operator T <span class="sy2">*</span> <span class="kw4">const</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>
	<span class="br0">&#123;</span>
		<span class="kw3">assert</span><span class="br0">&#40;</span> mPointer <span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="kw1">return</span> mPointer<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	T <span class="sy2">*</span> operator <span class="sy2">-</span><span class="sy1">&gt;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw3">assert</span><span class="br0">&#40;</span> mPointer <span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="kw1">return</span> mPointer<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	T <span class="sy2">*</span> <span class="kw4">const</span> operator <span class="sy2">-</span><span class="sy1">&gt;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>
	<span class="br0">&#123;</span>
		<span class="kw3">assert</span><span class="br0">&#40;</span> mPointer <span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="kw1">return</span> mPointer<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	T <span class="sy2">*</span> GetPointerValue<span class="br0">&#40;</span><span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> mPointer<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	T <span class="sy2">*</span> <span class="kw4">const</span> GetPointerValue<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> mPointer<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
<span class="kw2">private</span><span class="sy4">:</span>
	<span class="co1">//</span>
	T <span class="sy2">*</span> mPointer<span class="sy4">;</span>
<span class="br0">&#125;</span><span class="sy4">;</span></pre></div></div>
<p>When declaring your pointer, use the pointer type as the template argument. For example, if you want a pointer to an integer, AssertPointer&lt;int&gt;, or a pointer to an integer pointer, AssertPointer&lt;int *&gt;.</p>
<p>For class or structure pointers, the -&gt; operator is overloaded so you can access its members as you would a normal pointer.</p>
<p>If you need access to the raw pointer value without the assert, just use the GetPointerValue() functions.</p>
<p>I may use it to replace the Delegate structure I created for callbacks in Athena.</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="kw2">template</span><span class="sy1">&lt;</span><span class="kw2">typename</span> T, <span class="kw2">class</span> U <span class="sy1">=</span> <span class="kw4">void</span><span class="sy1">&gt;</span>
<span class="kw4">struct</span> Delegate
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	Delegate<span class="br0">&#40;</span><span class="br0">&#41;</span>
		<span class="sy4">:</span>
		func<span class="br0">&#40;</span> <span class="kw2">NULL</span> <span class="br0">&#41;</span>,
		object<span class="br0">&#40;</span> <span class="kw2">NULL</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	Delegate<span class="br0">&#40;</span> T inFunc, U <span class="sy2">*</span> inObject <span class="sy1">=</span> <span class="kw2">NULL</span> <span class="br0">&#41;</span>
		<span class="sy4">:</span>
		func<span class="br0">&#40;</span> inFunc <span class="br0">&#41;</span>,
		object<span class="br0">&#40;</span> inObject <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	T func<span class="sy4">;</span>
	U <span class="sy2">*</span> object<span class="sy4">;</span>
<span class="br0">&#125;</span><span class="sy4">;</span></pre></div></div>
<p>It is used to store a class object and a function pointer within that class in a single structure. I can use the AssertPointer class and pass the class template type to its template argument, giving me better feedback when I get null pointers being used in my delegates.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/05/09/assert-pointer-and-delegate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Floating Point Comparison with Epsilon</title>
		<link>http://www.lastrayofhope.com/2011/04/09/floating-point-comparison-with-epsilon/</link>
		<comments>http://www.lastrayofhope.com/2011/04/09/floating-point-comparison-with-epsilon/#comments</comments>
		<pubDate>Sat, 09 Apr 2011 15:38:46 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3253</guid>
		<description><![CDATA[With my recent update of Athena's math library to use vDSP_* functions for faster math, I decided to add a unit test to all my math functions since I have had occasions where I change it and then it breaks some code elsewhere that I do not notice. I got to writing the unit test [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2011/04/160px-Epsilon_uc_lc.png"><img class="alignleft size-thumbnail wp-image-3254" title="Greek Epsilon" src="http://www.lastrayofhope.com/wp-content/uploads/2011/04/160px-Epsilon_uc_lc-150x107.png" alt="Greek Epsilon" width="150" height="107" /></a>With my recent update of Athena's math library to use vDSP_* functions for faster math, I decided to add a unit test to all my math functions since I have had occasions where I change it and then it breaks some code elsewhere that I do not notice.</p>
<p>I got to writing the unit test for my vector divided by a scalar function when it started to assert. On initial look at the values printed out into the TTY they were both correct, so I extended the precision to 9 and two slightly different values.</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">TTY</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;">Calculated<span class="sy4">:</span> <span class="nu16">4.059524059</span>
&nbsp;
Precomputed<span class="sy4">:</span>  <span class="nu16">4.059523582</span>
&nbsp;
Difference<span class="sy4">:</span> <span class="nu16">0.000000477</span></pre></div></div>
<p>The values were not that far apart, and I know floating point math can differ for the same value so when comparing usually epsilon gets involved since it is the maximum error. Printing out epsilon, I discovered that the value was just slightly bigger than it making the two numbers different enough.</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">TTY</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;">Difference<span class="sy4">:</span> <span class="nu16">0.000000477</span>
&nbsp;
Epsilon<span class="sy4">:</span> <span class="nu16">0.000000119</span></pre></div></div>
<p>I could just have my own custom version of epsilon but it would become incorrect for use with smaller and larger number. Or for this case in the unit test I could just use a custom compare, but I wanted something more reusable, and the first <a title="Comparing Floats" href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm" target="_blank">google result</a> found me a function.</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Relative Floating Point Comparison</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="kw4">bool</span> AlmostEqualRelative2<span class="br0">&#40;</span><span class="kw4">float</span> A, <span class="kw4">float</span> B, <span class="kw4">float</span> maxRelativeError<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> A <span class="sy1">==</span> B <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> <span class="kw2">true</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw4">float</span> relativeError<span class="sy4">;</span>
&nbsp;
	<span class="kw1">if</span><span class="br0">&#40;</span> <span class="kw3">fabs</span><span class="br0">&#40;</span>B<span class="br0">&#41;</span> <span class="sy1">&gt;</span> <span class="kw3">fabs</span><span class="br0">&#40;</span>A<span class="br0">&#41;</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		relativeError <span class="sy1">=</span> <span class="kw3">fabs</span><span class="br0">&#40;</span><span class="br0">&#40;</span>A <span class="sy2">-</span> B<span class="br0">&#41;</span> <span class="sy2">/</span> B<span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
	<span class="kw1">else</span>
	<span class="br0">&#123;</span>
		relativeError <span class="sy1">=</span> <span class="kw3">fabs</span><span class="br0">&#40;</span><span class="br0">&#40;</span>A <span class="sy2">-</span> B<span class="br0">&#41;</span> <span class="sy2">/</span> A<span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw1">if</span><span class="br0">&#40;</span> relativeError <span class="sy1">&lt;=</span> maxRelativeError <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> <span class="kw2">true</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw1">return</span> <span class="kw2">false</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>It is a fairly simple one, rather than just comparing the difference to a larger, it determines the relative error and the user passes in a percentage of how large an error it can be.</p>
<p>Although they recommend on the site that you still do not use it, or now it keeps my unit test working allowing me to continue until I have read the rest of the website.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/04/09/floating-point-comparison-with-epsilon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Syntax Highlighting</title>
		<link>http://www.lastrayofhope.com/2011/03/31/syntax-highlighting/</link>
		<comments>http://www.lastrayofhope.com/2011/03/31/syntax-highlighting/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 19:11:26 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3219</guid>
		<description><![CDATA[The syntax highlight I've been using for a while now, SyntaxHighlighter Evolved, destroyed my post earlier. So I did a bit of research and got a much better one called WP-SynHighlight. Although I haven't figured out how to reedit pasted code yet, its much faster for inserting code since it adds a button to the [...]]]></description>
			<content:encoded><![CDATA[<p>The syntax highlight I've been using for a while now, SyntaxHighlighter Evolved, destroyed my post earlier. So I did a bit of research and got a much better one called <a title="WP-SynHighlight Plugin" href="http://www.viper007bond.com/wordpress-plugins/syntaxhighlighter/" target="_blank">WP-SynHighlight</a>.</p>
<p>Although I haven't figured out how to reedit pasted code yet, its much faster for inserting code since it adds a button to the visual toolbar. However it does mean all my old code snippets need update with the new one... maybe there is a plugin to do that too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/03/31/syntax-highlighting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Xcode4: Adding Local Git Repository to a Xcode3 project</title>
		<link>http://www.lastrayofhope.com/2011/03/25/xcode4-adding-local-git-repository-to-a-xcode3-project/</link>
		<comments>http://www.lastrayofhope.com/2011/03/25/xcode4-adding-local-git-repository-to-a-xcode3-project/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 23:55:26 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3093</guid>
		<description><![CDATA[With the new version of xcode4 it comes with the option to version control source code locally. However when opening old Xcode3 projects, it does not ask you if you want to add a local repository. To add one is simple, open up Terminal and navigate to your xcode project folder. Now enter the following [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-3094" title="Shooter 2 Secret Area" src="http://www.lastrayofhope.com/wp-content/uploads/2011/03/shooter2-300x224.jpg" alt="Shooter 2 Secret Area" width="192" height="143" /></p>
<p>With the new version of xcode4 it comes with the option to version control source code locally.</p>
<p>However when opening old Xcode3 projects, it does not ask you if you want to add a local repository.</p>
<p>To add one is simple, open up <strong>Terminal </strong>and navigate to your xcode project folder.</p>
<p>Now enter the following without quotes:</p>
<p>"git init"</p>
<p>"git add ."</p>
<p>Depending on the size of your project folder, it may take a little time before it finishes. Once done, reboot xcode and your project should now be detected by xcode4 as under source control.</p>
<p>I had to recommit all my files before it showed up properly the organiser.</p>
<ul>
<li>File Menu</li>
<li>Source Control &gt; Commit...</li>
<li>Click Commit</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/03/25/xcode4-adding-local-git-repository-to-a-xcode3-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Re: Using C++ enums like C# enums</title>
		<link>http://www.lastrayofhope.com/2011/03/23/re-using-c-enums-like-c-enums/</link>
		<comments>http://www.lastrayofhope.com/2011/03/23/re-using-c-enums-like-c-enums/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 00:22:19 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Csharp]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3107</guid>
		<description><![CDATA[I found out that only the Microsoft allows you to forward declare enums, but standard compliant compilers do not allow it since its size is not yet known. I could use an int rather than the enum variable to get around the problem, but I prefer to be able to see what the enum is set to, [...]]]></description>
			<content:encoded><![CDATA[<p>I found out that only the Microsoft allows you to forward declare enums, but standard compliant compilers do not allow it since its size is not yet known.</p>
<p>I could use an int rather than the enum variable to get around the problem, but I prefer to be able to see what the enum is set to, so here is the macro to make the code work in non-Microsoft compilers.</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co2">#define ATHENA_START_CSHARP_ENUM( x )	 \
struct x	 			 \
{	 			 \
public:				 \
	enum EnumType</span>
&nbsp;
<span class="co2">#define ATHENA_END_CSHARP_ENUM( x )	 \
	;			 \
				 \
 	inline x()		 \
	{			 \
	}			 \
					 \
	template&lt;typename T&gt;	 \
	inline x( const T &amp; inValue )	 \
		:	 \
		value( (EnumType)inValue )	 \
	{	 \
	}	 \
		\
	inline x( const EnumType inValue )	 \
		:	 \
		value( inValue )	 \
	{	 \
	}	 \
		\
	inline void operator = ( const EnumType inValue )	 \
	{	 \
		value = inValue;	 \
	}	 \
\
	inline EnumType operator ~ () const	 \
	{	 \
		return (EnumType)~value;	 \
	}	 \
\
	inline EnumType operator &amp; ( const EnumType inValue ) const	\
	{	 \
		return (EnumType)( value &amp; inValue );	 \
	}	 \
\
	inline operator EnumType () const	 \
	{	 \
		return value;	 \
	}	 \
\
private:	 \
	EnumType value;	 \
}</span></pre></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/03/23/re-using-c-enums-like-c-enums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>POSIX Thread: Setting Priority and Stack Size</title>
		<link>http://www.lastrayofhope.com/2011/03/17/posix-thead-setting-priority-and-stack-size/</link>
		<comments>http://www.lastrayofhope.com/2011/03/17/posix-thead-setting-priority-and-stack-size/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 12:31:00 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[pthread]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3165</guid>
		<description><![CDATA[The POSIX Thread library, also known as pthread, has always annoyed me with trying to set thread priority. Some documentation for pthread_attr_setschedparam() say use PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY, however they never seem to exist. Some sites say use 0 and 31 for them respectively for min and max. Instead I found a function that will retrieve the min [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2011/03/images-1.jpeg"><img class="alignleft size-full wp-image-3170" title="Legendary Thread" src="http://www.lastrayofhope.com/wp-content/uploads/2011/03/images-1.jpeg" alt="Legendary Thread" width="180" height="180" /></a>The POSIX Thread library, also known as pthread, has always annoyed me with trying to set thread priority.</p>
<p>Some documentation for pthread_attr_setschedparam() say use PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY, however they never seem to exist.</p>
<p>Some sites say use 0 and 31 for them respectively for min and max.</p>
<p>Instead I found a function that will retrieve the min and max priority for a given scheduling policy, allowing you to set the thread priority before it is created.</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="kw4">void</span> SetAttributeMaxPriority<span class="br0">&#40;</span> pthread_attr_t <span class="sy3">&amp;</span> inThreadAttr <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">const</span> <span class="kw4">int</span> kPolicy <span class="sy1">=</span> SCHED_RR<span class="sy4">;</span>
	<span class="kw4">int</span> minPriority <span class="sy1">=</span> sched_get_priority_min<span class="br0">&#40;</span> kPolicy <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">int</span> maxPriority <span class="sy1">=</span> sched_get_priority_max<span class="br0">&#40;</span> kPolicy <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	sched_param schedParam<span class="sy4">;</span>
	schedParam.<span class="me1">sched_priority</span> <span class="sy1">=</span> maxPriority<span class="sy4">;</span>
	pthread_attr_setschedparam<span class="br0">&#40;</span> <span class="sy3">&amp;</span>inThreadAttr, <span class="sy3">&amp;</span>schedParam <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	pthread_attr_setschedpolicy<span class="br0">&#40;</span> <span class="sy3">&amp;</span>inThreadAttr, kPolicy <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>The stack size also needs to be a multiple of the page size, while this is not really an issue unless you go really low or don't use a power of 2 number, I added some code to reinforce it.</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="kw4">void</span> SetStackSize<span class="br0">&#40;</span> pthread_attr_t <span class="sy3">&amp;</span> inThreadAttr, <span class="kw4">const</span> <span class="kw4">int</span> inStackSize <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw4">int</span> stackSize <span class="sy1">=</span> <span class="br0">&#40;</span> inStackSize <span class="sy1">&lt;</span> PTHREAD_STACK_MIN <span class="br0">&#41;</span> <span class="sy4">?</span> PTHREAD_STACK_MIN <span class="sy4">:</span> inStackSize<span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw4">int</span> pageSize <span class="sy1">=</span> sysconf<span class="br0">&#40;</span> _SC_PAGESIZE <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> pageSize <span class="sy1">==</span> <span class="sy2">-</span>1 <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		pageSize <span class="sy1">=</span> PTHREAD_STACK_MIN<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw4">int</span> multipleRemainder <span class="sy1">=</span> mParams.<span class="me1">stackSize</span> <span class="sy2">%</span> pageSize<span class="sy4">;</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> multipleRemainder <span class="sy3">!</span><span class="sy1">=</span> 0 <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		stackSize <span class="sy2">+</span><span class="sy1">=</span> <span class="br0">&#40;</span> pageSize <span class="sy2">-</span> multipleRemainder <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	pthread_attr_setstacksize<span class="br0">&#40;</span> <span class="sy3">&amp;</span>inThreadAttr, stackSize <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>I wasn't sure if _SC_PAGESIZE would work on every system, so just incase I made page size equal PTHREAD_STACK_MIN if an error was returned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/03/17/posix-thead-setting-priority-and-stack-size/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C/C++ Operator Synonyms</title>
		<link>http://www.lastrayofhope.com/2011/03/16/cc-operator-synonyms/</link>
		<comments>http://www.lastrayofhope.com/2011/03/16/cc-operator-synonyms/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 12:55:34 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3175</guid>
		<description><![CDATA[I just discovered that there are defines as part of the standard that can be used instead of some operators. For example instead of this: You can do this: There also exists or, xor, and bitwise versions, bitand, bitor. For more, read the article on the Wikipedia.]]></description>
			<content:encoded><![CDATA[<p>I just discovered that there are defines as part of the standard that can be used instead of some operators. For example instead of this:</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="kw1">if</span><span class="br0">&#40;</span> something <span class="sy3">&amp;&amp;</span> somethingelse <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>You can do this:</p>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="kw1">if</span><span class="br0">&#40;</span> something and somethingelse <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>There also exists or, xor, and bitwise versions, bitand, bitor.<br />
For more, read the <a title="Operators in C and C++" href="http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#C.2B.2B_operator_synonyms" target="_blank">article on the Wikipedia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/03/16/cc-operator-synonyms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS Semaphore Problems</title>
		<link>http://www.lastrayofhope.com/2011/03/15/ios-semaphore-problems/</link>
		<comments>http://www.lastrayofhope.com/2011/03/15/ios-semaphore-problems/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 11:59:55 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[semaphore]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3161</guid>
		<description><![CDATA[Originally for my semaphore I used an Apple specific one that used variable type semaphore_t. But recently I decided to use a more generic unix one sem_t but started having trouble since it failed to create one. After a bit of changing the code, I found the call to sem_init() always failed, returning an error [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2011/03/1194993370744601105semaphore_november.svg_.med_.png"><img class="alignleft size-full wp-image-3177" title="Semaphore Flag" src="http://www.lastrayofhope.com/wp-content/uploads/2011/03/1194993370744601105semaphore_november.svg_.med_.png" alt="Semaphore Flag" width="180" height="140" /></a>Originally for my semaphore I used an Apple specific one that used variable type semaphore_t.</p>
<p>But recently I decided to use a more generic unix one sem_t but started having trouble since it failed to create one.</p>
<p>After a bit of changing the code, I found the call to sem_init() always failed, returning an error code on iOS.</p>
<p>The only way I could get it to work was to use sem_open() with the O_CREAT flag.</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co2">#include &lt;errno.h&gt;</span>
<span class="co2">#include &lt;fcntl.h&gt;</span>
<span class="co2">#include &lt;stdio.h&gt;</span>
<span class="co2">#include &lt;assert.h&gt;</span>
&nbsp;
<span class="co1">//</span>
sem_t <span class="sy2">*</span> CreateSemaphore<span class="br0">&#40;</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> inName, <span class="kw4">const</span> <span class="kw4">int</span> inStartingCount <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	sem_t <span class="sy2">*</span> semaphore <span class="sy1">=</span> sem_open<span class="br0">&#40;</span> inName, O_CREAT, 0644, inStartingCount <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> semaphore <span class="sy1">==</span> SEM_FAILED <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">switch</span><span class="br0">&#40;</span> <span class="kw2">errno</span> <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
		<span class="kw1">case</span> EEXIST<span class="sy4">:</span>
			<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Semaphore with name '%s' already exists.<span class="es1">\n</span>&quot;</span>, inName <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">default</span><span class="sy4">:</span>
			<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Unhandled error: %d.<span class="es1">\n</span>&quot;</span>, <span class="kw2">errno</span> <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
		<span class="br0">&#125;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw3">assert</span><span class="br0">&#40;</span><span class="kw2">false</span><span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="kw1">return</span> SEM_FAILED<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">return</span> semaphore<span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>This code however is flawed in that if the semaphore already exists it will use that one instead. So I tried to BitOR the flag O_EXCL to O_CREAT so it asserted if the semaphore existed already.</p>
<p>That worked, however now whenever the app didn't shutdown correctly, the semaphore would not be destroyed and running again would fail since it already existed. I have not yet found a way around this.</p>
<p>Finally to destroy the semaphore, you need to close it with sem_close() instead of sem_destroy().</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="kw4">bool</span> DestroySemaphore<span class="br0">&#40;</span> sem_t <span class="sy2">*</span> inSemaphore <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">int</span> retErr <span class="sy1">=</span> sem_close<span class="br0">&#40;</span> inSemaphore <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> retErr <span class="sy1">==</span> <span class="sy2">-</span><span class="nu0">1</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">//</span>
		<span class="kw1">switch</span><span class="br0">&#40;</span> <span class="kw2">errno</span> <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
		<span class="kw1">case</span> EINVAL<span class="sy4">:</span>
			<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;inSemaphore is not a valid sem_t object.&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">default</span><span class="sy4">:</span>
			<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Unhandled error: %d.<span class="es1">\n</span>&quot;</span>, <span class="kw2">errno</span> <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
		<span class="br0">&#125;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw3">assert</span><span class="br0">&#40;</span><span class="kw2">false</span><span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="kw1">return</span> <span class="kw2">false</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">return</span> <span class="kw2">true</span><span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="kw4">void</span> SignalSemaphore<span class="br0">&#40;</span> sem_t <span class="sy2">*</span> inSemaphore <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	sem_post<span class="br0">&#40;</span> inSemaphore <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="kw4">void</span> WaitSemaphore<span class="br0">&#40;</span> sem_t <span class="sy2">*</span> inSemaphore <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	sem_wait<span class="br0">&#40;</span> inSemaphore <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="kw4">bool</span> TryWaitSemaphore<span class="br0">&#40;</span> sem_t <span class="sy2">*</span> inSemaphore <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">int</span> retErr <span class="sy1">=</span> sem_trywait<span class="br0">&#40;</span> inSemaphore <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> retErr <span class="sy1">==</span> <span class="sy2">-</span><span class="nu0">1</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">//</span>
		<span class="kw1">if</span><span class="br0">&#40;</span> <span class="kw2">errno</span> <span class="sy3">!</span><span class="sy1">=</span> EAGAIN <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Unhandled error: %d<span class="es1">\n</span>&quot;</span>, <span class="kw2">errno</span> <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="kw3">assert</span><span class="br0">&#40;</span> <span class="kw2">false</span> <span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="br0">&#125;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw1">return</span> <span class="kw2">false</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">return</span> <span class="kw2">true</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/03/15/ios-semaphore-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

