<?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>Charles Solar &#187; Programming</title>
	<atom:link href="http://charlessolar.com/post/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://charlessolar.com</link>
	<description></description>
	<lastBuildDate>Fri, 29 Apr 2011 04:38:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Project Management 101</title>
		<link>http://charlessolar.com/post/237</link>
		<comments>http://charlessolar.com/post/237#comments</comments>
		<pubDate>Fri, 29 Apr 2011 04:36:17 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Management]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[advice]]></category>
		<category><![CDATA[dry]]></category>
		<category><![CDATA[gmake]]></category>
		<category><![CDATA[kiss]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[project management]]></category>
		<category><![CDATA[series]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=237</guid>
		<description><![CDATA[When working on projects its helpful to know in which direction you are headed.  Its good to place goals, do a bit of planning, and get things right to save time in the future.  Unfortunately however, there is little to no information online on how to successfully manage and keep a big software project going.  [...]]]></description>
			<content:encoded><![CDATA[<p>When working on projects its helpful to know in which direction you are headed.  Its good to place goals, do a bit of planning, and get things right to save time in the future.  Unfortunately however, there is little to no information online on how to successfully manage and keep a big software project going.  Sure there are management books on how to motivate people, or how to lead coworkers, or even how to plan every step of the project with careful diagrams, flow charts, and process graphs.  As far as I could find a year ago there was nothing on how to get down and dirty and manage things like a build process, or an automatic unit test suite, or how to deal with different platforms in your build system.  For the past year I have been working on a big project that required all these things and whenever I fell into a trap, or discovered a new idea, or came up with a new operation I wrote down little snip-its to be eventually published in this article.  I hope others looking for information on how to manage a project effectively and efficiently will find some of these ideas very helpful.</p>
<p><span id="more-237"></span>The very first obstacle that any project can face is most certainly compiling on any platform other than the &#8220;primary&#8221; operating system.  Putting developers outside their development comfort zone can breed resentment for the new platform.  This resentment manifests itself into very sub-par code for the platform in question and only breeds more resentment as the project progresses and developers start complaining about how the other platform &#8220;just is not working.&#8221;  I see this all the time when developers start using the Linux tool chain when used to Visual Studio, and when a Linux guru tries to use platforms like Windows.  What eventually ends up happening is a bunch of smelly code gets written for the platform no one likes and is put together using a terrible build system that only further breeds discomfort and anger against the offending OS.  This reflects in the final product where users of the accursed OS are shunned for their complaints, new feature requests are not taken seriously, and one by one new developers that were brought in to work on the platform leave the company because even they get shot down when offering advice to the &#8220;project gurus&#8221; who hate the operating system they do not understand.</p>
<p>Fortunately scenarios such as this can be quickly remedied before they even start.  As a general rule, never put someone on a project unless they understand all the pieces involved.  When a Linux developer is put in charge of a Windows build, their first instinct is going to want to build a set of recursive nmake files.  Nmake is sort of like a command line make tool for Windows for those unaware.  Development of the tool was canceled after Visual Studio 2005, but it still works and some Linux developers still try and use the woefully inadequate tool as a replacement to gnu make.  As the Linux developer learns about the limitations of nmake he starts building up his resentment for the platform in general.  The final product being a nmake build system that is full of quirky &#8220;hacks&#8221; to work around nmakes limitations and emulate some of the functionality of gnu make.  For example, with gnu make you can setup automatic dependency scanning so if a developer modifies a header file all .c and .cpp files that depend on it are recompiled.  If the Linux developer does not find similar functionality with nmake, he might just decide to setup nmake  to delete all object files every run and recompile the whole project each build.  This in turn causes other developers to not want to use the Windows compiler because Linux compiles in half the time and they can make quick changes without waiting hours for a new build.  This in turn causes development on Windows to slow to a trickle and the platform falls behind on enhancement requests as Windows developers start leaving for better prospects.  And thus the downward spiral continues.</p>
<p>In a different scenario lets put a Windows developer in charge of the Windows build.  He is most likely fluent in the best tool for compiling on Windows, Visual Studio.  He knows that nmake is a disaster and instead builds easy to maintain Visual Studio solutions which allows other developers to open the project and compile effortlessly.  He then utilizes Microsoft&#8217;s MSBuild tool to setup automated build and packaging processes and the project continues moving forward drawing in the best Windows development talent.</p>
<p>Sometimes due to lack of budget or development talent it is not always possible to put the best talent on the right job.  In my recent project I had to build both a Linux and Windows build system.  At the time I knew about Linux make and even maintained a couple make files, but other than that I had no experience writing one.  At these times its important to stick to the core principles of software development.  <a href="http://en.wikipedia.org/wiki/KISS_principle">KISS</a> ( Keep It Simple and Stupid ) and <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> ( Don&#8217;t Repeat Yourself ).  Using these two techniques developers on an unfamiliar system can sacrifice time for quality.  Building the gnu make system took me longer than someone who knew Linux make backwards and forwards, but the end result is about the same.  As an added benefit, the developer gains insight into how the new system works and in the future will not take so long to create new make files.</p>
<p>Learning how to effectively apply KISS and DRY is a skill worth developing as it takes quite a few mistakes and failures to get right.  The important questions to ask yourself while working is &#8220;Is this as simple and concise as it could be?&#8221; and &#8220;Is this same concept used more than once in my project?&#8221;  Google is most definitely your friend as you struggle with new software or design.  Basically, how I go about using a new system is imagine what I want it to do, and Google for others who got it to work that way.  In some cases people will instead tell you that your idea is dumb and is not what the software was designed to do.  Which may make poorly trained developers insert a hack or two into the system to make it work like he wants.  But this is the important point!  If you are using KISS and DRY you should <strong>never</strong> allow any hacks of any kind into your work.  Work with the software, do not make the software work for you.  The world is a much nicer place when software is doing the job it was designed to do as it was designed to do it.  <strong>Do not</strong> make software work how your warped perspective says it should work.  If you take nothing else from this article take that bit of advice.</p>
<p>When developing large systems it sometimes becomes apparent that some changes require large amounts of copy and pasting.  In my project I had 20-30 individual build projects with 20-30 individual makefiles and visual studio projects.  At one point each makefile held a lot of duplicate information.  If I wanted to add a CFLAG to the whole project I would have to edit each makefile or if I was lucky, have sed do it for me.  The whole system was one big violation of the DRY principle, and I was wasting valuable time writing sed scripts rather than doing real work.  Recognizing the problem I quickly redesigned the system to correctly leverage the capabilities of the makefile include directive.  Ideally when working on a project that contains many individual builds its advantageous to keep the actual build logic out of the makefiles.  The only thing needed for an individual build should be a list of source files and any extra flags the build might need.  By using the include directive you can separate the build and link implicit rules into different makefiles that your project makefiles include and use.  Changes to the global project are then applied to a single makefile and are included by all projects.</p>
<p>Same goes for Visual Studio projects.  In a solution containing 20-30 project files, changing something as simple as the release flags takes a long time.  Thankfully Visual Studio allows you to use property sheets which are basically a collection of properties that can be applied to many projects.  I use them for specifying all properties in the project except build type and source files.</p>
<p>I have uploaded the current version of my makefiles and property sheet to bitbucket, feel free to <a href="https://bitbucket.org/redcomet/build/src">check them out</a>.</p>
<p>Moving on to compilers and source files.  I think it is more important to pay attention to KISS and DRY when writing code then when writing a build system.  Build systems can be changed, code changes can get complicated, especially if you care about backwards compatibility.  When writing code start simple and get more complex.  Think of an easy representation to shoot for and adjust your expectations as the requirements get more specific.  Pay attention to things like namespaces, type names, capitalization, and other minor style issues.  Do not throw all your types into a single namespace as that gets confusing when you start dealing with fields of 30 or more types.  Pick a standard coding style and stick to it.  New users may have to get used to it, but the last thing you want is a code base covered in five different standards.  This obviously breaks programmer flow and causes severe loss of likability as users cannot adapt to your project and code based on their expectations rather then reading straight from the doc.</p>
<p>My next post will contain tips specifically for coding.  Related to GCC, Pimpls, smart pointers, and unit testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/237/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Static Object Initialization in a Static Library</title>
		<link>http://charlessolar.com/post/230</link>
		<comments>http://charlessolar.com/post/230#comments</comments>
		<pubDate>Fri, 18 Dec 2009 03:34:23 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Help]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[pluggable factories]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=230</guid>
		<description><![CDATA[I ran into an interesting problem the other day that I think is worth sharing.  Fans of the pluggable factory design pattern, inversion of control, etc will be interested to know that these methods do not work very well when compiled into static libraries.  Sounds obvious but what may not be so obvious is why. [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into an interesting problem the other day that I think is worth sharing.  Fans of the pluggable factory design pattern, inversion of control, etc will be interested to know that these methods do not work very well when compiled into static libraries.  Sounds obvious but what may not be so obvious is why.</p>
<p>I stumbled onto this problem when I changed one of my exe projects to a static library so I could setup two exe&#8217;s, one for unit tests, the other for releasing.  (Which by the way is a wonderful way to unit test an exe project)  Most of my projects these days use some form of inversion of control so I had static variables creating maker classes which would register themselves with the factory.  As explained in one of my previous articles <a href="http://www.mrlwork.com/post/50">here</a>.</p>
<p>When I changed my project to a static library and linked it to a separate exe project my maker classes stopped registering themselves.  Why does this happen?  Well after a bit of a research I found out that when you link in a static library, the linker only links code into your project that you actually use.  All extra code is left uninitialized and put in a corner to sit while the big boss code runs.  A very interesting side effect of static linking.</p>
<p>Anyway, to fix this problem in visual studio you need to set &#8220;Use Library Dependency Inputs&#8221; in Linker options on your exe project.  This will make the linker link in all the object files themselves, instead of selectively linking them as needed.  I am researching some way to do this per library, because as of right now if you set this it will link ALL libraries like that.</p>
<p>In linux it would seem the option is -z allextract, but I have not tested this one.</p>
<p>If you need your static objects from a static library to be initiated like normal, this should do the trick.</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/230/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft SQL User Defined Types</title>
		<link>http://charlessolar.com/post/170</link>
		<comments>http://charlessolar.com/post/170#comments</comments>
		<pubDate>Fri, 21 Aug 2009 16:25:29 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Help]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[msdn]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=170</guid>
		<description><![CDATA[User defined types in Microsoft SQL are really handy.  Using these objects you can construct queries like select duration.minutes from cdrs; select duration.seconds from cdrs; You can also program them to read weird string inputs that other data types can not handle.  Recently I wrote a custom data type to read a duration field of [...]]]></description>
			<content:encoded><![CDATA[<p>User defined types in Microsoft SQL are really handy.  Using these objects you can construct queries like</p>
<pre class="sql">select duration.minutes from cdrs;
select duration.seconds from cdrs;</pre>
<p>You can also program them to read weird string inputs that other data types can not handle.  Recently I wrote a custom data type to read a duration field of the format minutes:seconds.tenths; so standard input went something like &#8217;002:34.2.&#8217;</p>
<p>Writing a custom data type is actually very simple.  You just open a new sql data type project in Visual Studio and VS will integrate nicely with your sql server; even going so far as to upload the data type for you and run tests.  There are numerous tutorials online for programming a data type however so I will not cover that aspect.</p>
<p>Like I said I recently created a data type for my data parsing.  I put it into my table but soon found that assigning my specific data processing user permissions to the object was harder than you would expect.  I kept getting the error</p>
<pre class="sql">
[Microsoft][ODBC SQL Server Driver][SQL Server]EXECUTE permission denied on object 'data_type', database 'db', schema 'dbo'. (SQL-42000)
</pre>
<p>Trying</p>
<pre class="sql">
grant execute on [data_type] to cdr;
</pre>
<p>returned &#8216;object not found.&#8217;  Ms sql&#8217;s security assignment tool did not help either, it never did &#8216;find&#8217; the object I created.</p>
<p>I eventually found my way to <a href="http://msdn.microsoft.com/en-us/library/bb522526.aspx">this msdn page</a> which at least revealed the secret missing keyword.  To set the permission on the object use this query</p>
<pre class="sql">
grant execute on TYPE::[dbo].[data_type] to cdr;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/170/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternative to Unique Character Names in MMOs</title>
		<link>http://charlessolar.com/post/137</link>
		<comments>http://charlessolar.com/post/137#comments</comments>
		<pubDate>Mon, 17 Aug 2009 17:11:14 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[mmo]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=137</guid>
		<description><![CDATA[Unique character names in MMOs have long been a pet peeve of mine.  I am sure most of you have experienced the frustration when your favorite character name ends up taken by some else, and if you are like me it might take as many as 20 tries to get an available name.  Well, here [...]]]></description>
			<content:encoded><![CDATA[<p>Unique character names in MMOs have long been a pet peeve of mine.  I am sure most of you have experienced the frustration when your favorite character name ends up taken by some else, and if you are like me it might take as many as 20 tries to get an available name.  Well, here is a question for all you MMO programmers, why the hell are you still using character names as a unique identifier?  There are so many better options to identify players in the world, every name does not need to be unique.  In fact I frequently find that running across players entitled &#8216;FancyPants12&#8242; or &#8216;Moooo4me&#8217; does more damage to game play than a simple change would.  When I am in the process of being adsorbed into an MMO through very nicely executed immersion techniques, stumbling onto these players breaks flow, <a title="ScreenShot_071107_195300" href="http://www.flickr.com/photos/9248805@N04/3760387724/" target="_blank"><img class="alignleft" style="border: 0pt none; margin: 10px;" src="http://farm4.static.flickr.com/3436/3760387724_e785982fb8_m.jpg" border="0" alt="ScreenShot_071107_195300" width="240" height="160" /></a>disrupts my concentration and I find it harder to follow the story line (or even care about it).</p>
<p><small><a title="Attribution-ShareAlike License" href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank"><img class="alignleft" style="border: 0pt none;" src="../wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="Idhren" href="http://www.flickr.com/photos/9248805@N04/3760387724/" target="_blank">Idhren</a></small></p>
<p><small></small>I bring this up because this last weekend I started playing Aion.  A very nice game that I have high hopes for, but its probably the first MMO that I am actually interested in a bit of the lore; I even went so far as to read some of the important quest text.  Its shocking yes, but they really did do a good job making the player feel like they are not just a drop of water in an ocean; which is worthy of recognition in itself.  Unfortunately, Aion suffers from the unique name dilemma so while I am trying to rescue the damsel from a horde of black winged bandits I am constantly running into &#8216;XXSlayerXX&#8217; or &#8216;RoGeGardian&#8217; and that is just terrible from an immersion perspective.</p>
<p>But what can be done about it?  After all, a lot depends on character names.  PMs, brokers, mail, friend lists, etc etc.  Obviously a new system has to enhance game play, not restrict it.  I propose a system where each character is assigned a unique number, not a name, which can be used for all these means of communication with just a little extra effort on the programmers side.</p>
<p><span id="more-137"></span></p>
<p>To illustrate my concept I will run through a few normal MMO actions and highlight how this idea works in comparison to the character name.</p>
<p><strong>Auctions</strong></p>
<p>This is the easiest, when a character posts an item you simply register the item with the character id instead of the character name.  All transactions, searches, bids, use the character id instead of the name.  A really simple swap out in this instance.</p>
<p><strong>PMs</strong></p>
<p>Probably the first problem that would come to your mind.  Obviously having to type /tell 00283023 to talk to a player would be murder on your player base.  PMs can be handled nicely, it just takes a little more work and polish out of the box.  For instance, to talk to someone who made a comment in the chat window, just click on their name and reply.  The reply would pull the character number from the chat window so the player would see the character name but actually be replying using his number.  Now, if you want to type up a pm to someone you met in an instance but did not befriend, things get a little more complicated.  Lets say for the sake of argument that you remember his character name and you wish to send him a message.</p>
<p>There are two solutions I can think of off the top of my head.  First, typing /tell William will bring up a list of all &#8216;Williams&#8217; in the world sorted by last interaction.  Meaning if he met William during a live event the previous night, that William would be listed above a William he met out in the plains last week.  For this to work players would need to keep record of the players they meet and timestamps, not impossible but not very practical.</p>
<p>My next solution would be to improve the friend system to be more intelligent.  Instead of storing a list of people the player manually types in, it could store a list of acquaintances that the player has met over time.  It could list people he met, ordered by zones, time spent with the person, number of interactions with the person, etc.  This way the player would build a network of contacts in the world without having to manually type in their names or numbers.  These two systems not only improve the experience for the player, but also improve immersion since this is normally how people relate to each other.  I can think of five different &#8216;Chris&#8217;s I know, but when attempting to address one I use the social path that has been created between us.  My social network determines the means to communicate.</p>
<p><strong>Mail</strong></p>
<p>Another tricky problem which can be gracefully solved by an expansion on the friend list concept I wrote about above.  When the player tries to send mail he can browse his social network for a contact to send mail to.  This is a good time to mention that chatting with another player would make a new network with that person.  This includes someone chatting in general, city, trade, whatever.  To model those interactions a network of recent chat messages would be displayed for the player to pick out the appropriate recipient.</p>
<p><strong>Groups / Raids</strong></p>
<p>Most groups and raids are formed by pm messages or chat window invites.  Since we have already established that in the chat window a name is really a character id handing invite requests is as easy as opening a new pm.</p>
<p>A final note about the new social system.  Updating and management does not need to be left completely to the program.  With a new friends system comes a completely different way of interacting with players in the world.  Players should be able to modify their networks, add people to groups, tag communities of links.  I have more than once wished to have a system where I could tag players or organize relationships by specific words.  Not wishing to come off as an elitist but I am sure more than one of you have wanted to tag someone &#8216;noob&#8217;, &#8216;idiot&#8217;, or  &#8216;can&#8217;t play his class.&#8217;  With a more friendly social network system you could organize your contacts, including all the people you no longer want to interact with into a group.  Then when they say something in chat, send you a pm, invite you to a raid, you will have fair warning.</p>
<p>When all is said and done I am really advocating a new type of system for interacting with players in an online world.  One built on networks of links instead of a simple one dimensional friends list.</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/137/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The world between runtime programming and scripting</title>
		<link>http://charlessolar.com/post/81</link>
		<comments>http://charlessolar.com/post/81#comments</comments>
		<pubDate>Wed, 27 Aug 2008 16:23:02 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[bafprp]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[pluggable factories]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=81</guid>
		<description><![CDATA[Here is a problem for all the theoretical computer scientists out there.  Say you want to write a program, this program needs to parse a certain file structure and extract useful data.  A single file is setup as a bunch of independent records built on a handful of fields in said record.  There are almost [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a problem for all the theoretical computer scientists out there.  Say you want to write a program, this program needs to parse a certain file structure and extract useful data.  A single file is setup as a bunch of independent records built on a handful of fields in said record.  There are almost 1000 different kinds of fields, and close to 200 different kinds of records.  The requirements for output is standard duplicate checking and removal, and programmable output, meaning you can change how the output looks or is handled without changing the program.</p>
<p>If you can think of a good solution feel free to post your ideas, today I will be writing about my solution and things that worked and did not work, and things that still do not work.</p>
<p><span id="more-81"></span></p>
<p>The situation I am referring to here is parsing a call data file known as a BAF file.  The file structure and definitions for these files are described in detail in a document known as the <a href="http://telecom-info.telcordia.com/site-cgi/ido/newcust.pl?page=idosearch&amp;docnum=GR-1100&amp;">GR-1100</a> which is published by Telcordia.</p>
<p>To parse this file your program must know and recognize about  1000 different field types and 200 different record types.  Fields are organized in records, you can probably say that records are defined by their fields.  Oh, also, there are about 100-200 defined modules that theoretically can be appended to any record.  Each module is again a set of fields.  So if you were to summarize this file it would be laid out like this.</p>
<blockquote><p>Record Start<br />
Fields<br />
Record End<br />
Any number of modules<br />
New Record</p></blockquote>
<p>I have already talked about <a href="http://www.mrlwork.com/post/50">pluggable factories</a>, which is the main design element I chose when first designing <a href="http://code.google.com/p/bafprp/">bafprp</a>.  I decided to create a maker factory for fields and records, and define each field and record as a class.  Now this was BEFORE I found out exactly how many different types of fields and records there were in total.  All I had to build off of was an open source project called <a href="http://www.xach.com/misc/bafview/">bafview</a> which contained a very small subset of the actual fields and records that are defined in the GR-1100.  I only recently learned about the inadequacies of this subset when I was notified about missing records and fields that were not in bafview, and thus, not in bafprp.</p>
<p>When I released version 1.0 of bafprp it contained only about 100 different fields and records.  A number small enough that the maker factory pattern made sense.  Obviously however this needed to be changed if bafprp was to become a complete GR-1100 compliant program.  So now we must free ourselves from the basic design and fall back to the drawing board.  Something that many designers, myself included, are very bad at doing, and so I did not.  Instead I though about another solution, which extended the concept of pluggable factories into what I will very loosely call run time programming.</p>
<p>Now do not jump to any conclusions about a self correctly, self building, automated executable.  This is NOT run time programming in the strict sense of the word.  What I did not was allow for the extendability of certain classes at run time.  Meaning that a basic class is interpreted hundreds of different ways depending on run time data. Using this method I created a handful of basic classes that could be extended by strings passed by a user to define the specifics of the field.  To define the default fields the default data is hard coded into the program, but the user has complete control over any field in the program through two specific command line options.</p>
<p>There are some similarities to basic scripting, if that is what you were thinking.  There is one major difference however, bafprp remains in complete control over the program.  With scripting languages the program surrenders part of itself so the user can change default behavior.  In bafprp the basic classes remain in complete control of the operation, but the user has complete control over the data.  Something that I have not seen done in any other program yet.</p>
<p>So by now if you are still reading you are probably itching for some source code.  The design is pretty simple, we have eight or so basic types using the pluggable factory design.  These basic types operate on string data passed to them, how they operate and the string syntax is completely up to them, however for these elements I had them conform to property name, property value pair of strings for operating data.</p>
<p>Here is a simple example of my Date field type</p>
<pre name="code" class="cpp">
std::string DateField::getString() const
	{
		LOG_TRACE( "DateField::getString" );

		std::string ret;
		if( !_converted )
		{
			LOG_WARN( "Tried to get string before field was converted" );
			ret = "";
		}
		else
		{
			char year[5] = "";
			time_t ltime;
			struct tm* mytm;
			ltime = time( NULL );
			mytm = localtime( &amp;ltime );
			strftime( year, sizeof( year ), "%Y", mytm );
			year[3] = _return[0];

			property_map::const_iterator itr = _properties.find( "format" );
			if( itr != _properties.end() &amp;&amp; itr-&gt;second.find( "Y" ) != std::string::npos &amp;&amp; itr-&gt;second.find( "M" ) != std::string::npos &amp;&amp; itr-&gt;second.find( "D" ) != std::string::npos )
			{
				ret = itr-&gt;second;
				ret.replace( ret.find("Y"), strlen( year ), year );
				ret.replace( ret.find("M"), 1, _return.substr(1,2) );
				ret.replace( ret.find("D"), 1, _return.substr(3,2) );
			}
			else
			{
				std::ostringstream os;
				os &lt;&lt; year &lt;&lt; "-" &lt;&lt; _return[1] &lt;&lt; _return[2] &lt;&lt; "-" &lt;&lt; _return[3] &lt;&lt; _return[4];
				ret = os.str();
			}
		}

		LOG_TRACE( "/DateField::getString" );
		return ret;
	}
</pre>
<p>The date field will take a format property and change the data it converted appropriately.  This is an example of a basic property that each field can have.  For a more complex operation here is the same switch function</p>
<pre name="code" class="cpp">
std::string SwitchField::getString() const
	{
		LOG_TRACE( "SwitchField::getString" );

		std::string ret = "";
		if( !_converted )
		{
			LOG_WARN( "Tried to get string before field was converted" );
			ret = "";
		}
		else
		{
			props_pair switches = _properties.equal_range( "switch" );
			std::string sw;
			if( switches.first == switches.second )
			{
				// No "switch" property so assume we switch on character 0
				sw = "0" + _return;
				sw.resize( 2 );
				property_map::const_iterator string = _properties.find( sw );
				if( string != _properties.end() )
					ret = string-&gt;second + " ";
				else
					ret = "Unknown: " + sw.substr(1);
			}
			else
			{
				for( property_map::const_iterator pos = switches.first; pos != switches.second; pos++ )
				{
					sw = pos-&gt;second + (char*)&amp;_return[ atoi( pos-&gt;second.c_str() ) ];
					sw.resize(2);
					property_map::const_iterator string = _properties.find( sw );
					property_map::const_iterator desc = _properties.find( pos-&gt;second );
					if( string != _properties.end() )
					{
						if( desc != _properties.end() )
							ret += desc-&gt;second + " = " + string-&gt;second + " : ";
						else
							ret += string-&gt;second + " : ";
					}
					else
						ret += "Unknown: " + sw.substr(1) + " : ";
				}
				ret.resize( ret.length() - 3 ); // remove last ':'
			}
		}
		LOG_TRACE( "/SwitchField::getString" );
		return ret;
	}
}
</pre>
<p>Now here is my general disclaimer, if you see problems with this code feel free to let me know I know it is not 100% efficient and perfected and this is because I have never done this before so I was making up design as I wrote the code.  Eventually I will do yet another rewrite and fix the code with a more complete understanding of the design.</p>
<p>But the switch function needs some explaining.  I think it would be best to see an example switch field</p>
<pre name="code" class="cpp">
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "datatype:switch" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "size:1" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "desc:Called Party Answer Indicator" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "switch:0" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "00:Called Party Answer Detected" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "01:Called Party Answer not Detected" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "02:Answered Attempt" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "03:Simulated Called Party Off-Hook Indicator" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "04:NCD, CAS, Blocked After Answer" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "05:NCD, CAS, Blocked Before Answer" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "07:Service Features Not Provided, Call Answered" );
		FieldMaker::setFieldProperty( "calledpartyanswerindicator", "08:Service Features Not Provided, Call Unanswered" );
</pre>
<p>Ok so first we set the basic data that defines a field: type, size, and desc.  This is all that is required to make a field.  We then setup a switch on character zero by setting the switch property to zero.  The properties after that list the values for the switch.  If the switch character is zero print &#8220;Called Party Answer Detected,&#8221; and so on.  The syntax for these are to have the property name before the value, separated by a colon.  This of course is completely arbitrary and can be anything depending on how you want to assign properties.  The property name syntax is the switch number first, then the switch value.  Only two characters are required because I only allow one character switches.</p>
<p>The trick here is how the switch class uses its properties.  For the date object, all it did was change the format of the date, however the switch completely depends on the properties to be set correctly to function properly.  The properties of a switch class define the switch, whereas the properties of the date class hardly do anything.  It is this range of effective designs that make the application successful and indicate proper orthogonality, which is the number one most important design philosophy.</p>
<p>I am finding that increasingly complex posts are getting difficult to write and even more difficult to read so from now on I am going to try and keep posts small and exercise a specific element instead of the whole design.  I left a lot out of this post simply because this is a blog not a research paper, so I will try and get some more data posted soon.<br />
In the meantime, you can always check out the source at <a href="http://code.google.com/p/bafprp/">bafprp&#8217;s web site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/81/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pluggable Factories</title>
		<link>http://charlessolar.com/post/50</link>
		<comments>http://charlessolar.com/post/50#comments</comments>
		<pubDate>Tue, 12 Aug 2008 04:36:49 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[pluggable]]></category>
		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=50</guid>
		<description><![CDATA[So in my last article I touched on the subject of what I termed static factories. I did a little searching online and found the original article that first gave me the idea.  Apparently this type of design is called pluggable factories.  A term I had forgotten until just now. Here is the basic idea, [...]]]></description>
			<content:encoded><![CDATA[<p>So in my last article I touched on the subject of what I termed static factories.  I did a little searching online and found the <a href="http://www.gamedev.net/reference/articles/article841.asp" target="_blank">original article</a> that first gave me the idea.  Apparently this type of design is called pluggable factories.  A term I had forgotten until just now.</p>
<p>Here is the basic idea, you have x number of objects your program can create.  These objects are all children of a base interface.  You have cleverly created an abstract factory to create the various instances of your objects, but there is a problem, you are asked to add several new objects and/or remove several objects.  So you jump back into your code and make a new object or remove an object.  You compile your code and try to use the new object, OOPS, you forgot to add it to the abstract factory!   Or, after removing the object you run some old data and OOPS, your program crashes as it tried to return an instance to a class that does not exist.</p>
<p>To be fair, if your program was written correctly it would not compile in the latter case, but that is besides the point.  Sometimes abstract factories can be a pain in the ass.  So, here is where pluggable factories step up to the plate.<br />
<span id="more-50"></span><br />
They offer you the ability to create or remove objects without modifying any part of the project.  All you, as the programmer, need to do is create a new cpp with two well defined classes and you can use your new object.  How does this work?  It relies on a very simple but powerful aftereffect of static objects.</p>
<p>See, the basic idea of a static object is that only one exists.  It gets setup at the start of your program and never moves or changes.  If it is a method, it gets a special place in memory and any children of your class call that exact function.  If it is a variable, it gets initiated at the start of the program and all references to get sent directly to that location, no mater where in the program you are.</p>
<p>The idea behind pluggable factories is that you silently initiate several classes that only exist to make one specific type of object.  These classes are known as makers, and they are the key to pluggable factories.</p>
<p>Makers come in many different flavors and styles, but we will focus on one type for now.  Let us define an object derived by a common interface.</p>
<pre name="code" class="cpp">
class Controller_Letter : public IController
{
    friend class Letter_Maker;
public:
    ~Controller_Letter() {}

    int fitness( void* data )
    {
    if( (char)data == _letter || (char)data == toupper(_letter) )
        return 1;
    return 0;
    }
    void perform() { printf( "%c", _letter ); }

protected:
    Controller_Letter() {}

private:
    void setLetter( char letter ) { _letter = letter; }
    char _letter;
};
</pre>
<p>Now this is a basic controller class for a project of mind, so do not pay to much attention to the methods.  From the basic idea of this controller, I would want at least 26 of these classes, but to be safe I will instead make <a href="http://www.asciitable.com/">255</a> of them.  Can you imagine a switch statement with 255 cases?</p>
<p>To make our lives easier we will construt a pluggable factory to automatically create each class.  A pluggable factory is made of three parts, a std::map, a constructor, and a pure virtual make method.  The map will store our database of makers, the constructor will be used by our children to register with the database, and the virtual method will be defined by our children so they can make their object.  Lets take a look at a basic parent maker class</p>
<pre name="code" class="cpp">
class Controller_Maker
{
public:
    static IController* newController( const std::string&amp; type, void* data );
private:
    typedef std::map&lt;std::string, Controller_Maker*&gt; maker_map;

    static maker_map&amp; getReg()
    {
        static maker_map registry;
        return registry;
    }
    Controller_Maker() {}
protected:
    Controller_Maker( const std::string&amp; type )
    {
        getReg().insert( std::make_pair( type, this ) );
    }
    virtual IController* make( void* data ) const = 0;
};
</pre>
<p>We have defined all three parts, but there is something I would like to point out to everyone.  Notice the getReg function.  It would make sense to have our registry variable static inside the class, then have the children directly access it to register themselves, HOWEVER, here is the problem.  When your program gets compiled all your static elements are juggled by the linker and eventually end up sitting in an initialization list somewhere in your binary.  Among these variables are your child makers trying to register themselves in the registry.  Imagine what would happen if a child is initialized before the registry map is.  BOOM CRASH BANG, yep, you get a splendid show of fireworks before your program disintegrates back into the void.  The getReg function is how we avoid this very nasty problem.  See, functions do not need to be setup before hand, a static function is just an address, it can be called at any time.  Therefore we put the registry inside a static function, and we can guarantee that it gets initialized before a child tries to use it, because when a child uses it they initialize it.  Simple eh?</p>
<p>So now you have a maker, an interface, and an object you want many many variations of.  How do you define the individual makers to first register with the parent maker, and second, create the desired object?  Child makers will, as you may expect, define the make function from the parent maker and call the parents constructor.  However the key to child makers are the static instances of themselves they define.  It may sound weird, but each child defines a variable inside itself of itself.  This variable is initiated at the start of the program ( because its static, remember? ) and thus the child is registered with the parent.  Here is the child maker</p>
<pre name="code" class="cpp">
class Letter_Maker : public Controller_Maker
{
protected:
    IController* make( void* data ) const;
private:
    Letter_Maker() : Controller_Maker( "letter" ) {}
    static const Letter_Maker registerThis;
};
</pre>
<p>Notice the constructor, it calls the parent with the type of object it makes, thus registered a new type of object with the pluggable factory.  Also key here is the registerThis variable.  The only purpose of this variable is to register the class without modifying any factory or project source.  This class is naturally defined with the class it creates, this way you just add a cpp to the project and you have a new object.  Impressed yet?  Lets dive a bit further.</p>
<p>If you have been paying attention you have noticed that I have not defined some key methods, namely, Controller_Maker::newController or Letter_Maker::make.  Well it does not take much imagination to define these, here they are</p>
<pre name="code" class="cpp">
IController* Letter_Maker::make( void* data ) const
{
    Controller_Letter* ret = new Controller_Letter();
    ret-&gt;setLetter( (char)data );  // Sending NULL will not hurt anything
    return ret;
}

IController* Controller_Maker::newController( const std::string&amp; type, void* data )
{
    maker_map::iterator itr = getReg().find( type );
    if( itr != getReg().end() )
        return itr-&gt;second-&gt;make( data );
    return NULL;
}
</pre>
<p>I usually keep these methods separate from the header files for neatness sake.  Here is the quick run down on these.  Letter_Maker::make will make the object it was build to make.  Think of it as a small builder ( if you have read <em>Design Patterns</em> ).  Its important to not fall into temptation and pass void* data to the new object.  Notice how I create the object then call its setLetter method?  The maker data should stay independent of the object definition.  Remember DRY?</p>
<p>Controller_Maker::newController is just what you probably expected, it simply searches the registry for the type of maker we ask for and calls the specific maker&#8217;s make function.</p>
<p>In the end, we are left with a nice set of classes that can easily encapsulate all 255 acsii characters in just 255 lines ( if someone wants to add up the maker and controller class lines be my guest ).</p>
<p>Here is how we use the maker</p>
<pre name="code" class="cpp">
IController* a = Controller_Maker::newController( "letter", (void*)'a' );
IController* b = Controller_Maker::newController( "letter", (void*)'b' );
IController* c = Controller_Maker::newController( "letter", (void*)'c' );
IController* d = Controller_Maker::newController( "letter", (void*)'d' );
IController* e = Controller_Maker::newController( "letter", (void*)'e' );
IController* f = Controller_Maker::newController( "letter", (void*)'f' );
</pre>
<p>Keep in mind that is by far the simplest way to use pluggable factories.  For more examples you can check out bafprp&#8217;s source code, pluggable factories are used for creating fields, records, and the most interesting of all is the output class, where I merged the maker and the object into one class.  And that is not all as far as I know I have barely even scratched the surface of pluggable factory design, so if you come up with a new technique, feel free to post!</p>
<p><a href="http://www.mrlwork.com/downloads/SOURCE/pluggable_factory_source.rar">Project source code here</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/50/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BafPRP Release</title>
		<link>http://charlessolar.com/post/38</link>
		<comments>http://charlessolar.com/post/38#comments</comments>
		<pubDate>Sun, 10 Aug 2008 04:59:38 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[bafprp]]></category>
		<category><![CDATA[dsn]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[odbc]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=38</guid>
		<description><![CDATA[I setup the release of version 1.0 for bafprp the other day. It has been a fun project that I hope is useful to others like myself who needed an improvement over bafview without going to the big software companies. For today&#8217;s post I want to list some of the helpful tips I picked up [...]]]></description>
			<content:encoded><![CDATA[<p>I setup the release of version 1.0 for <a href="http://code.google.com/p/bafprp/">bafprp</a> the other day.  It has been a fun project that I hope is useful to others like myself who needed an improvement over bafview without going to the big software companies.  For today&#8217;s post I want to list some of the helpful tips I picked up while writing the program.</p>
<p><strong>DSN-Less ODBC Connection String</strong></p>
<p>I found it really annoying that in order to connect through ODBC you had to setup a data source.  In windows this process involves going to the control panel, administrative tools, and data sources.  In Linux, this involves setting up the ODBC driver, setting up unixODBC to recognize the driver, and finally setting up the server information and data source in both your driver and unixODBC.</p>
<p>I spent some time looking online and found a nice and easy way to connect without an external data source in windows.  Basically it involves supplying all the information in one string like so:</p>
<p>std::string dsn = &#8220;DRIVER=sql server;DATABASE=&#8221; + _database + &#8220;;SERVER=&#8221; + _server + &#8220;;Uid=&#8221; + _user + &#8220;;Pwd=&#8221; + _password + &#8220;;&#8221;;</p>
<p>You must also use SQLDriverConnect instead of the usual SQLConnect function since the former will accept a dsn string, instead of just a dsn name and login info.</p>
<p>In linux its still a little annoying.  You must still define your driver in unixODBC for starters but you do not need to setup a dsn or give server type information.  Also, the support the DSN-less connections depends on the driver you choose.  Since my primary use for bafprp was to connect to a ms sql database, I used FreeTDS which as of a recent update does have dsn-less capability.  If you are using something else, please consult the documentation about this as well.</p>
<p>In FreeTDS the basic connection string goes like this</p>
<p>std::string dsn = &#8220;DRIVER=FreeTDS;SERVER=&#8221; + _server + &#8220;;Uid=&#8221; + _user + &#8220;;Pwd=&#8221; + _password + &#8220;;DATABASE=&#8221; + _database + &#8220;;TDS_VERSION=8.0;Port=1433;&#8221;;</p>
<p>TDS_VERSION and Port are FreeTDS specific settings, but the basic idea remains the same.  Also it should be noted that different version of ms sql require a different TDS_VERSION.  If you are using FreeTDS its important to know the correct version.</p>
<p>As a final side note about connection strings, make sure to include the terminating semi-colon.  If you find your string unable to connect no matter what you do, this is probably the problem.</p>
<p><strong>File Output</strong></p>
<p>Sometimes when your program terminates unexpectedly, text being written to a file can be lost.  If you are using fprintf or writef or any of the stdio functions for logging you will probably come across this problem.  The only solution I found to guarantee that the text gets written is to use std::fstream and call the flush() method when you are done writing.  Flush will make sure the text gets written before returning so it will be a bit slower, but for something as important as logging this is important.</p>
<p><strong>Duplicate Removal</strong></p>
<p>I remember reading about a similar situation in <em>Programming Perls</em>.  It involved making a hash of your data and comparing collisions I believe.  The situation I was in was that I had thousands of records that could be byte for byte duplicates with any other record in the original binary file.  Like <em>Programming Perls</em> states, comparing each record against every other record is a joke.  Hashing is definitely a better solution, but you need not create such a complex hash table for something like this.  I ended up pulling a crc32 method to calculate the crc for the originals bytes in the record.  After the record parsing was completed I sorted the array of records by their crc value.  It was then a very easy procedure to remove any duplicates since they would be sitting right next to one another with the same crc.</p>
<p>One thing to note however, std::unique in algorithm.h seems like a wonderful function, but I could not get it to work for the life of me.  It is supposed to sort the array, and place any duplicates at the end of the array, returning an iterator pointing at the start of the duplicates.  Theoretically you can then use std::remove to remove all elements after that point to erase the duplicates.  I managed to get the list sorted and std::unique did identify the correct number of duplicates ( the number of elements after the returned iterator matched the number of duplicates I later removed by said method ), but it did not seem to place the real duplicates at the end of the array.  I ended up removing valid records that were unlucky enough to have a high crc and thus were at the end of the array.</p>
<p>So in the end I went through the entire list and removed neighbors with the same crc, which worked quite nicely.</p>
<p><strong>Static Factories</strong></p>
<p>I do not believe I have covered this concept here before so I will do a brief summary.  This subject requires a much more detailed post but here is the cut and dry.  If you are familiar with abstract factories you might know its a bit of a pain to add a new object.  If you are using some kind of enumeration you need to add the id to that list, and add the correct new object code in the create method of your factory.  Eventually you end up with enumerations of 100+ elements and a very scary switch statement.  Fear not however, there is a better way!</p>
<p>Imagine a system where all you need to do to add a new object in your factory is compile a cpp.  Thanks to static factories this is not just a dream.  The trick involves a very natural side effect of static objects.  The basic idea is simple.  You have a main &#8216;maker&#8217; class with a static registry variable that stores the names and pointers to other maker classes.  When you want an object to be built through this factory you need to create a simple maker class for your object with a method called make, which is defined in the parent maker as pure virtual.  The child maker defines an instance of itself as static and thus when the program starts it is created.</p>
<p>When the child gets created it calls the parent constructor with the name, or some other form of identification, of the object it creates.  The parent maker then adds the information to its static database.  When the programmer needs an instance of that object it simply calls the parent&#8217;s make method which looks at the database, pulls up the correct child maker and has the child make the object.</p>
<p>This technique is quite powerful if used correctly.  It is absolutely necessary for data driven applications in my opinion, and very handy when working with any kind of file data.  Using this method you can seperate file structure from logic in a very effective and pretty design.</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/38/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Log Files</title>
		<link>http://charlessolar.com/post/25</link>
		<comments>http://charlessolar.com/post/25#comments</comments>
		<pubDate>Fri, 18 Jul 2008 21:11:46 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bafprp]]></category>
		<category><![CDATA[cpu]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=25</guid>
		<description><![CDATA[While working on the BAF file project I learned a very interesting life lesson. See like most new computer science graduates I have worked on few actual projects, and quite frankly the few I have finished could not be considered corporation ready. While writing a few console applications, file parsers, corba interfaces, etc I learned [...]]]></description>
			<content:encoded><![CDATA[<p>While working on the <a href="http://code.google.com/p/bafprp/">BAF file project</a> I learned a very interesting life lesson.  See like most new computer science graduates I have worked on few actual projects, and quite frankly the few I have finished could not be considered corporation ready.  While writing a few console applications, file parsers, corba interfaces, etc I learned a valuable lesson in documentation and log files.  Log files can save your life if done correctly, and if you are really up to speed they can save many long hours of debuging too.  In my case I fell in love with trace log messages.  Trace logs are the log files that trace a programs execution so you can get a general idea of where your software is failing or producing an error.  Now, most trace logs I have had the pleasure to work with are not quite where I wanted to be when I approached trace logs in my program.  Most of the time they are a little more helpful then debug messages when trying to pinpoint an application&#8217;s problem.  So when I faced the decision to add trace messages I decided to go all out.</p>
<p>My first logging framework was <a href="http://log4cplus.sourceforge.net/">log4cplus</a>, which is a very nice and complete logging framework for C++.  I used it for one linux application I wrote and it works perfectly despite being several years old.  Unfortunetly I was uncomfortable with how much work it took to write a message each new function ( two lines instead of one ) and it would not compile in windows so for my next application, bafprp, I set out to write my own.</p>
<p>When I completed the class I was left with one macro to print a log message depending on the level you wanted, ie, LOG_TRACE( string ), LOG_DEBUG( string ), etc.  I then made sure I was adding trace messages to each and every function as I made them, instead of later on in the design.  For example, an empty function would look like</p>
<p><code><br />
void BafRecord::getType()<br />
{<br />
LOG_TRACE( "BafRecord::getType" );<br />
LOG_TRACE( "/BafRecord::getType" );<br />
return;<br />
}</code></p>
<p>Each function would have a start and stop trace message.</p>
<p>Now this might raise an eyebrow or two but I assure you it definitely helps when your program crashes and you do not have a nice debugger on hand.  Like say, if a non-technical user is using it.</p>
<p>This is how my linux application was programmed and I went the extra step in bafprp to work these in while I was working.  Enough about this though, a short while ago, after adding one of the major structure types to the program my application slowed down from parsing a 12 meg file in 2 seconds to 2 minutes and I was greatly concerned over the well being of my design.</p>
<p>I tried many things, first I greatly reduced the number of memory copies my program executed, then I changed my file input so that it would read the entire file at the start and reference a data bank instead of reading the file each cycle.  However none of these things put a dent in the processing time.  So then I went online to try and find a nice and easy code profiler.  Code profilers will basically watch your program execution and tell you which function your program is spending the most amount of time in.  I ended up finding <a href="http://www.lw-tech.com/">LTProf</a> which allowed me to profile my program without recompiling or changing my program at all.  I am actually very surprised at how well it works with compiled binaries.  After running a release version of my program it was still able to accuratly determine function names and operate like it had a window into the source code.<br />
<a href="http://www.mrlwork.com/images/ltprof.png"><img src="http://www.mrlwork.com/images/ltprof-thumb.png" alt="" hspace="10" vspace="10" width="161" height="100" align="right" /></a></p>
<p>I found that my time stamp function, NowTime, which returns the current time as a string, was taking a noticably large amount of my program&#8217;s time.  Thinking back to what code uses this function I discovered the flaw.</p>
<p>When I wrote the log program I wrote the log level exception into the log class.  This way if the program tried to log a trace message it would get sent to the output class, which would then pass it on to the logs if the level was at or below the log level type.  However this was not enough.  as it turns out simply creating the log message twice in each function had a substantial effect on the processing speed of my program.</p>
<p>Needless to say as soon as I moved the log level check to the macro the process speed dropped from 2 minutes to 30 seconds.  Now some might say that trace logs this detailed are excessive, however I believe they can help greatly when dealing with a malfunctioning program.  So as a final statement, be careful when you design systems, and beaware of how to use your tools.  And if your program runs 3 times longer then a similar program, there is probably something horribly wrong.</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/25/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Projects Online and Open Source</title>
		<link>http://charlessolar.com/post/24</link>
		<comments>http://charlessolar.com/post/24#comments</comments>
		<pubDate>Fri, 11 Jul 2008 05:31:04 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bafprp]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=24</guid>
		<description><![CDATA[I finally got around to setting up svn for my project. Thanks in part to some new tools I recently came across the transition was fairly painless and after about 2 hours of cleaning up my old project and trimming the fat I uploaded what I will be working on to http://code.google.com/p/anaa/ I also created [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got around to setting up svn for my project.  Thanks in part to some new tools I recently came across the transition was fairly painless and after about 2 hours of cleaning up my old project and trimming the fat I uploaded what I will be working on to <a href="http://code.google.com/p/anaa/" target="_blank">http://code.google.com/p/anaa/</a></p>
<p>I also created a second project of a more work related function.  This project deals with reading and parsing Bellcore BAF files created by various soft switches in use around the world.  These records contain call records in a highly coded and formated to Telcordia specifications in their GR-1100 document.  This document is not very cheap to come by so I have started work on improving one of the best free parser&#8217;s available today, <a href="http://www.xach.com/misc/bafview/" target="_blank">bafview</a>.  You can find this project here <a href="http://code.google.com/p/bafprp/" target="_blank">http://code.google.com/p/bafprp/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/24/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Driven Apps!</title>
		<link>http://charlessolar.com/post/20</link>
		<comments>http://charlessolar.com/post/20#comments</comments>
		<pubDate>Mon, 17 Mar 2008 18:29:49 +0000</pubDate>
		<dc:creator>Charles Solar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://www.mrlwork.com/?p=20</guid>
		<description><![CDATA[Ok ok, I am writing again! If you read my previous posts on dynamic npc actions then this is an update on my progress. If you have not, this is about how to create good file formats for your applications. As some might have expected this project is taking more time then expected, partially because [...]]]></description>
			<content:encoded><![CDATA[<p>Ok ok, I am writing again!  If you read my previous posts on dynamic npc actions then this is an update on my progress.  If you have not, this is about how to create good file formats for your applications.</p>
<p>As some might have expected this project is taking more time then expected, partially because I am currently downing bosses in WoW, and partially because its very hard.  So lets just cover what has been going on since my last post.</p>
<p>I spent a lot of time thinking of the best structure for my idea in terms of objects and file formats.  I do not know if I covered this before because I am to lazy to view my past posts so heres a recap of the basic composition of what I have been calling a &#8216;Puppet.&#8217;  A puppet is a game object with x number of movable joints.  These joints are built in a tree structure and move based on messages passed to his parent.  These messages are generated from a actor class who learns actions and movements as I previously discussed.  When I was designing the puppet class I hit a big fat road block.  I wanted the ability for a puppet to be independent of his underlying joint structure.  That way I could have a puppet control everything from a human to grass.  So you might think, &#8216;No problem, do some tricky interface work and have derived classes for each type of puppet.&#8217;  WRONG!</p>
<p>See I myself have read many articles about component game object design, and I do believe I wrote an article on this in the past.   However do not get the wrong idea, what I did is not EXACTLY component objects, but kind of close.  Because when I thought about components I also thought how to best describe them in a file format.  And after thinking about that I decided that I wanted a global file format to describe not only each type of puppet but the entire world.  And that the entire game will be built out of components in a tree structure built at load time from this file format.  I am getting a bit ahead of myself here so lets get some examples here.</p>
<blockquote><p> &lt;world&gt;<br />
&lt;name type=string&gt;Main&lt;/name&gt;<br />
&lt;description type=string&gt;<br />
Big world for which to run tests in<br />
&lt;/description&gt;<br />
&lt;floor type=plane&gt;<br />
&lt;p1 type=vector&gt;-1000 -1000 -1000&lt;/p1&gt;<br />
&lt;p2 type=vector&gt;1000 1000 1000&lt;/p2&gt;<br />
&lt;material&gt;dirt01.mat&lt;/material&gt;<br />
&lt;/floor&gt;<br />
&lt;joint&gt;<br />
&lt;position type=vector&gt;0 0 0&lt;/position&gt;<br />
&lt;orientation type=vector&gt;0 0 0&lt;/orientation&gt;<br />
&lt;puppet&gt;arm.puppet&lt;/puppet&gt;<br />
&lt;/joint&gt;<br />
&lt;/world&gt;</p></blockquote>
<p>This is my toy world I am working in.  The file format is xml and I use tinyXML to load and parse these files.  For those familiar with ogre, I made a resource of type XMLFile to load these.  Now here comes the clincher, this XMLFile resource will load every type of game object I desire, regardless of type or purpose.  Not only that, but I only added 2 methods to the class.</p>
<p>So let me explain a bit about these methods, they are named &#8216;build&#8217; and they are called usually immediately after loading the file.  Basically what they do is look at an element in the xml, find a builder for it, have the builder build it, and attach the new object to the world tree.  In this example, the world has no parent and all elements inside the world tags are children of the world.  I use the type descriptors so that I would not have to write many classes that do the exact same thing.  Elements that load another file, like the puppet tags, will load that file in their own world and all objects created from that are children of the puppet.  After the build the main program is not aware of any object other then the world.  Messages are passed to the world when need be, however most thought processing is done in the world object, not by the game engine.</p>
<p>Lets get an example puppet up here:</p>
<blockquote><p> &lt;puppet&gt;<br />
&lt;name type=string&gt;arm&lt;/name&gt;<br />
&lt;description type=string&gt;<br />
Generic arm<br />
&lt;/description&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;shoulder&lt;/name&gt;<br />
&lt;gib&gt;upperarm.gib&lt;/gib&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;elbow&lt;/name&gt;<br />
&lt;gib&gt;forearm.gib&lt;/gib&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;wrist&lt;/name&gt;<br />
&lt;gib&gt;hand.gib&lt;/gib&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;pinky&lt;/name&gt;<br />
&lt;gib&gt;pinky.gib&lt;/gib&gt;<br />
&lt;/joint&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;ring&lt;/name&gt;<br />
&lt;gib&gt;ring.gib&lt;/gib&gt;<br />
&lt;/joint&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;middle&lt;/name&gt;<br />
&lt;gib&gt;middle.gib&lt;/gib&gt;<br />
&lt;/joint&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;index&lt;/name&gt;<br />
&lt;gib&gt;index.gib&lt;/gib&gt;<br />
&lt;/joint&gt;<br />
&lt;joint&gt;<br />
&lt;name type=string&gt;thumb&lt;/name&gt;<br />
&lt;gib&gt;thumb.gib&lt;/gib&gt;<br />
&lt;/joint&gt;<br />
&lt;/joint&gt;<br />
&lt;/joint&gt;<br />
&lt;/joint&gt;<br />
&lt;/puppet&gt;</p></blockquote>
<p>As you can see this file format is very flexible, the same structure that built the world can build a puppet and also a gib:</p>
<blockquote><p> &lt;gib&gt;<br />
&lt;name type=string&gt;forearm&lt;/name&gt;<br />
&lt;notes type=string&gt;<br />
Fore arm gib<br />
&lt;/notes&gt;<br />
&lt;mesh&gt;forearm.mesh&lt;/mesh&gt;<br />
&lt;input&gt;<br />
&lt;name type=string&gt;shoulder&lt;/name&gt;<br />
&lt;position type=vector&gt;0 0 0&lt;/position&gt;<br />
&lt;/input&gt;<br />
&lt;output&gt;<br />
&lt;name type=string&gt;elbow&lt;/name&gt;<br />
&lt;position type=vector&gt;7 0 0&lt;/position&gt;<br />
&lt;/output&gt;<br />
&lt;/gib&gt;</p></blockquote>
<p>I want to clarify one thing before I go on.  Elements called position or orientation, these attributes are not given any attention by the builders.  Instead those objects simply exist on the tree for someone else to read them and make the correct adjustments.  For example, the puppet&#8217;s name element simply sits as an element on the tree until a debugger opens and the entire tree structure is displayed.  At that time the debugger will go through each element and find name tags that it will use to give more information on the node.  And the description works the same way.</p>
<p>They are both created by the string builder, as denoted by the &#8216;type=string&#8217; attribute I give them.  The value is assigned and the name of the new object is set, but the type is always a string.</p>
<p>So what exactly is required to declare a new type of object?  Just a builder and an actual object.  Take this example of the string object.</p>
<blockquote><p> class ScktString : public ISocketable<br />
{<br />
friend class StringBuilder;<br />
public:<br />
void setValue( std::string val ) { _string = val; }<br />
std::string getString() { return _string; }<br />
private:<br />
ScktString( std::string propName ) : ISocketable( propName ) {}</p>
<p>std::string _string;<br />
};</p>
<p>class StringBuilder : public IBuilder<br />
{<br />
public:<br />
StringBuilder() : IBuilder() {}<br />
Socket* build( std::string element, Socket* parent );<br />
const std::string&amp; getType() const;<br />
static std::string BUILDER_TYPE_NAME;<br />
private:<br />
static const StringBuilder registerThis;<br />
};</p></blockquote>
<p>As you can see its relatively easy to create a new object.  Lets start at the builder, when the xmlfile is building it will search for a builder based on its type name, which is assigned by you.  A builder list is created at the start of the program, as each builder has a static &#8216;registerThis&#8217; variable that adds it to the builder list.  Those familar with static factories should be familiar with this.  The builder&#8217;s build method gets called with the name of the element and its parent&#8217;s socket.  A socket is an object that accepts links from children.  The builder then builds the object passing its name.  The sockable base class is for objects that can be socketed or linked with a parent.</p>
<p>So the string object is created and the string builder passes back the parent object.  This is done in case the new object has a socket himself.  In that case he would pass back his socket and the builder would build his children and attach them to him.  In string&#8217;s case however, there is no socket and a little while later the builder assigns the string object a value by calling the setValue method in the parent object with the objects name and value as parameters.</p>
<p>After all the work we are left with one tree that contains all objects in the game.  Kind of like a scene manager, but for ALL objects, not just renderable ones.  And oh, don&#8217;t forget that the entire tree was created by one instance of XMLFile.  As programmers we should always strive to achieve the cleanest and most powerful format we can muster.  This format is not only really easy to implement, but also holds unlimited power.  You want to load a texture?  Create a texture socketable object, and implement file specific loading inside it.  You want to script AI?  Either create an AI socketable object that reads children objects that decribe a strategy, or create a python socketable object that will load your ai scripts and parse them.  In this way our file format can act a proxy for another format or as the direct format.  The most important thing however is that none of our in game objects need to give file formats a single thought.</p>
]]></content:encoded>
			<wfw:commentRss>http://charlessolar.com/post/20/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

