<?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>SIGTTOU &#187; C/C++</title>
	<atom:link href="http://sigttou.com/category/c/feed" rel="self" type="application/rss+xml" />
	<link>http://sigttou.com</link>
	<description>Just another background process...</description>
	<lastBuildDate>Sun, 04 Jul 2010 02:08:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Include dependencies</title>
		<link>http://sigttou.com/include-dependencies</link>
		<comments>http://sigttou.com/include-dependencies#comments</comments>
		<pubDate>Tue, 22 Dec 2009 06:56:46 +0000</pubDate>
		<dc:creator>Bob Somers</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://sigttou.com/?p=139</guid>
		<description><![CDATA[Generally when I write software, I try to keep things relatively well organized. Inevitably, however, things are going to get a bit messy, especially if you&#8217;re working on a large, disorganized codebase that you didn&#8217;t write to begin with&#8230; say, &#8230; <a href="http://sigttou.com/include-dependencies">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Generally when I write software, I try to keep things relatively well organized. Inevitably, however, things are going to get a bit messy, especially if you&#8217;re working on a large, disorganized codebase that you didn&#8217;t write to begin with&#8230; say, oh&#8230; something like the <a href="http://developer.valvesoftware.com">Source SDK</a>.</p>
<p>Frequently you have some class which is composed inside another class, but occasionally needs to access the class it&#8217;s composed inside of. Basically, the classes are composed inside each other, though the abstraction really only makes sense in one direction. Confused yet?</p>
<p>In this example, we&#8217;ll use a Refrigerator class which stores inside it an instance of a Cheese class. Why cheese, you ask? Because cheese is delicious. Also, our refrigerator is from the future and can slice and serve cheese just like the built in ice maker and water dispenser. It&#8217;s a pretty sweet fridge.</p>
<p>Now, we were all taught to keep our <code class="prettyprint">#include</code>s in our header files, not the implementation files, so like good little programmers we construct our classes like so:</p>
<p><strong>refrigerator.h</strong></p>
<pre class="prettyprint">#include "cheese.h"

class Refrigerator {
private:
    Cheese *pCheese;
    int temp = 35;

public:
    void ServeCheese();
    int GetTemp();
};</pre>
<p><strong>refrigerator.cpp</strong></p>
<pre class="prettyprint">#include "refrigerator.h"

void Refrigerator::ServeCheese() {
    printf("Now dispensing %s cheese!\n", pCheese->GetFlavor());
}

int Refrigerator::GetTemp() {
    return temp;
}</pre>
<p><strong>cheese.h</strong></p>
<pre class="prettyprint">class Cheese {
private:
    Refrigerator *pFridge;
public:
    char *GetFlavor();
    void CheckTemp();
    void BeginMolding();
};</pre>
<p><strong>cheese.cpp</strong></p>
<pre class="prettyprint">#include "cheese.h"

void Cheese::CheckTemp() {
    if (pFridge->GetTemp() > 45) {
        BeginMolding();
    }
}

char *Cheese::GetFlavor() {
    return "cheddar";
}
</pre>
<p>I&#8217;ve left out the constructors in this example for brevity, but let&#8217;s assume that they get the pointers set up correctly so that our instance of the Refrigerator class has a correct pointer to an instance of the Cheese class and vice versa.</p>
<p>Now, at this point you may be screaming that this needs to be refactored and reorganized. Yes, it probably does. But there are many instances where you simply can&#8217;t, and in fact the abstraction really only makes sense one way. The fridge has cheese in it, but the cheese certainly doesn&#8217;t have a fridge in it. We just need that pesky reference around so we can check the temperature of the fridge every so often.</p>
<p><em>(Yes, I am aware that the fridge could push it&#8217;s temperature down to all the items in it, ala the <a href="http://en.wikipedia.org/wiki/Observer_pattern">Observer Pattern</a>. Yes I am aware that would be a better solution. But this is a contrived example anyway, so stick with me here.)</em></p>
<p>Now, the code given above doesn&#8217;t compile, because the Cheese class has no idea what the heck this Refrigerator class is, so we either need to include it or forward declare it. If we try to do this:</p>
<p><strong>cheese.h</strong></p>
<pre class="prettyprint">#include "refrigerator.h"</pre>
<p>our compiler (more specifically, the preprocessor) is going to get very angry at us, depending on which order it decides to compile refrigerator and cheese. The solution, is a forward delcaration:</p>
<p><strong>cheese.h</strong></p>
<pre class="prettyprint">class Refrigerator;

class Cheese {
    // ...etc...
};</pre>
<p>Basically what this does is tell the compiler, &#8220;Hey! There&#8217;s this class called Refrigerator that I might talk about, so here&#8217;s an empty declaration of it!&#8221;</p>
<p>The problem, though, is that this is rather limiting. Within the Cheese class, we can declare pointers to Refrigerator class, no problem. Pointers are of fixed size, so the compiler doesn&#8217;t much what care what it&#8217;s a pointer <strong><em>to</em></strong>, since it knows how much memory it needs to hold a pointer to it. When we try to access members of the class, though, like properties or methods, it falls apart because as far as the compiler knows, the class is empty. After all, we told it the Refrigerator class didn&#8217;t have anything in it.</p>
<p>So if we can&#8217;t <code class="prettyprint">#include</code> it and forward declaring it doesn&#8217;t give us what we want, what can we do?</p>
<p>Well, we can do <strong>both</strong>. Kind of.</p>
<p>The solution is to forward declare in your header file, and <code>#include</code> in your implementation file. This will avoid the preprocessor headaches of of the chicken and egg <code class="prettyprint">#include</code>, while allowing us to access the members of forward declared class in the implementation. In other words, here&#8217;s the fix:</p>
<p><strong>cheese.h</strong></p>
<pre class="prettyprint">class Refrigerator;

class Cheese {
    ...
};</pre>
<p><strong>cheese.cpp</strong></p>
<pre class="prettyprint">#include "cheese.h"
#include "refrigerator.h"
</pre>
<p>Again, it goes without saying, the better solution is to refactor or rearchitect your code if you can. These kind of hacks can get really out of hand and are usually a good <a href="http://en.wikipedia.org/wiki/Code_smell">code smell</a> that something needs to be fixed at a deeper level. However, if you&#8217;re working on a large codebase that you can&#8217;t change, this can help out a lot.</p>
]]></content:encoded>
			<wfw:commentRss>http://sigttou.com/include-dependencies/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Removing entities in the Source SDK</title>
		<link>http://sigttou.com/removing-entities</link>
		<comments>http://sigttou.com/removing-entities#comments</comments>
		<pubDate>Sat, 24 Oct 2009 23:54:49 +0000</pubDate>
		<dc:creator>Bob Somers</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Gaming]]></category>

		<guid isPermaLink="false">http://sigttou.com/?p=133</guid>
		<description><![CDATA[I haven&#8217;t written for a while, mainly because I&#8217;ve been busy with classes and studying for the GRE for my grad school applications, but here&#8217;s a quick tip for those of you meddling around with the Source SDK. It&#8217;s well &#8230; <a href="http://sigttou.com/removing-entities">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t written for a while, mainly because I&#8217;ve been busy with classes and studying for the GRE for my grad school applications, but here&#8217;s a quick tip for those of you meddling around with the Source SDK.</p>
<p>It&#8217;s well documented on how you go about spawning entities, but I couldn&#8217;t find a good place explaining how to <em>remove</em> spawned entities through code.</p>
<p><strong>Don&#8217;t</strong> try meddling with the global entity list (<code class="prettyprint">gEntList</code>) or calling its <code class="prettyprint">RemoveEntity()</code> method. It doesn&#8217;t do what you want.</p>
<p><strong>Instead</strong>, used one of the super-handy <code class="prettyprint">UTIL_*</code> functions. Given a pointer to the entity you want to remove, simply use:</p>
<p><code class="prettyprint">UTIL_Remove(pEntity);</code></p>
<p>Poof. Entity gone. Remember, entities are created and destroyed on the <strong>server side only.</strong> The server will automatically broadcast any changes to the entity list to its connected clients for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://sigttou.com/removing-entities/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
