<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
	<title>General Programming - Articles</title>
	<link>http://www.gamedev.net/page/resources/_/technical/general-programming/</link>
	<pubDate>Sat, 25 Feb 2012 21:04:36 +0000</pubDate>
	<ttl>43200</ttl>
	<description>Programming topics and resources not strictly related to gaming but at the same time still useful in gaming</description>
	<item>
		<title>Producer Consumer Using Double Queues</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/producer-consumer-using-double-queues-r2878</link>
		<description><![CDATA[<ul class='bbc'><br /><li><a href='http://uploads.gamedev.net/cp/1329923428-DoubleQueue.zip' class='bbc_url' title='External link' rel='nofollow external'>Download source - 4.02 KB</a><br /></li></ul><br />
License: <a href='http://www.opensource.org/licenses/zlib-license.php' class='bbc_url' title='External link' rel='nofollow external'><span class='bbc_underline'><strong class='bbc'>Zlib</strong></span></a><br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Introduction</strong></span><br />
<br />
Producer consumer is an old computer science problem. Many solutions have been proposed to solve this problem. The general approach is using a common queue. A single queue to which the producer writes data to and the consumer reads data from. Avoiding race conditions is done by locking the queue so that when the producer is writing, the consumer is not reading and vice versa.<br />
<br />
The problem with this approach is that whenever the producer puts data to the queue, it has to acquire a lock. Similarly whenever the consumer gets data, it has to lock the queue. This solution is inadequate on systems where the producer generates data in excess of a hundred thousand updates per second. In my opinion, a hundred thousand locks on the queue in one second is a huge performance penalty.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Background</strong></span><br />
<br />
Once while going over this conundrum with my better half, I was introduced to the concept of double queuing. Something similar to the double buffering used in graphics applications to avoid flicker. As far as my research goes, I was not able to find a similar solution to the producer consumer problem. In case this is duplication, my apologies. I would be indebted if you can point me to the original work.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Double Queuing</strong></span><br />
<br />
The solution proposed is very simple and elegant. Instead of using a single queue, we use two queues. At a given time, one queue is designated write queue and the other, the read queue. This helps us to avoid locking the queue while reading and writing to it. The producer is free to fill the write queue and the consumer is free to empty the read queue.<br />
<br />
When the consumer is done reading the read queue, the producer is blocked briefly and the queues are switched. Now the old read queue becomes the new write queue and the old write queue becomes the new read queue. After switching, if there is no data in the new read queue, the consumer is blocked. When the producer gets more data, it unblocks the consumer.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>The Code</strong></span><br />
<br />
This solution makes use of Events for synchronization between the consumer and the producer. The producer code looks like this:<br />
<pre class='prettyprint'><br /><br />public void ProducerFunc()<br />{<br />	int data = 0;<br /><br />	for (int i = 0; i &lt; 10000; i++)<br />	{<br />		data += 1;<br />		MessageHandler(data);<br />		Thread.Sleep(m_Random.Next(0, 2));<br />	}<br />}<br /><br />private void MessageHandler(int data)<br />{<br />	m_UnblockHandlerEvent.WaitOne();<br />	m_HandlerFinishedEvent.Reset();<br /><br />	m_CurrentWriteQueue.Enqueue(data);<br />	m_ProducerData.WriteLine(data); // logging<br /><br />	m_DataAvailableEvent.Set();<br />	m_HandlerFinishedEvent.Set();<br />}<br /></pre><br />
<br />
<br />
<br />
The ProducerFunc() is a thread function that is invoked 10,000 times. Every time MessageHandler() is called, it checks to see if it has been blocked by the consumer. If not, it resets the ManualResetEvent to indicate to the consumer that the producer has not yet finished. This is useful when the consumer is trying to switch the queues and wants to know if the producer is finished writing to the queue. The rest of the code is straight forward. Data is written to queue and an event is set to wake up the consumer incase it was blocked. Finally, an event is set to indicate that the producer is finished writing to queue.<br />
<br />
This is the consumer code:<br />
<pre class='prettyprint'><br /><br />public void ConsumerFunc()<br />{<br />	int count;<br />	int data;<br />	Queue&lt;int&gt; readQueue;<br /><br />	while (true)<br />	{<br />		m_DataAvailableEvent.WaitOne();<br /><br />		m_UnblockHandlerEvent.Reset(); // block the producer<br />		m_HandlerFinishedEvent.WaitOne(); // wait for the producer to finish<br />		readQueue = m_CurrentWriteQueue;<br />		// switch the write queue<br />		m_CurrentWriteQueue = (m_CurrentWriteQueue == m_Q1) ? m_Q2 : m_Q1;<br />		m_UnblockHandlerEvent.Set(); // unblock the producer<br /><br />		count = ;<br />		while (readQueue.Count &gt; )<br />		{<br />			count += 1;<br /><br />			data = readQueue.Dequeue();<br />			m_ConsumerData.WriteLine(data); // logging<br /><br />			Thread.Sleep(m_Random.Next(0, 2));<br />		}<br />		Console.WriteLine("Removed {0} items from queue: {1}",<br />					count, readQueue.GetHashCode());<br />	}<br />}<br /></pre><br />
<br />
<br />
<br />
The ConsumerFunc() is a separate thread function. When the function starts, it checks to see if the data present event has been set. If not, then it is blocked till we get data written by the producer. Once data is present in the queue, the consumer resets the event to block the producer and waits for the producer to finish. The current write queue is now cached for reading and the current write queue is switched. After the queue is switched, the producer is unblocked to enable it to start writing to the new write queue. Meanwhile the consumer is busy reading from the read queue. Once the queue is empty, the entire process is repeated.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Points of Interest</strong></span><br />
<br />
On further discussing this solution with others, I was told using synchronization events causes more performance problems than using lock. What do you think about that argument? Can you shed some light on this from your experiences with lock and synchronization events?<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>About the Author</strong></span><br />
<br />
	Nothing to brag about! Just another passionate software developer!<br />
<br />
Work to make a living, don't live to work!	<br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>License</strong></span><br />
<br />
This article was authored by Ram Mohan Raja and reproduced for the benefit of our viewers under the terms of the <a href='http://www.opensource.org/licenses/zlib-license.php' class='bbc_url' title='External link' rel='nofollow external'>Zlib</a> license.]]></description>
		<pubDate>Wed, 22 Feb 2012 15:11:21 +0000</pubDate>
		<guid isPermaLink="false">cb4b635a95a5e567747155f54a000542</guid>
	</item>
	<item>
		<title>JIRA: Programming Workflows</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/jira-programming-workflows-r2870</link>
		<description><![CDATA[In this article by <strong class='bbc'>Jobin Kuruvilla</strong> author of <a href='http://www.packtpub.com/jira-to-develop-customize-plugins-program-workflows-cookbook/book/sk/jira-abr1/1211?utm_source=sk_jira_abr1_1211&utm_medium=content&utm_campaign=sakina' class='bbc_url' title='External link' rel='nofollow external'>JIRA Development Cookbook</a>, we will cover:<ul class='bbc'><br /><li>Writing a workflow condition<br /></li><li>Writing a workflow validator<br /></li><li>Writing a workflow post function<br /></li><li>Editing an active workflow<br /></li></ul><br />
<span style='font-size: 18px;'><strong class='bbc'>Introduction</strong></span><br />
Workflows are one standout feature which help users to transform JIRA into a user-friendly system. It helps users to define a lifecycle for the issues, depending on the issue type, the purpose for which they are using JIRA, and so on. As the Atlassian documentation says at <a href='http://confluence.atlassian.com/display/JIRA/Configuring+Workflow' class='bbc_url' title='External link' rel='nofollow external'>http://confluence.at...guring+Workflow</a>:<br />
<br />
<br />
<br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'>A JIRA workflow is the set of steps and transitions an issue goes through during its lifecycle. Workflows typically represent business processes. </em></p><br />
<br />
JIRA uses Opensymphony's OSWorkflow which is highly configurable, and more importantly pluggable, to cater for the various requirements. JIRA uses three different plugin modules to add extra functionalities into its workflow, which we will see in detail through this chapter.<br />
To make things easier, JIRA ships with a default workflow. We can't modify the default workflow, but can copy it into a new workflow and amend it to suit our needs. Before we go into the development aspect of a workflow, it makes sense to understand the various components of a workflow.<br />
The two most important components of a JIRA workflow are <strong class='bbc'>Step</strong> and <strong class='bbc'>Transition</strong>. At any point of time, an <strong class='bbc'>Issue</strong> will be in a step. Each step in the workflow is linked to a workflow Status (<a href='http://confluence.atlassian.com/display/JIRA/Defining+%27Status%27+Field+Values' class='bbc_url' title='External link' rel='nofollow external'>http://confluence.at...+%27Status%27+F</a> ield+Values) and it is this status that you will see on the issue at every stage. A transition, on the other hand, is a link between two steps. It allows the user to move an issue from one step to another (which essentially moves the issue from one status to another).<br />
Few key points to remember or understand about a workflow:<ul class='bbc'><br /><li>An issue can exist in only one step at any point in time<br /></li><li>A status can be mapped to only one step in the workflow<br /></li><li>A transition is always one-way. So if you need to go back to the previous step, you need a different transition<br /></li><li>A transition can optionally specify a screen to be presented to the user with the right fields on it<br /></li></ul><br />
OSWorkflow, and hence JIRA, provides us with the option of adding various elements into a workflow transition which can be summarized as follows:<ul class='bbc'><br /><li><strong class='bbc'>Conditions</strong>: A set of conditions that need to be satisfied before the user can actually see the workflow action (transition) on the issue<br /></li><li><strong class='bbc'>Validators</strong>: A set of validators which can be used to validate the user input before moving to the destination step<br /></li><li><strong class='bbc'>Post Functions</strong>: A set of actions which will be performed after the issue is successfully moved to the destination step<br /></li></ul><br />
These three elements give us the flexibility of handling the various use cases when an issue is moved from one status to another. JIRA ships with a few built-in conditions, validators, and post functions. There are plugins out there which also provide a wide variety of useful workflow elements. And if you still don't find the one you are looking for, JIRA lets us write them as plugins. We will see how to do it in the various recipes in this chapter. Hopefully, that gives you a fair idea about the various workflow elements. A lot more on JIRA workflows can be found in the JIRA documentation at <a href='http://confluence.atlassian.com/display/JIRA/Configuring+Workflow' class='bbc_url' title='External link' rel='nofollow external'>http://confluence.at...guring+Workflow</a>.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Writing a workflow condition</strong></span><br />
What are workflow conditions? They determine whether a workflow action is available or not. Considering the importance of a workflow in installations and how there is a need to restrict the actions either to a set of people, roles, and so on, or based on some criteria (for example, the field is not empty!), writing workflow conditions is inevitable.<br />
Workflow conditions are created with the help of the <em class='bbc'>workflow-condition</em> module. The following are the key attributes and elements supported. See <a href='http://confluence.atlassian.com/display/JIRADEV/Workflow+Plugin+Modules#WorkflowPluginModules-Conditions' class='bbc_url' title='External link' rel='nofollow external'>http://confluence.at...ules-Conditions</a> for more details.<br />
<br />
<strong class='bbc'>Attributes:</strong><br />
<br />
<br />
										<br />
Class to provide contexts for rendered velocity templates. Must implement the com.atlassian.jira.plugin.workflow.WorkflowPluginConditionFactory interface.						<br />
<strong class='bbc'>Elements:</strong><br />
<br />
<br />
										<br />
Class to determine whether the user can see the workflow transition. Must implement com.opensymphony.workflow.Condition. Recommended to extend the com.atlassian.jira.workflow.condition.AbstractJiraCondition class.						<br />
<span style='font-size: 14px;'><strong class='bbc'>Getting ready</strong></span><br />
As usual, create a skeleton plugin. Create an eclipse project using the skeleton plugin and we are good to go!<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>How to do it...</strong></span><br />
In this recipe, let's assume we are going to develop a workflow condition that limits a transition only to the users belonging to a specific project role. The following are the steps to write our condition:<ul class='bbcol decimal'><br /><li>Define the inputs needed to configure the workflow condition.<br />			We need to implement the <em class='bbc'>WorkflowPluginFactory</em> interface, which mainly exists to provide velocity parameters to the templates. It will be used to extract the input parameters that are used in defining the condition. To make it clear, the inputs here are not the inputs while performing the workflow action, but the inputs in defining the condition.<br />			The condition factory class, <em class='bbc'>RoleConditionFactory</em> in this case, extends the <em class='bbc'>AbstractWorkflowPluginFactory</em>, which implements the <em class='bbc'>WorkflowPluginFactory </em>interface. There are three abstract methods that we should implement, that is, <em class='bbc'>getVelocityParamsForInput</em>, <em class='bbc'>getVelocityParamsForEdit</em>, and <em class='bbc'>getVelocityParamsForView</em>. All of them, as the name suggests, are used for populating the velocity parameters for the different scenarios.<br />			In our example, we need to limit the workflow action to a certain project role, and so we need to select the project role while defining the condition. The three methods will be implemented as follows:<br />			<pre class='prettyprint'>	<br />		private static final String ROLE_NAME = "role";<br />			private static final String ROLES = "roles";<br />			.......<br />			@Override<br />			  protected void getVelocityParamsForEdit(Map<br />			velocityParams, AbstractDescriptor descriptor) {<br />				velocityParams.put(ROLE, getRole(descriptor));<br />				velocityParams.put(ROLES, getProjectRoles());<br />			  }  @Override<br />			  protected void getVelocityParamsForInput(Map<br />			velocityParams) {<br />				velocityParams.put(ROLES, getProjectRoles());<br />			  }  @Override<br />			  protected void getVelocityParamsForView(Map<br />			velocityParams, AbstractDescriptor descriptor) {<br />				velocityParams.put(ROLE, getRole(descriptor));<br />			  }	<br />			</pre><br />			<br />		Let's look at the methods in detail:<br /><ul class='bbc'><br /><li><em class='bbc'>getVelocityParamsForInput</em>: This method defines the velocity parameters for input scenario, that is, when the user initially configures the workflow. In our example, we need to display all the project roles so that the user can select one to define the condition. The method getProjectRoles merely returns all the project roles and the collection of roles is then put into the velocity parameters with the key <em class='bbc'>ROLES</em>.<br /></li><li><em class='bbc'>getVelocityParamsForView</em>: This method defines the velocity parameters for the view scenario, that is, how the user sees the condition after it is configured. In our example, we have defined a role and so we should display it to the user after retrieving it back from the workflow descriptor. If you have noticed, the descriptor, which is an instance of <em class='bbc'>AbstractDescriptor</em>, is available as an argument in the method. All we need is to extract the role from the descriptor, which can be done as follows:<br />			<pre class='prettyprint'>	<br />		private ProjectRole getRole(AbstractDescriptor descriptor){<br />				if (!(descriptor instanceof ConditionDescriptor)) {<br />				  throw new IllegalArgumentException("Descriptor must be a<br />			ConditionDescriptor.");<br />				}<br />				<br />			  ConditionDescriptor functionDescriptor = (ConditionDescriptor)<br />			descriptor;<br />				<br />			 String role = (String) functionDescriptor.getArgs().get(ROLE);<br />				if (role!=null && role.trim().length()&gt;0)<br />				  return getProjectRole(role);<br />				else<br />				  return null;<br />			  }	<br />			</pre><br />			<br />		Just check if the descriptor is a condition descriptor or not, and then extract the role as shown in the preceding snippet.<br /></li><li><em class='bbc'>getVelocityParamsForEdit</em>: This method defines the velocity parameters for the edit scenario, that is, when the user modifies the existing condition. Here we need both the options and the selected value. Hence, we put both the project roles collection and the selected role on to the velocity parameters.<br /></li></ul>	<br /></li></ul><br />
	<ul class='bbc'><br /><li>The second step is to define the velocity templates for each of the three aforementioned scenarios: input, view, and edit. We can use the same template here for input and edit with a simple check to keep the old role selected for the edit scenario. Let us look at the templates:<br /><ul class='bbc'><br /><li><em class='bbc'>edit-roleCondition.vm</em>: Displays all project roles and highlights the already-selected one in the edit mode. In the input mode, the same template is reused, but the selected role will be null and hence a null check is done:<br />	<pre class='prettyprint'>	<br />	<br />	<br />		<br />		<br />			<br />			#foreach ($field in $roles)<br />						  #if ($role && (${field.id}==${role.id}))<br />					SELECTED<br />				#end<br />				&gt;$field.name<br />			#end<br />			<br />			<br />	Select the role in which the user<br />	should be present!<br />		<br />	<br />	<br />	</pre><br /></li><li><em class='bbc'>view-roleCondition.vm</em>: Displays the selected role:<br /></li></ul>	<pre class='prettyprint'>	<br />#if ($role)<br />	  User should have ${role.name} Role!<br />	#else<br />	  Role Not Defined<br />	#end	<br />	</pre><br />	<br />	</li><li>The third step is to write the actual condition. The condition class should extend the <em class='bbc'>AbstractJiraCondition</em> class. Here we need to implement the <em class='bbc'>passesCondition</em> method. In our case, we retrieve the project from the issue, check if the user has the appropriate project role, and return true if the user does:<br />	<pre class='prettyprint'>	<br />public boolean passesCondition(Map transientVars, Map args,<br />	PropertySet ps) throws WorkflowException {<br />		Issue issue = getIssue(transientVars);<br />		User user = getCaller(transientVars, args);	project project = issue.getProjectObject();<br />		String role = (String)args.get(ROLE);<br />		Long roleId = new Long(role);	return projectRoleManager.isUserInProjectRole(user,<br />	projectRoleManager.getProjectRole(roleId), project);<br />	}	<br />	</pre><br />	<br />The issue on which the condition is checked can be retrieved using the <em class='bbc'>getIssue</em> method implemented in the <em class='bbc'>AbstractJiraCondition</em> class. Similarly, the user can be retrieved using the <em class='bbc'>getCaller</em> method. In the preceding method, <em class='bbc'>projectRoleManager</em> is injected in the constructor, as we have seen before.<br /></li><li>We can see that the <em class='bbc'>ROLE</em> key is used to retrieve the project role ID from the args parameter in the <em class='bbc'>passesCondition</em> method. In order for the <em class='bbc'>ROLE</em> key to be available in the args map, we need to override the <em class='bbc'>getDescriptorParams</em> method in the condition factory class, <em class='bbc'>RoleConditionFactory </em>in this case. The <em class='bbc'>getDescriptorParams</em> method returns a map of sanitized parameters, which will be passed into workflow plugin instances from the values in an array form submitted by velocity, given a set of <em class='bbc'>name:value </em>parameters from the plugin configuration page (that is, the 'input-parameters' velocity template). In our case, the method is overridden as follows:<br />	<pre class='prettyprint'>	<br />public Map getDescriptorParams(Map<br />	conditionParams) {<br />		if (conditionParams != null &&<br />	conditionParams.containsKey(ROLE))<br />			{<br />				return EasyMap.build(ROLE,<br />	extractSingleParam(conditionParams, ROLE));<br />			}<br />			// Create a 'hard coded' parameter<br />			return EasyMap.build();<br />	  }	<br />	</pre><br />	<br />The method here builds a map of the <em class='bbc'>key:value</em> pair, where key is <em class='bbc'>ROLE</em> and the value is the role value entered in the input configuration page. The <em class='bbc'>extractSingleParam</em> method is implemented in the <em class='bbc'>AbstractWorkflowPluginFactory</em> class. The <em class='bbc'>extractMultipleParams</em> method can be used if there is more than one parameter to be extracted!<br /></li><li>All that is left now is to populate the <em class='bbc'>atlassian-plugin.xml</em> file with the aforementioned components. We use the <em class='bbc'>workflow-condition</em> module and it looks like the following block of code:<br />	<pre class='prettyprint'>	<br />	<br />	</pre><br /></li><li>Package the plugin and deploy it!<br />How it works...<br />After the plugin is deployed, we need to modify the workflow to include the condition. The following screenshot is how the condition looks when it is added initially. This, as you now know, is rendered using the input template:<br /></li></ul><br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-01.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
After the condition is added (that is, after selecting the <strong class='bbc'>Developers</strong> role), the view is rendered using the view template and looks as shown in the following screenshot:<br />
 <br />
<p class='bbc_center'>/sites/default/files/Article-Images/1803-04-02.png</p><br />
<br />
<p class='bbc_center'><em class='bbc'>(Move the mouse over the image to enlarge.)</em></p><br />
<br />
If you try to edit it, the screen will be rendered using the edit template, as shown in the following screenshot:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-03.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
Note that the <strong class='bbc'>Developers </strong>role is already selected.<br />
After the workflow is configured, when the user goes to an issue, he/she will be presented with the transition only if he/she is a member of the project role where the issue belongs. It is while viewing the issue that the <em class='bbc'>passesCondition</em> method in the <em class='bbc'>condition</em> class is executed.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Writing a workflow validator</strong></span><br />
Workflow validators are specific validators that check whether some pre-defined constraints are satisfied or not while progressing on a workflow. The constraints are configured in the workflow and the user will get an error if some of them are not satisfied. A typical example would be to check if a particular field is present or not before the issue is moved to a different status.<br />
Workflow validators are created with the help of the <em class='bbc'>workflow- validator</em> module. The following are the key attributes and elements supported.<br />
<br />
<strong class='bbc'>Attributes:</strong><br />
<br />
<br />
										<br />
Class to provide contexts for rendered velocity templates. Must implement the com.atlassian.jira.plugin.workflow.WorkflowPluginValidatorFactory interface.						<br />
<strong class='bbc'>Elements:</strong><br />
<br />
<br />
										<br />
Class which does the validation. Must implement com.opensymphony.workflow.Validator.						<br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'>See <a href='http://confluence.atlassian.com/display/JIRADEV/Workflow+Plugin+Modules#WorkflowPluginModules-Validators' class='bbc_url' title='External link' rel='nofollow external'>http://confluence.at...ules-Validators</a> for more details. </em></p><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Getting ready</strong></span><br />
As usual, create a skeleton plugin. Create an eclipse project using the skeleton plugin and we are good to go!<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>How to do it...</strong></span><br />
Let us consider writing a validator that checks whether a particular field has a value entered on the issue or not! We can do this using the following steps:<br />
<ul class='bbcol decimal'><br /><li>Define the inputs needed to configure the workflow validator:<br />			We need to implement the <em class='bbc'>WorkflowPluginValidatorFactory</em> interface, which mainly exists to provide velocity parameters to the templates. It will be used to extract the input parameters that are used in defining the validator. To make it clear, the inputs here are not the input while performing the workflow action, but the inputs in defining the validator.<br />			The validator factory class, <em class='bbc'>FieldValidatorFactory</em> in this case, extends the <em class='bbc'>AbstractWorkflowPluginFactory</em> interface and implements the <em class='bbc'>WorkflowPluginValidatorFactory</em> interface. Just like conditions, there are three abstract methods that we should implement. They are <em class='bbc'>getVelocityParamsForInput</em>, <em class='bbc'>getVelocityParamsForEdit</em>, and <em class='bbc'>getVelocityParamsForView</em>. All of them, as the names suggest, are used for populating the velocity parameters in different scenarios.<br />			In our example, we have a single input field, which is the name of a custom field. The three methods will be implemented as follows:<br />			<pre class='prettyprint'>	<br />		@Override<br />			protected void getVelocityParamsForEdit(Map velocityParams,<br />			AbstractDescriptor descriptor) {<br />				velocityParams.put(FIELD_NAME, getFieldName(descriptor));<br />			  velocityParams.put(FIELDS, getCFFields());<br />			}@Override<br />			protected void getVelocityParamsForInput(Map velocityParams) {<br />				velocityParams.put(FIELDS, getCFFields());<br />			}@Override<br />			protected void getVelocityParamsForView(Map velocityParams,<br />			AbstractDescriptor descriptor) {<br />				velocityParams.put(FIELD_NAME, getFieldName(descriptor));<br />			}	<br />			</pre><br />			<br />		You may have noticed that the methods look quite similar to the ones in a workflow condition, except for the business logic! Let us look at the methods in detail:<br /><ul class='bbc'><br /><li><em class='bbc'>getVelocityParamsForInput</em>: This method defines the velocity parameters for input scenario, that is, when the user initially configures the workflow. In our example, we need to display all the custom fields, so that the user can select one to use in the validator. The method <em class='bbc'>getCFFields</em> returns all the custom fields and the collection of fields is then put into the velocity parameters with the key fields.<br /></li><li><em class='bbc'>getVelocityParamsForView</em>: This method defines the velocity parameters for the view scenario, that is, how the user sees the validator after it is configured. In our example, we have defined a field and so we should display it to the user after retrieving it back from the workflow descriptor. You may have noticed that the descriptor, which is an instance of <em class='bbc'>AbstractDescriptor</em>, is available as an argument in the method. All we need is to extract the field name from the descriptor, which can be done as follows:<br />			<pre class='prettyprint'>	<br />		private String getFieldName(AbstractDescriptor descriptor){<br />			  if (!(descriptor instanceof ValidatorDescriptor)) {<br />				throw new IllegalArgumentException('Descriptor must be a<br />			ValidatorDescriptor.');<br />			  }<br />			  <br />			  ValidatorDescriptor validatorDescriptor = (ValidatorDescriptor)<br />			descriptor;  String field = (String)<br />			validatorDescriptor.getArgs().get(FIELD_NAME);<br />			  if (field != null && field.trim().length() &gt; 0)<br />				return field;<br />			  else<br />				return NOT_DEFINED;<br />			}	<br />			</pre><br />			<br />		Just check if the descriptor is a validator descriptor or not and then extract the field as shown in the preceding snippet.<br /></li><li><em class='bbc'>getVelocityParamsForEdit</em>: This method defines the velocity parameters for the edit scenario, that is, when the user modifies the existing validator. Here we need both the options and the selected value. Hence we put both the custom fields' collection and the field name onto the velocity parameters.<br /></li></ul>	<br /></li></ul><br />
	<ul class='bbc'><br /><li>The second step is to define the velocity templates for each of the three aforementioned scenarios, namely, input, view, and edit. We can use the same template here for input and edit with a simple checking to keep the old field selected for the edit scenario. Let us look at the template:<br /><ul class='bbc'><br /><li><em class='bbc'>edit-fieldValidator.vm</em>: Displays all custom fields and highlights the already selected one in edit mode. In input mode, the field variable will be null, and so nothing is pre-selected:<br />	<pre class='prettyprint'>	<br />	<br />	<br />	  <br />	  <br />		<br />		#foreach ($cf in $fields)<br />				  #if ($cf.name.equals($field)) SELECTED #end<br />		  &gt;$cf.name<br />		#end<br />		<br />		<br />	Select the Custom Field to be validated<br />	for NULL<br />	  <br />	<br />	<br />	</pre><br /></li><li>view-fieldValidator.vm: Displays the selected field:<br />	<pre class='prettyprint'>	<br />#if ($field)<br />	  Field '$field' is Required!<br />	#end	<br />	</pre><br /></li></ul>	</li><li>The third step is to write the actual validator. The <em class='bbc'>validator</em> class should implement the <em class='bbc'>Validator</em> interface. All we need here is to implement the <em class='bbc'>validate</em> method. In our example, we retrieve the custom field value from the issue and throw an <em class='bbc'>InvalidInputException</em> if the value is null (empty):<br />	<pre class='prettyprint'>	<br />public void validate(Map transientVars, Map args, PropertySet ps)<br />	throws InvalidInputException, WorkflowException {<br />		Issue issue = (Issue) transientVars.get("issue");<br />		String field = (String) args.get(FIELD_NAME);	  CustomField customField =<br />	customFieldManager.getCustomFieldObjectByName(field);	if (customField!=null){<br />		  //Check if the custom field value is NULL<br />		  if (issue.getCustomFieldValue(customField) == null){<br />			throw new InvalidInputException("The field:"+field+" is<br />				 required!"); }<br />		}<br />	  }	<br />	</pre><br />	<br />The issue on which the validation is done can be retrieved from the <em class='bbc'>transientVars</em> map. <em class='bbc'>customFieldManager</em> is injected in the constructor as usual.<br /></li><li>All that is left now is to populate the <em class='bbc'>atlassian-plugin.xml</em> file with these components. We use the <em class='bbc'>workflow-validator</em> module, and it looks like the following block of code:<br />	<pre class='prettyprint'>	<br />class="com.jtricks.FieldValidatorFactory"&gt;<br />		Field Not Empty Workflow Validator	com.jtricks.FieldValidator	location="templates/com/jtricks/view-fieldValidator.vm"/&gt;<br />		location="templates/com/jtricks/edit-fieldValidator.vm"/&gt;<br />		location="templates/com/jtricks/edit-fieldValidator.vm"/&gt;	<br />	</pre><br /></li><li>Package the plugin and deploy it!<br />Note that we have stored the role name instead of the ID in the workflow, unlike what we did in the workflow condition. However, it is safe to use the ID because administrators can rename the roles, which would then need changes in the workflows.<br /><br />How it works...<br />After the plugin is deployed, we need to modify the workflow to include the validator. The following screenshot is how the validator looks when it is added initially. This, as you now know, is rendered using the input template:<br /></li></ul><br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-04.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
After the validator is added (after selecting the <strong class='bbc'>Test Number</strong> field), it is rendered using the view template and looks as follows:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-05.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
If you try to edit it, the screen will be rendered using the edit template, as shown in the following screenshot:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-06.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
Note that the <strong class='bbc'>Test Number</strong> field is already selected.<br />
After the workflow is configured, when the user goes to an issue and tries to progress it, the validator will check if the <strong class='bbc'>Test Number</strong> field has a value or not. It is at this point that the validate method in the <em class='bbc'>FieldValidator</em> class is executed.<br />
If the value is missing, you will see an error, as shown in the following screenshot:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-07.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Writing a workflow post function</strong></span><br />
Let us now look at workflow post functions. Workflow post functions are very effective and heavily used. They allow you to do a lot of things when you progress on the workflow on an issue. A lot of customizations and workarounds take this route!<br />
Workflow post functions are created with the help of the <em class='bbc'>workflow-function</em> module. The following are the key attributes and elements supported.<br />
<br />
<strong class='bbc'>Attributes:</strong><br />
<br />
<br />
										<br />
Class to provide contexts for rendered velocity templates. Must implement the com.atlassian.jira.plugin.workflow.WorkflowNoInputPluginFactory interface if the function doesn't need input, or com.atlassian.jira.plugin.workflow.WorkflowPluginFunctionFactory if it needs input.						<br />
<strong class='bbc'>Elements:</strong><br />
<br />
<br />
										<br />
Class which does the validation. Must implement com.opensymphony.workflow.FunctionProvider. Recommended to extend com.atlassian.jira.workflow.function.issue.AbstractJiraFunctionProvider, as it already implements many useful methods.						<br />
There are three other elements that can be used with a post function. They are explained as follows:<br />
<ul class='bbc'><br /><li><em class='bbc'>orderable </em>– (true/false) Specifies if this function can be re-ordered within the list of functions associated with a transition. The position within the list determines when the function actually executes.<br /></li><li><em class='bbc'>unique </em>– (true/false) Specifies if this function is unique, that is, if it is possible to add multiple instances of this post function on a single transition.<br /></li><li><em class='bbc'>deletable</em> – (true/false) Specifies if this function can be removed from a transition.<br /></li></ul><br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'>See <a href='http://confluence.atlassian.com/display/JIRADEV/Workflow+Plugin+Modules#WorkflowPluginModules-Functions' class='bbc_url' title='External link' rel='nofollow external'>http://confluence.at...dules-Functions</a> for more details. </em></p><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Getting ready</strong></span><br />
As usual, create a skeleton plugin. Create an eclipse project using the skeleton plugin and we are good to go!<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>How to do it...</strong></span><br />
Assume we have a user custom field and we want to set the current user or a specified user name on to the custom field when a particular transition happens. A typical use case for this will be to store the name of the user who last resolved an issue. The following are the steps to write a generic post function that sets the current username or a username provided by the user on a user custom field:<br />
<ul class='bbcol decimal'><br /><li>Define the inputs needed to configure the workflow post function:<br />			As opposed to workflow conditions and validators, there are two interfaces available for a workflow post function factory class. If there are no inputs needed to configure the function, the factory class must implement <em class='bbc'>WorkflowNoInputPluginFactory</em>.<br />			An example will be to set the current user's name as the custom field value instead of the user configured name. If inputs are needed to configure the post function, the factory class must implement <em class='bbc'>WorkflowPluginFunctionFactory</em>. In our example, we take the username as the input.<br />			Both the interfaces mainly exist to provide velocity parameters to the templates. They will be used to extract the input parameters that are used in defining the functions. To make it clear, the inputs here are not the input while performing the workflow action, but the inputs in defining the post function.<br />			The function factory class, <em class='bbc'>SetUserCFFunctionFactory</em> in this case, extends the <em class='bbc'>AbstractWorkflowPluginFactory</em> and implements the <em class='bbc'>WorkflowPluginFunctionFactory</em> interface. Just like conditions, there are three abstract methods that we should implement, namely, <em class='bbc'>getVelocityParamsForInput</em>, <em class='bbc'>getVelocityParamsForEdit</em>, and <em class='bbc'>getVelocityParamsForView</em>. All of them, as the names suggest, are used for populating the velocity parameters for the different scenarios:<br />			<pre class='prettyprint'>	<br />		@Override<br />			protected void getVelocityParamsForEdit(Map velocityParams,<br />			AbstractDescriptor descriptor) {<br />				velocityParams.put(USER_NAME, getUserName(descriptor));<br />			}@Override<br />			protected void getVelocityParamsForInput(Map velocityParams) {<br />				velocityParams.put(USER_NAME, CURRENT_USER); }@Override<br />			protected void getVelocityParamsForView(Map velocityParams,<br />			AbstractDescriptor descriptor) {<br />				velocityParams.put(USER_NAME, getUserName(descriptor));<br />			}	<br />			</pre><br />			<br />		You may have noticed that the methods look very similar to the ones in workflow conditions or validators, except for the business logic! Let us look at the methods in detail:<br /><ul class='bbc'><br /><li><em class='bbc'>getVelocityParamsForInput</em> : This method defines the velocity parameters for input scenario, that is, when the user initially configures the workflow. In our example, we need to use a text field that captures the username to be added on the issue.<br /></li><li><em class='bbc'>getVelocityParamsForView</em>: This method defines the velocity parameters for the view scenario, that is, how the user sees the post function after it is configured. In our example, we have defined a field, and so we should display it to the user after retrieving it from the workflow descriptor. You may have noticed that the descriptor, which is an instance of <em class='bbc'>AbstractDescriptor</em>, is available as an argument in the method. All we need is to extract the username from the descriptor, which can be done as follows:<br />			<pre class='prettyprint'>	<br />		private String getUserName(AbstractDescriptor descriptor){<br />				if (!(descriptor instanceof FunctionDescriptor)) {<br />				  throw new IllegalArgumentException("Descriptor must be a<br />			 FunctionDescriptor.");<br />				}	FunctionDescriptor functionDescriptor =<br />			(FunctionDescriptor) descriptor;<br />			 String user = (String)<br />			functionDescriptor.getArgs().get(USER_NAME);<br />				if (user!=null && user.trim().length()&gt;0)<br />				  return user;<br />				else<br />				  return CURRENT_USER;<br />			  }	<br />			</pre><br />			<br />		Just check if the descriptor is a validator descriptor or not, and then extract the field as shown in the preceding snippet.<br /></li><li><em class='bbc'>getVelocityParamsForEdit</em>: This method defines the velocity parameters for the edit scenario, that is, when the user modifies the existing validator. Here we need both the options and the selected value. Hence, we put both the custom fields' collection and the field name on to the velocity parameters.<br /></li></ul>	<br /></li></ul><br />
	<ul class='bbc'><br /><li>The second step is to define the velocity templates for each of the three scenarios: input, view, and edit. We can use the same template here for input and edit with a simple checking to keep the old field selected for the edit scenario. Let us look at the templates:<br /><ul class='bbc'><br /><li><em class='bbc'>edit-userCFFunction.vm</em>: Displays all custom fields and highlights the already selected one in the edit mode:<br />	<pre class='prettyprint'>	<br />	<br />	</pre><br /></li><li><em class='bbc'>view-userCFFunction.vm</em> .displays the selected field:<br />	<pre class='prettyprint'>	<br />#if ($user)<br />	  The 'Test User' CF will be set with value : $user!<br />	#end	<br />	</pre><br /></li></ul>	</li><li>The third step is to write the actual function. The function class must extend the <em class='bbc'>AbstractJiraFunctionProvider</em> interface. All we need here is to implement the <em class='bbc'>execute</em> method. In our example, we retrieve the username from the issue and set it on the <em class='bbc'>Test User</em> custom field:<br />	<pre class='prettyprint'>	<br />public void execute(Map transientVars, Map args, PropertySet ps)<br />	throws WorkflowException {<br />		MutableIssue issue = getIssue(transientVars);<br />		User user = null;	if (args.get("user") != null) {<br />		  String userName = (String) args.get("user");<br />		  if (userName.equals("Current User")){<br />			// Set the current user here!<br />			user = authContext.getUser();<br />		  } else {<br />			user = userUtil.getUser(userName);<br />		  }<br />		} else {<br />		  // Set the current user here!<br />		  user = authContext.getUser();<br />		}<br />		// Now set the user value to the custom field<br />		CustomField userField =<br />	customFieldManager.getCustomFieldObjectByName("Test User");<br />		if (userField != null) {<br />		  try {<br />			setUserValue(issue, user, userField);<br />		  } catch (FieldLayoutStorageException e) {<br />			System.out.println("Error while setting the user Field");<br />		  }<br />		}<br />	 }	<br />	</pre><br />	<br />Like a validator, the issue on which the post function is executed can be retrieved using the <em class='bbc'>transientVars</em> map. The user can be retrieved from the <em class='bbc'>args</em> map.<br />	Here the <em class='bbc'>setUserValue </em>method simply sets the username on the passed custom field, as shown in the following block of code:	<br />	<pre class='prettyprint'>	<br />private void setUserValue(MutableIssue issue, User user,<br />	CustomField userField) throws FieldLayoutStorageException {<br />		issue.setCustomFieldValue(userField, user);<br />		Map modifiedFields = issue.getModifiedFields();<br />		FieldLayoutItem fieldLayoutItem =<br />	ComponentManager.getInstance().getFieldLayoutManager().getFieldLay<br />	out(issue).getFieldLayoutItem(userField);<br />		DefaultIssueChangeHolder issueChangeHolder = new<br />	DefaultIssueChangeHolder();<br />		final ModifiedValue modifiedValue = (ModifiedValue)<br />	modifiedFields.get(userField.getId());	<br />	userField.updateValue(fieldLayoutItem, issue, modifiedValue,<br />	issueChangeHolder);<br />	  }	<br />	</pre><br /></li><li>All that is left now is to populate the <em class='bbc'>atlassian-plugin.xml</em> file with these components. We use the <em class='bbc'>workflow-condition</em> module and it looks like the following block of code:<br />	<pre class='prettyprint'>	<br />	<br />	</pre><br /></li><li>Package the plugin and deploy it!<br />How it works...<br />After the plugin is deployed, we need to modify the workflow to include the function. The following is where the function appears along with the built-in ones:<br /></li></ul><br />
<p class='bbc_center'>/sites/default/files/Article-Images/1803-04-08.png</p><br />
<br />
Clicking on our post function takes us to the configuration page, shown next. This, as you now know, is rendered using the input template:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-09.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
After the function is added (after entering in the <strong class='bbc'>UserName</strong> field), it looks as follows:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-10.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
If you try to edit, the screen will be rendered using the edit template, as shown in the following screenshot:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/1803-04-11.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
Note that the <strong class='bbc'>UserName</strong> field is already populated.<br />
After the workflow is configured, when the user executes the workflow action, the <strong class='bbc'>Test User</strong> custom field is set with the value <strong class='bbc'>jobinkk</strong>.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Editing an active workflow</strong></span><br />
We have seen how the workflow plays an important role in configuring our JIRA and how we can write plugins to add more workflow conditions, validators, and post functions. Once these plugins are added, we need to modify the workflow to include the newly created components at the appropriate transitions.<br />
Modifying an inactive workflow or creating a new workflow is pretty easy. You can add the conditions/validators/post functions when you create the transition or just click on the transition to modify them. But to edit an active workflow, there are a few more steps involved which we will see in this recipe.<br />
A workflow is active when it is being used in an active workflow scheme that is tied to a project. You can check whether a workflow is active by navigating to <strong class='bbc'>Administration Global Settings | Workflows|</strong>.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>How to do it...</strong></span><br />
The following are the steps to edit an active workflow:<ul class='bbcol decimal'><br /><li>Login as a JIRA Administrator.<br /></li><li>Navigate to <strong class='bbc'>Administration Global Settings | Workflows|</strong>.<br /></li><li>Click on the <strong class='bbc'>Create a draft workflow</strong> link on the workflow you want to edit. The link can be found under the <strong class='bbc'>Operations</strong> column.<br /></li><li>Click on the step or transition that you want to modify.<br /></li><li>Make the changes. The changes won't be effective until the workflow is published.<br /></li><li>After all the changes are made, click on the <strong class='bbc'>publish this draft</strong> link at the top of the page if you are still viewing the modified workflow. You can also click on <strong class='bbc'>Publish </strong>under the <strong class='bbc'>Operations</strong> column while viewing all the workflows.<br /></li><li>Make a copy of the old workflow, when prompted, if you need a backup, and click on <strong class='bbc'>Publish</strong>.<br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>How it works...</strong></span><br />
After making changes on the draft and clicking on <strong class='bbc'>Publish</strong>, the new workflow will be active. However, there are some limitations to this procedure, which are detailed as follows:<br />
<ul class='bbc'><br /><li>You can't delete an existing workflow step<br /></li><li>You can't edit the status associated with an existing step<br /></li><li>If an existing step has no outgoing transitions, you can't add any new outgoing transitions<br /></li><li>You can't change the step IDs for any existing steps<br /></li></ul><br />
If you want to overcome these limitations, you need to copy the workflow, modify the copy, and make it active by migrating the projects on to the new workflow.<br />
After the new workflow is active, any transitions on the issue will be based on the new workflow.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>There's more...</strong></span><br />
If you want to modify an active workflow, thus overcoming some of the limitations aforementioned but don't want to go through the pain of migrating all the projects involved, you might want to look at modifying it directly in the JIRA database.<br />
Note that we should be careful about the workflow changes when we do this. For example, if there are issues in a status that is removed in the modified workflow, those issues will be stuck at the removed status. The same can happen for the removed steps.<br />
<br />
<strong class='bbc'>	Modifying workflows in JIRA database</strong><br />
<br />
The following are the steps to modify the workflows in the database:<ul class='bbcol decimal'><br /><li>export the workflow that needs to be modified into XML. You can do it using the XML link under the <strong class='bbc'>Operations</strong> column of a workflow.<br /></li><li>Modify the XML to include your changes (or alternatively, make changes in a copy of the JIRA workflow and export that as XML).<br /></li><li>Stop the JIRA instance.<br /></li><li>Connect to your JIRA database.<br /></li><li>Take a backup of the existing database. We can revert to this backup if anything goes wrong.<br /></li><li>Update the <em class='bbc'>JIRAWORKFLOWS </em>table to modify the <em class='bbc'>descriptor</em> column with the new XML file for the appropriate workflow. When the workflow XML is huge, it might be useful to rely on database-specific methods to update the table. For example, we can use Oracle XML database utilities (<a href='http://download.oracle.com/docs/cd/B12037_01/appdev.101/b10790/xdb01int.htm' class='bbc_url' title='External link' rel='nofollow external'>http://download.orac...90/xdb01int.htm</a>), if JIRA is connected to the Oracle database.<br /></li><li>Commit the changes and disconnect from the database.<br /></li><li>Start the JIRA instance.<br /></li><li>Re-index JIRA.<br /></li></ul>]]></description>
		<pubDate>Wed, 01 Feb 2012 03:30:23 +0000</pubDate>
		<guid isPermaLink="false">8c23abf230b77ce18d89e5c51ee4f509</guid>
	</item>
	<item>
		<title>.NET Generics 4.0: Container Patterns and Best...</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/net-generics-40-container-patterns-and-best-r2869</link>
		<description><![CDATA[There are several generic containers and generic algorithms available in the .NET Framework and a couple of other majorly accepted APIs such as Power Collections and C5.<br />
In this article by <strong class='bbc'>Sudipta Mukherjee</strong>, author of <a href='http://www.packtpub.com/net-generics-4-0-beginners-guide/book/vf/netgenerics-abr1/0112?utm_source=vf_netgenerics_abr1_0112&utm_medium=content&utm_campaign=veronica' class='bbc_url' title='External link' rel='nofollow external'>.NET Generics 4.0 Beginner’s Guide</a>, we will take a look at:<br />
<ul class='bbc'><br /><li>Generic container patterns: There are several patterns that are used more than the others in code bases that use Generics. Here, we shall walk through some of these very popular generic structures.<br /></li><li>Best practices: Here we shall walk through a list of best practices with succinct causes to back them.<br /></li></ul><br />
<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Generic container patterns</strong></span><br />
There are several generic containers such as <em class='bbc'>List</em>, <em class='bbc'>Dictionary</em>, and so on. Now, let's take a look at some of the patterns involving these generic containers that show up more often in code.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>How these are organized</strong></span><br />
Each pattern discussed in this article has a few sections. First is the title. This is written against the pattern sequence number. For example, the title for <em class='bbc'>Pattern 1</em> is <em class='bbc'>One-to-one mapping</em>. The <em class='bbc'>Pattern interface</em> section denotes the interface implementation of the pattern. So anything that conforms to that interface is a concrete implementation of that pattern. For example, <em class='bbc'>Dictionary</em> is a concrete implementation of <em class='bbc'>IDictionary</em>. The Example usages section shows some implementations where <em class='bbc'>TKey</em> and <em class='bbc'>TValue</em> are replaced with real data types such as <em class='bbc'>string</em> or <em class='bbc'>int</em>. The last section, as the name suggests, showcases some ideas where this pattern can be used.<br />
<br />
<strong class='bbc'>	Pattern 1: One-to-one mapping</strong><br />
<br />
One-to-one mapping maps one element to another.<br />
<br />
<strong class='bbc'>	Pattern interface</strong><br />
<br />
The following is an interface implementation of this pattern:<br />
<em class='bbc'>IDictionary</em><br />
<br />
<strong class='bbc'>	Some concrete implementations</strong><br />
<br />
Some concrete implementations of this pattern are as follows:<br />
<br />
<strong class='bbc'>	Example usages</strong><br />
<br />
The following are examples where <em class='bbc'>TKey</em> and <em class='bbc'>TValue</em> are replaced with real data types such as <em class='bbc'>string</em> or <em class='bbc'>int</em>:<br />
<ul class='bbc'><br /><li><em class='bbc'>Dictionary</em><br /></li><li><em class='bbc'>SortedDictionary</em><br /></li><li><em class='bbc'>SortedList</em><br /></li><li><em class='bbc'>Dictionary</em><br /></li></ul><br />
<strong class='bbc'>	Some situations where this pattern can be used</strong><br />
<br />
One-to-one mapping can be used in the following situations:<br />
<ul class='bbc'><br /><li>Mapping some class objects with a string ID<br /></li><li>Converting an <em class='bbc'>enum</em> to a string<br /></li><li>General conversion between types<br /></li><li>Find and replace algorithms where the <em class='bbc'>find</em> and <em class='bbc'>replace</em> strings become key and value pairs<br /></li><li>Implementing a state machine where each state has a description, which becomes the key, and the concrete implementation of the <em class='bbc'>IState</em> interface becomes the value of a structure such as <em class='bbc'>Dictionary</em><br /></li></ul><br />
<strong class='bbc'>	Pattern 2: One-to-many unique value mapping</strong><br />
<br />
One-to-many unique value mapping maps one element to a set of unique values.<br />
<br />
<strong class='bbc'>	Pattern interface</strong><br />
<br />
The following is an interface implementation of this pattern:<br />
<em class='bbc'>IDictionary></em><br />
<br />
<strong class='bbc'>	Some concrete implementations</strong><br />
<br />
Some concrete implementations of this pattern are as follows:<br />
<ul class='bbc'><br /><li><em class='bbc'>Dictionary></em><br /></li><li><em class='bbc'>SortedDictionary></em><br /></li><li><em class='bbc'>SortedList></em><br /></li><li><em class='bbc'>Dictionary></em><br /></li></ul><br />
<strong class='bbc'>	Example usages</strong><br />
<br />
The following are examples where <em class='bbc'>TKey</em> and <em class='bbc'>TValue</em> are replaced with real data types such as <em class='bbc'>string</em> or <em class='bbc'>int</em>:<br />
<br />
<strong class='bbc'>	Some situations where this pattern can be used</strong><br />
<br />
One-to-many unique value mapping can be used in the following situations:<br />
<ul class='bbc'><br /><li>Mapping all the anagrams of a given word<br /></li><li>Creating spell check where all spelling mistakes can be pre-calculated and stored as unique values<br /></li></ul><br />
<strong class='bbc'>	Pattern 3: One-to-many value mapping</strong><br />
<br />
One-to-many value mapping maps an element to a list of values. This might contain duplicates.<br />
<br />
<strong class='bbc'>	Pattern interface</strong><br />
<br />
The following are the interface implementations of this pattern:<br />
<br />
<strong class='bbc'>	Some concrete implementations</strong><br />
<br />
Some concrete implementations of this pattern are as follows:<br />
<ul class='bbc'><br /><li><em class='bbc'>Dictionary></em><br /></li><li><em class='bbc'>SortedDictionary></em><br /></li><li><em class='bbc'>SortedList></em><br /></li><li><em class='bbc'>Dictionary></em><br /></li></ul><br />
<strong class='bbc'>	Example usages</strong><br />
<br />
The following are examples where <em class='bbc'>TKey</em> and <em class='bbc'>TValue</em> are replaced with real data types such as <em class='bbc'>string</em> or <em class='bbc'>int</em>:<br />
<ul class='bbc'><br /><li><em class='bbc'>Dictionary></em><br /></li><li><em class='bbc'>SortedDictionary></em><br /></li><li><em class='bbc'>SortedList></em><br /></li><li><em class='bbc'>Dictionary></em><br /></li></ul><br />
<strong class='bbc'>	Some situations where this pattern can be used</strong><br />
<br />
One-to-many value mapping can be used in the following situations:<br />
<ul class='bbc'><br /><li>Mapping all the grades obtained by a student. The ID of the student can be the key and the grades obtained in each subject (which may be duplicate) can be stored as the values in a list.<br /></li><li>Tracking all the followers of a Twitter account. The user ID for the account will be the key and all follower IDs can be stored as values in a list.<br /></li><li>Scheduling all the appointments for a patient whose user ID will serve as the key.<br /></li></ul><br />
<strong class='bbc'>	Pattern 4: Many-to-many mapping</strong><br />
<br />
Many-to-many mapping maps many elements of a group to many elements in other groups. Both can have duplicate entries.<br />
<br />
<strong class='bbc'>	Pattern interface</strong><br />
<br />
The following are the interface implementations of this pattern:<br />
<br />
<strong class='bbc'>	Some concrete implementations</strong><br />
<br />
A concrete implementation of this pattern is as follows:<br />
<em class='bbc'>IList>></em><br />
<br />
<strong class='bbc'>	Example usages</strong><br />
<br />
The following are examples where <em class='bbc'>TKey</em> and <em class='bbc'>TValue</em> are replaced with real data types such as <em class='bbc'>string</em> or <em class='bbc'>int</em>:<br />
<br />
<strong class='bbc'>	Some situations where this pattern can be used</strong><br />
<br />
Many-to-many mapping can be used in the following situations:<br />
<ul class='bbc'><br /><li>If many independent values can be mapped to a set of values, then these patterns should be used. <em class='bbc'>ISet</em> implementations don't allow duplicates while <em class='bbc'>ICollection</em> implementations, such as <em class='bbc'>IList</em>, do.<br /></li><li>Imagine a company wants to give a pay hike to its employees based on certain conditions. In this situation, the parameters for conditions can be the independent variable of the Tuples, and IDs of employees eligible for the hike can be stored in an <em class='bbc'>ISet</em> implementation.<br /></li></ul><br />
For concurrency support, replace non-concurrent implementations with their concurrent cousins. For example, replace <em class='bbc'>Dictionary</em> with <em class='bbc'>ConcurrentDictionary</em>.<br />
<br />
<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>A special Tuple pattern</strong></span><br />
Since Tuple is a new inclusion in the .NET 4.0 Framework and can prove to be handy in a lot of situations, I thought it would be a great place to showcase an unusual use of this beautiful data representation technique.<br />
<br />
Let's say we have situations involving four integers. This is generally represented using nested if-else blocks, as shown in the following code. However, we can use a Tuple to refactor this code, as we shall see in a while:<br />
<ul class='bbcol decimal'><br /><li>Create a console app and add these four variables:<br />	<pre class='prettyprint'>	<br />static int x = 11;<br />	static int y = 9;<br />	static int z = 20;<br />	static int w = 30;	<br />	</pre><br /></li><li>Now, add these nested if-else blocks in the <em class='bbc'>Main()</em> method:<br />	<pre class='prettyprint'>	<br />if (x &gt; 9)<br />	{<br />	  if (y &gt; x)<br />	  {<br />		if (z &gt; y)<br />		{<br />		  if (w &gt; 10)<br />		  {<br />			Console.WriteLine(" y &gt; x and z &gt; y and w &gt; 10 ");<br />		  }<br />		  else<br />		  {<br />			Console.WriteLine(" y &gt; x and z &gt; y and w	   }<br />		}<br />		else<br />		{<br />		  if (w &gt; 10)<br />		  {<br />			Console.WriteLine(" y &gt; x and z  10 ");<br />		  }<br />		  else<br />		  {<br />			Console.WriteLine(" y &gt; x and z	   }<br />		}<br />	  }<br />	  else<br />	  {<br />		if (z &gt; y)<br />		{<br />		  if (w &gt; 10)<br />		  {<br />			Console.WriteLine(" y  y and w &gt; 10 ");<br />		  }<br />		  else<br />		  {<br />			Console.WriteLine(" y  y and w	   }<br />		}<br />		else<br />		{<br />		  if (w &gt; 10)<br />		  {<br />			Console.WriteLine(" y  10 ");<br />		  }<br />		  else<br />		  {<br />			Console.WriteLine(" y	   }<br />		}<br />	  }<br />	}	<br />	</pre><br /></li><li>If you run this, you shall see the following output:<br />	<pre class='prettyprint'>	<br />y  y and w &gt; 10	<br />	</pre><br /></li><li>Although this code performs the job, it has the following problems:<br /><ul class='bbc'><br /><li>It is very long. Most programmers will lose track of what they were reading.<br /></li><li>It is impossible to re-use any part of the code that is available under a specific branching.<br /></li></ul>	<br /></li></ul><br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – refactoring deeply nested if-else blocks</strong></span><br />
Follow the given steps:<br />
<ul class='bbcol decimal'><br /><li>We must acknowledge that by using a maximum of four variables there can be 16 different branchings. Of these only eight are shown here. This type of code can be remodeled using the following structure. Now, delete everything in the <em class='bbc'>Main()</em> method and copy the following code:<br />	<pre class='prettyprint'>	<br />//Creating rules list<br />	List&gt;&gt;<br />	rules =<br />	new Liststring&gt;&gt;&gt;();<br />	<br />	//adding rules.The benefit is that entire nested if-else is<br />	//flatend.<br />	rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y &gt; x, z &gt; y, w &gt; 10, DoThis));<br />	rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y &gt; x, z &gt; y, w rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y &gt; x, z  10, DoThis));<br />	rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y &gt; x, z rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y  y, w &gt; 10, DoThis));<br />	rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y  y, w rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y  10, DoThis));<br />	rules.Add(new Tupleint, string&gt;&gt;<br />	(x &gt; 9, y <br />	Console.WriteLine ("Printing using tuple");<br />	//Finding the first rule that matches these conditions.<br />	Tuple&gt;<br />	matchingRule<br />				= rules.First<br />				   (<br />					 rule =&gt;<br />						rule.Item1 == GetExp&lt;b&gt;&lt;/b&gt;ressi&#111;n(x, 9)<br />						//represents x &gt; 9<br />						&& rule.Item2 == GetExp&lt;b&gt;&lt;/b&gt;ressi&#111;n(y, x)<br />						//represents y &gt; x<br />						&& rule.Item3 == GetExp&lt;b&gt;&lt;/b&gt;ressi&#111;n(z, y)<br />						//represents z &gt; y<br />						&& rule.Item4 == GetExp&lt;b&gt;&lt;/b&gt;ressi&#111;n(w, 10)<br />						//represents w &gt; 10<br />					  );<br />	//Find the Matching function.<br />	Func function = matchingRule.Item5;<br />	//Invoke the function.<br />	Console.WriteLine(function.Invoke(x,y,z,w));	<br />	</pre><br /></li><li>This will not compile yet because the <em class='bbc'>DoThis()</em> and <em class='bbc'>GetExp<b></b>ressi&#111;n()</em> methods are not defined. So add these methods as follows:<br />	<pre class='prettyprint'>	<br />private static string DoThis(int x, int y, int z, int w)<br />	{<br />	  string partOne = y &gt; x ? " y &gt; x " : " y   string partTwo = z &gt; y ? " z &gt; y " : " z   string partThree = w &gt; 10 ? " w &gt; 10 " : " w   return partOne + "and" + partTwo + "and" + partThree;<br />	}<br />	<br />	private static bool GetExp&lt;b&gt;&lt;/b&gt;ressi&#111;n(int x, int y)<br />	{<br />	  return x &gt; y;<br />	}	<br />	</pre><br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
Now if you run the program, you shall see the same output as before:<br />
<br />
<pre class='prettyprint'><br />y  y and w &gt; 10<br /><br /></pre><br />
So you saw how the if-else blocks were removed while the rules are still intact. Moreover, the code is more readable now.<br />
<br />
Let's see how this worked. Notice carefully that the last parameter of the Tuples used in the rules is <em class='bbc'>Func</em>. This means, we can place any function that takes four integer parameters and returns a string. The <em class='bbc'>DoThis()</em> method matches this requirement and so we place it in the list:<br />
<br />
<pre class='prettyprint'><br />Tuple&gt;<br /><br /></pre><br />
The previous list represents a relationship between four Boolean exp<b></b>ressi&#111;ns and a function. These Boolean exp<b></b>ressi&#111;ns are independent and the associated function is found using the following concern:<br />
<br />
<pre class='prettyprint'><br />Func function = matchingRule.Item5;<br /><br /></pre><br />
<em class='bbc'>Invoke()</em> is a method to call the function.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Best practices when using Generics</strong></span><br />
The following are the best practices to be followed while using Generics:<br />
<ul class='bbcol decimal'><br /><li>Don't use Generics when you know the situation won't always be generic.<br /></li><li>Use <em class='bbc'>Stack</em> for a Last In First Out (LIFO) list implementation.<br /></li><li>Use <em class='bbc'>Queue</em> for a First In First Out (FIFO) implementation.<br /></li><li>Use <em class='bbc'>List</em> for random access lists with zero-based indexing.<br /></li><li>Use <em class='bbc'>LinkedList</em> to implement a deque because it offers faster insertion and deletion at both ends as opposed to other collections.<br /></li><li>If you have to frequently insert random locations in the list, prefer <em class='bbc'>LinkedList</em> over <em class='bbc'>List</em> because <em class='bbc'>LinkedList</em> is a constant time operation to insert one item in between two elements in a linked list.<br /></li><li>If the random list does not have a duplicate, use <em class='bbc'>HashSet</em> instead. It is the fastest.<br /></li><li>Don't use a for loop over a <em class='bbc'>IDictionary</em> implementation. It can give incorrect results. Use a foreach loop over the keys collection instead.<br /></li><li>Avoid using <em class='bbc'>ElementAt()</em> or, its cousin, the <em class='bbc'>ElementAtOrDefault()</em> method on generic collections that natively don't support zero-based integer indexing, (for example, <em class='bbc'>Stack</em> or <em class='bbc'>Dictionary</em>); since dictionary elements are not guaranteed to be available on an index where they were added.<br /></li><li>Don't use a Tuple with more than seven parameters. Create a class with those parameters and then create a list of that class' objects. Using a Tuple with more than four parameters makes the code look messy and it's not efficient.<br /></li><li>Use <em class='bbc'>SortedDictionary</em> just to get the entries sorted. Don't use it to store simple associative "one-to-one" and "one-to-many" relationships as keeping the keys sorted isn't absolutely necessary. It is very slow compared to a normal dictionary.<br /></li><li>Use <em class='bbc'>HashSet</em> to create a generic set. It's the fastest set implementation available in the .NET Framework.<br /></li><li>Use <em class='bbc'>SortedSet</em> only to create a sorted generic set. Don't use it when you don't want the set to be sorted because it is slower than <em class='bbc'>HashSet</em>.<br /></li><li>Don't expect indexing to work on an array or a list as it would on sets or dictionaries, because sets and dictionaries use hashing or tree-based internal data structures to place elements in the collection.<br /></li><li>Use the available bare minimum implementation for your customized need or resort to creating your own custom collection class from the interfaces. This is a design guideline. Try to make sure your code is as lightweight as possible. If you want a queue, use a <em class='bbc'>Queue</em> instead of a <em class='bbc'>List</em>.<br /></li><li>Use <em class='bbc'>LinkedList</em> when you need to insert elements at arbitrary positions but you don't need to access them randomly via integer indexing.<br /></li><li>Use <em class='bbc'>KeyValuePair</em> to store a key value pair. Avoid using <em class='bbc'>Tuple></em> for this purpose because <em class='bbc'>KeyValuePair</em> is more efficient.<br /></li><li>Prefer <em class='bbc'>Dictionary></em> over <em class='bbc'>List></em> whenever possible for representing a one-to- many relationship between two entities. Lookup speed will be much faster and client code will be less clumsy.<br /></li><li>Prefer <em class='bbc'>List></em> to represent a many-to-one relationship between two entities over <em class='bbc'>Dictionary,TValue></em> if there is a duplicate.<br /></li><li>If no more entries need to be added to a collection, call TrimExcess() to free up the extra memory.<br /></li><li>Prefer LINQ Standard Query Operator <em class='bbc'>Where()</em> over <em class='bbc'>Contains()</em>, <em class='bbc'>Exists()</em>, or <em class='bbc'>Find()</em> methods to check whether a value is present in a <em class='bbc'>List</em> instance.<br /></li><li>If you implement <em class='bbc'>IEnumerable</em> in your custom collection, don't forget to implement IEnumerable also. This is to ensure backward compatibility and the Liskov substitution principle.<br /></li><li>Use <em class='bbc'>SortedList</em> if you want a concrete implementation of <em class='bbc'>IDictionary</em> that supports native indexing. However, it is slower than <em class='bbc'>Dictionary</em>. Avoid <em class='bbc'>SortedList</em> when you don't want indexing.<br /></li><li>Don't use the <em class='bbc'>ElementAt()</em> or the <em class='bbc'>ElementAtOrDefault()</em> method on <em class='bbc'>IDictionary</em> implementations except <em class='bbc'>SortedList</em>, because dictionary elements are not guaranteed to be in the index where they are added.<br /></li><li>Avoid conversions between data structure formats (for example, array to list, or vice versa using LINQ operators) as far as possible. Use other language constructs. For example, consider using a foreach loop than converting an <em class='bbc'>IEnumerable</em> instance to a list using the <em class='bbc'>ToList()</em> method before performing some operations on each item in the list.<br /></li><li>Use the <em class='bbc'>OrderBy()</em> or the <em class='bbc'>OrderByDescending()</em> method to sort any <em class='bbc'>IEnumerable</em>.<br /></li></ul><br />
<span style='font-size: 18px;'><strong class='bbc'>Selecting a generic collection</strong></span><br />
Not all generic containers are geared to do each job well, as we already know it. Here is a table that will let you pick a generic container that supports some features listed in the lefthand column.<br />
<br />
<br />
<br />
<br />
<br />
You can download the <strong class='bbc'>GenGuru</strong> app from the <a href='http://www.packtpub.com/support' class='bbc_url' title='External link' rel='nofollow external'>support website</a>. It's a kind of wizard that will recommend the generic containers available in .NET Generics that you should use. It will ask you few questions and recommend a collection. You can see it in action at <a href='http://sudipta.posterous.com/private/niixaeFlmk' class='bbc_url' title='External link' rel='nofollow external'>http:// sudipta.posterous.com/private/niixaeFlmk</a>.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Best practices when creating custom generic collections</strong></span><br />
The following are the best practices while creating custom generic collections:<br />
<ul class='bbcol decimal'><br /><li>Try to use built-in generic classes for maintaining internal data structures.<br /></li><li>Try to use interfaces as return type and method arguments for publicly exposed methods for your collection. For example, consider using <em class='bbc'>IEnumerable</em> over <em class='bbc'>List</em>.<br /></li><li>Try to keep the names aligned as much as possible towards the names available in frameworks. For example, if you are creating a concrete implementation of <em class='bbc'>IDictionary</em> then try to use the suffix Dictionary in the name of the class, such as <em class='bbc'>MultiDictionary</em>.<br /></li><li>Try to provide overloaded constructors to offer flexibility such that the class can be created from many diverse data sources.<br /></li><li>Use Generics constraints judiciously. Remember that these constraints are like boomerangs. You should know what exactly you can allow as an input parameter for a constrained generic type. Otherwise, these can backfire and hurt you and the users of your collection.<br /></li><li>Make sure to always implement the <em class='bbc'>IEnumerable</em>, <em class='bbc'>IEnumerable</em>, <em class='bbc'>ICollection</em>, <em class='bbc'>IDisposable</em>, and <em class='bbc'>IClonable</em> interfaces.<br /></li><li>Make sure that your collection confirms to the Liskov substitution principle. Divide into subclasses only when it makes sense, otherwise use composition.<br /></li><li>Offer a constructor overload to create a thread-safe instance of your collection.<br /></li><li>Make sure it supports LINQ, which will be obvious if you implement <em class='bbc'>IEnumearble</em> properly. LINQ is changing the way we see and solve problems. So if you miss LINQ, your collection will probably fail to impress a large section of the target audience.<br /></li><li>Thrive for performance. Make sure that performance is as best as it could be.<br /></li><li>Make sure your collection is as lightweight as possible. If a <em class='bbc'>Queue</em> can do what you want done internally in your collection, use it. Don't consider using a more versatile list implementation such as <em class='bbc'>List</em>.<br /></li><li>Don't write raw events by yourself to monitor the collection; rather expose <em class='bbc'>ObservableCollection</em> to offer this functionality.<br /></li><li>Don't provide functionalities that can be achieved by a simple combination of exposed functions. For example, take a look at the Date Time API from the .NET Framework. There is a method called <em class='bbc'>AddDays()</em> that can take a positive or negative integer such that you can go to a past or a future date. However, the framework doesn't provide a method or a public property to compute <em class='bbc'>Tomorrow()</em> or <em class='bbc'>Yesterday()</em> as they can be easily calculated using the <em class='bbc'>AddDays()</em> method. While exposing public functions for your generic collection, remember to expose only those methods that can be used as building blocks for several reasons.<br /></li><li>However, you have to strike a balance. The framework also has the <em class='bbc'>AddYears()</em> method because it will be cumbersome, although technically achievable, to duplicate <em class='bbc'>AddYears()</em> using the <em class='bbc'>AddDays()</em> method.<br /></li></ul><br />
Remember these points while coming up with your own generic collection.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Summary</strong></span><br />
In this article, we took a look at generic container patterns and best practices for Generics.]]></description>
		<pubDate>Wed, 01 Feb 2012 03:28:05 +0000</pubDate>
		<guid isPermaLink="false">5ed17b7aeb1316a3f8daf637f4a7a6b4</guid>
	</item>
	<item>
		<title>Sedge: An Automated Error Reporting Tool</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/sedge-an-automated-error-reporting-tool-r2867</link>
		<description><![CDATA[<ul class='bbc'><br /><li><a href='http://uploads.gamedev.net/cp/1327954995-sedge.bin.1.0.zip' class='bbc_url' title='External link' rel='nofollow external'>Download demo - 248 KB</a><br /></li><li><a href='http://uploads.gamedev.net/cp/1327954995-sedge.src.1.0.zip' class='bbc_url' title='External link' rel='nofollow external'>Download source - 1.26 MB</a><br /></li></ul><br />
<br />
License: <a href='http://www.opensource.org/licenses/ms-pl.html' class='bbc_url' title='External link' rel='nofollow external'><span class='bbc_underline'><strong class='bbc'>Ms-PL</strong></span></a><br />
 <br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954995-Screen1.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Introduction</strong></span><br />
<br />
Every developer knows how important a good bug report is, and every developer wants as much details about the issue as the user can provide. There are different solutions that aim to facilitate the error reporting process, from tiny third-party components to such powerful technologies like Microsoft Windows Error Reporting, but most of them are designed to report fatal error only.<br />
<br />
Missed text, incorrect functionality, wrong output - your users may want to report these issues too. And, even if it looks like a minor problem for the user, you may want to see log files, configurations, system information, etc. Moreover, the user may use multiple applications developed by your company, and to locate the problem, you may be interested to add to the report a few common logs or configurations.<br />
<br />
All these thoughts were taken into account during the designing of <a href='http://sedge.codeplex.com/' class='bbc_url' title='External link' rel='nofollow external'>Sedge</a> - an Open Source, free utility that helps to automate error reporting by collecting all the necessary information, files, descriptions, and presenting it in a nice HTML view. The target platform is Microsoft .NET 2.0.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>How it works</strong></span><br />
<br />
Sedge is a wizard style application, and proposes the user to take an issue screenshot, enter description and contact information, and add the user's files. The last step of the wizard is a report generation. Sedge copies all the files you want to see in the report, collects system information, creates report HTML pages, and zips the report. The user can review the report and email it to you.<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954995-Screen2.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
The configuration file (default extension is <em class='bbc'>.sedge</em>) is the key to all the features of the tool. The configuration describes all the aspects of the process - the wizard's steps, files, and information to collect, report representation, etc. If the path to the configuration file is not provided as a command line parameter, Sedge will try to read it from <em class='bbc'>Sedge.exe.config</em> and tries to search for it in the current folder and subfolders.<br />
<br />
Command line example:<br />
<pre class='prettyprint'><br /><br />Sedge.exe /suite="C:&#092;Apps&#092;QuickStart.sedge"<br /></pre><br />
<br />
<br />
<br />
Configuration example:<br />
<pre class='prettyprint'><br /><br />?&gt;<br />xml version="1.0" encoding="UTF-8" &gt;<br />&lt;Sedge&gt;<br />  &lt;Suite name="LunarFrog.TaggedFrog" <br />	   caption="TaggedFrog Error Reporting" partial="no"&gt;<br />	&lt;Schedule&gt;<br />	  &lt;Step name="Screenshot" /&gt;<br />	  &lt;Step name="ErrorDetails" /&gt;<br />	  &lt;Step name="Contacts" /&gt;<br />	  &lt;Step name="Collect" /&gt;<br />	&lt;/Schedule&gt;<br /><br />	&lt;Transport name="Email"&gt;<br />	  &lt;Option name="to" value="error_mail@company.com" /&gt;<br />	  &lt;Option name="subject" value="Error report" /&gt;<br />	&lt;/Transport&gt;<br /><br />	&lt;Report&gt;<br />	  &lt;Group name="UserGroup" caption="User Information"&gt;<br />		&lt;Page name="ImagePage" caption="Screenshot"&gt;<br />		  &lt;Temp name="screenshot" caption="Screen" /&gt;<br />		&lt;/Page&gt;<br />		&lt;Page name="InfoPage" caption="Information"&gt;<br />		  &lt;Temp name="ReportDetails" caption="Details"/&gt;<br />		  &lt;Temp name="Contacts" caption="Contact Information" /&gt;<br />		  &lt;Temp name="UserFiles" /&gt;<br />		&lt;/Page&gt;<br />	  &lt;/Group&gt;<br />	  &lt;Group name="EnvGroup" caption="Enviroment"&gt;<br />		&lt;Page name="ProcessesPage" caption="Processes"&gt;<br />		  &lt;Data name="Processes" /&gt;<br />		&lt;/Page&gt;<br />		&lt;Page name="SystemPage" caption="System"&gt;<br />		  &lt;Data name="System" /&gt;<br />		&lt;/Page&gt;<br />	  &lt;/Group&gt;<br />	&lt;/Report&gt;<br /><br />	&lt;Application name="TaggedFrog" caption="TaggedFrog"&gt;<br />	  &lt;Report&gt;<br />		&lt;Group name="ApplicationGroup" caption="Application"&gt;<br />		&lt;/Group&gt;<br />	  &lt;/Report&gt;<br />	&lt;/Application&gt;<br /><br />  &lt;/Suite&gt;<br />&lt;/Sedge&gt;<br /></pre><br />
<br />
<br />
<br />
The main unit of the configuration is a suite. The suite represents a collection of the related applications, and could be described in one file or split into multiple parts. This means that you can have one main configuration file describing the wizard UI and the common properties, and add/remove the suite part when you install/uninstall applications. The error report will include information requested in the main configuration plus information specific for the reported application (see Sample 4 in the attached archive).<br />
<br />
The schedule section describes the steps that will be displayed to the user. Every step tag represents a class implementing the IStepController interface, and you can easily create your own steps to add or replace the existing ones (Sample 5). Because the GUI code is separated from the logic, you can even completely replace the user interface, for example, if you like to use WPF instead of WinForms.<br />
<br />
Eventually, you are able to customize almost every aspect of the process. For example, if you don't like e-mail as a method of reports delivering, you can add your own transport protocol.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Report</strong></span><br />
<br />
The next aspect of the report generations is... the report itself. The configuration defines the structure of the report while the style is controlled by the template. The template consists of the HTML, CSS files, and the images, so you can modify it to match your corporate style.<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954995-HtmlReport.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
Every page may include one or more sources of information to report. There are four types of sources:<br />
<ul class='bbc'><br /><li>displays information provided by the user (screenshots, error details, etc.)<br /></li><li>displays information about the files - path, size, and version<br /></li><li>instructs the application to copy files to the report and print links to these files on the page<br /></li><li>is actually a meta-source; the name attribute is used to select a class implementing the IDataSource interface and returning the required information<br /></li></ul><br />
Out of the box, there are three classes:<br />
<ul class='bbc'><br /><li>Processes to display the running processes information<br /></li><li>System to display information about the OS and the hardware<br /></li><li>Table to print the provided information in the table form; it can be used to display the information contained in the properties<br /></li></ul><br />
And as usual, you can create your own source:<br />
<pre class='prettyprint'><br /><br />&lt;&lt;/span&gt;Data name="CurrentDate" assembly="${custom.dll}" caption="Now" /&gt;<br /></pre><br />
<br />
<br />
<br />
A property is a container that can store text information, and can be set directly in the configuration to get a value from the Registry. If you'd like to setup a property in a different way, for example from <em class='bbc'>.ini</em> files, you are able to create a custom property handler.<br />
<pre class='prettyprint'><br /><br />public class EchoProperty : ICustomProperty<br />{<br />	public string GetValue(Options options)<br />	{<br />		return String.Concat("Echo: ", <br />		  options.GetGeneralOption("Param", "param"));<br />	}<br />}<br /></pre><br />
<br />
<br />
<pre class='prettyprint'><br /><br />&lt;Properties&gt;<br />	&lt;Property name="custom.dll" <br />	  value="${sedge.this.folder}&#092;DLL&#092;Demo.Customization.dll" /&gt;<br />	&lt;CustomProperty name="echo.message" <br />	  assembly="${custom.dll}" value="Echo"&gt;<br />		&lt;Option name="Param" value="Hello World!" /&gt;<br />	&lt;/CustomProperty&gt;<br />&lt;/Properties&gt;<br /></pre><br />
<br />
<br />
<br />
Two predefined properties are available in every configuration: <em class='bbc'>${sedge.this.folder}</em> - the path to the configuration file, and <em class='bbc'>${sedge.this.file}</em> - the configuration file name.<br />
<br />
To be usable in the real world, Sedge supports localization and rebranding. All strings are located in a separate file; you can modify the current translation or add a new one, and change the application name.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Conclusion</strong></span><br />
<br />
Sedge aims to help your customers create better bug reports, and gives you a way to control what information should be added to the report. With Sedge, users can report not only fatal crashes but also usability issues, incorrect functionality, etc. One of the main tasks of the application is to provide an error report in the context of the application suites, and it could be achieved using partial configurations. Sedge can be used as a ready-to-go tool, or customized to satisfy your specific goals.<br />
<br />
Project site: <a href='http://sedge.codeplex.com/' class='bbc_url' title='External link' rel='nofollow external'>sedge.codeplex.com</a>.<br />
<br />
First version of the article.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>About the Author</strong></span><br />
<br />
	I am a software developer from Toronto with 15 years of experience in software design and development. My major professional area is the development of highly customized software solutions, including desktop and web applications. Industrial process automation and hardware-related software development are among my favorite projects and I enjoyed developing several applications for semiconductor manufacturing companies.<br />
 <br />
My blog: <a href='http://LunarFrog.com/blog' class='bbc_url' title='External link' rel='nofollow external'>http://LunarFrog.com/blog</a>	<br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>License</strong></span><br />
<br />
This article was authored by Andrei Marukovich and reproduced for the benefit of our viewers under the terms of the <a href='http://www.opensource.org/licenses/ms-pl.html' class='bbc_url' title='External link' rel='nofollow external'>Ms-PL</a> license.]]></description>
		<pubDate>Mon, 30 Jan 2012 20:26:03 +0000</pubDate>
		<guid isPermaLink="false">ae306dc92ae6dfe9049d4b2177bb932d</guid>
	</item>
	<item>
		<title>Inter-Process Communication (IPC) Introduction...</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/inter-process-communication-ipc-introduction-r2865</link>
		<description><![CDATA[<ul class='bbc'><br /><li><a href='http://uploads.gamedev.net/cp/1327954670-IPC_src.zip' class='bbc_url' title='External link' rel='nofollow external'>Download IPC source code - 229.21 KB</a><br /></li></ul><br />
<br />
License: <a href='http://www.opensource.org/licenses/ms-pl.html' class='bbc_url' title='External link' rel='nofollow external'><span class='bbc_underline'><strong class='bbc'>Ms-PL</strong></span></a><br />
 <br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Introduction</strong></span><br />
<br />
Inter-Process Communication (IPC) is a set of techniques for the exchange of data among multiple threads in one or more processes. Processes may be running on one or more computers connected by a network. IPC techniques include Named Pipes, File Mapping, Mailslot, Remote Procedure Calls (RPC), etc.<br />
<br />
In All-In-One Code Framework, we have already implemented samples (C++ and C#) for Named Pipes, File Mapping, Mail Slot, and Remoting. We are going to add more techniques like: Clickbord, Winsock, etc. You can download the latest code from <a href='http://cfx.codeplex.com/' class='bbc_url' title='External link' rel='nofollow external'>http://cfx.codeplex.com/</a>.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Background</strong></span><br />
<br />
<a href='http://cfx.codeplex.com/' class='bbc_url' title='External link' rel='nofollow external'>All-In-One Code Framework</a> (short as AIO) delineates the framework and skeleton of most Microsoft development techniques (e.g., COM, Data Access, IPC) using typical sample codes in different programming languages (e.g., Visual C#, VB.NET, Visual C++).<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Using the Code</strong></span><br />
<br />
Find samples by following the steps below:<br />
<ul class='bbcol decimal'><br /><li>Download the zip file and unzip it.<br /></li><li>Open the folder <em class='bbc'>[Visual Studio 2008]</em>.<br /></li><li>Open the solution file <em class='bbc'>IPC.sln</em>. You must pre-install Visual Studio 2008 on the machine.<br /></li><li>In the Solution Explorer, open the <em class='bbc'>[Process] &#092; [IPC and RPC]</em> folder.<br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>Samples Structure and Relationship</strong></span><br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954670-Structure_of_CodeFx_2008.jpg' alt='Posted Image' class='bbc_img' /></span><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Named Pipe</strong></span><br />
<br />
Named pipes is a mechanism for one-way or bi-directional inter-process communication between a pipe server and one or more pipe clients in the local machine or across computers in an intranet:<br />
<pre class='prettyprint'><br /><br />&#91;b&#93;PIPE_ACCESS_INBOUND:&#91;/b&#93;<br /><br />Client (GENERIC_WRITE) ---&gt; Server (GENERIC_READ)<br /><br /><br />&#91;b&#93;PIPE_ACCESS_OUTBOUND:&#91;/b&#93;<br /><br />Client (GENERIC_READ) &lt;--- Server (GENERIC_WRITE)<br /><br /><br />&#91;b&#93;PIPE_ACCESS_DUPLEX:&#91;/b&#93;<br /><br />Client (GENERIC_READ or GENERIC_WRITE, or both) <br />				&lt;--&gt; Server (GENERIC_READ and GENERIC_WRITE)<br /></pre><br />
<br />
<br />
<br />
This sample demonstrates a named pipe server, <em class='bbc'>&#092;&#092;.&#092;pipe&#092;HelloWorld</em>, that supports PIPE_ACCESS_DUPLEX. It first creates such a named pipe, then it listens to the client's connection. When a client is connected, the server attempts to read the client's requests from the pipe and writes a response.<br />
<br />
A named pipe client attempts to connect to the pipe server, <em class='bbc'>&#092;&#092;.&#092;pipe&#092;HelloWorld</em>, with the GENERIC_READ and GENERIC_WRITE permissions. The client writes a message to the pipe server and receives its response.<br />
<br />
<strong class='bbc'>	Code Logic</strong><br />
<br />
Server-side logic:<br />
<ul class='bbcol decimal'><br /><li>Create a named pipe. (CreateNamedPipe)<br /></li><li>Wait for the client to connect. (ConnectNamedPipe)<br /></li><li>Read client requests from the pipe and write the response. (ReadFile, WriteFile)<br /></li><li>Disconnect the pipe, and close the handle. (DisconnectNamedPipe, CloseHandle)<br /></li></ul><br />
Client-side logic:<br />
<ul class='bbcol decimal'><br /><li>Try to open a named pipe. (CreateFile)<br /></li><li>Set the read mode and the blocking mode of the specified named pipe. (SetNamedPipeHandleState)<br /></li><li>Send a message to the pipe server and receive its response. (WriteFile, ReadFile)<br /></li><li>Close the pipe. (CloseHandle)<br /></li></ul><br />
<strong class='bbc'>	Code - CreateNamedPipe (C++)</strong><br />
<pre class='prettyprint'><br /><br />// Create the named pipe.<br />HANDLE hPipe = CreateNamedPipe(<br /><br />strPipeName,					  // The unique pipe name. This string must<br />								  // have the form of &#092;&#092;.&#092;pipe&#092;pipename<br />PIPE_ACCESS_DUPLEX,			   // The pipe is bi-directional; both <br />								  // server and client processes can read<br />								  // from and write to the pipe<br />PIPE_TYPE_MESSAGE &#124;			   // Message type pipe<br />PIPE_READMODE_MESSAGE &#124;		   // Message-read mode<br />PIPE_WAIT,						// Blocking mode is enabled<br />PIPE_UNLIMITED_INSTANCES,		 // Max. instances<br /><br />// These two buffer sizes have nothing to do with the buffers that<br />// are used to read from or write to the messages. The input and<br />// output buffer sizes are advisory. The actual buffer size reserved<br />// for each end of the named pipe is either the system default, the<br />// system minimum or maximum, or the specified size rounded up to the<br />// next allocation boundary. The buffer size specified should be<br />// small enough that your process will not run out of nonpaged pool,<br />// but large enough to accommodate typical requests.<br /><br />BUFFER_SIZE,					  // Output buffer size in bytes<br />BUFFER_SIZE,					  // Input buffer size in bytes<br />NMPWAIT_USE_DEFAULT_WAIT,		 // Time-out interval<br />&sa							   // Security attributes<br />)<br /></pre><br />
<br />
<br />
<br />
For more code samples, please download AIO source code.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Security Attribute for Named Pipes</strong></span><br />
<br />
If lpSecurityAttributes of CreateNamedPipe is NULL, the named pipe gets a default security descriptor and the handle cannot be inherited. The ACLs in the default security descriptor for a named pipe grants full control to the LocalSystem account, administrators, and the creator owner. They also grant read access to members of the Everyone group and the anonymous account. In other words, with NULL as the security attribute, the named pipe cannot be connected with WRITE permission across the network, or from a local client running as a lower integrity level. Here, we fill the security attributes to grant EVERYONE all access (not just the connect access) to the server. This solves the cross-network and cross-IL issues, but it creates a security hole right there: the clients have WRITE_OWNER access and then the server just loses the control of the pipe object.<br />
<br />
<strong class='bbc'>	Code - Security Attributes (C++)</strong><br />
<pre class='prettyprint'><br /><br />SECURITY_ATTRIBUTES sa;<br />sa.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR)malloc(SECURITY_DESCRIPTOR_MIN_LENGTH);<br />InitializeSecurityDescriptor(sa.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);<br />// ACL is set as NULL in order to allow all access to the object.<br />SetSecurityDescriptorDacl(sa.lpSecurityDescriptor, TRUE, NULL, FALSE);<br />sa.nLength = sizeof(sa);<br />sa.bInheritHandle = TRUE;<br /></pre><br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>.NET Named Pipe</strong></span><br />
<br />
.NET supports named pipes in two ways:<br />
<ul class='bbcol decimal'><br /><li>P/Invoke the native APIs.	<br />By P/Invoke-ing the native APIs from .NET, we can mimic the code logic in CppNamedPipeServer to create the named pipe server, <em class='bbc'>&#092;&#092;.&#092;pipe&#092;HelloWorld</em>, that supports PIPE_ACCESS_DUPLEX.	<br />	<br />PInvokeNativePipeServer first creates such a named pipe, then it listens to the client's connection. When a client is connected, the server attempts to read the client's requests from the pipe and write a response.<br /></li><li>System.IO.Pipes namespace	<br />In .NET Framework 3.5, the namespace System.IO.Pipes and a set of classes (e.g., PipeStream, NamedPipeServerStream) are added to the .NET BCL. These classes make the programming of named pipes in .NET much easier and safer than P/Invoke-ing the native APIs directly.	<br />	<br />BCLSystemIOPipeServer first creates such a named pipe, then it listens to the client's connection. When a client is connected, the server attempts to read the client's requests from the pipe and write a response.<br /></li></ul><br />
<strong class='bbc'>	Code - Create Named Pipe (C#)</strong><br />
<pre class='prettyprint'><br /><br />// Prepare the security attributes <br />// Granting everyone the full control of the pipe is just for <br />// demo purpose, though it creates a security hole. <br />PipeSecurity pipeSa = new PipeSecurity(); <br />pipeSa.SetAccessRule(new PipeAccessRule("Everyone", <br />	   PipeAccessRights.ReadWrite, AccessControlType.Allow)); <br /><br />// Create the named pipe <br />pipeServer = new NamedPipeServerStream( <br />	strPipeName,					// The unique pipe name. <br />	PipeDirection.InOut,			// The pipe is bi-directional <br />	NamedPipeServerStream.MaxAllowedServerInstances, <br />	PipeTransmissionMode.Message,   // Message type pipe <br />	PipeOptions.None,			   // No additional parameters <br />	BUFFER_SIZE,					// Input buffer size <br />	BUFFER_SIZE,					// Output buffer size <br />	pipeSa,						 // Pipe security attributes <br />	HandleInheritability.None	   // Not inheritable <br />);<br /></pre><br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>File Mapping</strong></span><br />
<br />
File mapping is a mechanism for one-way or bi-directional inter-process communication among two or more processes in the local machine. To share a file or memory, all of the processes must use the name or the handle of the same file mapping object.<br />
<br />
To share a file, the first process creates or opens a file by using the CreateFile function. Next, it creates a file mapping object by using the CreateFileMapping function, specifying the file handle and a name for the file mapping object. The names of events, semaphores, mutexes, waitable timers, jobs, and file mapping objects share the same namespace. Therefore, the CreateFileMapping and OpenFileMapping functions fail if they specify a name that is in use by an object of another type.<br />
<br />
To share memory that is not associated with a file, a process must use the CreateFileMapping function and specify INVALID_HANDLE_VALUE as the hFile parameter instead of an existing file handle. The corresponding file mapping object accesses memory backed by the system paging file. You must specify a size greater than zero when you specify an hFile of INVALID_HANDLE_VALUE in a call to CreateFileMapping.<br />
<br />
Processes that share files or memory must create file views by using the MapViewOfFile or MapViewOfFileEx functions. They must coordinate their access using semaphores, mutexes, events, or some other mutual exclusion techniques.<br />
<br />
This example demonstrates a named shared memory server, <em class='bbc'>Local&#092;HelloWorld</em>, that creates the file mapping object with INVALID_HANDLE_VALUE. By using the PAGE_READWRITE flag, the process has read/write permission to the memory through any file view that is created.<br />
<br />
The named shared memory client, <em class='bbc'>Local&#092;HelloWorld</em>, can access the string written to the shared memory by the first process. The console displays the message "Message from the first process" that is read from the file mapping created by the first process.<br />
<br />
<strong class='bbc'>	Code Logic</strong><br />
<br />
Service-side logic:<br />
<ul class='bbcol decimal'><br /><li>Create a file mapping. (CreateFileMapping)<br /></li><li>Map the view of the file mapping into the address space of the current process. (MapViewOfFile)<br /></li><li>Write message to the file view. (CopyMemory)<br /></li><li>Unmap the file view and close the file mapping objects. (UnmapViewOfFile, CloseHandle)<br /></li></ul><br />
Client-side logic:<br />
<ul class='bbcol decimal'><br /><li>Try to open a named file mapping. (OpenFileMapping)<br /></li><li>Maps the view of the file mapping into the address space of the current process. (MapViewOfFile)<br /></li><li>Read message from the view of the shared memory.<br /></li><li>Unmap the file view and close the file mapping objects. (UnmapViewOfFile, CloseHandle)<br /></li></ul><br />
<strong class='bbc'>	Code - CreateFileMapping (C++)</strong><br />
<pre class='prettyprint'><br /><br />// In terminal services: The name can have a "Global&#092;" or "Local&#092;" prefix<br />// to explicitly create the object in the global or session namespace.<br />// The remainder of the name can contain any character except the  <br />// backslash character (&#092;). For details, please refer to: <br />// http&#58;//msdn.microsoft.com/en-us/library/aa366537.aspx <br />TCHAR szMapFileName&#91;&#93; = _T("Local&#092;&#092;HelloWorld"); <br /><br />// Create the file mapping object <br />HANDLE hMapFile = CreateFileMapping( <br />	   INVALID_HANDLE_VALUE,	  // Use paging file instead of existing file. <br />								  // Pass file handle to share in a file. <br /><br />	   NULL,					  // Default security <br />	   PAGE_READWRITE,			// Read/write access <br />	   ,						 // Max. object size <br />	   BUFFER_SIZE,			   // Buffer size  <br />	   szMapFileName			  // Name of mapping object <br />);<br /></pre><br />
<br />
<br />
<br />
.NET only supports P/Invoke native APIs currently. By P/Invoke, .NET can simulate similar behaviors as native code.<br />
<br />
<strong class='bbc'>	Sample Code 4 (C# - P/Invoke)</strong><br />
<pre class='prettyprint'><br /><br />/// &lt;&lt;/span&gt;summary&gt; <br />/// Creates or opens a named or unnamed file mapping object for <br />/// a specified file. <br />/// &lt;&lt;/span&gt;/summary&gt; <br />/// &lt;&lt;/span&gt;param name="hFile"&gt;A handle to the file from which to create <br />/// a file mapping object.&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="lpAttributes"&gt;A pointer to a SECURITY_ATTRIBUTES <br />/// structure that determines whether a returned handle can be <br />/// inherited by child processes.&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="flProtect"&gt;Specifies the page protection of the <br />/// file mapping object. All mapped views of the object must be <br />/// compatible with this protection.&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="dwMaximumSizeHigh"&gt;The high-order DWORD of the <br />/// maximum size of the file mapping object.&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="dwMaximumSizeLow"&gt;The low-order DWORD of the <br />/// maximum size of the file mapping object.&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="lpName"&gt;The name of the file mapping object. <br />/// &lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;returns&gt;If the function succeeds, the return value is a <br />/// handle to the newly created file mapping object.&lt;&lt;/span&gt;/returns&gt; <br />&#91;DllImport("Kernel32.dll", SetLastError = true)&#93; <br />public static extern IntPtr CreateFileMapping( <br />	IntPtr hFile,				   // Handle to the file <br />	IntPtr lpAttributes,			// Security Attributes <br />	FileProtection flProtect,	   // File protection <br />	uint dwMaximumSizeHigh,		 // High-order DWORD of size <br />	uint dwMaximumSizeLow,		  // Low-order DWORD of size <br />	string lpName				   // File mapping object name <br />);<br /></pre><br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Mailslot</strong></span><br />
<br />
Mailslot is a mechanism for one-way inter-process communication in the local machine or across computers in the intranet. Any client can store messages in a mailslot. The creator of the slot, i.e., the server, retrieves the messages that are stored there:<br />
<pre class='prettyprint'><br /><br />Client (GENERIC_WRITE) ---&gt; Server (GENERIC_READ)<br /></pre><br />
<br />
<br />
<br />
This sample demonstrates a mailslot server, <em class='bbc'>&#092;&#092;.&#092;mailslot&#092;HelloWorld</em>. It first creates such a mailslot, then it reads the new messages in the slot every five seconds. Then, a mailslot client connects and writes to the mailslot <em class='bbc'>&#092;&#092;.&#092;mailslot&#092;HelloWorld</em>.<br />
<br />
<strong class='bbc'>	Code Logic</strong><br />
<br />
Server-side logic:<br />
<ul class='bbcol decimal'><br /><li>Create a mailslot. (CreateMailslot)<br /></li><li>Check messages in the mailslot. (ReadMailslot)<br /><ul class='bbcol decimal'><br /><li>Check for the number of messages in the mailslot. (GetMailslotInfo)<br /></li><li>Retrieve the messages one by one from the mailslot. While reading, update the number of messages that are left in the mailslot. (ReadFile, GetMailslotInfo)<br /></li></ul>	</li><li>Close the handle of the mailslot instance. (CloseHandle)<br /></li></ul><br />
Client-side logic:<br />
<ul class='bbcol decimal'><br /><li>Open the mailslot. (CreateFile)<br /></li><li>Write messages to the mailslot. (WriteMailslot, WriteFile)<br /></li><li>Close the slot. (CloseHandle)<br /></li></ul><br />
<strong class='bbc'>	Code - GetMailslotInfo (C++)</strong><br />
<pre class='prettyprint'><br /><br />///////////////////////////////////////////////////////////////////////// <br />// Check for the number of messages in the mailslot. <br />//bResult = GetMailslotInfo( <br />		hMailslot,					// Handle of the mailslot <br />		NULL,						 // No maximum message size <br />		&cbMessageBytes,			  // Size of next message <br />		&cMessages,				   // Number of messages <br />		NULL);						// No read time-out<br /></pre><br />
<br />
<br />
<br />
<strong class='bbc'>	Code - CreateMailslot (C# - P/Invoke)</strong><br />
<pre class='prettyprint'><br /><br />/// &lt;&lt;/span&gt;summary&gt; <br />/// Creates an instance of a mailslot and returns a handle for subsequent <br />/// operations. <br />/// &lt;&lt;/span&gt;/summary&gt; <br />/// &lt;&lt;/span&gt;param name="lpName"&gt;mailslot name&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="nMaxMessageSize"&gt;The maximum size of a single message <br />/// &lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="lReadTimeout"&gt;The time a read operation can wait for a <br />/// message&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;param name="lpSecurityAttributes"&gt;Security attributes&lt;&lt;/span&gt;/param&gt; <br />/// &lt;&lt;/span&gt;returns&gt;If the function succeeds, the return value is a handle to <br />/// the server end of a mailslot instance.&lt;&lt;/span&gt;/returns&gt; <br />&#91;DllImport("kernel32.dll", SetLastError = true)&#93; <br />public static extern IntPtr CreateMailslot( <br />	string lpName,			  // Mailslot name <br />	uint nMaxMessageSize,	   // Max size of a single message in bytes <br />	int lReadTimeout,		   // Timeout of a read operation <br />	IntPtr lpSecurityAttributes // Security attributes <br />);<br /></pre><br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Remoting</strong></span><br />
<br />
.NET Remoting is a mechanism for one-way inter-process communication and RPC between .NET applications in the local machine or across computers in the intranet and internet.<br />
<br />
.NET Remoting allows an application to make a remotable object available across remoting boundaries, which includes different appdomains, processes, or even different computers connected by a network. .NET Remoting makes a reference of a remotable object available to a client application, which then instantiates and uses a remotable object as if it were a local object. However, the actual code execution happens at the server-side. All requests to the remotable object are proxied by the .NET Remoting runtime over Channel objects that encapsulate the actual transport mode, including TCP streams, HTTP streams, and named pipes. As a result, by instantiating proper Channel objects, a .NET Remoting application can be made to support different communication protocols without recompiling the application. The runtime itself manages the act of serialization and marshalling of objects across the client and server appdomains.<br />
<br />
<strong class='bbc'>	Code - Create and Register a Channel (C#)</strong><br />
<pre class='prettyprint'><br /><br />///////////////////////////////////////////////////////////////////// <br />// Create and register a channel (TCP channel in this example) that <br />// is used to transport messages across the remoting boundary. <br />//// Properties of the channel <br />IDictionary props = new Hashtable(); <br />props&#91;"port"&#93; = 6100;   // Port of the TCP channel <br />props&#91;"typeFilterLevel"&#93; = TypeFilterLevel.Full; <br />// Formatters of the messages for delivery <br />BinaryClientFormatterSinkProvider clientProvider = null; <br />BinaryServerFormatterSinkProvider serverProvider = <br />			  new BinaryServerFormatterSinkProvider(); <br />serverProvider.TypeFilterLevel = TypeFilterLevel.Full; <br /><br />// Create a TCP channel <br />TcpChannel tcpChannel = new TcpChannel(props, clientProvider, serverProvider); <br /><br />// Register the TCP channel <br />ChannelServices.RegisterChannel(tcpChannel, true); <br /></pre><br />
<br />
<br />
<br />
<strong class='bbc'>	Code - Register Remotable Types (VB.NET)</strong><br />
<pre class='prettyprint'><br /><br />'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br />' Register the remotable types on the service end as<br />' server-activated types (aka well-known types) or client-activated<br />' types.<br />' Register RemotingShared.SingleCallObject as a SingleCall server-<br />' activated type.<br />RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingShared.SingleCallObject), _<br />					  "SingleCallService", WellKnownObjectMode.SingleCall)<br />' Register RemotingShared.SingletonObject as a Singleton server-<br />' activated type.<br />RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingShared.SingletonObject), _<br />					  "SingletonService", WellKnownObjectMode.Singleton)<br />' Register RemotingShared.ClientActivatedObject as a client-<br />' activated type.<br />RemotingConfiguration.ApplicationName = "RemotingService"<br />RemotingConfiguration.RegisterActivatedServiceType(_<br />		GetType(Global.RemotingShared.ClientActivatedObject)) <br /></pre><br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Points of Interest</strong></span><br />
<br />
In the pilot phase of the AIO project, we focus on five techniques: COM, Library, IPC, Office, and Data Access. There has been 42 code examples in the project. The collection currently grows at a rate of seven examples per week.<br />
<br />
This article was created on 3/12/2009.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>About the Author</strong></span><br />
<br />
	Microsoft All-In-One Code Framework delineates the framework and skeleton of Microsoft development techniques through typical sample codes in three popular programming languages (Visual C#, VB.NET, Visual C++). Each sample is elaborately selected, composed, and documented to demonstrate one frequently-asked, tested or used coding scenario based on our support experience in MSDN newsgroups and forums. If you are a software developer, you can fill the skeleton with blood, muscle and soul. If you are a software tester or a support engineer like us, you may extend the sample codes a little to fit your specific test scenario or refer your customer to this project if the customer's question coincides with what we collected.<br />
<a href='http://cfx.codeplex.com/' class='bbc_url' title='External link' rel='nofollow external'>http://cfx.codeplex.com/</a>	<br />
<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>License</strong></span><br />
<br />
This article was authored by All-In-One Code Framework and reproduced for the benefit of our viewers under the terms of the <a href='http://www.opensource.org/licenses/ms-pl.html' class='bbc_url' title='External link' rel='nofollow external'>Ms-PL</a> license.]]></description>
		<pubDate>Mon, 30 Jan 2012 20:18:28 +0000</pubDate>
		<guid isPermaLink="false">576258b4a6dbcfc5b4839354868731d3</guid>
	</item>
	<item>
		<title>Parallel programming in .NET - Internals</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/parallel-programming-in-net-internals-r2864</link>
		<description><![CDATA[<ul class='bbc'><br /><li><a href='http://uploads.gamedev.net/cp/1327954523-InParallel.zip' class='bbc_url' title='External link' rel='nofollow external'>Download demo - 2.67 KB</a><br /></li></ul><br />
<br />
License: <a href='http://www.opensource.org/licenses/ms-pl.html' class='bbc_url' title='External link' rel='nofollow external'><span class='bbc_underline'><strong class='bbc'>Ms-PL</strong></span></a><br />
 <br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Introduction</strong></span><br />
<br />
.NET 4 brings a powerful Task library to support a piece of code to run in parallel processors. It just simply spawns threads into multiple processes using the newly written Task libraries (System.Threading.Tasks) in Mscorlib 4.0. Task libraries contain methods like For, ForEach, and Invoke to support parallelism in .NET languages, which I will discuss in detail later on.<br />
<br />
Let's see what's got changed in .NET 4.0 to bring up parallel extensions.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>CLR Thread Pool 4.0</strong></span><br />
<br />
The old thread pool of .NET 2.0 used to support only 1-4 processors, where the "single queue", "single lock" schema was sufficient. The improved thread pool in .NET 4.0 can hold up to 16-256 processors.<br />
<ul class='bbc'><br /><li>The new ThreadPool significantly reduces the synchronization overhead associated with pushing/pulling work items to/from the pool. While in previous versions, the ThreadPool queue was implemented as a linked list protected by a big lock, the new version of the queue is based on the new array-style, lock-free, GC-friendly <a href='http://msdn.microsoft.com/en-us/library/dd267265.aspx' class='bbc_url' title='External link' rel='nofollow external'>ConcurrentQueue</a> class.<br /></li><li>The CLR 4.0 thread pool also supports a local queue per task, and implements a work stealing algorithm that load balances the work amongst the queues.<br /></li><li>In the 4.0 version of the CLR thread pool, the 'thread injection and retirement algorithm' has changed. In cases where there are more running threads than CPUs, instead of creating new threads at the rate of 1 thread per 0.5 second, the new thread pool takes the liberty to 'hold back' threads (i.e., reduce the concurrency level) for a while.<br /></li></ul><span style='font-size: 14px;'><strong class='bbc'>MSCORLIB 4.0</strong></span><br />
<br />
<strong class='bbc'>	System.Collections.Concurrent Namespace</strong><br />
<br />
The System.Collections.Concurrent namespace provides several thread-safe collection classes that should be used in place of the corresponding types in the <a href='http://msdn.microsoft.com/en-us/library/system.collections.aspx' class='bbc_url' title='External link' rel='nofollow external'>System.Collections</a> and <a href='http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx' class='bbc_url' title='External link' rel='nofollow external'>System.Collections.Generic</a> namespaces whenever multiple threads are accessing the collection concurrently.<br />
<br />
<strong class='bbc'>	System.Threading.Tasks Namespace</strong><br />
<br />
The System.Threading.Tasks namespace provides types that simplify the work of writing concurrent and asynchronous code. The main types are <a href='http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx' class='bbc_url' title='External link' rel='nofollow external'>System.Threading.Tasks.Task</a>, which represents an asynchronous operation that can be waited on and cancelled, and <a href='http://msdn.microsoft.com/en-us/library/dd321424.aspx' class='bbc_url' title='External link' rel='nofollow external'>System.Threading.Tasks.Task(Of TResult)</a>, which is a task that can return a value. The Factory class provides static methods for creating and starting tasks, and the <a href='http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.aspx' class='bbc_url' title='External link' rel='nofollow external'>System.Threading.Tasks.TaskScheduler</a> class provides the default thread scheduling infrastructure.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>.NET 4 Cancellation Framework</strong></span><br />
<br />
A very interesting addition to .NET 4 is a set of new types that specifically assist with building cancellation-aware applications and libraries. The new types enable rich scenarios for convenient and safe cancellation, and help simplify situations that used to be be difficult and error-prone and non-composable.<br />
<br />
Two new types form the basis of the framework: a CancellationToken is a struct that represents a 'potential request for cancellation'. This struct is passed into method calls as a parameter, and the method can poll on it or register a callback to be fired when cancellation is requested. A CancellationTokenSource is a class that provides the mechanism for initiating a cancellation request, and it has a Token property for obtaining an associated token. It would have been natural to combine these two classes into one, but this design allows the two key operations (initiating a cancellation request vs. observing and responding to cancellation) to be cleanly separated. In particular, methods that take only a CancellationToken can observe a cancellation request but cannot initiate one.<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>How it Works</strong></span><br />
<br />
Now you have got enough familiarization of the changes in .NET to bring up code parallelization. Let's start with a simple entity of parallelization called Task in our case. The new System.Threading.Tasks.Task class takes care of thread allocation and synchronization; yeah, it's internally using threads, but in a very efficient way.<br />
<br />
The Task class implements the <a href='http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Threading.IThreadPoolWorkItem' class='bbc_url' title='External link' rel='nofollow external'>IThreadPoolWorkItem</a> interface, which is responsible for executing work items from the thread pool queue. The full flow of task parallelization is shown in the diagram below:<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-parallelarch_small.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
The ThreadPoolTaskScheduler class pushes Tasks (IThreadPoolWorkItem) to a global queue using the ThreadPool API. Worker threads picks up the tasks from Global Queue (QueueSegment). Once the processing is done, it informs the task by calling the manual reset event of System.Threading. If a task is created within a task, then it will not be pushed to the Global Queue, but instead maintained in a local queue (work stealing queue). Any task item in the work stealing queue can be shared by any other worker thread. How does work stealing work? The worker thread looks first into the local queue; if there is no work item to pick, then it searches the global queue. Still, if there is no work for the thread, it will look for any pending work item in other queues. So during the task processing, none of the threads are sitting idle, which actually gives 100% utilization of all cores of machines.<br />
<br />
I think we have a lot of benefits of Local Queues and Work Stealing Queues, which were missing in the legacy thread pool, like less contention in the Global Queue, thread workers are effectively used, and many more. One thing to notice is that the huge performance improvement is also caused by changing the linked list based queues to array style queues; all the local and global queues are array based, which means there is no Big Lock for concurrency.<br />
<br />
Let's hit Visual Studio now..<br />
<br />
I ran the below sample of code in my dual core machine, better to try in a quad core.<br />
<pre class='prettyprint'><br /><br />//In Static void Main<br />DoSomeBigStuff(1, 10000);<br />DoSomeBigStuff(2, 10000);<br /><br />//In some class<br />public void DoSomeBigStuff(int inp,int limit)<br />{<br />	ProcessItem();<br /><br />	if (inp &gt; limit)<br />	{<br />		return;<br />	}<br />	DoSomeBigStuff(++inp, limit);<br />}<br /><br />public void ProcessItem()<br />{<br />	Thread.SpinWait(100000);<br />}<br /></pre><br />
<br />
<br />
<br />
Running this on my dual core system gets the result in 8431ms, but if you check out the CPU performance, only 50% of the CPU is getting used.<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-cpu1.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
Let's make some changes to the above code, and put DoSomeBigStuff in a task:<br />
<pre class='prettyprint'><br /><br />Task t1 = new Task(() =&gt;<br />{<br />	DoSomeBigStuff(1, 10000);<br />});<br />Task t2 = new Task(() =&gt;<br />{<br />	DoSomeBigStuff(2, 10000);<br />});<br /></pre><br />
<br />
<br />
<br />
The CPU picture totally changes and now starts using both the cores. The code finishes in 4589 ms, which is a drastic improvement. Now I don't have to think about scaling up my code; just run the same code on an 8 proc machine and it will take lesser than 1000ms. The Task class encapsulates the logic of handling the number of threads based on processors and work item allocation.<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-cpu2.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
There is some overhead associated with threading, so the individual tasks may take a little longer - in this case, they went from about 82 milliseconds for the single-threaded example to a wide range, some as high as 300 milliseconds, in the parallel.<br />
<br />
This overhead should be part of your decision about when and what to parallelize in your application. What's more important for your application: that it finishes faster, or uses less total CPU time? Another consideration is ordering of the work.<br />
<br />
Let's look at the threads. Check the thread debug window; you can see two new threads spawned with the Main thread.<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-thread1_small.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
The Parallel Library has started two threads to do the work equal to the number of cores. One more thread got created, which handles the callbacks from the queue to the main thread.<br />
<br />
Check out the call stack of a single thread; it does the same stuff as we discussed above in the diagram.<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-callstack.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
You can figure out what the Execute method is doing; it just pushes the item into the global queue. The interesting method that I want to discuss here is ThreadPoolWorkQueue.Dispatch(). Let's go step by step in this method.<br />
<pre class='prettyprint'><br /><br />ThreadPoolGlobals.workQueue.MarkThreadRequestSatisfied();<br /></pre><br />
<br />
<br />
<br />
This decrements the outstanding thread request, which means it gets the thread out of the pool.<br />
<pre class='prettyprint'><br /><br />ThreadPoolWorkQueueThreadLocals tl = <br />   ThreadPoolGlobals.workQueue.EnsureCurrentThreadHasQueue();<br /></pre><br />
<br />
<br />
<br />
This creates a Local Queue if not already created for the current thread worker.<br />
<pre class='prettyprint'><br /><br />ThreadPoolGlobals.workQueue.Dequeue(tl, out callback, out missedSteal);<br /></pre><br />
<br />
<br />
<br />
This dequeues the task from the work queue. The Dequeue method is intelligent enough to pick the task if not present in the global or local queue; then it tries to steal the task from other queues.<br />
<pre class='prettyprint'><br /><br />callback.ExecuteWorkItem();<br />ThreadPool.NotifyWorkItemComplete();<br />ThreadPoolGlobals.workQueue.EnsureThreadRequested();<br /></pre><br />
<br />
<br />
<br />
Execute the work item and notify the thread pool that the work is completed. If successful, it returns the thread back to the pool.<br />
<br />
All this looks simple to me, but efficient logic to work out with threads. The picture below will give quite a good idea about the thread handling part done by the Task libraries.<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-threadsdetailed_small.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
<strong class='bbc'>	What's Extra</strong><br />
<br />
The Task Parallel library provides one more class to play with, called Parallel. The APIs present in the Parallel class are used for iterations like For, ForEach, and Invoke. All these extension methods use the same Task libraries discussed above.<br />
<br />
We will see one more example before proceeding ahead.<br />
										<pre class='prettyprint'>				<br /><br />				for (int i=0; i &lt; 1000; i++){<br />					Calculate(i);<br />				}<br />				public void Calculate(int pos){<br />					double result = new Random().NextDouble();<br />					for (int i = ; i &lt; 20000; i++){<br />						result += Math.Acos(new <br />						Random().NextDouble());<br />					}<br />				}				<br />				</pre><br />
				<br />
				 							<pre class='prettyprint'>				<br /><br />				Parallel.For(, 1000, delegate(int i)<br />				{<br />					Calculate(i);<br />				}<br />				);				<br />				</pre><br />
				<br />
				<br />
				<br />
Completed in half of the time compared to the legacy For loop.						<br />
Parallel.For directly jumps into the ForWorker method with the following params in the Parallel class:<br />
<pre class='prettyprint'><br /><br />ForWorker&lt;&lt;span class="code-keyword"&gt;object&gt;(fromInclusive, toExclusive, <br />		  s_defaultParallelOptions, body, null, null, null, null);<br /></pre><br />
<br />
<br />
<br />
Internally, the ForWorker method creates an instance of ParallelForReplicatingTask which inherits from Task, with the default set to InternalTaskOptions.SelfReplicating. ForWorker creates an empty root task, and iteration tasks are created as child tasks of the root task.<br />
<pre class='prettyprint'><br /><br />rootTask.RunSynchronously(parallelOptions.EffectiveTaskScheduler);<br /></pre><br />
<br />
<br />
<br />
This method starts the self-replication process of child tasks, and starts queuing them in the local queue. The Parallel class decides the number of thread workers which is equal to the number of processors.<br />
<br />
In all methods of the Parallel class, it's been taken care that if any cancel task request comes, the threads will not be aborted, but cancelled softly.<br />
<br />
While self replicating, .NET has to make sure the following points:<br />
<ul class='bbc'><br /><li>Self-replicating should have an inter-replica communication mechanism for communicating the completion of the overall activity.<br /></li><li>Only use when the cost of this communication and the management of partitions is considerably less than the potential benefit gained from parallelism.<br /></li><li>Do not assume that the task is always replicated. It is only replicated if there are available resources. For the same reason, do not assume that there will be a specific number of replicas.<br /></li><li>In some instances, the number of replicas could far exceed the number of cores in the machine.<br /></li><li>You may choose to use optimistic concurrency when it is possible to correctly deal with multiple executions of the same step.<br /></li></ul><br />
And I think they have done a good job in handling the number of thread workers, using concurrency queues and the effective cancellation framework.<br />
<br />
Here is the demo source code:<br />
<br />
Below are the results after running the source on a dual core and quad core:<br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-cpu2core.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
<span rel='lightbox'><img src='http://uploads.gamedev.net/cp/1327954523-cpu4core.png' alt='Posted Image' class='bbc_img' /></span><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>A Bonus Item</strong></span><br />
<br />
Visual Stuido Team Launched Concurrency Analyzer: If you have VS 2010, you can see the option under Analyze->Launch Performance Wizard->Concurrency.<br />
<br />
It generates a full analysis report with graphs about the thread and cores usage. If building a thread intensive app, then you should go through this analysis. Also see the <a href='http://channel9.msdn.com/shows/Going+Deep/Visualizing-Concurrency-Inside-the-Concurrency-Visualizer-Profiling-Tool/' class='bbc_url' title='External link' rel='nofollow external'>Concurrency Analyzer Video</a>.<br />
<br />
Good job done .NET Parallel Extension team!!<br />
<br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>License</strong></span><br />
<br />
This article was authored by Manabendra Roy (Manab) and reproduced for the benefit of our viewers under the terms of the <a href='http://www.opensource.org/licenses/ms-pl.html' class='bbc_url' title='External link' rel='nofollow external'>Ms-PL</a> license.]]></description>
		<pubDate>Mon, 30 Jan 2012 20:16:20 +0000</pubDate>
		<guid isPermaLink="false">240497d1c93f3ea543976e5f331f3f9d</guid>
	</item>
	<item>
		<title>TortoiseSVN: Revision Graphs</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/tortoisesvn-revision-graphs-r2859</link>
		<description><![CDATA[TortoiseSVN is a free and open-source Subversion client for Microsoft Windows. It is not tied to any particular Integrated Development Environment (IDE); instead, it is a shell extension which integrates into the Windows Explorer, giving you easy access to Subversion repositories from within applications you're already familiar with. This means that it can be used with any software, and by all members of your development team. In the previous article, <a href='http://www.packtpub.com/article/working-revision-logs-tortoisesvn' class='bbc_url' title='External link' rel='nofollow external'>Working with Revision Logs in TortoiseSVN</a>, we learnt about <strong class='bbc'>differences</strong> and changelists in TortoiseSVN 1.7.<br />
<br />
In this article by <strong class='bbc'>Lesley Harrison</strong>, author of <a href='http://www.packtpub.com/tortoisesvn-1-7-beginners-guide/book/ns/tortoisesvn-abr2/0111?utm_source=ns_tortoisesvn_abr2_0111&utm_medium=content&utm_campaign=naheed' class='bbc_url' title='External link' rel='nofollow external'>TortoiseSVN 1.7</a>, we shall:<br />
<ul class='bbc'><br /><li>Explore working with revision graphs<br /></li><li>Learn how to change <strong class='bbc'>views</strong> in revision graphs<br /></li><li>Learn how to prune <strong class='bbc'>trees</strong> to make the revision graph easier to understand<br /></li></ul><br />
<br />
Revision graphs provide an easy way for you to tell at-a-glance what is going on with your project. They provide a map, in easy-to-understand tree form, of the revision history of your project, including copies, branches, and tags.<br />
<br />
One useful feature of revision graphs is that you can export them into a vector graphics format (WMF is a good option because they scale well and produce fairly small file size images. If you need a more widely supported format, then PNG is a good option) for inclusion with your source code, or on your project's website, giving everyone an easy overview of the status of your project.<br />
<br />
You can view revision graphs for files, directories, or the whole project.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – viewing a revision graph</strong></span><ul class='bbcol decimal'><br /><li>To view a revision graph, go to your working copy, and right-click inside the project, and then select <strong class='bbc'>TortoiseSVN | Revision Graph</strong>.<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_12.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>The graph that appears in the following screenshot shows the history of any branches and tags created, in an easy-to-understand, tree-like structure:<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_13.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>If you prefer to read from top to bottom, rather than having the newest node at the top of the screen, then click the <strong class='bbc'>Show oldest node at top</strong> button (this can be found two buttons to the right of the drop-down which allows you to change the zoom level). This will invert the view, as shown in the following screenshot:<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_14.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>You can view information about a particular branch by right-clicking on it and selecting <strong class='bbc'>Show Log</strong>:<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_15.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>You can also use the same context menu to merge revisions, switch your working copy to a particular branch or tag, browse the repository, and collapse trees.<br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just used the revision graph to get an overview of the revisions, branches, and tags in your project. The last revision graph that we saw is a simple one, taken from our example project. In a real-world scenario, it is likely that the revision graph would be much more complex, and it is in these complex projects that having a revision graph becomes so useful.<br />
<br />
When there are dozens of branches which are being created and merged, it can become difficult to keep track of what happened when, and why. A revision graph gives you a clear high-level view of everything that is happening in your software project.<br />
<br />
To fully understand the revision graph, it helps to understand what each node means. The following table will help with this:<br />
										Items which have been added or copied											Branch HEAD revisions (if you have elected to show these)											Plain with bold outline (a red outline indicates modifications)			<br />
<br />
<br />
You can use the graph to get more detailed information about the differences between revisions. Just <em class='bbc'>Ctrl</em>-click on the two revisions you are interested in, right-click to bring up the context-sensitive menu, and then select <strong class='bbc'>Compare Revisions</strong>. You will be able to see a list of the revisions made to each file. Compare them using TortoiseDiff, as shown in the following screenshot:<br />
<br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_16.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
You can also compare HEAD revisions, and view the unified differences using this method.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Changing your view</strong></span><br />
If your software project is quite large and complex, then you may find it useful to change the view used in the revision graph. There are several options that you can use to change the view you are using, and they can all be found under the view menu.<br />
<br />
Rather than replicating the TortoiseSVN documentation by describing every single option, only the more interesting options will be described here. The other options are mostly clearly labelled, and otherwise are explained in the online help for TortoiseSVN:<br />
<ul class='bbc'><br /><li><strong class='bbc'>Group by Branch</strong>: This option is off by default, so all rows are sorted by revision. This can be a problem if you have branches with a long life and a few commits, because those branches will occupy a whole column, making the graph expand unnecessarily. This is demonstrated in the following screenshot:<br /><br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_17.png' alt='Posted Image' class='bbc_img' /></span></p><br /><br />Turning on <strong class='bbc'>Group by Branch</strong> will change this so that revisions on a branch will be shown on consecutive lines, and branches will be grouped into columns, keeping the graph slim. The previous screenshot shows the default appearance of the revision graph (a shorter revision graph has been used here, for ease of viewing), the next screenshot shows the same revision graph with <strong class='bbc'>Group by Branch</strong>:<br /><br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_18.png' alt='Posted Image' class='bbc_img' /></span></p><br /><br />With this fairly simple tree layout, the difference isn't immediately clear, but if you have a lot of branches, you'll find that the group by branch feature keeps the layout much neater, and avoids needless scrolling.<br /></li><li><strong class='bbc'>Oldest on top</strong>: This option switches the graph so that the oldest revisions are shown at the top of the screen. By default, the oldest revisions are at the bottom, and the 'tree' grows upwards.<br /></li><li><strong class='bbc'>Align trees on top</strong>: This option forces trees to grow down, rather than appearing in their natural revision order, or aligned at the bottom of the wind&#111;w.<br /></li><li><strong class='bbc'>Reduce cross lines</strong>: This option cleans up the revision graph if there are lots of crossing lines. In some cases, this option can make the layout appear less logical, and can also make the graph take up a larger area of the screen.<br /></li><li><strong class='bbc'>Differential path names</strong>: This option makes the path names in the node boxes as short as possible—so if you create a branch called /<em class='bbc'>branches/katakana/images/characters</em> out of <em class='bbc'>/trunk/images/characters</em>, the branch would be shown as merely <em class='bbc'>/branches/katakana/</em>.. the remainder of the path has not changed.<br /></li><li><strong class='bbc'>Exact copy sources</strong>: The default behavior of the revision graph is to show branches as being taken from the last node where the change was made. In practice, many people make branches from the HEAD rather than from a specific revision. If you have a reason for needing to know which revision was used to create a copy, then you can use this option to show those details.<br /></li><li><strong class='bbc'>Fold tags</strong>: If your project has a lot of tags, then you may find that they take up unnecessary screen space, hiding the information that you are interested in. You can use this option to hide the nodes for tags. If you still need to find a tag, you will find them displayed as tooltips on the node that they were copied from. Each source node that had a tag made from it will have an icon on the right-hand side indicating that a tag was made.<br /></li><li><strong class='bbc'>Tree stripes</strong>: No, this option isn't related to landscape gardening. The tree stripes option tells TortoiseSVN to use alternating background colors so that it is easy to distinguish between different trees in the graph.<br /></li></ul><br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'><strong class='bbc'>Keeping your view up-to-date</strong><br />
If you are viewing a revision graph of an active project, you may want to check for updates. Just as you would in your web browser, you can refresh the revision graph by pressing F5. This will connect you to the server (if you have been working offline) and check to see if there have been any new commits. Pressing F5 to refresh works for most screens in TortoiseSVN. You can update your log dialog, for example, by pressing F5 too.</em></p><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Pruning trees</strong></span><br />
Large software projects can end up with lots of trees. This can make the revision graph look excessively complex, and can make it harder for you to find the information that you need. The good news is that you can tame the trees in your graph, shrinking and expanding them as you need them.<br />
<br />
To shrink a tree or a branch, simply hover your mouse over the point where the branch begins (where the node link enters the node), and you will be given the option to collapse the related tree (<strong class='bbc'>-</strong>), or expand it (<strong class='bbc'>+</strong>). If applicable, you will also be presented with the option to split a sub-tree into a separate graph (<strong class='bbc'>x</strong>), or re-attach a tree that had been split (<strong class='bbc'>o</strong>):<br />
<br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_19.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Summary</strong></span><br />
In this article you learned how to view revision graphs, and how to manipulate your view of the graph to give a clearer view of the things that you are interested in.<br />
<br />
Revision graphs are useful for even small development teams. The good thing about the way TortoiseSVN displays them is that they are highly customizable, and scale well for larger projects. So, if you need to keep up with the lifecycle of branches and tags in a big project, then you can do so easily—thanks to the highly customizable views offered by TortoiseSVN!]]></description>
		<pubDate>Fri, 27 Jan 2012 04:38:47 +0000</pubDate>
		<guid isPermaLink="false">8c203562649c8b57b30f64b549b688f8</guid>
	</item>
	<item>
		<title>Working with Revision Logs in TortoiseSVN</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/working-with-revision-logs-in-tortoisesvn-r2858</link>
		<description><![CDATA[TortoiseSVN is a popular and easy-to-use Subversion client for Microsoft Windows. It is a Windows Shell extension, and is not limited to any particular IDE. TortoiseSVN is a free software which has been released under the GNU General Public License.<br />
<br />
In this article by <strong class='bbc'>Lesley Harrison</strong>, author of <a href='http://www.packtpub.com/tortoisesvn-1-7-beginners-guide/book/ns/tortoisesvn-abr1/0111?utm_source=ns_tortoisesvn_abr1_0111&utm_medium=content&utm_campaign=naheed' class='bbc_url' title='External link' rel='nofollow external'>TortoiseSVN 1.7</a>, we shall learn about differences and changelists in TortoiseSVN 1.7<br />
<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Differences in detail</strong></span><br />
Differences are useful to allow you to see what has changed between recent revisions of a file. There are several different ways that you can view differences between files, or between a file and a previous version of the file.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Viewing differences between versions of a specific file in your working copy</strong></span><br />
You can view differences using the change log. There is another way to view differences from within your working copy folder, using the right-click menu.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – viewing differences in a working copy</strong></span><br />
To view the differences between a file in your working copy and a previous version, follow these steps:<br />
<ul class='bbcol decimal'><br /><li>Navigate to your working copy folder.<br /></li><li>Select the file that you want to compare version history for.<br /></li><li>Right-click the file, and select <strong class='bbc'>TortoiseSVN | Diff with previous version</strong>.<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_1.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>A <strong class='bbc'>TortoiseMerge</strong> window will appear, showing the differences between the current version of the file and the previous version.<br /></li></ul><br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_2.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
<p class='bbc_center'><em class='bbc'>(Move the mouse over the image to enlarge.)</em></p><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have viewed the differences between a current version of a file and the previously checked in one. If you have made changes to a file, but not yet checked in those changes, you can view the difference between the BASE revision (the version that was in the repository when you last updated), and the working copy by selecting <strong class='bbc'>TortoiseSVN | Diff</strong>, instead of <strong class='bbc'>TortoiseSVN | Diff with previous version</strong>.<br />
<br />
In addition to using this method, you can view specific differences by going to <strong class='bbc'>TortoiseSVN | Show Log</strong> and selecting the revision you are interested in from the list at the top of the wind&#111;w. Double-click on the relevant file in the list at the bottom of the window—this will bring up a <strong class='bbc'>TortoiseMerge</strong> window showing the difference list for the revisions in question.<br />
<br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_3.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Viewing differences between files outside your working copy</strong></span><br />
You don't have to be inside your working copy to view differences. In fact, you can view differences between files that aren't under version control.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – viewing differences in files outside your working copy</strong></span><br />
To view the differences between files that aren't within your working copy:<br />
<ul class='bbcol decimal'><br /><li>Navigate to the folder where the files are stored.<br /></li><li>Click on the older version of the file.<br /></li><li>Hold down the <em class='bbc'>Ctrl</em> key and then click on the newer version of the file.<br /></li><li>Right-click on the file, and select <strong class='bbc'>TortoiseSVN | Diff</strong>.<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_4.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>The next window will show the differences between the two files (depending on what applications you have installed, and the file type of the file you view the differences for, your screen may differ from the following screenshot):<br /></li></ul><br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_5.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just viewed the differences between two files which are not under version control. You could use this to compare submitted patches, documents or source code. When you use this method to compare differences, the differences may be displayed in the editor or IDE that supports the file (as shown in the previous screenshot), or using the <strong class='bbc'>TortoiseMerge</strong> window:<br />
<br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_6.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<strong class='bbc'>	TortoiseSVN 1.7 Beginner's Guide</strong><br />
<br />
										<br />
Perform version control in the easiest way with the best SVN client – TortoiseSVN						<br />
<span style='font-size: 18px;'><strong class='bbc'>Comparing folders in the repository browser</strong></span><br />
If you want to make a bigger comparison, for example between two folders, you can use the repository browser to achieve this. Simply open the repository browser, right-click on one of the folders that you want to include in the comparison, and select <strong class='bbc'>Mark for comparison</strong>, then right-click on the second, and select <strong class='bbc'>Show differences as a unified diff</strong>:<br />
<br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_7.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
Comparing two folders is useful if you want to view the differences between several files that are in different folders. You get an at-a-glance comparison of each file.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Working with changelists</strong></span><br />
The best-case scenario for software development would be to work on one thing at a time, committing each change as you go. In the real world, however, things don't always work like that.<br />
<br />
Quinn discovered that while attempting to make some network code changes, the current UI setup didn't have anywhere to display the network status. Being easily distracted, he decided to quickly create a status bar, and ended up tweaking some other parts of the UI too, before getting back to his network coding task. Before he even realized he'd gotten distracted, he had edited half the files in the project! Or so it felt when he saw the huge list in the commit wind&#111;w. Which file affects which task?<br />
<br />
If Quinn was working on tasks which affected separate files, then he could group the files into <strong class='bbc'>changelists</strong>, separating those changelists according to task. This makes it easy for him to see what he is doing. He can commit the files related to UI changes by selecting the files in that changelist, and then later commit the network changes by selecting that changelist.<br />
<br />
Sadly, this works well only if each task affects a different set of files. If one file is touched by more than one task, then you can't add that file to more than one changelist. Of course, that's a strong case for keeping your code as organized as possible!<br />
<br />
Another important thing to remember is that changelists are stored on your local computer, rather than on the Subversion server. They're really a convenience tool for you, rather than a part of the version tracking. This means that you should still make sure to provide clear commit log messages so that other developers understand what is happening with each commit. Even small changes should be documented—your fellow developers will thank you for it, and you'll probably be glad of your log messages yourself a few months down the line!<br />
<br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'><strong class='bbc'>Changelists are not available for Windows 2000.</strong><br />
If you're a user of Windows 2000, then you won't be able to take advantage of the Changelists feature in TortoiseSVN. Unfortunately, this feature is only available for Windows XP and above. If you're still using Windows 2000 after all these years, then it's likely that you will have encountered other applications that also have problems with the operating system. Perhaps now is a good time to ask your boss for an upgrade!</em></p><br />
<br />
The first thing you need to learn is how to create a new changelist and add some files to it.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – working with changelists</strong></span><br />
To work with changelists, you will need to have worked on several different files before a commit. So before you proceed with this <em class='bbc'>Time for action</em>, modify several files within your working copy.<br />
<ul class='bbcol decimal'><br /><li>Navigate to your working copy.<br /></li><li>Right-click inside the working copy folder and select <strong class='bbc'>TortoiseSVN | Check for Modifications</strong>.<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_8.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>You will see that all of the modified files appear in the status list. This might be OK for relatively small commits, but could be confusing for bigger commits. To group the files into changelists, select the files in question, right-click on them, and then select <strong class='bbc'>Move to changelist | <new changelist></strong>.<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_9.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>The <strong class='bbc'>Create Changelist</strong> window will appear. Enter a suitable name for your changelist.<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_10.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>The files you have moved to the changelist will appear listed under that category. You can create as many changelists as you wish. The following screenshot shows two changelists for this particular commit:<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_06_11.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>If you want to add a file to an already created changelist, simply right-click the file and select <strong class='bbc'>Move to Changelist</strong>, then click the name of the changelist in question.<br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have learned how to create changelists. You can create more than one changelist, and you can add multiple files to each changelist. Once you have created a changelist, it will show in the right-click menu so that you can easily add files to it in the future.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Removing a file from a changelist</strong></span><br />
Just as you can add a file to a change list, it's possible to remove them too. Simply right-click on the file in question, and select <strong class='bbc'>Remove from changelist</strong>.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Ignoring files on commit</strong></span><br />
As mentioned previously, it's possible to tell TortoiseSVN to ignore files unless it is told otherwise. TortoiseSVN uses a special changelist for this called ignore-on-commit. If you add files to this changelist, then TortoiseSVN will ignore them in the future, even if the files have been modified locally.<br />
<br />
This feature is useful for IDE project files – all developers probably have their own settings which they like to use, and there's no point synching their settings to the repository, as that would annoy the other developers on the team.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Summary</strong></span><br />
In this article you learned ways to view differences and to add files to change lists.<br />
<br />
In the next article, <a href='http://www.packtpub.com/article/tortoisesvn-revision-graphs' class='bbc_url' title='External link' rel='nofollow external'>TortoiseSVN: Revision Graphs</a>, you will see:<br />
<ul class='bbc'><br /><li>How to view a revision graph<br /></li><li>How to manipulate the view to your tastes<br /></li><li>How to prune trees in the graph to make your view clearer<br /></li></ul>]]></description>
		<pubDate>Fri, 27 Jan 2012 04:36:13 +0000</pubDate>
		<guid isPermaLink="false">552d21cc507489b0da0c67a0713f80bb</guid>
	</item>
	<item>
		<title>TortoiseSVN: Getting Started</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/tortoisesvn-getting-started-r2857</link>
		<description><![CDATA[In this article you will get your first taste of using TortoiseSVN. This article will explain the concept of working copies and will cover how to check out a <strong class='bbc'>working copy</strong>, how to manage <strong class='bbc'>copy depth</strong>, and how to <strong class='bbc'>commit</strong> a copy after you have made some changes to it. This process is the nuts-and-bolts of version management and something that you will be doing a lot during your work with TortoiseSVN.<br />
<br />
In this article by <strong class='bbc'>Lesley Harrison</strong>, author of <a href='http://www.packtpub.com/tortoisesvn-1-7-beginners-guide/book/ns/tortoisesvn-abr3/0111?utm_source=ns_tortoisesvn_abr3_0111&utm_medium=content&utm_campaign=naheed' class='bbc_url' title='External link' rel='nofollow external'>TortoiseSVN 1.7</a>, we shall:<ul class='bbc'><br /><li>Learn the benefits of using a working copy<br /></li><li>Learn how to check out a working copy and how to check in after making changes<br /></li><li>See some of the more common <strong class='bbc'>commit log messages</strong> and learn what they mean<br /></li><li>Explore the repository browser<br /></li></ul><br />
<span style='font-size: 18px;'><strong class='bbc'>Our case study</strong></span><br />
<strong class='bbc'>Shiny Moose Software</strong> is a software house with a small team of developers. They have just started working on their first project—a Hiragana Learning Game written in Python. The lead developer, Quinn, has created a skeleton for the project. He has written the code for the games "splash screen".<br />
<br />
One of the other developers, Mowbray, downloaded a compressed archive containing Quinn's code and found that on his older computer, the CPU usage spiked massively when the code was run. He looked at the code and noticed that Quinn had made a poor choice when deciding how to detect mouse events. The problem is simple to fix, but Mowbray knows that communicating the changes to Quinn could be problematic.<br />
<br />
Mowbray could make the required changes and then e-mail the updated code to Quinn, but what if Quinn returns to his computer and resumes his work on the application before he checks his e-mail? What if one of the other developers at Shiny Moose Software has also decided to make some changes to the code? Keeping track of changes submitted by several different developers would be confusing enough even with this relatively small application. Imagine how difficult it would become when the code is measured in hundreds, or thousands of lines, rather than just a few dozen!<br />
<br />
This is where Subversion saves the day. Instead of copying the code from a normal shared folder, or downloading it from the company's intranet site, Mowbray can use TortoiseSVN to <strong class='bbc'>check out</strong> a <strong class='bbc'>working copy</strong> of the code, inform the Subversion server that is currently working on that file, make the changes, and check it back in.<br />
<br />
<br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'><strong class='bbc'>A word about our examples</strong><br />
The code snippets used in this article are incredibly simplistic. Please don't use them as examples for how to write a Python application! Also, don't worry too much about the language or IDE used in these examples. TortoiseSVN can be used with any language and any development environment. Even team members working on other areas, such as documentation or translation work, can take advantage of TortoiseSVN. The most important thing is to understand the version control principles which are being applied.</em></p><br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Working copies explained</strong></span><br />
The first thing Mowbray needs to do is check out a working copy. He can make changes to this copy and then submit the changes once he is done.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – checking out a working copy</strong></span><br />
Checking out a working copy takes just a few simple steps.<br />
<ul class='bbcol decimal'><br /><li>Create a folder which you will use to store your working copies. For example, <em class='bbc'>C:&#092;Projects&#092;MooseHiragana</em>.<br /></li><li>Right-click inside that folder and select <strong class='bbc'>SVN Checkout</strong>... from the menu that appears.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_01.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>Browse to your project's repository (or enter the correct network path) and click <strong class='bbc'>OK</strong>.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_02.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>A window containing a list of the files which have been checked out will appear.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_03.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>If the checkout was successful, you should see a list of files in your chosen directory, with a green tick on the icon of each file.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_04.png' alt='Posted Image' class='bbc_img' /></span></p>		<br />		<br />	<br /><p class='bbc_center'><em class='bbc'>(Move the mouse over the image to enlarge.)</em></p><br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just checked out a working copy of the files which are stored on the repository. This is useful because it allows you to safely test, change, and experiment with the code without the risk of breaking the original code.<br />
<br />
Once you are happy with the changes you have made, you can check them in to the repository, so that your fellow developers can synchronize their copies to see your changes.<br />
<br />
<br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'><strong class='bbc'>Local repositories vs. remote repositories</strong><br />
We will use remote repositories in most of our examples. It is likely that you will, at some point, need to work with a remote repository—either one which is accessed via the internet, or one which is part of your company's network. In that case, all you need to do is enter the full network/ internet address of the repository in the place of the <em class='bbc'>file:///</em> reference in the URL of Repository box.</em></p><br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Checkout depth</strong></span><br />
The MooseHiragana project is quite small, so there is no issue with checking out the entire repository. If you were working on a much larger project, which had thousands of files, then you may prefer to save time, bandwidth and storage space by checking out only the folders that relate to the part of the project you are working on.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – using checkout depth</strong></span><ul class='bbcol decimal'><br /><li>Using a different folder for this working copy, right-click inside the folder, and select <strong class='bbc'>SVN Checkout....</strong><br /></li><li>This time, in the <strong class='bbc'>Checkout</strong> window that appears, as well as selecting the correct repository, choose <strong class='bbc'>Only file children</strong> from the <strong class='bbc'>Checkout Depth</strong> dropdown.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_05.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>You should see that when the checkout process completes, all the files that are part of the root folder appear in your working copy, but none of the folders have been checked out.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_06.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just checked out only the files that are in the root folder of the project on the Subversion server. That may not seem like a particularly useful feature when the project consists of just a few files, but imagine if there were thousands of files.<br />
<br />
The <em class='bbc'>checkout depth</em> feature becomes useful when projects increase greatly in size. It is also useful if your team has members that work only on specific parts of the application. For example, an artist could check out only the images folder, or a translator could check out only the folder containing the localization files for the language he is working in. If the artist then needs to expand his checkout to include deeper folders, he can use the repository browser to select the extra folders that he needs.<br />
<br />
There are a number of different checkout depth options. The following table explains what each option means.<br />
										Checks out the entire tree, including all child folders and sub-folders.											Checks out the specified directory, including all files and child folders, but does not populate the child folders.											Checks out the specified directory, including all files, but does not check out any child folders.											Checks out the selected directory only. Does not populate it with files or child folders.											This option is not shown on the <strong class='bbc'>checkout </strong>dialog, but it is the default for all other dialogs which have a depth setting. This option tells TortoiseSVN to adhere to the depth specified in the working copy.											This option is not shown on the <strong class='bbc'>checkout </strong>dialog. It is used to reduce the depth of the working copy after a folder has already been populated.			<br />
<span style='font-size: 18px;'><strong class='bbc'>Have a go hero – working with checkout depth</strong></span><br />
Imagine that you are an artist. You have been hired by Shiny Moose Software and asked to redesign the logo used on the splash screen and also to create a smaller one for use in the theme of the game.<br />
<br />
The directory structure chosen by the developers looks like this:<br />
<br />
<br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_07.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
The images that you will be working on are stored in the <em class='bbc'>/images</em> folder. You will not need to work on anything inside the <em class='bbc'>/images/artwork</em> or <em class='bbc'>/images/japanese folders</em>.<br />
<br />
You need to set up your working copy. Rather than cluttering your filesystem with files that you will never need to use, you have decided to checkout only the folder you need for your work—the <em class='bbc'>/images</em> folder.<br />
<br />
Using another user account (or another PC on your network), try checking out just the contents of that folder from the repository. If you prefer, you could simply create a new working directory using the same user. However, if you do this, you should note that any changes made in that folder will be marked with your own user name. That may be fine for testing TortoiseSVN's features, but is not good practice in a production environment.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Committing changes to a repository</strong></span><br />
Now that Mowbray has checked out a working copy of the MooseHiragana source code, he can change the source code to fix the speed issue he noticed and then commit the changes to the repository.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – committing changes to a repository</strong></span><ul class='bbcol decimal'><br /><li>After you have finished editing the files that you want to change in the project, save the files and open the project folder. You should see a red exclamation mark on the icon of any files that have been changed.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_08.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>Right-click inside the folder and choose <strong class='bbc'>SVN Commit...</strong> (you can also right-click on a specific file, if that is the only file you wish to commit).<br /></li><li>When the <strong class='bbc'>Commit</strong> dialog box appears, enter a note in the <strong class='bbc'>Message:</strong> text box explaining the changes you have made to the file.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_09.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>Notice that the <strong class='bbc'>Commit</strong> box offers a spell-checking feature. Words that the spellchecker does not recognize are highlighted with a dashed red line under them. The words shown in this example are spelled correctly (<strong class='bbc'>pygame</strong> is the name of a Python library which is used in the application). The spell-check feature works just like the spell-checking feature in Microsoft Word—to see suggestions for alternative spellings, right-click on a highlighted word.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_10.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>Since pygame is spelled correctly, instead of choosing an alternative spelling, click <strong class='bbc'>Add 'pygame' to dictionary</strong>. Pygame will no longer be flagged as being spelled incorrectly.<br /></li><li>To see the differences between the original file and the modified one, double-click on the filename in the <strong class='bbc'>Changes made</strong> section of the dialog.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_11.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>Click <strong class='bbc'>OK</strong> to commit the changes.<br /></li><li>A dialog will appear showing which files have been modified and the status of the commit process.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_12.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have successfully changed some files and committed the changes to the repository. Now other developers will be able to download your changes and add to the work that you have done.<br />
<br />
You can view differences between text files or images. Images can be viewed in <strong class='bbc'>TortoiseIDif</strong>, which shows a side-by-side comparison between different versions of a file, and also allows you to layer the different versions for a more detailed comparison:<br />
<br />
<br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_13.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
You may be wondering what would happen if two people both decided to modify the same file at the same time. The good news is that Subversion is equipped to handle such events for most file types.<br />
<br />
If the file being edited is a text file or some source code, then Subversion can cope with two or more people editing the same file at the same time. When the users submit their changes, Subversion will allow them to merge their changes into the new version of the file. If the changes cannot be merged for any reason, then Subversion will flag a conflict, alerting you to the problem so that you can resolve it and decide which changes to keep or discard.<br />
<br />
To prevent conflicts while you are working on files—be they images, documentation, or source code, it is possible to lock the file that you want to work on, preventing anyone else from committing changes until you release the lock.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>The importance of commit log messages</strong></span><br />
Commit log messages give you a chance to describe the changes that you are committing. It is good practice to provide a descriptive commit log message with each change that you commit—not just to explain to other developers what your changes do, but also to provide a record of the work that you have done.<br />
<br />
Commit log messages could be used as the basis for patch notes which will be released to your end users, or could be used by your manager to see how much work you have put into a project. It's likely that your manager will be more impressed with:<br />
<br />
<pre class='prettyprint'><br />Fixed "Bug #123 - client crashes when player submits high score" by<br />correcting the variable name to hiScore (matching naming convention).<br /><br /></pre><br />
than they would be with a blank commit, or a message such as following:<br />
<br />
<pre class='prettyprint'><br />Change hiscore to hiScore.<br /><br /></pre><br />
Of course, you should comment your source code too, but a good commit log message should explain what you changed and why you changed it.<br />
<br />
It is possible to add some basic formatting to a commit log message using the following formatting conventions:<br />
<br />
In addition to the spell-checking feature already discussed, the commit log message box also supports filename and function auto-completion. The auto-completion box will appear automatically after you have typed the first three characters of the name of a file or a function included in your commit.<br />
<br />
You can also bring up the auto-complete box by typing one or two characters from a file or function’s name, and then pressing <em class='bbc'>Control + Space</em>:<br />
 <br />
<p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_14.png' alt='Posted Image' class='bbc_img' /></span></p><br />
<br />
<span style='font-size: 14px;'><strong class='bbc'>Have a go hero – committing some changes</strong></span><br />
In the previous <em class='bbc'>Have a go hero</em> section you checked out some image files. Change the images, and then commit the changes to the repository. Leave a clear commit log message to explain what you have done.<br />
<br />
<br />
<br />
<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Excluding items from a commit</strong></span><br />
There are a couple of reasons why you may want to exclude an item from a commit. The two main reasons are as follows:<br />
<ul class='bbc'><br /><li>Your IDE creates files which contain personal settings and data—the content of these files may differ from developer to developer. These files do not need to be synched, so can be excluded from the commit process permanently.<br /></li><li>Your IDE has changed the timestamp on a project file—but the rest of the file has not changed. There is no need to commit the file every time the timestamp is changed. It makes sense to exclude the file temporarily.<br /></li></ul><br />
In the first instance, you can set up template files which are subject to version control and contain the basic IDE settings most people like to use for their project. The template file can be renamed to indicate that it is a template, and individual developers can copy the template file and give it the correct file extension.<br />
<br />
To ensure that the actual project settings files are excluded from versioning, let's create a rule which tells TortoiseSVN to ignore those files when performing a commit. This can be done by creating a special setting—the <em class='bbc'>svn:ignore</em> property for the file in question.<br />
<br />
To add a file which is not currently versioned to the ignore list, simply right-click on it and select <strong class='bbc'>TortoiseSVN | Add</strong>, to ignore <strong class='bbc'>list | File Name</strong>. Or, if you want to ignore all files of that type—for example, all Komodo <em class='bbc'>.kpf</em> files, select <em class='bbc'>TortoiseSVN</em> | Add to ignore <em class='bbc'>list | *.kpf</em>.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – excluding files that are already versioned</strong></span><br />
You may be wondering how to exclude a file from versioning if you have already accidentally committed it. Fortunately, this mistake is easy to rectify.<ul class='bbcol decimal'><br /><li>Hold down the <em class='bbc'>Shift</em> key and right-click on the file you want to remove from versioning. This will bring up the extended context menu.<br /></li><li>Select <strong class='bbc'>TortoiseSVN | Delete (Keep Local)</strong>.<br /></li><li>A red X icon should appear on the icon of the file you do not want to be versioned.<br /></li><li>Now right-click on the file and select <strong class='bbc'>SVN Commit…</strong>.<br /></li><li>Once the commit has completed, right-click on the file and select <strong class='bbc'>TortoiseSVN | Add to Ignore List | filename</strong><br /></li><li>You should see the ignored file get a new overlay icon (similar to the following one shown below), indicating that it has been ignored.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_15.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just removed a file from versioning without removing the local copy. After you removed the file, you committed the changes to the Subversion server, and then told TortoiseSVN to ignore any changes to that file in the future.<br />
<br />
This works well for files which change frequently and are not an integral part of the project, but what if you need to submit one or two changes to the server, but are not ready to submit all your changes? In this case, you can choose to temporarily exclude certain files from the check in.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – temporarily excluding files from committing</strong></span><ul class='bbcol decimal'><br /><li>Imagine that you have been making some changes to <em class='bbc'>hiragana.py</em>, and you have also decided to create a small Help feature, which is contained in a file called <em class='bbc'>help.py</em>. You are ready to commit your work on the Help feature, so you right-click on the file and select <em class='bbc'>Add...</em> to add it to the server, but you are not ready to commit <strong class='bbc'>hiragana.py</strong>.<br /></li><li>Right-click inside the Working Copy folder and select <strong class='bbc'>SVN Commit…</strong><br /></li><li>In the window that appears, ensure that <em class='bbc'>help.py</em> is ticked and <em class='bbc'>hiragana.py</em> is un-ticked in the <strong class='bbc'>Changes made</strong> list.<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_16.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>Now, when you click <strong class='bbc'>OK</strong>, only the changes to <em class='bbc'>help.py</em> will be submitted<br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have successfully submitted the changes you have made to one file without updating other files that you have changed. This is a temporary measure and next time you go to make a commit, the files that you ignored this time will again appear in the commit list. If you want to ignore a certain file on every commit (for example, a file relating to your own IDE settings), then you could move that file to the ignore-on-commit change list, so that you don't have to worry about it in the future.<br />
<br />
<br />
<p class='bbc_indent' style='margin-left: 40px;'><em class='bbc'><strong class='bbc'>Committing one file at a time</strong><br />
Instead of right-clicking inside the working directory folder and selecting the files you do not wish to commit, you could simply right-click on the file that you wish to commit, and open a commit dialogue for only that file (or the files that you have selected—if you have selected several files by using the Shift or Control keys and clicking on the files you want to commit). This can save you a lot of time if you wish to commit only one or two files. This approach should be used sparingly, however, because you run the risk of missing warnings about unversioned files that you would otherwise see if you committed from the root of your working copy folder.</em></p><br />
<br />
If you want to ensure that a file type is ignored on a long term basis, then you can either use the ignore list detailed in the previous <em class='bbc'>Time for Action</em>, or, to ignore a file type across all Subversion projects accessed via this particular client, then you should add the file to the client's Global Ignore list:<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – using the global ignore list</strong></span><ul class='bbcol decimal'><br /><li>Bring up the TortoiseSVN context menu and select <strong class='bbc'>Settings</strong>.<br /></li><li>In the <strong class='bbc'>General</strong> section of the Settings - TortoiseSVN window which appears, add the file type or filename that you wish to ignore to the end of the list in the <strong class='bbc'>Global ignore patterns</strong> box (patterns are separated with a space):<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_17.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>Click <strong class='bbc'>OK</strong>.<br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just added a file to the global ignore list—this list is used by the copy of TortoiseSVN on your machine. Any files included in the list will be ignored when you make a commit to any Subversion project. The global ignore list is specific to your client (TortoiseSVN is not the only client that has a global ignore list, other clients use it too, but it is not something that is sent to the server or shared with other members of your team), so other developers using other computers may have different settings and may ignore different files to you.<br />
<br />
The ignore list uses Unix-style <strong class='bbc'>wildcards</strong> to match filenames:<br />
<ul class='bbc'><br /><li>* : This wildcard matches any string of characters—including empty strings and spaces<br /></li><li>? : It matches any single character<br /></li><li>[...] : This matches any one of the characters contained within the brackets—for example [A-Dprz] would match the upper case characters A, B, C, or D, and the lower case characters p, r, and z<br /></li></ul><br />
<span style='font-size: 18px;'><strong class='bbc'>Keeping your working copy up-to-date</strong></span><br />
You should periodically update your working copy to make sure that you have the latest version of any files that you are working on. This ensures that you are working with the latest version of the source code, and are not wasting your time working on code that has been altered or fixing bugs that have already been fixed.<br />
<br />
If you have been following all of the <em class='bbc'>Have a go hero</em> sections, your main working copy is most likely out-of-date right now, because your artist has changed some artwork and committed the new art files to the server.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – updating your working copy</strong></span><ul class='bbcol decimal'><br /><li>To update your entire working copy, right-click inside the folder, and select <strong class='bbc'>SVN Update</strong> (You can update only specific files or folders by selecting them, then right-clicking on them. Developers probably wouldn't want to do this, but it is still a useful feature. An artist working remotely may choose to update only specific assets to save bandwidth.):<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_18.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>An update window will appear, listing files which were added, removed, merged, or updated:<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_19.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just updated your local working copy with any changes that have been made recently and submitted to the Subversion server. This is useful because it allows you to stay up-to-date with the work being done by your colleagues.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Using the repository browser</strong></span><br />
There may be some cases where you do not want to check out a working copy, but would prefer to perform actions directly on the repository. This is not advisable for routine development work, but can be useful in some instances. The <strong class='bbc'>Repository Browser</strong> allows you to explore a large project's file structure without checking out the entre project's directory structure. It also allows you to view revision logs and blame, and download unversioned copies of files quickly and easily.<br />
<br />
The repository browser is intuitive and easy to use.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Time for action – using the repository browser</strong></span><ul class='bbcol decimal'><br /><li>To access the repository browser, simply right-click anywhere in an Explorer window and select <strong class='bbc'>TortoiseSVN | Repo Browser</strong>.<br /></li><li>The <strong class='bbc'>Repository Browser</strong> window will appear, as shown:<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_20.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>The <strong class='bbc'>Repository Browser</strong> works in the same way as Windows Explorer—you can double-click on a folder to expand it. To get more information about a file, right-click on the file you are interested in—you will see several options:<br />		<br />	<br /><p class='bbc_center'><span rel='lightbox'><img src='http://uploads.gamedev.net/packtpub/3449_02_21.png' alt='Posted Image' class='bbc_img' /></span></p><br /></li><li>The <strong class='bbc'>Open</strong> and <strong class='bbc'>Open with...</strong> options allow you to open the file to view or edit its contents. The <strong class='bbc'>Show log</strong>, <strong class='bbc'>Revision graph</strong>, and <strong class='bbc'>Blame...</strong> options allow you to view information about the changes that have been made to the file.<br /></li></ul><br />
<span style='font-size: 14px;'><strong class='bbc'>What just happened?</strong></span><br />
You have just interacted directly with the repository. Remember that the Repository Browser allows you to work with the files stored on the repository—not your working copy. So, if you delete or rename a file using the Repository Browser, it will be removed from the current version on the repository and therefore be removed from other people's working copies next time they update them.<br />
<br />
The Repository Browser is a useful tool for viewing the directory structure of a project and looking at revision histories. However, you should not make a habit of altering files by accessing the Subversion server directly. In most cases, it would be better to make the changes on your local copy, and then check them in.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Summary</strong></span><br />
In this article, you used TortoiseSVN to perform some of the more common day-to-day version control tasks—checking out a working copy, making changes, and checking those changes in.<br />
<br />
Specifically, we covered:<br />
<ul class='bbc'><br /><li>Checking out a working copy<br /></li><li>Using the checkout depth feature to check out only the parts of the repository that you need to work on<br /></li><li>Checking in your changes<br /></li><li>Excluding items from a commit<br /></li></ul><br />
We also touched base on some of TortoiseSVN's other features, including file locking, commit log messages, and the repository browser.]]></description>
		<pubDate>Fri, 27 Jan 2012 04:32:39 +0000</pubDate>
		<guid isPermaLink="false">d17367b08356ccbdba2a5485b374c341</guid>
	</item>
	<item>
		<title><![CDATA[Owl's Programming Snippets]]></title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/owls-programming-snippets-r2852</link>
		<description></description>
		<pubDate>Tue, 17 Jan 2012 17:22:05 +0000</pubDate>
		<guid isPermaLink="false">186f8f4eba86cca7bdf06db31f155a15</guid>
	</item>
	<item>
		<title>Collision detection and bug detection</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/collision-detection-and-bug-detection-r2832</link>
		<description><![CDATA[A Few months ago I decided it was about time I write my own Pacman clone. I had it all figured out except for the AI. I wanted to make a good AI, and started looking for one. Diving more and more into the Pacman’s world, I became obsessed with the AI, and trust me the Pacman AI deserves an article of its own. But it was that obsession, which led me to a decision. I’m gonna to reverse engineer the original Pacman. Yes, I know. I got it real bad.<br />
<br />
So, there I was spending hours, late nights, trying to put the pieces together, placing break points, and playing tones of Pacman, where at one point I wanted to test something related to Pacman getting caught. I was running Pacman into ghosts, when suddenly; I just went through one of them.Skeptical, are you?! Look at the <a href='http://home.comcast.net/~jpittman2/pacman/pacmandossier.html#CH3_Just_Passing_Through' class='bbc_url' title='External link' rel='nofollow external'>link</a> from “The Pac-Man Dossier”. It gives a pretty good explanation of the bug. I would like to elaborate a bit more, and put it in context of game programming and programming in general.<br />
<br />
But first let me finish my story (It took me months of work,you can spare a few seconds). At one point while going through the code I found the collision detection procedure, and a few hours later, I found, yet another collision detection procedure. Weird. Here we a have, not one, but two ways to detect collisions and yet, we have a collision detection bug.<br />
<br />
Let’s look at the following game loop. I think it is pretty standard:<br />
<pre class='prettyprint'><br />Do {<br /> Get user input<br /> Move characters<br /> Detect collisions<br /> Render<br /> } While ( Player alive)<br /> </pre><br />
 Here is how the first collision detection works:<br />
<pre class='prettyprint'><br />For each ghost<br />	If ghost.MAZE_POS == pacman.MAZE_POS Then Return collision detected<br />Next<br /> Return no collision detected<br /> </pre><br />
The MAZE_POS are set at the [Move characters ] procedure, as follows<br />
 <pre class='prettyprint'>CHARCTER.SCREEN_POS.X = CHARCTER.SCREEN_POS.X + DIRECTION HORIZONTAL DELTA<br /> CHARCTER.SCREEN_POS.Y = CHARCTER.SCREEN_POS.Y+DIRECTION VERTICAL DELTA<br /> CHARCTER .MAZE_POS.X = CHARCTER .SCREEN_POS.X / 8<br /> CHARCTER .MAZE_POS.Y = CHARCTER .SCREEN_POS.Y / 8<br />  </pre><br />
Now let’s look at the following scenario:<br />
<pre class='prettyprint'>PACMAN.SCREEN_POSITION.X = 100<br /> GHOST.SCREEN_POSITION.X = 100<br />PACMAN.SCREEN_POSITION.Y = 7<br /> GHOST.SCREEN_POSITION.Y = 8<br /> PACMAN.DIRECTION = DOWN<br /> GHOST.DIRECTION = UP<br /> </pre><br />
 At the first iteration, the [Move characters ] will result <br />
<pre class='prettyprint'>PACMAN.MAZE_POSITION.X = PACMAN.SCREEN_POSITION.X/8  // 100/8 = 12<br /> PACMAN.MAZE_POSITION.Y = PACMAN.SCREEN_POSITION.Y/8  // 7/8 = 0<br /> GHOST.MAZE_POSITION.X =GHOST.SCREEN_POSITION.X/8  // 100/8 = 12<br /> GHOST.MAZE_POSITION.Y = GHOST.SCREEN_POSITION.Y/8  // 8/8 = 1<br /> </pre><br />
And in the collision detection we get:<br />
<pre class='prettyprint'>PACMAN.MAZE_POSITION (12,0) != GHOST.MAZE_POSITION(12,1)</pre><br />
At the next iteration, the [Move characters ] will result <br />
<pre class='prettyprint'>PACMAN. SCREEN_POSITION.X = PACMAN.SCREEN_POSITION.X +0   // stays 100<br /> PACMAN. SCREEN_POSITION.Y = PACMAN.SCREEN_POSITION.Y +1  // now it is 8<br /> GHOST. SCREEN_POSITION.X =GHOST.SCREEN_POSITION.X + 0 //stays 100<br /> GHOST. SCREEN_POSITION.Y = GHOST.SCREEN_POSITION.Y +(-1)  // now it is 7<br />  PACMAN.MAZE_POSITION.X = PACMAN.SCREEN_POSITION.X/8  // 100/8 = 12<br /> PACMAN.MAZE_POSITION.Y = PACMAN.SCREEN_POSITION.Y/8  // 8/8 = 1<br /> GHOST.MAZE_POSITION.X =GHOST.SCREEN_POSITION.X/8  // 100/8 = 12<br /> GHOST.MAZE_POSITION.Y = GHOST.SCREEN_POSITION.Y/8  // 7/8 = 0<br /> </pre><br />
And in the collision detection we get:<br />
<pre class='prettyprint'>PACMAN.MAZE_POSITION (12,1) != GHOST.MAZE_POSITION(12,0)</pre><br />
D’oh!, indeed.<br />
<br />
 In this case if the programmers used the bounding rectangle collision detection based on the screen position it would of worked, but notice that even that won’t always work for 2D games. Picture an asteroid game, where your spaceship advances more than a pixel on each frame and so is the asteroids.<br />
<br />
Moreover, this is not the only bug in Pacman. One might even think that the Pacman’s programmers were not such an elite coders. But when looking at the rest of the code, you get exactly the opposite impression. They were GREAT coders. So what exactly went wrong? How could they have missed that? <br />
<br />
If we look at it chronologically, they were inventing the game industry. There were games prior to Pacman, but I believe it was one of the firsts to have more software based code than hardware. <br />
<br />
Pong was, I think, mostly hardware. And the Atari games had a Hardware mechanism for detecting collisions. It does not mean that hardware programming is easier than software; in fact it is quite the opposite. But it does mean that there wasn’t a lot of code to learn from. They were writing some of the first lines in computer games industry, so show some respect. <br />
<br />
But there is also a timeless reason, I think, that programmers and QA everywhere, can relate to.<br />
<br />
Think of a program you write that for some reason is simply hell to debug. You either cannot:<br />
<br />
(A)Run it in a debugger.<br />
Or (B) The bug occurs only at theclient site, and you are not allowed to halt the system. <br />
Or ( C ) In cases like this where you don’t even have a line to place a breakpoint.<br />
<br />
You would have to look at the code and figure out the entire scenario.  But there’s more. If you wereQA-ing PacMan, what would you do? Would you spend most of your time escapingthe ghosts or trying to run into them?<br />
<br />
And that leads me to the second collision detection procedure. It had more testing based on screen position. It subtracts Pacman screen position from the ghost’s and tests if the result is lesser than 4 (but it does not test if it greater than -4).<br />
<br />
Oh yea, one more thing. It does that ONLY when in scared mode, when Pacman IS chasing the ghosts.<br />
<br />
So this is only speculating, but it is based on real life experience.<br />
<br />
If the bug happens when you are running into ghosts, it would probably appear mainly when YOU chase the ghosts. Now this is, I think, classic.The first thing that comes to mind is “something is wrong in the scared mode,so let’s add a scared mode collision detection patch!”. If you programmed for long enough, you are probably nodding now.<br />
<br />
So a nice conclusion would be, when testing, try to test NOTalways what is an expected behavior, try to test sometimes the unexpected. But a nicer conclusion would be, if you missed that one bug, well, you can still sale millions of copies. ka-ching!]]></description>
		<pubDate>Wed, 02 Nov 2011 19:23:48 +0000</pubDate>
		<guid isPermaLink="false">1dfe257477e8cc749ac0a3bdcd98fca0</guid>
	</item>
	<item>
		<title>An Introduction to Version Control</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/an-introduction-to-version-control-r2831</link>
		<description><![CDATA[<strong class='bbc'><span style='font-size: 18px;'> Introducing Beanstalk</span></strong><br />
<br />
We have some exciting news!  GameDev.net has partnered with <a href='http://beanstalkapp.com/' class='bbc_url' title='External link' rel='nofollow external'>Beanstalk</a>, one of the most reliable Git and Subversion hosts, to bring you a powerful subscription service that both shows your support for GameDev.net while giving you top-notch version control.   Beanstalk allows you to manage your code, collaborate on projects, and even deploy updates.  Version control services are one of the most long-standing features requested by users of the site and we wanted to make sure that the services we provide are rock-solid and are best-of-class.  Because we aren't experts in version control system hosting we approached Beanstalk with the idea of partnering together with us to provide our users with Subversion & Git access.<br />
<br />
<span class='bbc_underline'>You want to use Beanstalk because you get a service that is reliable, safe & secure, and upgradeable. </span> That is why we chose them.   One of our <strong class='bbc'>biggest </strong>concerns when relying on a company for source control is whether or not that company will be around tomorrow.   No offense to the companies that offer free-to-play source version control services, but you have to wonder how important you are to them.  <br />
<ul class='bbc'><li>Are they even going to take care of you if the do go out of business?</li><li>Will they delete your account tomorrow because you haven't worked on your project for a few weeks?</li><li>Can they afford to put in the resources to ensure that your project isn't one server crash away from being lost?</li></ul><br />
Source control is just too important to risk.  Beanstalk gives you peace of mind, and that should be worth a lot if you value your own projects.  <em class='bbc'>With Beanstalk you can be sure that when you wake up tomorrow, a month from now, or even years from now that your project will be safe and secure.  </em><br />
<br />
Read through the following article as it is a great introduction to version control.   If you are interested in signing up with this great company, then <a href='http://www.gamedev.net/subscribe' class='bbc_url' title=''><strong class='bbc'>click here to read more</strong></a> about our GDNet+ Beanstalk subscription service.<br />
<br />
<p class='bbc_right'>- Michael Tanczos<br />
CTO, GameDev.net<br />
</p><br />
<br />
<strong class='bbc'><span style='font-size: 18px;'>An introduction to version control</span></strong><br />
<br />
<a href='http://guides.beanstalkapp.com/' class='bbc_url' title='External link' rel='nofollow external'>Note:  This article is republished from guides.beanstalkapp.com and is one of many helpful articles that Beanstalk provides to help users out<br />
</a><br />
If  you are interested in version control, but have not made the jump yet,  the most common reason we’ve heard is how confusing it seems. The best  place to start if you are new to version control are the basic concepts.<br />
<br />
    <br />
<span style='font-size: 18px;'><strong class='bbc'>Why is version control important?</strong></span><br />
<br />
  If you are reading this, it’s possible that you are updating  documents that look something like this: index-v12-old2.html. Let’s get  away from this and on to something that will not only allow you to  control your source code and files, but become more productive as a  team. If this sounds familiar:<br />
 <ul class='bbc'><li>Communicated with your team via email about updates.</li><li>Made updates directly on your production server.</li><li>Accidentally overwrote some files, which can never be retrieved again.</li></ul>  You can now look forward to this instead:<br />
 <ul class='bbc'><li>File names and directory structures that are consistent for all team members.</li><li>Making changes with confidence, and even reverting when needed.</li><li>Relying on source control as the communication medium for your team.</li><li>Easily deploying different versions of your code to staging or production servers.</li><li>Understanding who made a change and when it happened.</li></ul>    <br />
<span style='font-size: 18px;'><strong class='bbc'>The basic concepts</strong></span><br />
  <br />
<span style='font-size: 12px;'><strong class='bbc'>Tracking changes</strong></span><br />
<br />
A version control system is mostly based around one concept, tracking  changes that happen within directories or files. Depending on the  version control system, this could vary from knowing a file changed to  knowing specific characters or bytes in a file have changed.<br />
<br />
 In most cases, you specify a directory or set of files that should  have their changes tracked by version control. This can happen by  checking out (or cloning) a repository from a host, or by telling the  software which of your files you wish to have under version control.<br />
<br />
 The set of files or directories that are under version control are more commonly called a repository.<br />
<br />
 As you make changes, it will track each change behind the scenes. The  process will be transparent to you until you are ready to commit those  changes.<br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>Committing</strong></span><br />
<br />
As you work with your files that are under version control, each  change is tracked automatically. This can include modifying a file,  deleting a directory, adding a new file, moving files or just about  anything else that might alter the state of the file. Instead of  recording each change individually, the version control system will wait  for you to submit your changes as a single collection of actions. In  version control, this collection of actions is known as a <strong class='bbc'>“commit.”</strong><br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>Revisions and Changesets</strong></span><br />
<br />
When a commit is made, the changes are recorded as a <strong class='bbc'>changeset</strong> and given a unique revision. This revision could be in the form of an incremented number (1, 2, 3) or a unique hash (like 846eee7d92415cfd3f8a936d9ba5c3ad345831e5)  depending on the system. By knowing the revision of a changeset it  makes it easy to view or reference it later. A changset will include a  reference to the person who made the commit, when the change was made,  the files or directories affected, a comment and even the changes that  happened within the files (lines of code).<br />
<br />
 When it comes to collaboration, viewing past revisions and changesets  is a valuable tool to see how your project has evolved and for  reviewing teammates’ code. Each version control system has a formatted  way to view a complete history (or log) of each revision and changeset  in the repository.<br />
<br />
<p class='bbc_center'> <a class='resized_img' rel='lightbox[b789576d82cf526145d4a310cb2ac87e]' id='ipb-attach-url-5934-0-28012400-1330203877' href="http://www.gamedev.net/index.php?app=core&module=attach&section=attach&attach_rel_module=ccs&attach_id=5934" title="bs-changeset.png - Size: 95.39K, Downloads: 214"><img src="http://public.gamedev.net/uploads/monthly_10_2011/ccs-8549-0-75044800-1319966691_thumb.png" id='ipb-attach-img-5934-0-28012400-1330203877' style='width:250;height:123' class='attach' width="250" height="123" alt="Attached Image: bs-changeset.png" /></a>  <br />
</p><strong class='bbc'><br />
<span style='font-size: 12px;'>Getting updates</span></strong><br />
 <br />
As members of your team commit changes, it is important that you have  the latest version. Having the latest version reduces the chance of a  conflict. Getting the latest changes from a repository is as simple as  doing a pull or update from another computer (usually a hosted or  centralized server). When an update or pull is requested, only the  changes since your last request are downloaded.<br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>Conflicts</strong></span><br />
<br />
What if the latest update or commit results in a conflict? That is,  what if your changes are so similar to the changes that another team  member made that the version control system can’t determine which is the  correct and authoritative change?  In most cases, the version control  system will provide a way to view the difference between the conflicting  versions, allowing you to make a choice. You can either edit the files  manually to merge the options, or allow one revision to wins over the  other. You may want to collaborate with the person who made the other  commit to make sure you’re not undoing their important work!<br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>Diffing (or, viewing the differences)</strong></span><br />
<br />
Since each commit is recorded as a change to a file or set of files  and directories, it is sometimes useful to view what changed between  revisions. For instance, if a recent deployment of your web site is  broken and you narrowed down the cause to a particular file, the best  action to take is to see what recently changed in that file. By viewing a  diff, you can compare two files or even a set of files to see what  lines of code changed, when it changed and who changed it. Most version  control tools let you compare two sequential revisions, but also two  revisions from anywhere in the history.<br />
<br />
<p class='bbc_center'> <a class='resized_img' rel='lightbox[b789576d82cf526145d4a310cb2ac87e]' id='ipb-attach-url-5935-0-28078100-1330203877' href="http://www.gamedev.net/index.php?app=core&module=attach&section=attach&attach_rel_module=ccs&attach_id=5935" title="bs-diff.png - Size: 35.33K, Downloads: 208"><img src="http://public.gamedev.net/uploads/monthly_10_2011/ccs-8549-0-95549900-1319966739_thumb.png" id='ipb-attach-img-5935-0-28078100-1330203877' style='width:250;height:57' class='attach' width="250" height="57" alt="Attached Image: bs-diff.png" /></a>  <br />
</p><strong class='bbc'><br />
<span style='font-size: 12px;'>Branching and merging</span></strong><br />
<br />
There are some cases when you want to experiment or commit changes to  the repo that could break things elsewhere in your code (like working  on a new feature). Instead of committing this code directly to the main  set of files (usually referred to as <strong class='bbc'>trunk</strong> or <strong class='bbc'>master</strong>),  you can create something called a branch. A branch allows you to create  a copy (or snapshot) of the repository that you can modify in parallel  without altering the main set. You can continue to commit new changes to  the branch as you work, while others commit to the trunk or master  without the changes affecting each other.<br />
<br />
 Once you’re comfortable with the experimental code, you will want to make it part of the trunk or master again. This is where <strong class='bbc'>merging</strong>  comes in. Since the version control system has recorded every change so  far, it knows how each file has been altered. By merging the branch  with the trunk or master (or even another branch), your version control  tool will attempt to seamlessly merge each file and line of code  automatically. Once a branch is merged it then updates the trunk or  master with the latest files.<br />
<br />
 For example, let’s say you want to experiment with a new layout for a  web site. This may require heavy changes in many files, could break  existing code and it may not turn out as expected. It could also take a  long time, so you want to continue committing the changes. Instead of  committing to the trunk or master set of files, you create a branch.  From this point forward, any changes made in the new branch will not  affect others in the trunk or master. Days or weeks may go by allowing  you to commit changes, test and refine. When the new layout is working  properly and you are comfortable with the result you are probably ready  to make it a permanent part of the site. This is the point where you  will merge the branch with the trunk or master. Once the merge is  complete, it will combine the changes from the branch with the most  recent version of the trunk or master.<br />
<br />
 In some cases the version control system might not be able to figure  out which change to apply between two revisions when doing a merge. If  this happens a conflict will arise. A conflict in this scenario is the  same as the conflict mentioned above and requires manual intervention to  decide which files or lines of code should remain.<br />
<br />
    <br />
<span style='font-size: 18px;'><strong class='bbc'>Types of Version Control Systems</strong></span><br />
<br />
The three most popular version control systems are broken down into two main categories, <strong class='bbc'>centralized</strong> and <strong class='bbc'>decentralized</strong> (also known as distributed).<br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>Centralized Version Control</strong></span><br />
<br />
<p class='bbc_center'> <a class='resized_img' rel='lightbox[b789576d82cf526145d4a310cb2ac87e]' id='ipb-attach-url-5936-0-28090400-1330203877' href="http://www.gamedev.net/index.php?app=core&module=attach&section=attach&attach_rel_module=ccs&attach_id=5936" title="cvc.png - Size: 10.07K, Downloads: 109"><img src="http://public.gamedev.net/uploads/monthly_10_2011/ccs-8549-0-72240200-1319966781_thumb.png" id='ipb-attach-img-5936-0-28090400-1330203877' style='width:250;height:125' class='attach' width="250" height="125" alt="Attached Image: cvc.png" /></a> <br />
</p><br />
At the time of this writing, the most popular version control system  is known as Subversion, which is considered a centralized version  control system. The main concept of a centralized system is that it  works in a client and server relationship. The repository is located in  one place and provides access to many clients. It’s very similar to FTP  in where you have an FTP client which connects to an FTP server. All  changes, users, commits and information must be sent and received from  this central repository.<br />
<br />
 The primary benefits of Subversion are:<br />
 <ul class='bbc'><li>It is easy to understand.</li><li>You have more control over users and access (since it is served from one place).</li><li>More GUI & IDE clients (Subversion has been around longer).</li><li>Simple to get started.</li></ul> At the same time, Subversion has some drawbacks:<br />
 <ul class='bbc'><li>Dependent on access to the server.</li><li>Hard to manage a server and backups (well, not with Beanstalk of course!)</li><li>It can be slower because every command connects to the server.</li><li>Branching and merging tools are difficult to use.</li></ul>  <br />
<span style='font-size: 12px;'><strong class='bbc'>Distributed Version Control</strong></span><br />
 <br />
<p class='bbc_center'><a class='resized_img' rel='lightbox[b789576d82cf526145d4a310cb2ac87e]' id='ipb-attach-url-5937-0-28102500-1330203877' href="http://www.gamedev.net/index.php?app=core&module=attach&section=attach&attach_rel_module=ccs&attach_id=5937" title="dvc.png - Size: 16.13K, Downloads: 73"><img src="http://public.gamedev.net/uploads/monthly_10_2011/ccs-8549-0-06454700-1319966820_thumb.png" id='ipb-attach-img-5937-0-28102500-1330203877' style='width:250;height:172' class='attach' width="250" height="172" alt="Attached Image: dvc.png" /></a> <br />
</p><br />
Distributed systems are a newer option. In distributed version  control, each user has their own copy of the entire repository, not just  the files but the history as well. Think of it as a network of  individual repositories. In many cases, even though the model is  distributed, services like Beanstalk are used for simplifying the  technical challenges of sharing changes. The two most popular  distributed version control systems are Git and Mercurial. <br />
<br />
 The primary benefits of Git and Mercurial are:<br />
 <ul class='bbc'><li>More powerful and detailed change tracking, which means less conflicts.</li><li>No server necessary – all actions except sharing repositories are local (commit offline).</li><li>Branching and merging is more reliable, and therefore used more often.</li><li>It’s fast.</li></ul> At the same time, Git and Mercurial do have some drawbacks:<br />
 <ul class='bbc'><li>The distributed model is harder to understand.</li><li>It’s new, so not as many GUI clients.</li><li>The revisions are not incremental numbers, which make them harder to reference.</li><li>It’s can be easier to make mistakes until you are familiar with the model.</li></ul> Which one is right for you? This really depends on how you work. We  usually recommend Subversion if you are just getting started, are not  comfortable with the command line or do not plan to do a lot of  branching and merging. Otherwise, we encourage you to try Git or  Mercurial and see if you like it. With Beanstalk, you’re free to go back  and forth as needed without switching hosts.]]></description>
		<pubDate>Sun, 30 Oct 2011 09:23:29 +0000</pubDate>
		<guid isPermaLink="false">d0e7244b36e4e2cfcc04d247bff16291</guid>
	</item>
	<item>
		<title>A Type-safe Generic Pointer</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/a-type-safe-generic-pointer-r2768</link>
		<description><![CDATA[<br />
<strong class='bbc'>Introduction</strong><br />
 Sometimes there comes the need to store a pointer to an object whose type is not known at compile-time. The common method to achieve this in C++ is to store it in a void pointer. The void pointer can then be cast back to the appropriate type and used when required. Call-back functions<sup class='bbc'>[1]</sup> in libraries are a well-known example of this method; the user-data pointer is often a void pointer. This works, but has a glaring problem: it’s not type-safe; it’s up to the programmer to interpret the void pointer in the appropriate manner. Although programmers are quite used to taking care when dealing with void pointers, every once in a while a void pointer gets accidently cast to the wrong type of object. When this happens, the programmer is actually considered lucky if a crash occurs.<br />
<br />
 <br />
<strong class='bbc'>Enter any_ptr</strong><br />
 The any_ptr smart pointer<sup class='bbc'>[2]</sup> can point to any type of object and provide type-safe access to it. In some sense, any_ptr is to pointers what boost::any<sup class='bbc'>[3]</sup> is to objects. There are no requirements to be fulfilled by the types of objects that any_ptr can point to. In fact, the performance and size penalty incurred by its use is so low, that it could potentially be used almost wherever a void pointer is required. To start using any_ptr, we simply add a single header <em class='bbc'>any_ptr.h</em> (<a href='http://downloads.gamedev.net/features/programming/TypeSafeGenPtr/any_ptr%20code.zip' class='bbc_url' title='External link' rel='nofollow external'>see source code</a> accompanying this article) to our project. any_ptr has no dependencies on any library and can be used independently. any_ptr also does not require exceptions<sup class='bbc'>[5]</sup> or RTTI<sup class='bbc'>[6]</sup> to be enabled.<br />
<br />
 <br />
<strong class='bbc'>Type-safety</strong><br />
 any_ptr behaves almost like a void pointer, except for when it is type-cast. A cast to the wrong type of pointer will yield a null pointer which the programmer can check for. <br />
<strong class='bbc'>Example</strong><br />
  int a = 10; any_ptr pAny = &a; int   *pA = pAny; // 'pA' will point to 'a'; type-compatible float *pB = pAny; // 'pB' will point to null; type-incompatible  <br />
<strong class='bbc'>Const-correctness</strong><br />
 any_ptr also takes care of const-correctness<sup class='bbc'>[4]</sup>; a cast to an incompatible type with respect to const-correctness will yield a null pointer. <br />
<strong class='bbc'>Example 1</strong><br />
  int a = 10; any_ptr pAny = &a; const int *p1 = pAny; // 'p1' will point to 'a'; const-compatible int   	*p2 = pAny; // 'p2' will point to 'a'; const-compatible  <br />
<strong class='bbc'>Example 2</strong><br />
  const int a = 10; any_ptr pAny = &a; const int *p1 = pAny; // 'p1' will point to 'a'; const-compatible int   	*p2 = pAny; // 'p2' will point to null; const-incompatible  <br />
<strong class='bbc'>Example: passing user-data to call-back functions</strong><br />
 Instead of using raw void pointers, any_ptr could be used to pass user-defined data into call-back functions. This would provide the author of the call-back function with type-safe access to the user-defined data. Consider the following pseudo-code:  #include  #include  // Call back for key presses typedef void (*KeyPressedCallback)(void *pUserData); class KeyPressNotifier { private: 	struct RegistrationInfo 	{     	KeyPressedCallback  _pCallback;     	void           	*_pUserData; 	}; 	typedef std::map InfoMap; 	InfoMap _infoMap; public: 	void Register(int key, KeyPressedCallback pCallback, void *pUserData) 	{     	RegistrationInfo &info = _infoMap[key];     	info._pCallback = pCallback;     	info._pUserData = pUserData; 	} 	void UpdateKeyPress(int key) 	{     	InfoMap::const_iterator itr = _infoMap.find( key );     	if( itr != _infoMap.end() )         	itr->second._pCallback( itr->second._pUserData ); 	} }; void OnAccelerate(void *pUserData) { 	// Expect the speed to be passed in 	float *pSpeed = static_cast(pUserData); 	assert( pSpeed ); 	// Increment speed 	++(*pSpeed); } void OnDrinkPotion(void *pUserData) { 	// Expect the health to be passed in 	int *pHealth = static_cast(pUserData); 	assert( pHealth ); 	// Increment health 	++(*pHealth); } enum Keys { 	UpArrowKey, 	EnterKey }; int main(int /*argc*/, char * /*argv*/[]) { 	KeyPressNotifier notifier; 	float speed = 10.5f; 	notifier.Register( UpArrowKey, OnAccelerate, &speed ); 	float health = 100; 	notifier.Register( EnterKey, OnDrinkPotion, &health ); 	notifier.UpdateKeyPress( UpArrowKey ); 	notifier.UpdateKeyPress( EnterKey ); 	assert( speed == 11.5f ); 	assert( health == 101 ); }  On first look the code above seems okay, but there’s a bug. In the OnDrinkPotion function, the player health is expected to be passed in as an integer. If null is passed in, an assertion failure is triggered. Now although the player health is passed in, its type is of float. In this case, since the pointer obtained from the void pointer to user-data is invalid but not null, no assertion failure is triggered. In this simple example, the consequences of this bug are not much. But imagine this simple example’s real-life counterpart with complex objects being passed in as user-data instead of primitive types. In this scenario, simply replacing the void pointers with any_ptrs provides increased type-safety. The following is the same code as above, but with any_ptrs used instead of void pointers:<br />
<br />
  <strong class='bbc'><span class='bbc_underline'>#include "any_ptr.h"</span></strong> #include  #include  // Call back for key presses typedef void (*KeyPressedCallback)(<strong class='bbc'><span class='bbc_underline'>any_ptr</span></strong> pUserData); class KeyPressNotifier { private: 	struct RegistrationInfo 	{     	KeyPressedCallback  _pCallback;     	<strong class='bbc'><span class='bbc_underline'>any_ptr</span></strong>         	_pUserData; 	}; 	typedef std::map InfoMap; 	InfoMap _infoMap; public: 	void Register(int key, KeyPressedCallback pCallback, <strong class='bbc'><span class='bbc_underline'>any_ptr</span></strong> pUserData) 	{     	RegistrationInfo &info = _infoMap[key];     	info._pCallback = pCallback;     	info._pUserData = pUserData; 	} 	void UpdateKeyPress(int key) 	{     	InfoMap::const_iterator itr = _infoMap.find( key );     	if( itr != _infoMap.end() )         	itr->second._pCallback( itr->second._pUserData ); 	} }; void OnAccelerate(<strong class='bbc'><span class='bbc_underline'>any_ptr</span></strong> pUserData) { 	// Expect the speed to be passed in 	float *pSpeed = static_cast(pUserData); 	assert( pSpeed ); 	// Increment speed 	++(*pSpeed); } void OnDrinkPotion(<strong class='bbc'><span class='bbc_underline'>any_ptr</span></strong> pUserData) { 	// Expect the health to be passed in 	int *pHealth = static_cast(pUserData); 	assert( pHealth ); 	// Increment health 	++(*pHealth); } enum Keys { 	UpArrowKey, 	EnterKey }; int main(int /*argc*/, char * /*argv*/[]) { 	KeyPressNotifier notifier; 	float speed = 10.5f; 	notifier.Register( UpArrowKey, OnAccelerate, &speed ); 	float health = 100; 	notifier.Register( EnterKey, OnDrinkPotion, &health ); 	notifier.UpdateKeyPress( UpArrowKey ); 	notifier.UpdateKeyPress( EnterKey ); 	assert( speed == 11.5f ); 	assert( health == 101 ); }  When the author of the OnDrinkPotion function tries to access the player’s health through the any_ptr assuming it to be an integer, the cast yields a null pointer. This causes the author’s assertion in the next line to fail, alerting the author that something is wrong. <br />
<strong class='bbc'>Using any_ptr with existing call-back functions</strong><br />
 Unlike the previous example, existing library code can’t always be freely modified to make use of any_ptrs in place of void pointers. In this case, instead of passing a void pointer to the required data, the programmer could pass a void pointer to an any_ptr which points to the required data. A convention could be followed that user-data void pointers in call-back functions always point to any_ptrs. Although (for various practical reasons) some exceptions to the convention might be required occasionally, if used properly it could increase type-safety. <br />
<strong class='bbc'>Limitations</strong><br />
 <ul class='bbc'><li>Although this should never really be an issue, an any_ptr occupies more space than a raw void pointer (its size is a void pointer plus an integer). </li><li>Casting from an any_ptr is slower than casting from a void pointer. Again this should never really be an issue. </li><li>Casting to base class pointers from an any_ptr will not work. This was pointed out to me by the thorough GameDev.net review staff.</li></ul> <br />
<strong class='bbc'>Closing</strong><br />
 The any_ptr source code is released under the MIT license and has been tested on the following compilers: <ul class='bbc'><li>Microsoft Visual C++ 2005/2008/2010 </li><li>gcc 4.4.1 (TDM-2 mingw32)</li></ul> There is much potential for improvement; if you make changes to the code, improve it, or have some better ideas, I would love to know. I can be reached by email at francisxavierjp [at] gmail [dot] com. Comments and suggestions are always welcome! <br />
<strong class='bbc'>References</strong><br />
 <ul class='bbcol decimal'><li><a href='http://en.wikipedia.org/wiki/Callback_%28computer_science%29' class='bbc_url' title='External link' rel='nofollow external'>Callback (computer science), from Wikipedia</a> </li><li><a href='http://en.wikipedia.org/wiki/Smart_pointer' class='bbc_url' title='External link' rel='nofollow external'>Smart pointer, from Wikipedia</a> </li><li><a href='http://www.boost.org/doc/libs/1_43_0/doc/html/any.html' class='bbc_url' title='External link' rel='nofollow external'>Boost.Any, from Boost Library Documentation</a> </li><li><a href='http://www.parashift.com/c++-faq-lite/const-correctness.html' class='bbc_url' title='External link' rel='nofollow external'>Const correctness, from C++ FAQ Lite</a> </li><li><a href='http://en.wikipedia.org/wiki/Exception_handling' class='bbc_url' title='External link' rel='nofollow external'>Exception handling, from Wikipedia</a> </li><li><a href='http://en.wikipedia.org/wiki/Run-time_type_information' class='bbc_url' title='External link' rel='nofollow external'>Run-time type information, from Wikipedia</a></li></ul>]]></description>
		<pubDate>Tue, 17 Aug 2010 12:23:26 +0000</pubDate>
		<guid isPermaLink="false">efcce1c8f8c7b18ffa9c63bf6a2713a7</guid>
	</item>
	<item>
		<title>A Collection of Examples of 64-bit Errors in Re...</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/a-collection-of-examples-of-64-bit-errors-in-re-r2767</link>
		<description><![CDATA[<br />
<strong class='bbc'>Introduction</strong><br />
 Our company OOO "Program Verification Systems" develops a special static analyzer Viva64 that detects <a href='http://www.viva64.com/terminology/64-bit_error.html' class='bbc_url' title='External link' rel='nofollow external'>64-bit errors</a> in the code of C/C++ applications. During this development process we constantly enlarge our collection of examples of 64-bit defects, so we decided to gather the most interesting ones in this article. Here you will find examples both taken directly from the code of real applications and composed synthetically relying on real code since such errors are too "extended" throughout the native code.<br />
<br />
 The article only demonstrates various types of 64-bit errors and does not describe methods of detecting and preventing them. If you want to know how to diagnose and fix defects in 64-bit programs, please see the following sources:<br />
<br />
 <ul class='bbcol decimal'><li><a href='http://www.viva64.com/articles/x64-lessons/' class='bbc_url' title='External link' rel='nofollow external'>Lessons on development of 64-bit C/C++ applications</a> [1]; </li><li><a href='http://www.viva64.com/art-1-2-710804781.html' class='bbc_url' title='External link' rel='nofollow external'>About size_t and ptrdiff_t</a> [2]; </li><li><a href='http://www.viva64.com/art-1-2-599168895.html' class='bbc_url' title='External link' rel='nofollow external'>20 issues of porting C++ code on the 64-bit platform</a> [3]; </li><li><a href='http://www.viva64.com/art-4-2-747004748.html' class='bbc_url' title='External link' rel='nofollow external'>PVS-Studio Tutorial</a> [4]; </li><li><a href='http://www.viva64.com/art-1-2-377673569.html' class='bbc_url' title='External link' rel='nofollow external'>A 64-bit horse that can count</a> [5].</li></ul> You may also try the demo version of the <a href='http://www.viva64.com/pvs-studio/' class='bbc_url' title='External link' rel='nofollow external'>PVS-Studio</a> tool that includes the Viva64 static code analyzer which detects almost all the errors described in this article. The demo version of the tool can be downloaded here: <a href='http://www.viva64.com/pvs-studio/download/' class='bbc_url' title='External link' rel='nofollow external'>http://www.viva64.co...tudio/download/</a>.<br />
<br />
 <br />
<strong class='bbc'>Example 1. Buffer overflow</strong><br />
  struct STRUCT_1 {   int *a; }; struct STRUCT_2 {   int x; }; ... STRUCT_1 Abcd; STRUCT_2 Qwer; memset(&Abcd, 0, sizeof(Abcd)); memset(&Qwer, 0, sizeof(Abcd));  In this program, two objects of the STRUCT_1 and STRUCT_2 types are defined which must be zeroed (all the fields must be initialized with nulls) before being used. While implementing the initialization, the programmer decided to copy a similar line and replaced "&Abcd" with "&Qwer" in it. But he forgot to replace "sizeof(Abcd)" with "sizeof(Qwer)". Due to mere luck, the sizes of the STRUCT_1 and STRUCT_2 structures coincided on a 32-bit system and the code has been working correctly for a long time.<br />
<br />
 When porting the code on the 64-bit system, the size of the Abcd structure increased and it resulted in a buffer overflow error (see Figure 1).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862044308/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4075/4862044308_16b23604af.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 1 - Schematic explanation of the buffer overflow example<br />
<br />
 Such an error is difficult to detect if the data which should be used much later get spoiled.<br />
<br />
 <br />
<strong class='bbc'>Example 2. Unnecessary type conversions</strong><br />
  char *buffer; char *curr_pos; int length; ... while( (*(curr_pos++) != 0x0a) &&     	((UINT)curr_pos - (UINT)buffer < (UINT)length) );  This code is bad yet it is real. Its task is to search for the end of the line marked with the 0x0A symbol. The code will not process lines longer than INT_MAX characters since the length variable has the int type. But we are interested in another error, so let's assume that the program works with a small buffer and it is correct to use the int type here.<br />
<br />
 The problem is that the buffer and curr_pos pointers might lie outside the first 4 Gbytes of the address space in a 64-bit system. In this case, the explicit conversion of the pointers to the UINT type will through away the significant bits and the algorithm will be violated (see Figure 2).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862044278/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4093/4862044278_cc06912343.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 2 - Incorrect calculations when searching for the terminal symbol<br />
<br />
 What is unpleasant about this error is that the code can work for a long time as long as buffer memory is allocated within the first four Gbytes of the address space. To fix the error, you should remove the type conversions which are absolutely unnecessary:<br />
<br />
  while(curr_pos - buffer < length && *curr_pos != '&#092;r')   curr_pos++;  <br />
<strong class='bbc'>Example 3. Incorrect #ifdef's</strong><br />
 You may often see code fragments wrapped in #ifdef - -#else - #endif constructs in programs with long history. When porting programs to the new architecture, the incorrectly written conditions might result in compilation of other code fragments than the developers intended before (see Figure 3). For example:<br />
<br />
  #ifdef _WIN32 // Win32 code   cout << "This is Win32" << endl; #else     	// Win16 code   cout << "This is Win16" << endl; #endif //Alternative incorrect variant: #ifdef _WIN16 // Win16 code   cout << "This is Win16" << endl; #else     	// Win32 code   cout << "This is Win32" << endl; #endif  <a href='http://www.flickr.com/photos/gamedevnet/4862044236/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4102/4862044236_06072ed7c9.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 3 - Two variants - this is too little<br />
<br />
 It is dangerous to rely on the #else variant in such cases. It is better to explicitly check behavior for each case (see Figure 4) and add a message about a compilation error into the #else branch:<br />
<br />
  #if   defined _M_X64 // Win64 code (Intel 64)   cout << "This is Win64" << endl; #elif defined _WIN32 // Win32 code   cout << "This is Win32" << endl; #elif defined _WIN16 // Win16 code   cout << "This is Win16" << endl; #else   static_assert(false, "Unknown platform "); #endif  <a href='http://www.flickr.com/photos/gamedevnet/4861422557/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4137/4861422557_f44b586689.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 4 - All the possible compilation ways are checked<br />
<br />
 <br />
<strong class='bbc'>Example 4. Confusion of int and int*</strong><br />
 In obsolete programs, especially those written in C, you may often see code fragments where a pointer is stored in the int type. However, sometimes it is done through lack of attention rather than on purpose. Let's consider an example with confusion caused by using the int type and a pointer to the int type:<br />
<br />
  int GlobalInt = 1; void GetValue(int **x) {   *x = &GlobalInt; } void SetValue(int *x) {   GlobalInt = *x; } ... int XX; GetValue((int **)&XX); SetValue((int *)XX);   In this sample, the XX variable is used as a buffer to store the pointer. This code will work correctly on those 32-bit systems where the size of the pointer coincides with the int type's size. In a 64-bit system, this code is incorrect and the call<br />
<br />
  GetValue((int **)&XX); will cause corruption of the 4 bytes of memory next to the XX variable (see Figure 5).  <a href='http://www.flickr.com/photos/gamedevnet/4861422511/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4135/4861422511_51d44e8540.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
  Figure 5 - Memory corruption near the XX variable  This code was being written either by a novice or in a hurry. The explicit type conversions signal that the compiler was resisting the programmer till the last hinting to him that the pointer and the int type are different entities. But crude force won.<br />
<br />
 Correction of this error is elementary and lies in choosing an appropriate type for the XX variable. The explicit type conversion becomes no longer necessary:<br />
<br />
  int *XX; GetValue(&XX); SetValue(XX);  <br />
<strong class='bbc'>Example 5. Using deprecated (obsolete) functions</strong><br />
 Some API-functions can be dangerous when developing 64-bit applications although they were composed for compatibility purpose. The functions SetWindowLong and GetWindowLong are a typical example of these. You may often see the following code fragment in programs:<br />
<br />
  SetWindowLong(window, 0, (LONG)this); ... Win32Window* this_window = (Win32Window*)GetWindowLong(window, 0);  You cannot reproach the programmer who once wrote this code for anything. During the development process, he created this code relying on his experience and MSDN five or ten years ago and it is absolutely correct from the viewpoint of the 32-bit Windows. The prototype of these functions looks as follows:<br />
<br />
  LONG WINAPI SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong); LONG WINAPI GetWindowLong(HWND hWnd, int nIndex);  The explicit conversion of the pointer to the LONG type is also justified since the sizes of the pointer and the LONG type coincide in <a href='http://www.viva64.com/terminology/Win32.html' class='bbc_url' title='External link' rel='nofollow external'>Win32</a> systems. However, I think you understand that these type conversions might cause a crash or false behavior of the program after its being recompiled in the 64-bit version.<br />
<br />
 What is unpleasant about this error is that it occurs irregularly or very rarely at all. Whether the error will reveal itself or not depends upon the area of memory where the object is created referred to by the "this" pointer. If the object is created in the 4 least significant Gbytes of the address space, the 64-bit program can work correctly. The error might occur unexpectedly in a long time when the objects will start to be created outside the first four Gbytes due to memory allocation.<br />
<br />
 In a 64-bit system, you can use the SetWindowLong/GetWindowLong functions only if the program really saves some values of the LONG, int, bool types and the like. If you need to work with pointers, you should use the following extended function versions: <strong class='bbc'>SetWindowLongPtr/</strong>GetWindowLongPtr. However, I should recommend you to use new functions anyway in order to avoid new errors in future.<br />
<br />
 Examples with the SetWindowLong and GetWindowLong functions are classic and cited almost in all the articles on 64-bit software development. But you should understand that it is not only these functions that you must consider. Among other functions are: SetClassLong, GetClassLong, GetFileSize, EnumProcessModules, GlobalMemoryStatus (see Figure 6).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862044142/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4123/4862044142_1336e45e8e.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 6 - A table with the names of some obsolete and contemporary functions<br />
<br />
 <br />
<strong class='bbc'>Example 6. Truncation of values at an implicit type conversion</strong><br />
 An implicit conversion of the <a href='http://www.viva64.com/terminology/size_t.html' class='bbc_url' title='External link' rel='nofollow external'>size_t</a> type to the unsigned type and similar conversions are easily diagnosed by the compiler's warnings. But in large programs, such warnings might be easily missed. Let's consider an example similar to real code where the warning was ignored because it seemed to the programmer that nothing bad might happen when working with short strings.<br />
<br />
  bool Find(const ArrayOfStrings &arrStr) {   ArrayOfStrings::const_iterator it;   for (it = arrStr.begin(); it != arrStr.end(); ++it)   { 	unsigned n = it->find("ABC"); // Truncation 	if (n != string::npos)   	return true;   }   return false; };  The function searches for the text "ABC" in the array of strings and returns true if at least one string contains the sequence "ABC". After recompilation of the 64-bit version of the code, this function will always return true.<br />
<br />
 The "string::npos" constant has value 0xFFFFFFFFFFFFFFFF of the size_t type in the 64-bit system. When putting this value into the "n" variable of the unsigned type, it is truncated to 0xFFFFFFFF. As a result, the condition " n != string::npos" is always true since 0xFFFFFFFFFFFFFFFF is not equal to 0xFFFFFFFF (see Figure 7).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862044124/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4093/4862044124_8dbdc83331.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 7 - Schematic explanation of the value truncation error<br />
<br />
 The correction of this error is elementary - you just should consider the compiler's warnings:<br />
<br />
  for (auto it = arrStr.begin(); it != arrStr.end(); ++it) {   auto n = it->find("ABC");   if (n != string::npos) 	return true; } return false;  <br />
<strong class='bbc'>Example 7. Undefined functions in C</strong><br />
 Despite the passing years, programs or some of their parts written in C remain as large as life. The code of these programs is much more subject to 64-bit errors because of less strict rules of type checking in the C language.<br />
<br />
 In C, you can use functions without preliminary declaration. Let's look at an interesting example of a 64-bit error related to this feature. Let's first consider the correct version of the code where allocation takes place and three arrays, one Gbyte each, are used:<br />
<br />
  #include  void test() {   const size_t Gbyte = 1024 * 1024 * 1024;   size_t i;   char *Pointers[3];   // Allocate   for (i = 0; i != 3; ++i) 	Pointers[i] = (char *)malloc(Gbyte);   // Use   for (i = 0; i != 3; ++i) 	Pointers[i][0] = 1;   // Free   for (i = 0; i != 3; ++i) 	free(Pointers[i]); }  This code will correctly allocate memory, write one into the first item of each array and free the occupied memory. The code is absolutely correct on a 64-bit system.<br />
<br />
 Now let's remove or write a comment on the line "#include ". The code will be still compiled but the program will crash right after the launch. If the header file "stdlib.h" is not included in, the C compiler supposes that the malloc function will return the int type. The first two instances of memory allocation will be most likely successful. When the memory is being allocated for the third time, the malloc function will return the array address outside the first 2 Gbytes. Since the compiler supposes that the function's result has the int type, it will interpret the result incorrectly and save an incorrect value of the pointer in the Pointers array.<br />
<br />
 Let's consider the assembler code generated by the Visual C++ compiler for the 64-bit Debug version. In the beginning, there is the correct code that will be generated when the definition of the malloc function is present (i.e. the "stdlib.h" file is included in):<br />
<br />
  Pointers[i] = (char *)malloc(Gbyte); mov   rcx,qword ptr [Gbyte] call  qword ptr [__imp_malloc (14000A518h)] mov    rcx,qword ptr [i] mov    qword ptr Pointers[rcx*8],rax  Now let's look at the incorrect code when the definition of the malloc function is absent:<br />
<br />
  Pointers[i] = (char *)malloc(Gbyte); mov    rcx,qword ptr [Gbyte] call   malloc (1400011A6h) cdqe mov    rcx,qword ptr [i] mov    qword ptr Pointers[rcx*8],rax  Note that there is the CDQE (Convert doubleword to quadword) instruction. The compiler supposes that the result is contained in the eax register and extends it to a 64-bit value in order to write it into the Pointers array. Correspondingly, the most significant bits of the rax register will be lost. Even if the address of the allocated memory lies within the first four Gbytes, we will still get an incorrect result if the most significant bit of the eax register equals 1. For instance, address 0x81000000 will turn into 0xFFFFFFFF81000000.<br />
<br />
 <br />
<strong class='bbc'>Example 8. Remains of dinosaurs in large and old programs</strong><br />
 Large old program systems that have been developing for tens of years are abound in various atavisms and code fragments written with popular paradigms and styles of different years. In such systems, you can watch the evolution of programming languages when the oldest fragments are written in C and the freshest ones contain complex templates of Alexandrescu style.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422433/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4077/4861422433_bd77cbab5c.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 8 - Dinosaur excavations<br />
<br />
 There are atavisms referring to 64 bits as well. To be more exact, these are atavisms that prevent contemporary 64-bit code from correct work. Consider an example:<br />
<br />
  // beyond this, assume a programming error #define MAX_ALLOCATION 0xc0000000  void *malloc_zone_calloc(malloc_zone_t *zone,   size_t num_items, size_t size) {   void *ptr;   ...   if (((unsigned)num_items >= MAX_ALLOCATION) ||   	((unsigned)size >= MAX_ALLOCATION) ||   	((long long)size * num_items >=        (long long) MAX_ALLOCATION))   {   	fprintf(stderr,   	"*** malloc_zone_calloc[%d]: arguments too large: %d,%d&#092;n",   	getpid(), (unsigned)num_items, (unsigned)size); 	return NULL;   }   ptr = zone->calloc(zone, num_items, size);   ...   return ptr; }  First, the function's code contains the check of accessible sizes of allocated memory which are strange for the 64-bit system. Second, the generated diagnostic message is incorrect because if we ask to allocate memory for 4 400 000 000 items, we will see a strange message saying that the program cannot allocate memory for (only) 105 032 704 items. This happens because of the explicit type conversion to the unsigned type.<br />
<br />
 <br />
<strong class='bbc'>Example 9. Virtual functions</strong><br />
 One of the nice examples of 64-bit errors is the use of wrong argument types in definitions of virtual functions. Usually it is not one's mistake but just an "accident". It is nobody's fault but the error still remains. Consider the following case.<br />
<br />
 For a very long time there has been the CWinApp class in the MFC library that has the WinHelp function:<br />
<br />
  class CWinApp {   ...   virtual void WinHelp(DWORD dwData, UINT nCmd); };  To show the program's own help in a user application you had to overlap this function:<br />
<br />
  class CSampleApp : public CWinApp {   ...   virtual void WinHelp(DWORD dwData, UINT nCmd); };  Everything was alright until 64-bit systems appeared. The MFC developers had to change the interface of the WinHelp function (and some other functions as well) in the following way:<br />
<br />
  class CWinApp {   ...   virtual void WinHelp(DWORD_PTR dwData, UINT nCmd); };  The DWORD_PTR and DWORD types coincided in the 32-bit mode but they do not coincide in the 64-bit mode. Of course, the user application's developers must also change the type to DWORD_PTR but they have to learn about it somehow before doing this. As a result, an error occurs in the 64-bit version since the WinHelp function cannot be called in the user class (see Figure 9).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422413/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4080/4861422413_e790ca59e6.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 9 - The error related to virtual functions<br />
<br />
 <br />
<strong class='bbc'>Example 10. Magic constants as parameters</strong><br />
 Magic numbers contained in bodies of programs provoke errors and using them is a bad style. Such numbers are, for instance, numbers 1024 and 768 that strictly define screen resolution. Within the scope of this article, we are interested in those magic numbers that might cause issues in a 64-bit application. The most widely used magic numbers dangerous for 64-bit programs are shown in the table in Figure 10.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422375/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4142/4861422375_2983000ce6.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 10 - Magic numbers dangerous for 64-bit programs<br />
<br />
 Consider an example of working with the CreateFileMapping function taken from some CAD-system:<br />
<br />
  HANDLE hFileMapping = CreateFileMapping(   (HANDLE) 0xFFFFFFFF,   NULL,   PAGE_READWRITE,   dwMaximumSizeHigh,   dwMaximumSizeLow,   name);  Number 0xFFFFFFFF is used instead of the correct reserved constant INVALID_HANDLE_VALUE. It is incorrect from the viewpoint of a <a href='http://www.viva64.com/terminology/Win64.html' class='bbc_url' title='External link' rel='nofollow external'>Win64</a>-program where the INVALID_HANDLE_VALUE constant takes value 0xFFFFFFFFFFFFFFFF. Here is a correct way of calling the function:<br />
<br />
  HANDLE hFileMapping = CreateFileMapping(   INVALID_HANDLE_VALUE,   NULL,   PAGE_READWRITE,   dwMaximumSizeHigh,   dwMaximumSizeLow,   name);  Note. Some people think that value 0xFFFFFFFF turns into 0xFFFFFFFFFFFFFFFF while extending to the pointer. It is not so. According to C/C++ rules, value 0xFFFFFFFF has the "unsigned int" type since it cannot be represented with the "int" type. Correspondingly, value 0xFFFFFFFFu turns into 0x00000000FFFFFFFFu when extending to the 64-bit type. But if you write (size_t)(-1), you will get the expected 0xFFFFFFFFFFFFFFFF. Here "int" extends to "ptrdiff_t" first and then turns into "size_t".<br />
<br />
 <br />
<strong class='bbc'>Example 11. Magic constants denoting size</strong><br />
 Another frequent error is using magic constants to define an object's size. Consider an example of buffer allocation and zeroing:<br />
<br />
  size_t count = 500; size_t *values = new size_t[count]; // Only a part of the buffer will be filled memset(values, 0, count * 4);  In this case, in the 64-bit system, the amount of memory being allocated is larger than the amount of memory which is filled with zero values then (see Figure 11) . The error lies in the assumption that the size of the size_t type is always four bytes.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422365/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4080/4861422365_e2b3bf3dee.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 11 - Only a part of the array is filled<br />
<br />
 This is the correct code:<br />
<br />
  size_t count = 500; size_t *values = new size_t[count]; memset(values, 0, count * sizeof(values[0]));  You may encounter similar errors when calculating sizes of memory being allocated or data serialization.<br />
<br />
 <br />
<strong class='bbc'>Example 12. Stack overflow</strong><br />
 In many cases, a 64-bit program consumes more memory and stack. Allocation of more physical memory is not dangerous since a 64-bit program can access much larger amounts of this type of memory than a 32-bit one. But increase of stack memory consumption might cause a stack overflow.<br />
<br />
 The mechanism of using the stack differs in various operating systems and compilers. We will consider the specifics of using the stack in the code of Win64 applications built with the Visual C++ compiler.<br />
<br />
 When developing <a href='http://www.viva64.com/go.php?url=328' class='bbc_url' title='External link' rel='nofollow external'>calling conventions</a> in Win64 systems, the developers decided to bring an end to different versions of function calls. In Win32, there were a lot of calling conventions: stdcall, cdecl, fastcall, thiscall and so on. In Win64, there is only one "native" calling convention. The compiler ignores modifiers like __cdecl.<br />
<br />
 The calling convention on the x86-64 platform resembles the fastcall convention in x86. In the x64-convention, the first four integer arguments (left to right) are passed in 64-bit registers used specially for this purpose:<br />
<br />
 RCX: 1-st integer argument<br />
<br />
 RDX: 2-nd integer argument<br />
<br />
 R8: 3-rd integer argument<br />
<br />
 R9: 4-th integer argument<br />
<br />
 All the rest integer arguments are passed through the stack. The "this" pointer is considered an integer argument so it is always put into the RCX register. If floating-point values are passed, the first four of them are passed in the XMM0-XMM3 registers and all the next are passed through the stack.<br />
<br />
 Although arguments may be passed in registers, the compiler will still reserve space for them in stack therefore reducing the value of the RSP register (stack pointer). Each function must reserve at least 32 bytes (four 64-bit values corresponding to the registers RCX, RDX, R8, R9) in the stack. This space in the stack lets you easily save the contents of registers passed into the function in the stack. The function being called is not required to drop input parameters passed through the registers into the stack but stack space reservation allows to do this if necessary. If more than four integer parameters are passed, the corresponding additional space is reserved in the stack.<br />
<br />
 The described feature leads to a significant growth of stack consumption speed. Even if the function does not have parameters, 32 bytes will be "bit off" the stack all the same and they will not be used anyhow then. The use of such a wasteful mechanism is determined by the purposes of unification and debugging simplification.<br />
<br />
 Consider one more thing. The stack pointer RSP must be aligned on a 16-byte boundary before the next call of the function. Thus, the total size of the stack being used when calling a function <strong class='bbc'>without parameters</strong> in 64-bit code is <strong class='bbc'>48 bytes:</strong> 8 (return address) + 8 (alignment) + 32 (reserved space for arguments).<br />
<br />
 Can everything be so bad? No. Do not forget that a larger number of registers available to the 64-bit compiler allows it to build a more effective code and avoid reserving stack memory for some local function variables. Thus, the 64-bit version of a function in some cases uses less stack memory than its 32-bit version. To learn more about this question, see the article "<a href='http://www.viva64.com/blog/en/2010/06/07/707/' class='bbc_url' title='External link' rel='nofollow external'>The reasons why 64-bit programs require more stack memory</a>".<br />
<br />
 It is impossible to predict if a 64-bit program will consume more or less stack memory. Since a Win64-program can use 2-3 times more stack memory, you should secure yourself and change the project option responsible for the size of stack being reserved. Choose the Stack Reserve Size (/STACK:reserve switch) parameter in the project settings and increase the size of stack being reserved three times. This size is 1 Mbyte by default.<br />
<br />
 <br />
<strong class='bbc'>Example 13. A function with a variable number of arguments and buffer overflow</strong><br />
 Although it is considered a bad style in C++ to use functions with a variable number of arguments such as printf and scanf, they are still widely used. These functions provoke a lot of problems while porting applications to other systems including 64-bit ones. Consider an example:<br />
<br />
  int x; char buf[9]; sprintf(buf, "%p", &x);  The author of this code did not take into account that the pointer's size might become larger than 32 bits in future. As a result, this code will cause a buffer overflow on the 64-bit architecture (see Figure 12). This error might be referred to the type of errors caused by magic numbers (number '9' in this case) but the buffer overflow can occur without magic numbers in a real application.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862044036/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4114/4862044036_b27fff1cc7.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 12 - A buffer overflow when working with the sprintf function<br />
<br />
 There are several ways to correct this code. The most reasonable one is to factor the code in order to get rid of dangerous functions. For example, you may replace printf with cout and sprintf with boost::format or std::stringstream.<br />
<br />
 Note. Linux-developers often criticize this recommendation arguing that gcc checks if the format string corresponds to actual parameters which are being passed, for instance, into the printf function. Therefore it is safe to use the printf function. But they forget that the format string can be passed from some other part of the program or loaded from resources. In other words, in a real program, the format string is seldom present explicitly in the code and therefore the compiler cannot check it. But if the developer uses Visual Studio 2005/2008/2010, he will not get a warning on the code like "void *p = 0; printf("%x", p);" even if he uses the /W4 and /Wall switches.<br />
<br />
 <br />
<strong class='bbc'>Example 14. A function with a variable number of arguments and wrong format</strong><br />
 You may often see incorrect format strings in programs when working with the printf function and other similar functions. Because of these you will get wrong output values. Although it will not cause a crash, it is certainly an error:<br />
<br />
  const char *invalidFormat = "%u"; size_t value = SIZE_MAX; // A wrong value will be printed printf(invalidFormat, value);  In other cases, an error in the format string will be crucial. Consider an example based on an implementation of the UNDO/REDO subsystem in one program:<br />
<br />
  // The pointers were saved as strings here int *p1, *p2; .... char str[128]; sprintf(str, "%X %X", p1, p2); // In another function this string // was processed in the following way: void foo(char *str) {   int *p1, *p2;   sscanf(str, "%X %X", &p1, &p2);   // The result is incorrect values of p1 and p2 pointers.   ... }  The "%X" format is not intended to work with pointers and therefore such code is incorrect from the viewpoint of 64-bit systems. In 32-bit systems, it is quite efficient yet looks ugly.<br />
<br />
 <br />
<strong class='bbc'>Example 15. Storing integer values in double</strong><br />
 We did not encounter this error ourselves. Perhaps it is rare yet quite possible.<br />
<br />
 The double type has the size 64 bits and it is compatible with the IEEE-754 standard on 32-bit and 64-bit systems. Some programmers use the double type to store and handle integer types:<br />
<br />
  size_t a = size_t(-1); double b = a; --a; --b; size_t c = b; // x86: a == c           	// x64: a != c  The code of this example can be justified in case of a 32-bit system since the double type has 52 significant bits and can store a 32-bit integer values without loss. But when you try to store a 64-bit integer value into double, you might lose an exact value (see Figure 13).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862044024/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4101/4862044024_96404d00a0.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 13 - The number of significant bits in the types size_t and double<br />
<br />
 <br />
<strong class='bbc'>Example 16. Address arithmetic. A + B != A - (-B)</strong><br />
 <a href='http://www.viva64.com/terminology/Address_arithmetic.html' class='bbc_url' title='External link' rel='nofollow external'>Address arithmetic</a> is a means of calculating an address of some object with the help of arithmetic operations over pointers and also using pointers in comparison operations. Address arithmetic is also called pointer arithmetic.<br />
<br />
 It is address arithmetic that many 64-bit errors refer to. Errors often occur in exp<b></b>ressi&#111;ns where pointers and 32-bit variables are used together.<br />
<br />
 Consider the first error of this type:<br />
<br />
  char *A = "123456789"; unsigned B = 1; char *X = A + B; char *Y = A - (-B); if (X != Y)   cout << "Error" << endl;  The reason why A + B == A - (-B) in a Win32 program is explained in Figure 14.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422287/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4118/4861422287_7a25722a36.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 14 - Win32: A + B == A - (-B)<br />
<br />
 The reason why A + B != A - (-B) in a Win64 program is explained in Figure 15.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043978/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4097/4862043978_6a8d680966.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 15 - Win64: A + B != A - (-B)<br />
<br />
 You can eliminate the error if you use an appropriate <a href='http://www.viva64.com/terminology/Memsize-type.html' class='bbc_url' title='External link' rel='nofollow external'>memsize-type</a>. In this case, the ptrdfiff_t type is used:<br />
<br />
  char *A = "123456789"; ptrdiff_t B = 1; char *X = A + B; char *Y = A - (-B);  <br />
<strong class='bbc'>Example 17. Address arithmetic. Signed and unsigned types.</strong><br />
 Consider one more kind of the error related to signed and unsigned types. In this case, the error will immediately cause a program crash instead of a wrong comparison operation.<br />
<br />
  LONG p1[100]; ULONG x = 5; LONG y = -1; LONG *p2 = p1 + 50; p2 = p2 + x * y; *p2 = 1; // Access violation  The "x * y" exp<b></b>ressi&#111;n has value 0xFFFFFFFB and its type is unsigned. This code is efficient in the 32-bit version since addition of the pointer to 0xFFFFFFFB is equivalent to its decrement by 5. In the 64-bit version, the pointer will point far outside the p1 array's boundaries after being added to 0xFFFFFFFB (see Figure 16).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422237/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4121/4861422237_49ac0779f9.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 16 - Out of the array's boundaries<br />
<br />
 To correct this issue, you should use memsize-types and be careful when working with signed and unsigned types:<br />
<br />
  LONG p1[100]; LONG_PTR x = 5; LONG_PTR y = -1; LONG *p2 = p1 + 50; p2 = p2 + x * y; *p2 = 1; // OK  <br />
<strong class='bbc'>Example 18. Address arithmetic. Overflows.</strong><br />
  class Region {   float *array;   int Width, Height, Depth;   float Region::GetCell(int x, int y, int z) const;   ... }; float Region::GetCell(int x, int y, int z) const {   return array[x + y * Width + z * Width * Height]; }  This code is taken from a real application of mathematic modeling where the size of physical memory is a very crucial resource, so the possibility to use more than 4 Gbytes of memory on the 64-bit architecture significantly increases the computational power. In programs of this class, one-dimensional arrays are often used in order to save memory, and they are handled like third-dimensional arrays. To do this, there exist functions similar to GetCell that provide access to necessary items.<br />
<br />
 This code works correctly with pointers if the result of the " x + y * Width + z * Width * Height" exp<b></b>ressi&#111;n does not exceed INT_MAX (2147483647). Otherwise an overflow will occur leading to an unexpected program behavior.<br />
<br />
 This code could always work correctly on the 32-bit platform. Within the scope of the 32-bit architecture, the program cannot get the necessary memory amount to create an array of such a size. But this limitation is absent on the 64-bit architecture and the array's size might easily exceed INT_MAX items.<br />
<br />
 Programmers often make a mistake trying to fix the code this way:<br />
<br />
  float Region::GetCell(int x, int y, int z) const {   return array[static_cast(x) + y * Width +                z * Width * Height];  They know that the exp<b></b>ressi&#111;n to calculate the index will have the <a href='http://www.viva64.com/terminology/ptrdiff_t.html' class='bbc_url' title='External link' rel='nofollow external'>ptrdiff_t</a> type according to C++ rules and try to avoid the overflow therefore. But the overflow might occur inside the "y * Width" or "z * Width * Height" subexp<b></b>ressi&#111;ns since it is still the int type that is used to calculate them.<br />
<br />
 If you want to fix the code without changing the types of the variables participating in the exp<b></b>ressi&#111;n, you may explicitly convert each subexp<b></b>ressi&#111;n to the ptrdiff_t type:<br />
<br />
  float Region::GetCell(int x, int y, int z) const {   return array[ptrdiff_t(x) +                ptrdiff_t(y) * Width +                ptrdiff_t(z) * Width * Height]; }  Another, better, solution is to change the variables' types:<br />
<br />
  typedef ptrdiff_t TCoord; class Region {   float *array;   TCoord Width, Height, Depth;   float Region::GetCell(TCoord x, TCoord y, TCoord z) const;   ... }; float Region::GetCell(TCoord x, TCoord y, TCoord z) const {   return array[x + y * Width + z * Width * Height]; }  <br />
<strong class='bbc'>Example 19. Changing an array's type</strong><br />
 Sometimes programmers change the type of an array while processing it for the purpose of convenience. The following code contains dangerous and safe type conversions:<br />
<br />
  int array[4] = { 1, 2, 3, 4 }; enum ENumbers { ZERO, ONE, TWO, THREE, FOUR }; //safe cast (for MSVC) ENumbers *enumPtr = (ENumbers *)(array); cout << enumPtr[1] << " "; //unsafe cast size_t *sizetPtr = (size_t *)(array); cout << sizetPtr[1] << endl; //Output on 32-bit system: 2 2 //Output on 64-bit system: 2 17179869187  As you may see, the output results differ in the 32-bit and 64-bit versions. On the 32-bit system, the access to the array's items is correct because the sizes of the size_t and int types coincide and we get the output "2 2".<br />
<br />
 On the 64-bit system, we got "2 17179869187" in the output since it is this very value 17179869187 which is located in the first item of the sizePtr array (see Figure 17). Sometimes this behavior is intended but most often it is an error.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043942/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4137/4862043942_e437464ae2.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 17 - Representation of array items in memory<br />
<br />
 Note. The size of the enum type by default coincides with the size of the int type in the Visual C++ compiler, i.e. the enum type is a 32-bit type. You can use enum of a different size only with the help of an extension which is considered non-standard in Visual C++. That is why the given example is correct in Visual C++ but from the viewpoint of other compilers conversion of an int-item pointer to an enum-item pointer is also incorrect.<br />
<br />
 <br />
<strong class='bbc'>Example 20. Wrapping a pointer in a 32-bit type</strong><br />
 Sometimes pointers are stored in integer types. Usually the int type is used for this purpose. This is perhaps one of the most frequent 64-bit errors.<br />
<br />
  char *ptr = ...; int n = (int) ptr; ... ptr = (char *) n;  In a 64-bit program, this is incorrect since the int type remains 32-bit and cannot store a 64-bit pointer. The programmer often cannot notice it at once. Due to mere luck, the pointer might always refer to objects located within the first 4 Gbytes of the address space during the testing. In this case, the 64-bit program will work efficiently and crash only in a large period of time (see Figure 18).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043924/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4095/4862043924_7fc76ae2ae.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 18 - Putting a pointer into a variable of int type<br />
<br />
 If you still need to store a pointer in a variable of an integer type, you should use such types as <a href='http://www.viva64.com/terminology/intptr_t.html' class='bbc_url' title='External link' rel='nofollow external'>intptr_t</a>, <a href='http://www.viva64.com/terminology/uintptr_t.html' class='bbc_url' title='External link' rel='nofollow external'>uintptr_t</a>, ptrdiff_t and size_t.<br />
<br />
 <br />
<strong class='bbc'>Example 21. Memsize-types in unions</strong><br />
 When you need to work with a pointer as an integer, it is sometimes convenient to use a union as shown in the example and work with the numeric representation of the type without explicit conversions:<br />
<br />
  union PtrNumUnion {   char *m_p;   unsigned m_n; } u; u.m_p = str; u.m_n += delta;  This code is correct on 32-bit systems and incorrect on 64-bit ones. Changing the m_n member on a 64-bit system, we work only with a part of the m_p pointer (see Figure 19).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422153/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4114/4861422153_06a22a4374.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 19 - Representation of a union in memory on a 32-bit system and 64-bit systems.<br />
<br />
 You should use a type that would correspond to the pointer's size:<br />
<br />
  union PtrNumUnion {   char *m_p;   uintptr_t m_n; //type fixed } u;  <br />
<strong class='bbc'>Example 22. An infinity loop</strong><br />
 Mixed use of 32-bit and 64-bit types can unexpectedly cause infinity loops. Consider a synthetic sample illustrating a whole class of such defects:<br />
<br />
  size_t Count = BigValue; for (unsigned Index = 0; Index != Count; Index++) { ... }    This loop will never stop if the Count value > UINT_MAX. Assume that this code worked with the number of iterations less than UINT_MAX on 32-bit systems. But the 64-bit version of this program can process more data and it may require more iterations. Since the values of the Index variable lie within the range [0..UINT_MAX], the condition "Index != Count" will never be fulfilled and it will cause an infinity loop (see Figure 20).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422133/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4099/4861422133_c9dfa4a2f3.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 20 - The mechanism of an infinity loop<br />
<br />
 <br />
<strong class='bbc'>Example 23. Bit operations and NOT operation</strong><br />
 Bit operations require special care from the programmer when developing crossplatform applications where data types may have different sizes. Since migration of a program to the 64-bit platform also makes capacity of some types change, it is highly probable that errors will occur in those code fragments that work with separate bits. Most often it happens when 32-bit and 64-bit data types are handled together. Consider an error occurring in the code because of an incorrect use of the NOT operation:<br />
<br />
  UINT_PTR a = ~UINT_PTR(0); ULONG b = 0x10; UINT_PTR c = a & ~(b - 1); c = c | 0xFu; if (a != c)   cout << "Error" << endl;  The error consists in that the mask defined by the "~(b - 1)" exp<b></b>ressi&#111;n has the ULONG type. It causes zeroing of the most significant bits of the "a" variable although it is only the four least significant bits that should have been zeroed (see Figure 21).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422099/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4075/4861422099_bc4032afe4.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 21 - The error occurring because of zeroing of the most significant bits<br />
<br />
 The correct version of the code looks as follows:<br />
<br />
  UINT_PTR c = a & ~(UINT_PTR(b) - 1);  This example is extremely simple but it is very good to demonstrate the class of errors that might occur when you actively work with bit operations.<br />
<br />
 <br />
<strong class='bbc'>Example 24. Bit operations, offsets</strong><br />
  ptrdiff_t SetBitN(ptrdiff_t value, unsigned bitNum) {   ptrdiff_t mask = 1 << bitNum;   return value | mask; }  This code works well on the 32-bit architecture and allows to set a bit with the numbers from 0 to 31 into one. After porting the program to the 64-bit platform, you need to set bits with the numbers from 0 to 63. But this code cannot set the most significant bits with the numbers 32-63. Note that the numeric literal "1" has the int type and an overflow will occur after an offset at 32 positions as shown in Figure 22. We will get 0 (Figure 22-B) or 1 (Figure 22-C) - it depends upon the compiler's implementation.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422077/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4135/4861422077_899317aec4.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 22 - a) correct setting of the 31st bit in the 32-bit code (the bits are counted beginning with 0); b,c) - The error of setting the 32nd bit on the 64-bit system (the two variants of behavior that depend upon the compiler)<br />
<br />
 To correct the code, you should make the "1" constant's type the same as the type of the mask variable:<br />
<br />
  ptrdiff_t mask = static_cast(1) << bitNum;  Note also that the incorrect code will lead to one more interesting error. When setting the 31-st bit on the 64-bit system, the result of the function is 0xffffffff80000000 (see Figure 23). The result of the 1 << 31 exp<b></b>ressi&#111;n is the negative number -2147483648. This number is represented in a 64-bit integer variable as 0xffffffff80000000.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043820/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4096/4862043820_11723a228a.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 23 - The error of setting the 31-st bit on the 64-bit system<br />
<br />
 <br />
<strong class='bbc'>Example 25. Bit operations and sign extension</strong><br />
 The error shown below is rare yet, unfortunately, quite difficult to understand. So let's discuss it in detail.<br />
<br />
  struct BitFieldStruct {   unsigned short a:15;   unsigned short b:13; }; BitFieldStruct obj; obj.a = 0x4000; size_t x = obj.a << 17; //Sign Extension printf("x 0x%Ix&#092;n", x); //Output on 32-bit system: 0x80000000 //Output on 64-bit system: 0xffffffff80000000  In the 32-bit environment, the sequence of exp<b></b>ressi&#111;n calculation looks as shown in Figure 24.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861422039/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4120/4861422039_b58e2b3190.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 24 - Calculation of the "obj.a << 17" exp<b></b>ressi&#111;n in the 32-bit code<br />
<br />
 Note that sign extension of the unsigned short type to int takes place during the calculation of the "obj.a << 17" exp<b></b>ressi&#111;n. The following code makes it clearer:<br />
<br />
  #include  template  void PrintType(T) {   printf("type is %s %d-bit&#092;n",       	(T)-1 < 0 ? "signed" : "unsigned", sizeof(T)*8); } struct BitFieldStruct {   unsigned short a:15;   unsigned short b:13; }; int main(void) {   BitFieldStruct bf;   PrintType( bf.a );   PrintType( bf.a << 2);   return 0; } Result: type is unsigned 16-bit type is signed 32-bit  Now let's see the consequence of a sign extension in 64-bit code. The sequence of exp<b></b>ressi&#111;n calculation is shown in Figure 25.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043800/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4094/4862043800_bafebb1ba4.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 25 - Calculation of the "obj.a << 17" exp<b></b>ressi&#111;n in 64-bit code<br />
<br />
 The member of the obj.a structure is cast from the bit field of the unsigned short type into int. The "obj.a << 17" exp<b></b>ressi&#111;n has the int type but it is cast to ptrdiff_t and then to size_t before being assigned to the addr variable. As a result, we will get value 0xffffffff80000000 instead of 0x0000000080000000 we have expected.<br />
<br />
 Be careful when working with bit fields. To avoid the described situation in our example, you just need to convert obj.a to the size_t type.<br />
<br />
  ... size_t x = static_cast(obj.a) << 17; // OK printf("x 0x%Ix&#092;n", x); //Output on 32-bit system: 0x80000000 //Output on 64-bit system: 0x80000000  <br />
<strong class='bbc'>Example 26. Serialization and data exchange</strong><br />
 Succession to the existing communications protocols is an important element in migration of a software solution to a new platform. You must provide the possibility of reading existing project formats, data exchange between 32-bit and 64-bit processes and so on.<br />
<br />
 In general, the errors of this kind consist in serialization of memsize-types and data exchange operations that use them.:<br />
<br />
  size_t PixelsCount; fread(&PixelsCount, sizeof(PixelsCount), 1, inFile);  You cannot use types that change their size depending upon the development environment in binary data exchange interfaces. In C++, most types do not have strict sizes and therefore they all cannot be used for these purposes. That is why the developers of development tools and programmers themselves create data types that have strict sizes such as __int8, __int16, INT32, word64, etc.<br />
<br />
 Even on correcting all the issues referring to type sizes, you might encounter the problem of incompatibility of binary formats. The reason lies in a different data representation. Most often it is determined by a different byte order.<br />
<br />
 <a href='http://www.viva64.com/go.php?url=296' class='bbc_url' title='External link' rel='nofollow external'>Byte order</a> is a method of writing bytes of multi-byte numbers (see Figure 26). The little-endian order means that writing begins with the least significant byte and ends with the most significant byte. This writing order is accepted in the memory of personal computers with <a href='http://www.viva64.com/terminology/x86.html' class='bbc_url' title='External link' rel='nofollow external'>x86</a> and <a href='http://www.viva64.com/terminology/x86-64.html' class='bbc_url' title='External link' rel='nofollow external'>x86-64</a>-processores. The big-endian order means that writing begins with the most significant byte and ends with the least significant byte. This order is a standard for TCP/IP protocols. That is why the big-endian byte order is often called the network byte order. This byte order is used in processors Motorola 68000 and SPARC.<br />
<br />
 By the way, some processors can work in both orders. For instance, <a href='http://www.viva64.com/terminology/IA-64.html' class='bbc_url' title='External link' rel='nofollow external'>IA-64</a> is such a processor.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043778/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4116/4862043778_5495744105.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 26 - Byte order in a 64-bit type in little-endian and big-endian systems<br />
<br />
 While developing a binary data interface or format, you should remember about the byte order. If the 64-bit system you are porting your 32-bit application to has a different byte order, you will just have to take this into account for your code. To convert between the big-endian and little-endian byte orders, you may use the functions htonl(), htons(), bswap_64, <a href='http://www.viva64.com/knowledge/ntohl_htonl_64_bit_value.html' class='bbc_url' title='External link' rel='nofollow external'>etc</a>.<br />
<br />
 <br />
<strong class='bbc'>Example 27. Changes in type alignment</strong><br />
 Besides changes of sizes of some data types, errors might also due to changes of rules of their alignment in a 64-bit system (see Figure 27).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861421975/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4077/4861421975_b1c7b192c8.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 27 - Sizes of types and their alignment boundaries (the figures are exact for Win32/Win64 but may vary in the "Unix-world", so they are given only for demonstration purpose)<br />
<br />
 Consider a description of the issue found in some forum:<br />
<br />
 I have encountered an issue in Linux today. There is a data structure consisting of several fields: a 64-bit double, 8 unsigned char and one 32-bit int. All in all there are 20 bytes (8 + 8*1 + 4). On 32-bit systems, sizeof equals 20 and everything is ok. But on the 64-bit Linux, sizeof returns 24. That is, there is a 64-bit boundary alignment.<br />
<br />
 Then this person discusses the problem of data compatibility and asks for advice how to pack the data in the structure. We are not interested in this at the moment. What is relevant, this is another type of errors that might occur when you port applications to 64-bit systems.<br />
<br />
 It is quite clear and familiar that changes of the sizes of fields in a structure cause the size of the structure itself to change. But here we have a different case. The sizes of the fields remain the same but the structure's size still changes due to other alignment rules (see Figure 28). This behavior might lead to various errors, for instance, errors of format incompatibility of saved data.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4861421947/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4120/4861421947_606d93d13d.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 28 - A scheme of structures and type alignment rules<br />
<br />
 <br />
<strong class='bbc'>Example 28. Type alignments and why you mustn't write sizeof(x) + sizeof(y)</strong><br />
 Sometimes programmers use structures with an array of a variable size at the end. Such a structure and the mechanism of memory allocation for it might look as follows:<br />
<br />
  struct MyPointersArray {   DWORD m_n;   PVOID m_arr[1]; } object; ... malloc( sizeof(DWORD) + 5 * sizeof(PVOID) ); ...  This code is correct in the 32-bit version but fails in the 64-bit version.<br />
<br />
 When allocating memory needed to store an object like MyPointersArray that contains 5 pointers, you should consider that the beginning of the m_arr array will be aligned on an 8-byte boundary. Data arrangement in memory on different systems (Win32/Win64) is shown in Figure 29.<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043738/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4114/4862043738_ba9f5e56bf.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 29 - Data arrangement in memory in 32-bit and 64-bit systems<br />
<br />
 The correct calculation of the size looks in the following way:<br />
<br />
  struct MyPointersArray {   DWORD m_n;   PVOID m_arr[1]; } object; ... malloc( FIELD_OFFSET(struct MyPointersArray, m_arr) +     	5 * sizeof(PVOID) ); ...  In this code, we determine the offset of the last structure's member and add this offset to its size. The offset of a structure's or class' member may be obtained with the help of the offsetof or FIELD_OFFSET macros. You should always use these macros to obtain the offset in a structure without relying on your assumptions about sizes of types and rules of their alignment.<br />
<br />
 <br />
<strong class='bbc'>Example 29. Overloaded functions</strong><br />
 When you recompile a program, some other overloaded function might start to be selected (see Figure 30).<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4862043714/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4101/4862043714_a4c5f649bd.jpg' alt='Posted Image' class='bbc_img' /></a><br />
<br />
 Figure 30 - Choosing an overloaded function in a 32-bit system and 64-bit system<br />
<br />
 Here is an example of the problem:<br />
<br />
  class MyStack { ... public:   void Push(__int32 &);   void Push(__int64 &);   void Pop(__int32 &);   void Pop(__int64 &); } stack; ptrdiff_t value_1; stack.Push(value_1); ... int value_2; stack.Pop(value_2);  The inaccurate programmer put and then chose from the stack values of different types (ptrdiff_t and int). Their sizes coincided on the 32-bit system and everything was alright. When the size of the ptrdiff_t type changed in the 64-bit program, the number of bytes put in the stack became larger than the number of bytes that would be fetched from it then.<br />
<br />
 <br />
<strong class='bbc'>Example 30. Errors in 32-bit units working in WoW64</strong><br />
 The last example covers errors in 32-bit programs that occur when they are executed in the 64-bit environment. 64-bit software systems will include 32-bit units for a long time and therefore we must provide for their correct work in the 64-bit environment. The <a href='http://www.viva64.com/terminology/WoW64.html' class='bbc_url' title='External link' rel='nofollow external'>WoW64</a> subsystem fulfills this task very well by isolating a 32-bit application, so that almost all 32-bit applications work correctly. However, sometimes errors happen and they refer most often to the <a href='http://www.viva64.com/knowledge/WoW64_redirection.html' class='bbc_url' title='External link' rel='nofollow external'>redirection mechanism</a> when working with files and Windows register.<br />
<br />
 For instance, when dealing with a system that consists of 32-bit and 64-bit units which interact with each other, you should consider that they use <a href='http://www.viva64.com/go.php?url=365' class='bbc_url' title='External link' rel='nofollow external'>different register representations</a>. Thus, the following line stopped working in a 32-bit unit in one program:<br />
<br />
  lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,   "SOFTWARE&#092;&#092;ODBC&#092;&#092;ODBC.INI&#092;&#092;ODBC Data Sources", 0,   KEY_QUERY_VALUE, &hKey);  To make this program friends with other 64-bit parts, you should insert the KEY_WOW64_64KEY switch:<br />
<br />
  lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,   "SOFTWARE&#092;&#092;ODBC&#092;&#092;ODBC.INI&#092;&#092;ODBC Data Sources", 0,   KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hKey);  <br />
<strong class='bbc'>Summary</strong><br />
 The method of <a href='http://www.viva64.com/terminology/Static_code_analysis.html' class='bbc_url' title='External link' rel='nofollow external'>static code analysis</a> shows the best result in searching for the errors described in the article. As an example of such a tool that performs this kind of analysis, we can name the Viva64 tool included into the <a href='http://www.viva64.com/pvs-studio/' class='bbc_url' title='External link' rel='nofollow external'>PVS-Studio</a> package we are developing.<br />
<br />
 The methods of static search of defects allow to detect defects relying on the source program code. The program behavior is estimated at all the execution paths simultaneously. Because of this, static analysis lets you find defects that occur only at non-standard execution paths with rare input data. This feature supplements other testing methods and increases security of applications. Static analysis systems might be used in source code audit, for the purpose of systematic elimination of defects in existing programs; they can integrate into the development process and automatically detect defects in the code being created.<br />
<br />
 <br />
<strong class='bbc'>References</strong><br />
 <ul class='bbcol decimal'><li>Andrey Karpov, Evgeniy Ryzhkov. Lessons on development of 64-bit C/C++ applications. <a href='http://www.viva64.com/articles/x64-lessons/' class='bbc_url' title='External link' rel='nofollow external'>http://www.viva64.co...es/x64-lessons/</a> </li><li>Andrey Karpov. About size_t and ptrdiff_t. <a href='http://www.viva64.com/art-1-2-710804781.html' class='bbc_url' title='External link' rel='nofollow external'>http://www.viva64.co...-710804781.html</a> </li><li>Andrey Karpov, Evgeniy Ryzhkov. 20 issues of porting C++ code on the 64-bit platform. <a href='http://www.viva64.com/art-1-2-599168895.html' class='bbc_url' title='External link' rel='nofollow external'>http://www.viva64.co...-599168895.html</a> </li><li>Evgeniy Ryzhkov. PVS-Studio Tutorial. <a href='http://www.viva64.com/art-4-2-747004748.html' class='bbc_url' title='External link' rel='nofollow external'>http://www.viva64.co...-747004748.html</a> </li><li>Andrey Karpov. A 64-bit horse that can count. <a href='http://www.viva64.com/art-1-2-377673569.html' class='bbc_url' title='External link' rel='nofollow external'>http://www.viva64.co...-377673569.html</a></li></ul>]]></description>
		<pubDate>Wed, 04 Aug 2010 21:13:04 +0000</pubDate>
		<guid isPermaLink="false">5644fb01b5333e2548d12dfbc3d5a0c8</guid>
	</item>
	<item>
		<title>Using Abstraction to Optimize Runtime Polymorphism</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/using-abstraction-to-optimize-runtime-polymorphism-r2763</link>
		<description><![CDATA[The C++ virtual function mechanism is one of the most useful features of the C++ language because it makes runtime polymorphism easy to implement. Polymorphism allows object-oriented programmers to write cleaner and much more extensible code. However, the use of virtual functions does have some overhead so these benefits are not entirely free. In this article, we will look at some ways to reduce this overhead without having to give up polymorphism entirely in our code.<br />
<br />
 <br />
<strong class='bbc'>What is the cost of polymorphism?</strong><br />
 As any experienced C++ programmer knows, virtual functions are slower than normal functions because they require more indirection. Each object must have its v-table pointer dereferenced in order to access the v-table itself, and then a table lookup has to be performed to call the actual function. In most situations, this overhead is acceptable and very much outweighed by the benefits of using virtual functions. Also, most modern compilers are pretty good at optimizing your code. They will probably be able to determine most instances in your code where a virtual function call is not really necessary and can be treated as a normal function call. However, there is another source of performance loss that should be of much greater concern to game programmers who need the most performance from their application. Virtual functions increase the probability of data cache misses especially on platforms with only a small amount of cache memory. These misses can be a significant source of performance loss on platforms such as consoles or mobile devices which have slow main memory speed. Accessing main memory can be several orders of magnitude greater than accessing the data cache.<br />
<br />
 In order to understand how virtual functions affect data cache performance, we must look at how a typical data cache system works. Data caches are divided into cache lines that are all of the same size, say 128 bytes. When a data item is loaded into the cache from main memory, the cache system will load enough bytes to fill one cache line. So if the data item is 32 bytes, the cache system will load those 32 bytes along with the next 96 bytes in order to fill one 128 byte cache line. If these extra bytes are not needed, then cache space is wasted and data access misses become more likely. Also, it chooses which cache line to load the data into by using a subset of bits from the data item’s memory address, so if the address is not aligned on the proper boundary, the data item may be placed far from other data already in the cache. We also need to understand that when cache systems need to replace the data in the cache, they often employ a Least-Recently-Used replacement policy in which the data items that are used the least are replaced first.<br />
<br />
 So, to make our code more cache-friendly, it is necessary for it to exhibit the principles of <em class='bbc'>spatial locality of reference</em> and <em class='bbc'>temporality of reference</em>. In other words, the data items referenced by the code should be adjacent to each other in contiguous memory aligned on the proper address boundary and they should be the most frequently used. It is harder to achieve these principles when using virtual functions because every virtual function call requires a v-table pointer as well the v-table itself to be loaded into the data cache. The v-table pointer is usually the first 4 bytes in an object, so after it is loaded into the cache, the next adjacent bytes in the object are loaded. If we want cache usage to be optimal, we should ensure that the most used member variables in the object’s class are the first ones declared so their memory will be adjacent to the v-table pointer memory. But in practice this is difficult to achieve for every member function and so quite often cache usage is less than optimal. Also, since we have no control over where the v-tables are located in memory, quite often they will not have spatial locality. If we need to load v-tables from many different objects in a random order, we also no longer have temporality of reference and data cache misses will be more likely. On top of all this, the data cache misses will be hard to detect in a profiler.<br />
<br />
 So why even use polymorphism in our code? Why not just avoid it entirely so virtual functions are not necessary? Well, that decision is entirely up to the programmer, but it should not be made lightly since the benefits provided by polymorphism are significant and cannot be ignored.<br />
<br />
 Let’s look at an example of how runtime polymorphism is often used in games to demonstrate the pros and cons of polymorphism.<br />
<br />
 <br />
<strong class='bbc'>Updating objects polymorphically</strong><br />
 When developing a game engine, it is often necessary to iterate through a large number of related objects and call one of their common methods. When this is done during the engine’s main loop, it must be as efficient as possible. If every object is the same type we could simply have a loop which calls some method on every object that exists in the system at that time. But when the objects are of many different types, we would need to have multiple loops. For example, if we have a system which contains three different object subtypes, such as Fighter, Bomber, and Recon, then we would have three loops, one for each subtype. Fig. 1<br />
<br />
  while( fighter )   {    fighter->updateAI( dt );    fighter  = fighter->next; } while( bomber ) {    bomber->updateAI( dt );    bomber = bomber->next; } etc.  The one major drawback to this approach however, is that it is not extensible at all. Extensibility is the ability to update the engine easily and reliably, such as by adding more game object subtypes to the list of already existing ones. So for instance, if we wanted to add a Seaplane subtype, we should be able to do this with no edits to the existing code base and with only minimal additions to the code, such as adding a new inherited class. The system in fig. 1 does not have the property of extensibility since adding more subtypes would require adding more loops to the code. If these loops are duplicated all throughout the code, then adding new ones is tedious and error prone. This property is known in object-oriented programming parlance as rigidity. Not a good thing to have in a game engine, or any software for that matter. Even if we use a single loop and check the object type using an if-else block or a switch statement, the code would still be riddled with large chunks of duplication and would therefore still be very rigid. If you don’t plan on updating your engine, rigidity might not be an issue however. Of course this is a classic example of when run-time polymorphism can be very beneficial. This is usually done using the commonly known template pattern (not to be confused with C++ templates). The template pattern is often implemented by having an abstract base interface class that all the subtype classes inherit from. So in our example, we would have:<br />
<br />
 Fig. 2<br />
<br />
  class GameObject { public:    virtual void updateAI( float dt ) = 0; }; class Fighter : public GameObject { public:    void updateAI( float dt ) {…} };  class Bomber : public GameObject public:    void updateAI( float dt ){…} } ; . . Etc… while( obj ) {    obj->updateAI( dt );    obj = obj->next; }  So now all we have to do to update all objects in the system is to have a single pointer which is of type GameObject and point it to each instance of a GameObject whether it is a Fighter, Bomber, or whatever, and call its updateAI method. At run-time, the type of the object will be used to determine which function is called, not the pointer type. We now only need one loop that will never change even if we add more subtypes to the system. <br />
<strong class='bbc'>Making Polymorphism faster</strong><br />
 Ok, so now that we have seen the benefits of polymorphism, let’s assume that we have decided it is indispensable in the design of our game’s code but the performance is still a concern. One way to improve the performance and still retain the benefits is to add a higher level of abstraction to the system. Let us consider the situation where we have one thousand objects that need to be updated, but only two of them are of subtype Bomber, and the other nine hundred ninety-eight are Fighters? Then the virtual table lookup that is being done on every object in the loop becomes redundant since nine hundred ninety-eight times the object is the same type. This redundancy is obviously not very desirable and begs the question, is there a better way to implement the loop that is not so redundant? First let us consider whether compile-time polymorphism is the answer. This technique was explained in great depth in the GameDev.net article, <a href='http://www.gamedev.net/reference/articles/article2015.asp' class='bbc_url' title=''>Improving Performance in C++ with Compile Time Polymorphism</a> by Ben Sunshine-Hill. With this technique we use C++ templates to move the class type of the objects in the loop out of the loop’s definition thereby making it more generic.<br />
<br />
 Fig. 3<br />
<br />
  template void updateAll( float dt ) {    while(  t )    {   	t->updateAI( dt )   	t = t->next; 	} }   So now the type of the object will be determined at compile-time instead of at run-time. But when we try to put this code into context within the engine we see we have not really achieved anything since we will have to call the updateAll function for every subtype in the system: Fig. 4<br />
<br />
  updateAll( dt ); updateAll( dt ); updateAll( dt );  This is akin to the situation we had in Fig. 1 at the start of the article because even though we don’t have to duplicate the loop code we do have to duplicate the <em class='bbc'>calling</em> of the loop code, and any kind of duplication is not conducive to clean, extensible, maintainable code. Compile-time polymorphism is a good solution to the problem of updating the same object many times, but not the problem of updating many <em class='bbc'>different</em> objects which have the same type.<br />
<br />
 So is there any way to increase performance in our system? Fortunately the answer is yes, there is a better way. As stated before, we can add more abstraction to our system in order to move the polymorphism to a higher layer and thus reduce the frequency of virtual function calls.<br />
<br />
 Let’s take another look at Fig.1. We will notice that we have an update loop for each object subtype. What if we contain such a loop within a virtual method of a new class? Could we not then use run-time polymorphism on this class instead of the object class? Yes, if we define a base class which acts as a container or a bucket for an object subtype, then we could have this class contain a list of objects of that subtype and only update these objects. Then we could inherit from this class for each of the various subtypes. Then at run-time we could use a base bucket class pointer to point to each of the various derived bucket class instances and call their updateAll functions. These bucket classes would not have to use virtual functions to update each of the objects in their list since the objects will always be of the same type. So now we have:<br />
<br />
 Fig. 5<br />
<br />
  class Bucket { public:    virtual void updateAll( float dt ) = 0; };  class FighterBucket : public Bucket { public:    virtual void updateAll()    {   	while ( fighter )   	{          fighter->updateAI( dt );  // call to non-virtual function          fighter = fighter->next;   	}    } }; class BomberBucket : public Bucket {    virtual void updateAll()    {   	while ( bomber)   	{          bomber->updateAI( dt );  // call to non-virtual function          bomber = bomber->next;   	}    } }; class ReconBucket: public Bucket etc. … while(bucket)   {    bucket->updateAll( dt );    bucket = bucket ->next; }  So now we have the same template pattern as in Fig. 2 but it is applied to the buckets instead of to the objects. This means that when we do our update of one thousand objects where most of the objects are the same type, we will only have to do a maximum of three virtual method calls, instead of one thousand. And we still have all the extensibility benefits of run-time polymorphism. Splendid! We have achieved the best of both worlds, a system that is both efficient and extensible. This kind of batch processing also makes it easier to optimize the memory usage of each object type by having each container keep its list of objects in contiguous memory which leads to better cache coherency through spatial locality. Essentially what we have done is to add another level of abstraction, the buckets, in order to improve efficiency. In the <a href='http://downloads.gamedev.net/features/programming/AbsPolyOpt/source_code.zip' class='bbc_url' title='External link' rel='nofollow external'>source code</a> for this article, the performances of the various methods outlined above are compared by recording their execution times. When the program is profiled in the AMD CodeAnalyst tool, we can see the the runSimple function takes 2.73 timer samples, the runPoly function takes 6.36, and runBucket takes 0. Total samples to update each system are:<br />
<br />
 Simple: 27.57<br />
<br />
 Bucket: 28.18<br />
<br />
 Polymorphism: 40.9<br />
<br />
 <a href='http://www.flickr.com/photos/gamedevnet/4777474227/' class='bbc_url' title='External link' rel='nofollow external'><img src='http://farm5.static.flickr.com/4123/4777474227_b3c770f245.jpg' alt='Posted Image' class='bbc_img' /></a> As you can see, polymorphism is quite fast, but ordering our objects by their type increases the speed by a noticeable amount. Batch processing is also beneficial in that it can help to resolve the conflicts caused by inter-object dependencies in complex game object systems.<br />
<br />
 Choosing the correct level of abstraction in object-oriented designs is always important for performance reasons. In another example, say we have a class that uses many small virtual functions that are only one or two lines and are usually called from within another function. In this case, it is a good idea to move the point of abstraction to a higher level in the code by combining all of the virtual calls into one large one.<br />
<br />
 Fig. 6<br />
<br />
  class SpaceShip { public:    virtual void MissileHit() {   	m_iEnergy -= 10;    }        virtual void OnMsg( Msg *msg ) {   	switch( msg->type_ ) {   	case HIT_BY_MISSILE:          MissileHit();          SendMsg( MSG_TARGET_DESTROYED, msg->parent );          break;   	}    } }; class EnemySpaceShip : public SpaceShip { public:    virtual void MissileHit() {   	// do some class specific stuff    } virtual void OnMsg( Msg *msg ) {   	switch( msg->type_ ){   	case HIT_BY_CANNON:          //case-specific code          break;   	default:          SpaceShip::OnMsg( msg );   	break;   	}    }  };  Here the programmer has chosen to make both the MissileHit function and the OnMsg function virtual so they can be overridden in derived classes and customized. However, MissileHit is not a good candidate for a virtual function since it is only one or two lines and is a better candidate for being inlined. Also, since it is called from within OnMsg, we now have nested virtual functions, which constitute unnecessary overhead and probably poor design. Let’s see if we can reduce the number of virtual function calls: Fig. 7<br />
<br />
  class SpaceShip { public:    inline void MissileHit() {   	m_iEnergy -= 10;    }        virtual void OnMsg( Msg *msg ) {   	//same as before    } }; class EnemySpaceShip : public SpaceShip { public:    inline void MissileHit() {   	// do some class specific stuff    }    virtual void OnMsg( Msg *msg ) {   	switch( msg->type_ ){   	case HIT_BY_CANNON:          //case-specific code          break;   	case HIT_BY_MISSILE:          EnemySpaceShip::MissileHit();          SendMsg( MSG_TARGET_DESTROYED, msg->parent );          break;   	}    } };  We have now made MissileHit an inline non-virtual function and we have added a new case to the derived version of OnMsg to call the derived version of MissileHit so it does not need to be virtual. This does make the code a little more redundant, but the performance benefit is very desirable and the redundancy can be removed by continuing to refactor the code into a better design such as using function pointers. In many high-level languages, such as Java, overridden methods in derived classes are always virtual, but in C++, we have the choice of specifying which methods should be virtual. This can be used to our advantage when trying to achieve better performance.<br />
<br />
 <br />
<strong class='bbc'>Conclusion</strong><br />
 Hopefully, this article will be beneficial to C++ programmers hoping to be able to get more performance from their code. Unlike many other high-level languages, C++ requires more programmer effort and has a much steeper learning curve, but it is often easier to wring more performance out of the language, which is what it’s designers intended. This is probably why it is still the most widely used language for game development and will continue to be so for quite some time. The reader is encouraged to check out the bibliography for this article which lists many sources that contain many more useful techniques for dealing with virtual functions.<br />
<br />
 Happy Coding,<br />
Gabriel T. Delarosa<br />
May 17, 2010<br />
<br />
 <br />
<strong class='bbc'>Bibliography:</strong><br />
 Llopis, Noel, “C++ for Game Programmers “(Charles River Media, 2003, ISBN: 1-58450-227-4 ) Hyde, Randall, “Write Great Code Understanding the Machine” (No Starch Press, 2004, ISBN: 1-59327-003-8)<br />
<br />
 <a href='http://aigamedev.com/open/highlights/optimize-virtual-functions/' class='bbc_url' title='External link' rel='nofollow external'>http://aigamedev.com...tual-functions/</a><br />
<br />
]]></description>
		<pubDate>Fri, 09 Jul 2010 14:41:27 +0000</pubDate>
		<guid isPermaLink="false">e6da32eef072f987685b6eddca072d4f</guid>
	</item>
	<item>
		<title>Response Curves in XML for Game Parametrization</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/response-curves-in-xml-for-game-parametrization-r2717</link>
		<description><![CDATA[<br />
<strong class='bbc'>Introduction to XML Parametrization</strong><br />
 In a video game's source code, there are many parameters and magic numbers . For instance, the maximum speed for a character running might be 20 km/h, their weapon shoots 1.5 bullets per second, and when they stop their speed drops by 60% per second.<br />
<br />
 A basic programming rule is to avoid using these 'magic' numbers directly in source code. Some of these numbers and parameters can be replaced with named constants, and some others are better located in configuration files (e.g. XML), out of the source code.<br />
<br />
 The advantages of taking the parameters out of the source code are well-known by video game programmers. These parametrization files can be changed without recompiling the source code, and in certain situations we can even change the parameter values and load them into the game at real-time, without a game restart.<br />
<br />
 One of the main points of parametrization files is that they can be configured by concept or level designers without any programming skills or access to source code changes. This is a must, for many of the game's parameters.<br />
<br />
 This article proposes an addition to this well-known design pattern, explaining a simple way to parametrize in XML the response curves that otherwise may be hard-coded.<br />
<br />
 <br />
<strong class='bbc'>Response Curves</strong><br />
 Let's explain some response curve applications by example. The “moving and shooting” character example described earlier is in development, and the design team has updated the game specification document by adding some fresh great new features. Sound familiar?<br />
<br />
 Our example character - named Pepe, has a “stamina” bar ranged from 0 to 1 that measures how fatigued he is. The new feature is that Pepe reaches different maximum running speeds depending on his level of fatigue.<br />
<br />
 The response curve for this example will return the maximum running speed of Pepe for each stamina value. This curve can be very simple (a linear interpolation between the minimum and maximum speed possible), or very complex. For instance, we may want Pepe to run real fast when his stamina is higher than 0.8, slow between 0.8 and 0.4 and with very little speed difference between 0.4 and 0 (because we may find out that running too slow is not possible).<br />
<br />
 These game-play configurations are usually one of the designers responsibilities, and this article explains how to make these parameterizations easily configurable by the design team.<br />
<br />
 <br />
<strong class='bbc'>Implementation</strong><br />
 One obvious option is to hard code this response curve, although it is the solution that this article avoids: <ul class='bbc'><li>Linear response curve: The maximum speed increases proportionally to the stamina bar. (fCurrentMaxSpeed = MAXSPEED * fStaminaBarValue) </li><li>Quadratic response curve: When the stamina is lower, the max-speed changes slower than when the stamina is higher. (fCurrentMaxSpeed = MAXSPEED * fStaminaBarValue * fStaminaBarValue) </li><li>More complex: It is a mix between the linear and quadratic, with a minimum speed of 0.2*MAXSPEED , but it could be whatever equation. (fCurrentMaxSpeed = MAXSPEED * ( 0.2 + ( fStaminaBarValue * fStaminaBarValue + fStaminaBarValue ) * 0.4f ))</li></ul> <a href='http://images.gamedev.net/features/snippets/xmlParam/response.png' class='bbc_url' title='External link' rel='nofollow external'><img src='http://images.gamedev.net/features/snippets/xmlParam/response_t.png' alt='Posted Image' class='bbc_img' /></a> The proposed implementation for this kind of response curves is to configure them in a XML file out of the code. This solution provides much more flexibility to the response curve, making it easier to adapt its shape to the game needs.<br />
<br />
 It is extremely more legible and easy to debug, understand and change, even by non-programmers.<br />
<br />
 The XML specifies the response curve, like so:<br />
<br />
  	< Curve Name="Max speed per stamina"                 	Input = " 0   0.4 0.7 0.8 1  "                 	Output= " 10  12  18  25  30 " />  The game code contains the implementation of the object that keeps the XML loaded values and finds the result of the response curve for each given input. The ResponseCurve class has two public functions :<br />
<br />
 <ul class='bbcol decimal'><li>The constructor, that has as input parameters the data read from the XML, the curve name and the array of its inputs and outputs. (e.g. ResponseCurve (string curveName, float inputs[], float outputs[]) ) </li><li>The function that returns the output for each specific input. (float GetResult (float input) )</li></ul> In our sample curve, GetResult(0.1) will return 10.5, meaning that with a stamina of 0.1 Pepe will have a maximum speed of 10.5 km/h. This function finds the two curve input values closest to the parameter input, and makes a linear interpolation between the two “output values”.<br />
<br />
 It is possible to make a better interpolation than linear between the output points. We can use a spline algorithm (Catmull-Rom for instance) to get a smoothed response curve. It is less efficient than the linear interpolation, and in most cases it is difficult (or impossible) to see the difference in the game.<br />
<br />
 In most cases the linear interpolation is the best option.<br />
<br />
]]></description>
		<pubDate>Mon, 30 Nov 2009 12:00:45 +0000</pubDate>
		<guid isPermaLink="false">646e058fac455de8d1e52c4c49baac06</guid>
	</item>
	<item>
		<title>A Data-driven Animation Manager</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/a-data-driven-animation-manager-r2704</link>
		<description><![CDATA[<br />
<strong class='bbc'>Introduction</strong><br />
 An animated character in a game is usually made up of more than one animation sequence. These sequences (or states) normally represent a distinct movement of the character, such as jumping or walking. When the sequences are chained together in game they provide a realistic animation. For example a character may run, then jump followed by a skid when they land, this whole animation could be created by chaining 3 or 4 separate sequences. As game development progresses characters can gain extra sequences (e.g. jumping and shooting) and sometimes a series of events may force you to make a decision about which animation should be played, do we perform the rolling animation when the character lands, or the skidding animation? If these decisions are hard coded into the game logic, they can be troublesome to alter.<br />
<br />
 This article explains a simple data driven animation manager which is designed to make adding/removing animations easy, without requiring heavy changes to the code.<br />
<br />
 The manager is a layer between the game state and the graphics engine. It takes as input the character state, and as output it plays the right animation sequence from the character animation set. It can be used in combination with any engine.<br />
<br />
 <br />
<strong class='bbc'>Implementation</strong><br />
 To keep the explanation simple, we are going to use plain C-style enums to represent states and events. A more elegant implementation could be achieved in higher level languages (such as C++) with a signal/slot implementation or member function pointers (e.g. boost::signals, boost::function). Also dictionary style data types (e.g. std::map, std::tr1::unordered_map) could simplify the implementation. Some graphic engines have an index number for each animation sequence .Each state of the manager can match an animation sequence, this way we don´t need to define our own, duplicated states.<br />
<br />
 We need access to an array of states that the character can be in, and also a list of all the possible triggers that may cause that state to change. These states and events are used in the event table.<br />
<br />
 The key to the animation manager is an event table which holds the following values:<br />
<br />
 <ul class='bbc'><li>The initial animation sequence ( this event can only be triggered from this state). </li><li>The change condition (what happened that would cause this sequence to change?) </li><li>A priority (if the conditions of several animations are fulfilled in the same game frame, which to use?) </li><li>A final animation sequence</li></ul> The event table can be loaded at runtime from file (which is the more flexible method) or stored statically in an array, like so:  //Array of events. int events [] [5] = ( / / Initial state , change condition 	,priority, final state        (ANY_STATE , TRIGGER_LEFT_PRESSED ,   10   , LEFT_START),        (JUMP_START, TRIGGER_ANIM_FINISHED,   200  , JUMP_CYCLE),        (SKID_CYCLE, TRIGGER_SKID_FINISHED,   100  , SKID_END) ... ... )  From this event table we can associate a state, with a series of conditions (triggers) that may cause that state to change. The following logic can then be used to calculate the correct state to change to during the game loop:  for(each event associated with the initial state) {     	if(IsFulfilled(event[change_condition]) &&          event[priority] > priority_of_the_current_state) {             	current_state = event[final_state];     	} }  “IsFulfilled” would simply return true if the change condition had occurred that frame, e.g.  switch (change_condition) {   case TRIGGER_LEFT_PRESSED: 	if left key pressed, return true.   case TRIGGER_ANIM_FINISHED: 	if the current animation has finished, return true.   case TRIGGER_SKID_FINISHED: 	if you are skidding, return true.   ...   ...   ... }  This implementation provides an efficient, extensible and flexible way to control animation sequences.]]></description>
		<pubDate>Tue, 20 Oct 2009 20:07:35 +0000</pubDate>
		<guid isPermaLink="false">f01287d4b412a2b16ec4a40af48d7c69</guid>
	</item>
	<item>
		<title>A Simple C++ Object Loader</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/a-simple-c-object-loader-r2698</link>
		<description><![CDATA[<span style='font-size: 18px;'><strong class='bbc'>Introduction</strong></span><br />
<br />
This is a guide to using Daabli: a simple serialization framework for C++. At this point it's worth mentioning that Daabli is only a deserialization framework; serialization support is currently under development. Still, I thought it would be worth writing a guide on how to use it for deserialization, instead of waiting for completion of serialization support. If your application needs to load objects and data from human readable text files, then Daabli could be useful to you. The currently supported features are: <ul class='bbc'><li>	loads objects from a "C" style format which is easy to read and edit using any plain text editor</li><li>	extremely simple to integrate and use</li><li>	portable code; implemented in standard C++</li><li>	does not require loadable types to derive from a specific base class</li><li>	does not add any overhead (in terms of space/time) to the types it can load</li><li>	non-intrusive loading support for types which cannot be modified (e.g., library types)</li><li>	built-in support for the following STL containers: <span style='font-family: Courier New'>string</span>, <span style='font-family: Courier New'>vector</span>, <span style='font-family: Courier New'>list</span>, <span style='font-family: Courier New'>deque</span>, <span style='font-family: Courier New'>pair</span>, <span style='font-family: Courier New'>map</span>, <span style='font-family: Courier New'>multimap</span>, <span style='font-family: Courier New'>set</span>, <span style='font-family: Courier New'>multiset</span></li><li>	supports loading enumerations<sup class='bbc'>[1]</sup></li><li>	supports loading pointers<ul class='bbc'><li>supports data-structures which form graphs</li><li>supports forward pointers to objects</li><li>supports (polymorphic and non-polymorphic) pointers to objects of derived types</li></ul> </li><li>	handles multiple inheritance (although it cannot handle virtual base classes as yet)</li><li>	does not require RTTI to be enabled</li><li>	does not require exceptions to be enabled</li></ul> If the above mentioned features are enough to load your objects, then let's proceed to getting started with Daabli.<br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>Getting started </strong></span><br />
<br />
First we need to get the source code. Using any SVN client (see note below), checkout the code from <a href='https://daabli.svn.sourceforge.net/svnroot/daabli/trunk/' class='bbc_url' title='External link' rel='nofollow external'>this repository URL</a>. Alternatively, you can download and extract an archive of the code <a href='http://daabli.svn.sourceforge.net/viewvc/daabli/trunk.tar.gz' class='bbc_url' title='External link' rel='nofollow external'>from here</a>. You can place the code anywhere you like. Apart from the framework source code, there are also some samples which demonstrate how to use it.<strong class='bbc'><br />
</strong><blockquote><strong class='bbc'><br />
Note:</strong> If you don't have an SVN client and you're on Windows, then I'd recommend getting <a href='http://tortoisesvn.tigris.org/' class='bbc_url' title='External link' rel='nofollow external'>TortoiseSVN</a>. If you're on Linux, then you could try <a href='http://zoneit.free.fr/esvn/' class='bbc_url' title='External link' rel='nofollow external'>eSvn</a><br />
</blockquote><br />
If the path where you've placed the downloaded code is <span style='font-family: Courier New'>INSTALLPATH</span>, then you need to add the folder <span style='font-family: Courier New'>INSTALLPATH/Daabli/</span> to the list of source code folders which your development environment searches. Also, you must add the file <span style='font-family: Courier New'>INSTALLPATH/Daabli/Daabli.cpp</span> to each project which uses Daabli and make sure it gets compiled. <br />
<br />
Now that we're all setup to use Daabli, let's take a look at how we can use Daabli to load objects.<br />
<br />
<strong class='bbc'><span style='font-size: 18px;'>Reading objects</span></strong><br />
<br />
 Objects are read using a Reader object. The following functions are commonly used for reading: <ul class='bbc'><li><span style='font-family: Courier New'>const bool FromFile(const string &fileName);</span><ul class='bbc'><li>Specifies which file the Reader should read from.</li></ul></li><li><span style='font-family: Courier New'>const bool Read(T &obj);</span><ul class='bbc'><li>Reads a required object of any type.</li><li>Data format: <span style='font-family: Courier New'>&lt;object&gt;</span></li></ul> </li><li><span style='font-family: Courier New'>const bool Read(const string &name, T &obj);</span><ul class='bbc'><li>Reads a required object of any type, associated with an identifier (like a name/value pair).</li><li>Data format: <span style='font-family: Courier New'>&lt;identifier&gt;  = &lt;object&gt;;</span></li></ul></li><li><span style='font-family: Courier New'>const bool Read(const string &name, T &obj, const T &defaultValue);</span><ul class='bbc'><li>Reads an optional object of any type, associated with an identifier; if not present, the specified default value is used.</li><li>Data format: <span style='font-family: Courier New'>[&lt;identifier&gt; = &lt;object&gt;;]</span></li></ul></li></ul> Any required cleanup is automatically done when the Reader object goes out of scope.<br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Basic types </strong></span><br />
<br />
Daabli has built-in support for loading the following basic types: <span style='font-family: Courier New'>bool</span>, <span style='font-family: Courier New'>char</span>, <span style='font-family: Courier New'>int</span>, <span style='font-family: Courier New'>unsigned int</span>, <span style='font-family: Courier New'>long</span>, <span style='font-family: Courier New'>unsigned long</span>, <span style='font-family: Courier New'>float</span>, <span style='font-family: Courier New'>double</span>. <br />
<br />
Numeric objects are described directly (without decoration), character objects can also be described as a literal enclosed in single quotes, and boolean objects are described with either <span style='font-family: Courier New'>true</span> or <span style='font-family: Courier New'>false</span>. For e.g., 15 could describe an <span style='font-family: Courier New'>int</span>, 3.14159 could describe a <span style='font-family: Courier New'>double</span>, true could describe a <span style='font-family: Courier New'>bool</span>, and 65 or 'A' could describe a <span style='font-family: Courier New'>char</span>. <br />
<br />
Let's look at an example of how to load some properties of a space ship from a description. <br />
<br />
In main.cpp:<br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;<br /><br />struct SpaceShip<br />{<br />  char    _type;<br />  float   _maxSpeed;<br />  float   _mass;<br />  bool    _invulnerable;<br />};<br /><br />int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />// Create a Reader object<br />Daabli::Reader r;<br /><br />// Specify which file to read from<br />if( !r.FromFile( "input.txt" ) )<br />  return -1;<br /><br />SpaceShip ship;<br /><br />// Read the space ship's properties<br />if( !r.Read( "type", 		ship._type                ) &#124;&#124; 	// required<br />    !r.Read( "maxSpeed", 	ship._maxSpeed            ) &#124;&#124; 	// required<br />    !r.Read( "mass", 		ship._mass, 		10.0f ) &#124;&#124; 	// optional<br />    !r.Read( "invulnerable", ship._invulnerable, false ) )      // optional<br />    return -1;<br /><br />// Display the space ship's properties<br />std::cout &lt;&lt; "type 		: " &lt;&lt; ship._type 		&lt;&lt; std::endl;<br />std::cout &lt;&lt; "maxSpeed 	: " &lt;&lt; ship._maxSpeed 	&lt;&lt; std::endl;<br />std::cout &lt;&lt; "mass 		: " &lt;&lt; ship._mass 		&lt;&lt; std::endl;<br />std::cout &lt;&lt; "invulnerable : " &lt;&lt; ship._invulnerable &lt;&lt; std::endl;<br /><br />return 0;<br />}</pre><br />
 Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>/* Player ship properties */<br />type 		= 'A';<br />maxSpeed 	= 10.2;<br />mass 		= 50.7;<br />//invulnerable = true;</pre><br />
 The output generated would be: <br />
<br />
<span style='font-family: Courier New'>type 		: A<br />
maxSpeed 	: 10.2<br />
mass 		: 50.7<br />
invulnerable : 0</span><br />
 <br />
Note from the input.txt file that Daabli supports C/C++ style comments.  <br />
<br />
<strong class='bbc'><span style='font-size: 18px;'>STL containers </span></strong><br />
<br />
Daabli has built-in support for loading the following STL containers: <span style='font-family: Courier New'>string</span>, <span style='font-family: Courier New'>vector</span>, <span style='font-family: Courier New'>list</span>, <span style='font-family: Courier New'>deque</span>, <span style='font-family: Courier New'>pair</span>, <span style='font-family: Courier New'>map</span>, <span style='font-family: Courier New'>multimap</span>, <span style='font-family: Courier New'>set</span>, <span style='font-family: Courier New'>multiset</span>. <br />
<br />
All the containers (except for <span style='font-family: Courier New'>string</span>) are described by elements enclosed in curly brackets and separated by commas. Strings are described by a string literal enclosed in double quotes. For example, "Hello World!" describes a <span style='font-family: Courier New'>string</span>, {1, 2, 3} could describe a <span style='font-family: Courier New'>vector&lt;int&gt;</span>, {"Hello", "World"} could describe a <span style='font-family: Courier New'>pair&lt;string, string&gt;</span>, and {{"One", 1}, {"Two",2}} could describe a <span style='font-family: Courier New'>map&lt;string, int&gt;</span>. <br />
<br />
Let's look at an example which loads data into some STL containers. <br />
<br />
In main.cpp:<br />
<br />
 <pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;<br /><br />typedef std::map&lt;int, float&gt; LevelPriceMap;<br />typedef std::vector&lt;float&gt; Row;<br />typedef std::vector&lt;Row&gt;   Matrix;<br /><br />struct Item<br />{<br />    std::string 	_name;<br />    LevelPriceMap   _levelPrices;<br />    Matrix          _transform;<br />};<br /><br />int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    Item item;<br /><br />    // Read the item properties<br />    if( !r.Read( "name",        item._name        ) &#124;&#124;<br />        !r.Read( "levelPrices", item._levelPrices ) &#124;&#124;<br />        !r.Read( "transform",   item._transform   ) )<br />        return -1;<br /><br />    // Display the item properties<br />    {<br />        std::cout &lt;&lt; "Item Properties" &lt;&lt; std::endl;<br />        std::cout &lt;&lt; "Name: " &lt;&lt; item._name &lt;&lt; std::endl;<br /><br />        for(LevelPriceMap::const_iterator itr = item._levelPrices.begin();<br />            itr != item._levelPrices.end(); ++itr)<br />        {<br />            std::cout &lt;&lt; "Price at Level " &lt;&lt; itr-&gt;first &lt;&lt; ": " &lt;&lt; itr-&gt;second &lt;&lt; std::endl;<br />        }<br /><br />        std::cout &lt;&lt; "Transformation: " &lt;&lt; std::endl;<br />        for(std::size_t j=0; j &lt; item._transform.size(); ++j)<br />        {<br />            for(std::size_t i=0; i &lt; item._transform&#91;j&#93;.size(); ++i)<br />                std::cout &lt;&lt; item._transform&#91;j&#93;&#91;i&#93; &lt;&lt; " ";<br /><br />            std::cout &lt;&lt; std::endl;<br />        }<br />    }<br /><br />    return 0;<br />}</pre><br />
 Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>/* Item properties */<br /><br />name = "Small Healing Potion";<br /><br />levelPrices = // Level =&gt; Price<br />{<br />    { 1, 10.5 },<br />    { 2, 15   },<br />    { 3, 16.5 }, // modest price increase at this level<br />    { 4, 20   }<br />};<br /><br />transform = // identity matrix; no transformation<br />{<br />    { 1, 0, 0 },<br />    { 0, 1, 0 },<br />    { 0, 0, 1 }<br />};</pre><br />
The output generated would be: <span style='font-family: Courier New'><br />
<br />
Item Properties<br />
Name: Small Healing Potion<br />
Price at Level 1: 10.5<br />
Price at Level 2: 15<br />
Price at Level 3: 16.5<br />
Price at Level 4: 20<br />
Transformation:<br />
1 0 0<br />
0 1 0<br />
0 0 1</span><br />
 <br />
Note how nested containers (like Matrix in the above example) are  handled automatically without the user having to write any extra code.  <br />
<br />
<strong class='bbc'><span style='font-size: 18px;'>Custom types</span></strong><br />
<br />
Daabli supports two methods for loading all types: Intrusive and Non-intrusive. <br />
<br />
The intrusive method is easier to implement for a type, but requires modification of the type (a public <span style='font-family: Courier New'>Read</span> function of a specified signature must be added). The non-intrusive method is a bit more difficult to implement, but doesn't require modification of the type; hence it's the only option for types which cannot be modified (like library types for example). Note that the non-intrusive method requires that the type can be loaded using only its public interface (since private members are inaccessible). <br />
<br />
When Daabli has to read a type, it first checks if a non-intrusive method for reading the type exists. If it does, then the type is read using the non-intrusive method, otherwise the type is read using the intrusive method. Let's take a look at the two methods of reading in detail.<br />
<br />
<strong class='bbc'><span style='font-size: 12px;'>Custom types - Intrusive method</span></strong><br />
<br />
 For a type to support the intrusive method of reading, it must have a public <span style='font-family: Courier New'>Read</span> function (which reads its members) with the following signature (const-correctness is optional): <br />
<br />
<span style='font-family: Courier New'>const bool Read(Daabli::Reader &r) const; </span><br />
<br />
Let's modify the space ship example from earlier, to support the intrusive method of reading. This time, we'll read a list of space ships instead of just one. <br />
<br />
In main.cpp: <br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;<br /><br />struct SpaceShip<br />{<br />    char    _type;<br />    float   _maxSpeed;<br />    float   _mass;<br />    bool    _invulnerable;<br /><br />    // Support the intrusive reading method<br />    const bool Read(Daabli::Reader &r)<br />    {<br />        return<br />            r.Read( "type", 		_type                ) &&<br />            r.Read( "maxSpeed", 	_maxSpeed            ) &&<br />            r.Read( "mass", 		_mass                ) &&<br />            r.Read( "invulnerable", _invulnerable, false );<br />    }<br />};<br /><br />int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    std::list&lt;SpaceShip&gt; ships;<br /><br />    // Read the list of space ships<br />    if( !r.Read( "ships", ships ) )<br />        return -1;<br /><br />    // Display the list of space ships<br />    for(std::list&lt;SpaceShip&gt;::const_iterator itr = ships.begin(); itr != ships.end(); ++itr)<br />    {<br />        std::cout &lt;&lt; "type 		: " &lt;&lt; (*itr)._type 		&lt;&lt; std::endl;<br />        std::cout &lt;&lt; "maxSpeed 	: " &lt;&lt; (*itr)._maxSpeed 	&lt;&lt; std::endl;<br />        std::cout &lt;&lt; "mass 		: " &lt;&lt; (*itr)._mass 		&lt;&lt; std::endl;<br />        std::cout &lt;&lt; "invulnerable : " &lt;&lt; (*itr)._invulnerable &lt;&lt; std::endl;<br />        std::cout &lt;&lt; std::endl;<br />    }<br /><br />    return 0;<br />}</pre><br />
 Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>ships =<br />{<br />    // Drone ship<br />    {<br />        type 	= 'D';<br />        maxSpeed = 10.2;<br />        mass 	= 50.7;<br />    },<br />    <br />    // Fighter ship<br />    {<br />        type 	= 'F';<br />        maxSpeed = 22.4;<br />        mass 	= 25.1;<br />    },<br />    <br />    // Shop ship<br />    {<br />        type 		= 'S';<br />        maxSpeed 	= 0;<br />        mass 		= 100;<br />        invulnerable = true;<br />    }<br />};</pre><br />
 The output generated would be: <br />
<br />
<span style='font-family: Courier New'>type 		: D<br />
maxSpeed 	: 10.2<br />
mass 		: 50.7<br />
invulnerable : 0<br />
<br />
type 		: F<br />
maxSpeed 	: 22.4<br />
mass 		: 25.1<br />
invulnerable : 0<br />
<br />
type 		: S<br />
maxSpeed 	: 0<br />
mass 		: 100<br />
invulnerable : 1</span><br />
<br />
Note that custom types read using the intrusive method can be (optionally) enclosed in curly brackets.  <br />
<br />
Derived classes must read their base object parts inside their own <span style='font-family: Courier New'>Read</span> methods in the manner shown below: <br />
<br />
<pre class='prettyprint'>class A<br />{<br />    /* A's members go here */<br />public:<br />    const bool Read(Daabli::Reader &r)<br />    {<br />        /* Read A's members here */<br />        return true;<br />    }<br />};<br /><br />class B<br />{<br />    /* B's members go here */<br />public:<br />    const bool Read(Daabli::Reader &r)<br />    {<br />        /* Read B's members here */<br />        return true;<br />    }<br />};<br /><br />class C : public A, public B<br />{<br />    /* C's members go here */<br />public:<br />    const bool Read(Daabli::Reader &r)<br />    {<br />        // Read bases<br />        if( !r.Read&lt;A&gt;( *this ) &#124;&#124;<br />            !r.Read&lt;B&gt;( *this ) )<br />            return false;<br /><br />        /* Read C's members here */<br />        return true;<br />    }<br />};</pre><br />
<br />
Note how the derived class C reads its base object parts inside its <span style='font-family: Courier New'>Read</span> method. It doesn’t directly call the <span style='font-family: Courier New'>Read</span> functions of its base classes, but rather, reads them like they were  normal members. That is the correct way to read base class object parts.  Calling the <span style='font-family: Courier New'>Read</span> method of the base classes directly might  seem to work, but it bypasses essential book-keeping code (like  object-tracking) and hence is not recommended.  <br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>Custom types - Non-intrusive method</strong></span> <br />
<br />
The non-intrusive method uses a traits class<sup class='bbc'>[2]</sup> <span style='font-family: Courier New'>ObjectReader</span>, which is specialized partially<sup class='bbc'>[3]</sup> by the user to provide reading support for the required types. The following is the skeleton of the <span style='font-family: Courier New'>ObjectReader</span> traits class:<br />
<br />
<pre class='prettyprint'>namespace Daabli<br />{<br />    template &lt;class T&gt; struct ObjectReader<br />    {<br />        // Must be true in all specializations<br />        enum { exists = true };<br />        // Could be true or false as required in specializations<br />        enum { group = false };<br /><br />        // Define this function for specializations<br />        static const bool Read(T &obj, Reader &r);<br />    };<br />}</pre><br />
The <span style='font-family: Courier New'>'exists'</span> enumerator must be set to true in all  specializations (Daabli uses this value to check if the specialization  exists or not). The '<span style='font-family: Courier New'>group</span>' enumerator can be configured as  required; if it is set to true, then Daabli expects object descriptions  for this type to be enclosed in curly brackets in the input. If it is  set to false, then no enclosing curly brackets are expected. The 'Read' function should be defined by the user to read objects of that type.  <br />
<br />
Let's look at an example. Suppose we have a Point structure defined in some library: <br />
<br />
<pre class='prettyprint'>struct Point<br />{<br />    int x;<br />    int y;<br />};</pre><br />
 Then we could provide reading support for it by specializing the <span style='font-family: Courier New'>ObjectReader</span> traits class like so: <br />
<br />
<pre class='prettyprint'>namespace Daabli<br />{<br />    template &lt;&gt; struct ObjectReader&lt;Point&gt;<br />    {<br />        enum { exists = true };<br />        enum { group = false };<br /><br />        static const bool Read(Point &obj, Reader &r)<br />        {<br />            // Read a Point of the form: &lt;x&gt;, &lt;y&gt;<br />            return<br />                r.Read( obj.x ) && r.ReadSeparator() &&<br />                r.Read( obj.y );<br />        }<br />    };<br />}</pre><br />
Since the <span style='font-family: Courier New'>ObjectReader</span> template is declared under the Daabli namespace, we put its specialization also under the same. The '<span style='font-family: Courier New'>exists</span>' enumerator must be defined as true (as stated before). The '<span style='font-family: Courier New'>group</span>'  enumerator is defined as false here, because we do not wish the  description to be enclosed in curly brackets in the input. The '<span style='font-family: Courier New'>ReadSeparator</span>'  function is new here; it reads a separator from the input (currently  defined as the comma character). Now let's put all this together into a  small program and see how it works.  <br />
In main.cpp: <br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;<br /><br />struct Point<br />{<br />    int x;<br />    int y;<br />};<br /><br />namespace Daabli<br />{<br />    template &lt;&gt; struct ObjectReader&lt;Point&gt;<br />    {<br />        enum { exists = true };<br />        enum { group = false };<br /><br />        static const bool Read(Point &obj, Reader &r)<br />        {<br />            // Read a Point of the form: &lt;x&gt;, &lt;y&gt;<br />            return<br />                r.Read( obj.x ) && r.ReadSeparator() &&<br />                r.Read( obj.y );<br />        }<br />    };<br />}<br /><br />int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    Point p;<br /><br />    // Read the Point<br />    if( !r.Read( "point", p ) )<br />        return -1;<br /><br />    // Display the point<br />    std::cout &lt;&lt; "Point: " &lt;&lt; p.x &lt;&lt; ", " &lt;&lt; p.y &lt;&lt; std::endl;<br /><br />    return 0;<br />}</pre><br />
 Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>point = 10, 20;</pre> <br />
The output generated would be: <br />
<br />
<span style='font-family: Courier New'>Point: 10, 20</span><br />
<br />
Simple enough; but what if our Point structure was templated? Would we need to partially specialize <span style='font-family: Courier New'>ObjectReader</span> for each instantiation of Point template? No we wouldn't! We could specialize <span style='font-family: Courier New'>ObjectReader</span> for all instantiations of Point template. So suppose our Point template looked like this: <br />
<br />
<pre class='prettyprint'>template &lt;class T&gt;<br />struct Point<br />{<br />    T   x;<br />    T   y;<br />};</pre><br />
 Then our <span style='font-family: Courier New'>ObjectReader</span> specialization to support reading of any Point template instantiation would be: <br />
<br />
<pre class='prettyprint'>namespace Daabli<br />{<br />    template &lt;class T&gt; struct ObjectReader&lt;Point&lt;T&gt; &gt;<br />    {<br />        enum { exists = true };<br />        enum { group = false };<br /><br />        static const bool Read(Point&lt;T&gt; &obj, Reader &r)<br />        {<br />            // Read a Point of the form: &lt;x&gt;, &lt;y&gt;<br />            return<br />                r.Read( obj.x ) && r.ReadSeparator() &&<br />                r.Read( obj.y );<br />        }<br />    };<br />}</pre><br />
 And then we could read objects of any Point template instantiation: <br />
<br />
<pre class='prettyprint'>int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    Point&lt;int&gt;          p1;<br />    Point&lt;float&gt;        p2;<br />    Point&lt;std::string&gt;  p3;<br /><br />    // Read the points<br />    if( !r.Read( "point&lt;int&gt;",    p1 ) &#124;&#124;<br />        !r.Read( "point&lt;float&gt;",  p2 ) &#124;&#124;<br />        !r.Read( "point&lt;string&gt;", p3 ) )<br />        return -1;<br /><br />    // Display the points<br />    std::cout &lt;&lt; "Point&lt;int&gt;    : " &lt;&lt; p1.x &lt;&lt; ", " &lt;&lt; p1.y &lt;&lt; std::endl;<br />    std::cout &lt;&lt; "Point&lt;float&gt;  : " &lt;&lt; p2.x &lt;&lt; ", " &lt;&lt; p2.y &lt;&lt; std::endl;<br />    std::cout &lt;&lt; "Point&lt;string&gt; : " &lt;&lt; p3.x &lt;&lt; ", " &lt;&lt; p3.y &lt;&lt; std::endl;<br /><br />    return 0;<br />}</pre><br />
 Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>point&lt;int&gt;    = 10, 20;<br />point&lt;float&gt;  = 10.2, 20.4;<br />point&lt;string&gt; = "Hello", "World";</pre><br />
 The output generated would be: <br />
<br />
 <span style='font-family: Courier New'>Point&lt;int&gt;    : 10, 20<br />
Point&lt;float&gt;  : 10.2, 20.4<br />
Point&lt;string&gt; : Hello, World</span><br />
<br />
 The Point is a somewhat silly example, but you get the  idea; reading works recursively. You could have a  Point > and it would work without any extra  code. In fact, all built-in types in Daabli (basic types, STL  containers) are supported via the non-intrusive method. If you look at  the source, you'll find that all the built-in types have <span style='font-family: Courier New'>ObjectReader</span> specializations.  <br />
<br />
<strong class='bbc'><span style='font-size: 18px;'>Enumerations </span></strong><br />
<br />
Adding support for loading enumerations (even library ones whose source cannot be modified) is quite easy. Daabli borrows from the article "Stringizing C++ Enums"<sup class='bbc'>[1]</sup> to convert enumerators to their string representations and vice versa (though only vice versa is important for loading purposes right now). The steps for adding loading support for enumerations are: <ul class='bbcol decimal'><li>Re-declare the enumeration in a different format for conversion support to/from string (see section <em class='bbc'>“String conversion support”</em>).</li><li>Declare the enumeration as 'readable' by Daabli.</li></ul>  For illustration purposes, let's bring back the Furious Five Master enumeration example from the article "Stringizing C++ Enums"<sup class='bbc'>[1]</sup>: <br />
<br />
<pre class='prettyprint'>// Furious Five Master<br />enum Master<br />{<br />    Tigress = 5,<br />    Viper = 3,<br />    Monkey = 4,<br />    Mantis = 1,<br />    Crane = 2<br />};</pre><br />
 To add reading support, we first add string conversion support (using helper macros): <br />
<br />
<pre class='prettyprint'>// String conversion support<br />Daabli_Begin_Enum( Master )<br />{<br />    Daabli_Enum( Tigress );<br />    Daabli_Enum( Viper );<br />    Daabli_Enum( Monkey );<br />    Daabli_Enum( Mantis );<br />    Daabli_Enum( Crane );<br />}<br />Daabli_End_Enum;</pre><br />
 And then we simply declare the enumeration as readable by Daabli: <br />
<br />
<pre class='prettyprint'>// Reading support<br />Daabli_Readable_Enum( Master );</pre><br />
 And we're done! Now you can read Master enumerations just like any other object: <br />
<br />
<pre class='prettyprint'>&#91;/cint main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    Master myMaster;<br /><br />    if( !r.Read( "myMaster", myMaster ) )<br />        return -1;<br /><br />    std::cout &lt;&lt; "myMaster: " &lt;&lt; myMaster &lt;&lt; std::endl;<br /><br />    return 0;<br />}</pre><br />
 Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>myMaster = Monkey;</pre> The output generated would be: <br />
<br />
<span style='font-family: Courier New'>myMaster: 4</span><br />
<br />
The above program outputs 4 which is the value for the Monkey  enumerator. As you might have guessed, enumerations are supported via  the non-intrusive method; the macro <span style='font-family: Courier New'>Daabli_Readable_Enum</span> expands to an <span style='font-family: Courier New'>ObjectReader</span> specialization for the enumeration type.  <br />
<br />
<strong class='bbc'><span style='font-size: 18px;'>Pointers</span></strong> <br />
<br />
In C++, runtime polymorphism is most often associated with dynamic allocation and pointers/references. So itÆs important that weÆre able to work with pointers on an object description level. Daabli has native support for pointers, making it easy to describe and load them. <br />
<br />
In Daabli, a pointer can be described as a reference to an object. To refer to a particular object, the object must be given a name. Names (which must be unique) are assigned to objects by prefixing the object with the name enclosed in square brackets. Once an object is named, pointers which point to it can be described as the name enclosed in square brackets. LetÆs look at a simple example: <br />
<br />
 <pre class='prettyprint'>value  = &#91;myValue&#93; 10;  // Integer object named 'myValue'<br />pValue = &#91;myValue&#93;; 	// Integer pointer, pointing to the object whose name is 'myValue'.</pre><br />
 And some code to read the above data: <br />
<br />
<pre class='prettyprint'>int  value;<br />int *pValue;<br /><br />if( !r.Read( "value",  value  ) &#124;&#124;<br />    !r.Read( "pValue", pValue ) )<br />    return -1;<br /><br />std::cout &lt;&lt; "&value: " &lt;&lt; &value &lt;&lt; std::endl; // Outputs address of 'value'<br />std::cout &lt;&lt; "pValue: " &lt;&lt; pValue &lt;&lt; std::endl; // Outputs NULL<br /><br />if( !r.ResolvePointers() )<br />    return -1;  // Handle error<br /><br />std::cout &lt;&lt; "pValue: " &lt;&lt; pValue &lt;&lt; std::endl; // Outputs address of 'value'</pre><br />
Pointers read using Read are not usable immediately; they are assigned <span style='font-family: Courier New'>NULL</span>. The <span style='font-family: Courier New'>ResolvePointers</span>  function is new here; it sets all recently read pointers to point to  the required objects. Why is this required, one might ask? The reason is  that a pointer might be referencing an object which has not yet been  read and hence cannot be assigned to it as yet. Hence the user should  postpone calling <span style='font-family: Courier New'>ResolvePointers</span> until he/she is sure that all referenceable objects have been loaded, or until a pointer actually needs to be used.  <br />
<br />
To create an object dynamically, specify the type of the object to be  created within parenthesis, followed by the object itself. Dynamically  created objects can also be named like normal objects, to allow other  pointers to point to them. As an example: <br />
<br />
<pre class='prettyprint'>pValue1 = (int) 10; 			// Pointer to dynamically allocated int<br />pValue2 = (int) &#91;myValue&#93; 20;   // Pointer to dynamically allocated int, named 'myValue'<br />pValue3 = &#91;myValue&#93;;            // Pointer to the object whose name is 'myValue'</pre><br />
 And some code to read the above data: <br />
<br />
<pre class='prettyprint'>int *pValue1;<br />int *pValue2;<br />int *pValue3;<br /><br />if( !r.Read( "pValue1", pValue1 ) &#124;&#124;<br />    !r.Read( "pValue2", pValue2 ) &#124;&#124;<br />    !r.Read( "pValue3", pValue3 ) )<br />    return -1;<br /><br />// Resolve pointers<br />if( !r.ResolvePointers() )<br />    return -1;  // Handle error<br /><br />std::cout &lt;&lt; "value1: " &lt;&lt; (*pValue1) &lt;&lt; std::endl; // Outputs 10<br />std::cout &lt;&lt; "value2: " &lt;&lt; (*pValue2) &lt;&lt; std::endl; // Outputs 20<br />std::cout &lt;&lt; "value3: " &lt;&lt; (*pValue3) &lt;&lt; std::endl; // Outputs 20<br /><br />delete pValue1;<br />delete pValue2;<br />//delete pValue3; // Same as pValue2</pre><br />
Custom types must be registered with Daabli before pointers to them can be used. This is done using one of the <span style='font-family: Courier New'>Daabli_Register_Type[_Base(n)]</span> macros which registers the custom type with a modified version of the factory presented in the article “Super Factory”<sup class='bbc'>[4]</sup>. This allows Daabli to create the required objects at runtime. For more details on how to use the macros, see “Super Factory”<sup class='bbc'>[4]</sup>.  <br />
Let’s look at an example which demonstrates how to use custom type pointers with Daabli – a list of heterogeneous item objects. <br />
<br />
In main.cpp: <br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;</pre> <br />
We first create an Item class which will be the base class of all items.  We also register it with Daabli, so that we can use pointers to it. <br />
<br />
<pre class='prettyprint'>struct Item<br />{<br />    virtual ~Item() =0<br />    {<br />    }<br />    virtual void Display() =0;<br />};<br /><br />Daabli_Register_Type( Daabli_Abstract, Item );</pre><br />
 Then we create a few items which derive from Item, and have properties of their own. A sword item: <br />
<br />
<pre class='prettyprint'>class Sword : public Item<br />{<br />    std::string _name;<br />public:<br />    virtual void Display()<br />    {<br />        std::cout &lt;&lt; "Sword: " &lt;&lt; _name &lt;&lt; std::endl;<br />    }<br />    const bool Read(Daabli::Reader &r)<br />    {<br />        return r.Read( "name", _name );<br />    }<br />};<br /><br />Daabli_Register_Type_Base1( Daabli_Concrete, Sword, Item );</pre> <br />
A healing potion item: <br />
<br />
<pre class='prettyprint'>class HealingPotion : public Item<br />{<br />    int _pointsHealed;<br />public:<br />    virtual void Display()<br />    {<br />        std::cout &lt;&lt; "Healing Potion: " &lt;&lt; _pointsHealed &lt;&lt; std::endl;<br />    }<br />    const bool Read(Daabli::Reader &r)<br />    {<br />        return r.Read( "pointsHealed", _pointsHealed );<br />    }<br />};<br /><br />Daabli_Register_Type_Base1( Daabli_Concrete, HealingPotion, Item );</pre>  <br />
And an armor item: <br />
<br />
<pre class='prettyprint'>struct Armor : public Item<br />{<br />    float _damageReduction;<br />public:<br />    virtual void Display()<br />    {<br />        std::cout &lt;&lt; "Armor: " &lt;&lt; _damageReduction &lt;&lt; std::endl;<br />    }<br />    const bool Read(Daabli::Reader &r)<br />    {<br />        return r.Read( "damageReduction", _damageReduction );<br />    }<br />};<br /><br />Daabli_Register_Type_Base1( Daabli_Concrete, Armor, Item );</pre><br />
  Then in main, we load a list of items from a description: <br />
<br />
<pre class='prettyprint'>int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    typedef std::list&lt;Item*&gt; Items;<br /><br />    // Read a list of items<br />    Items items;<br />    if( !r.Read( "myItems", items ) )<br />        return -1;<br /><br />    // Resolve pointers<br />    if( !r.ResolvePointers() )<br />        return -1;<br /><br />    // Display the list of items<br />    for(Items::const_iterator itr = items.begin(); itr != items.end(); ++itr)<br />        (*itr)-&gt;Display();<br /><br />    // Delete all the items in the list<br />    for(Items::const_iterator itr = items.begin(); itr != items.end(); ++itr)<br />        delete (*itr);<br /><br />    return 0;<br />}</pre><br />
 Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>myItems =<br />{<br />    (HealingPotion) { pointsHealed = 10; },<br />    (HealingPotion) { pointsHealed = 12; },<br />    (Sword) 		{ name = "Great Stinging Sword"; },<br />    (Armor) 		{ damageReduction = 50; }<br />};</pre><br />
 The output generated would be: <br />
<br />
<span style='font-family: Courier New'>Healing Potion: 10<br />
Healing Potion: 12<br />
Sword: Great Stinging Sword<br />
Armor: 50</span><br />
<br />
Note that since the Item base class was registered with Daabli as being  abstract, and it didn’t have any members of it’s own to read, we didn’t  have to define a Read function for it.   <br />
<br />
<strong class='bbc'><span style='font-size: 18px;'>Error Messages</span><br />
<br />
</strong> Sometimes the loading process doesnÆt quite go so smoothly and errors occur. When this happens, DaabliÆs policy is to abort reading at the first error that occurs and return failure (false). Since this is rarely enough information, Daabli also provides an error/warning log with more information for the user to chew over. <br />
<br />
The Log member of the Reader class contains this information and can be examined by the user at any time. The following code demonstrates how this information can be accessed: <br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include <br /><br />int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br /><br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    // Read an integer<br />    int value;<br />    r.Read( "myValue", value );<br /><br />    // Display error messages if any<br />    Daabli::MessageLog::MessageList::const_iterator itr = r.Log.Messages().begin();<br />    while( itr != r.Log.Messages().end() )<br />    {<br />        std::cout &lt;&lt; "At line number   : " &lt;&lt; itr-&gt;first.first  &lt;&lt; std::endl;<br />        std::cout &lt;&lt; "At column number : " &lt;&lt; itr-&gt;first.second &lt;&lt; std::endl;<br />        std::cout &lt;&lt; "Error message    : " &lt;&lt; itr-&gt;second   	&lt;&lt; std::endl;<br />        std::cout &lt;&lt; std::endl;<br /><br />        ++itr;<br />    }<br /><br />    return 0;<br />}</pre><br />
Given the description in input.txt:  <br />
<br />
<pre class='prettyprint'>myValue = 10.2;  </pre><br />
The output generated would be: <br />
<br />
<span style='font-family: Courier New'>At line number   : 2<br />
At column number : 10<br />
Error message    : invalid int value: 10.2<br />
<br />
At line number   : 2<br />
At column number : 10<br />
Error message    : failed to read object</span><br />
<br />
Actually, when the Reader object goes out of scope, it dumps all log  messages to the standard error output. So even if the user hadn’t  manually output the contents of the log, the following would have been  dumped to the standard error output: <br />
<br />
<span style='font-family: Courier New'>[Ln 2, Col 10] invalid int value: 10.2<br />
[Ln 2, Col 10] failed to read object</span><br />
<br />
Try changing the input and see the error messages which get generated;  also experiment with the other examples we’ve encountered so far.  <br />
 <br />
<span style='font-size: 12px;'><strong class='bbc'>Custom error messages</strong></span><br />
<br />
 Apart from being examined, the log can also be written to at any time.  Overloaded insertion operators allow the user to write string and  character data to the log. This is useful for logging custom error  messages when the user writes <span style='font-family: Courier New'>ObjectReader</span> specializations for custom types. This is also useful for logging data validation failure errors. Let’s look at an example: <br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;<br /><br />struct ClownHero<br />{<br />    int _health;<br />    int _ammo;<br /><br />    const bool Read(Daabli::Reader &r)<br />    {<br />        // Read and validate health<br />        {<br />            if( !r.Read( "health", _health ) )<br />                return false;<br /><br />            if((_health &lt; 1) &#124;&#124; (_health &gt; 100))<br />            {<br />                r.Log &lt;&lt; "Invalid value for health (1 to 100): "<br />                      &lt;&lt; Daabli::ToString(_health) &lt;&lt; Daabli::endl;<br />                return false;<br />            }<br />        }<br /><br />        // Read and validate ammo<br />        {<br />            if( !r.Read( "ammo", _ammo ) )<br />                return false;<br /><br />            if((_ammo &lt; 0) &#124;&#124; (_ammo &gt; 1000))<br />            {<br />                r.Log &lt;&lt; "Invalid value for ammo (0 to 1000): "<br />                      &lt;&lt; Daabli::ToString(_ammo) &lt;&lt; Daabli::endl;<br />                return false;<br />            }<br />        }<br /><br />        return true;<br />    }<br />};<br /><br />int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Daabli::Reader r;<br />    if( !r.FromFile( "input.txt" ) )<br />        return -1;<br /><br />    // Read a hero<br />    ClownHero hero;<br />    if( !r.Read( "hero", hero ) )<br />        return -1;<br /><br />    // Display the hero's properties<br />    std::cout &lt;&lt; "Clown Hero Properties" &lt;&lt; std::endl;<br />    std::cout &lt;&lt; "Health: " &lt;&lt; hero._health &lt;&lt; std::endl;<br />    std::cout &lt;&lt; "Ammo  : " &lt;&lt; hero._ammo &lt;&lt; std::endl;<br /><br />    return 0;<br />}</pre><br />
Given the description in input.txt: <br />
<br />
<pre class='prettyprint'>hero =<br />{<br />    health = 100;<br />    ammo = 1500;<br />};</pre><br />
The output generated would be: <br />
<br />
<span style='font-family: Courier New'>[Ln 5, Col 16] Invalid value for ammo (0 to 1000): 1500<br />
[Ln 5, Col 16] failed to read object</span><br />
<br />
String and character data can be streamed to the <span style='font-family: Courier New'>MessageLog</span>, as well as an endl  manipulator which indicates end of line. Other types of data must be  converted to string before being streamed to the log. In the above  example, <span style='font-family: Courier New'>_health</span> and <span style='font-family: Courier New'>_ammo</span> were converted to string using the ToString function (<em class='bbc'>see section “String conversion support”</em>).  <br />
<strong class='bbc'><br />
<span style='font-size: 18px;'>String conversion support</span></strong><br />
<br />
 Daabli includes a string conversion utility which can be used for converting objects to/from strings. This is handy not only for converting simple objects like ints, floats, doubles etc. to/from string, but is also extensible enough to provide string conversion support for custom types. Objects are converted to/from string using the following functions:<br />
<ul class='bbc'><li><span style='font-family: Courier New'>const string ToString(const T &val);</span><ul class='bbc'><li>Converts an object to its string representation; returns the string.</li></ul> </li><li><span style='font-family: Courier New'>const bool FromString(T &val, const string &str);</span><ul class='bbc'><li>Converts a string to an object; returns true if the conversion was successful, false otherwise.</li></ul></li></ul>The following code snippet demonstrates how to convert a simple integer to/from string: <br />
<br />
<pre class='prettyprint'>const int value = 256;<br />std::cout &lt;&lt; value &lt;&lt; std::endl;    // outputs 256<br /><br />const std::string valueStr = Daabli::ToString( value );<br />std::cout &lt;&lt; valueStr &lt;&lt; std::endl; // outputs 256<br /><br />int newValue = 0;<br />Daabli::FromString( newValue, valueStr );<br />std::cout &lt;&lt; newValue &lt;&lt; std::endl; // outputs 256</pre><br />
Recall from the <em class='bbc'>“Enumerations”</em> section that the first step in  providing loading support for an enumeration was to re-declare it for  conversion support to/from string. In fact, if only string conversion  support is required for an enumeration and not loading support, then  only the first step need be used. After that, values of the enumeration  can be converted to/from string just like any other basic object. Let’s  look at an example: <br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;<br /><br />// Weekend day<br />enum Weekend<br />{<br />    Sunday,<br />    Saturday<br />};<br /><br />// String conversion support<br />Daabli_Begin_Enum( Weekend )<br />{<br />    Daabli_Enum( Sunday );<br />    Daabli_Enum( Saturday );<br />}<br />Daabli_End_Enum;<br /><br />int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Weekend day = Sunday;<br />    <br />    // Convert the string "Saturday" into a day<br />    if( !Daabli::FromString( day, "Saturday" ) )<br />        return -1;<br /><br />    // Convert the day back into a string<br />    const std::string dayStr = Daabli::ToString( day );<br /><br />    std::cout &lt;&lt; dayStr &lt;&lt; std::endl;    // outputs Saturday<br /><br />    return 0;<br />}</pre><br />
String conversion support through the common <span style='font-family: Courier New'>ToString/FromString</span> interface can be provided for custom types by using a traits class<sup class='bbc'>[2]</sup> <span style='font-family: Courier New'>StringConverter</span>, which is specialized partially<sup class='bbc'><a href='http://archive.gamedev.net/reference/articles/article2698.asp#ref' class='bbc_url' title='External link' rel='nofollow external'>[3]</a></sup> by the user to provide support for the required types. The following is the skeleton of the <span style='font-family: Courier New'>StringConverter</span> traits class: <br />
<br />
<pre class='prettyprint'>namespace Daabli<br />{<br />    template &lt;class T&gt; struct StringConverter<br />    {<br />        // Must be true in all specializations<br />        enum { exists = true };<br /><br />        // Define these functions for specializations<br />        static const std::string ToString(const T &val);<br />        static const bool FromString(T &val, const std::string &str);<br />    };<br />}</pre><br />
The '<span style='font-family: Courier New'>exists</span>' enumerator must be set to true in all  specializations (Daabli uses this value to check if the specialization  exists or not). The <span style='font-family: Courier New'>ToString</span> and <span style='font-family: Courier New'>FromString</span> functions should be defined by the user to convert objects of that type to and from string.  <br />
<br />
Let's look at an example. Suppose we have the following Color class: <br />
<br />
<pre class='prettyprint'>#include "Daabli.h"<br />#include &lt;iostream&gt;<br /><br />struct Color<br />{<br />    typedef unsigned char Byte;<br /><br />    Byte _r;<br />    Byte _g;<br />    Byte _b;<br /><br />    const bool IsRed() const    { return (_r == 255) && (_g == 0  ) && (_b == 0  ); }<br />    const bool IsGreen() const  { return (_r == 0  ) && (_g == 255) && (_b == 0  ); }<br />    const bool IsBlue() const   { return (_r == 0  ) && (_g == 0  ) && (_b == 255); }<br /><br />    void Set(const Byte r, const Byte g, const Byte &lt;img src='&#91;url="&#91;url="http&#58;//public.gamedev.net/public/style_emoticons/"&#93;http&#58;//public.gamedev.net/public/style_emoticons/&#91;/url&#93;"&#93;&#91;url="http&#58;//public.gamedev.net/public/style_emoticons/"&#93;http&#58;//public.gamedev.net/public/style_emoticons/&#91;/url&#93;&#91;/url&#93;&lt;#EMO_DIR#&gt;/cool.gif' class='bbc_emoticon' alt='B)' /&gt;<br />    {<br />        _r = r; _g = g; _b = b;<br />    }<br />};</pre><br />
Then we could provide reading support for it by specializing the <span style='font-family: Courier New'>StringConverter</span> traits class like so: <br />
<br />
<pre class='prettyprint'>namespace Daabli<br />{<br />    template &lt;&gt; struct StringConverter&lt;Color&gt;<br />    {<br />        enum { exists = true };<br /><br />        static const std::string ToString(const Color &val)<br />        {<br />            if( val.IsRed()   ) return std::string( "Red" );<br />            if( val.IsGreen() ) return std::string( "Green" );<br />            if( val.IsBlue()  ) return std::string( "Blue" );<br />            return std::string( "Unknown" );<br />        }<br /><br />        static const bool FromString(Color &val, const std::string &str)<br />        {<br />            if( str.compare( "Red"   ) == 0 ) { val.Set( 255,   0,   0 ); return true; }<br />            if( str.compare( "Green" ) == 0 ) { val.Set(   0, 255,   0 ); return true; }<br />            if( str.compare( "Blue"  ) == 0 ) { val.Set(   0,   0, 255 ); return true; }<br />            return false;<br />        }<br />    };<br />}</pre><br />
Since the <span style='font-family: Courier New'>StringConverter</span> template is declared under the Daabli namespace, we put its specialization also under the same. The '<span style='font-family: Courier New'>exists</span>' enumerator must be defined as true (as stated before). For illustration purposes, the <span style='font-family: Courier New'>ToString</span> and <span style='font-family: Courier New'>FromString</span>  functions have been defined to convert color objects to and from some  simple color names. Now that this is done, we can convert color objects  to and from strings, just like any other basic object: <br />
<br />
<pre class='prettyprint'>int main(int /*argc*/, char * /*argv*/&#91;&#93;)<br />{<br />    Color color = { 0, 0, 0 };<br /><br />    // Convert the string "Red" into a color<br />    if( !Daabli::FromString( color, "Red" ) )<br />        return -1;<br /><br />    // Convert the color back into a string<br />    const std::string colorStr = Daabli::ToString( color );<br /><br />    std::cout &lt;&lt; colorStr &lt;&lt; std::endl; // outputs Red<br /><br />    return 0;<br />}</pre><br />
Note that although string conversion support is not directly related to  deserialization, I thought I’d cover it here anyway as it comes along  with Daabli; why let it go to waste.  <br />
<br />
<span style='font-size: 18px;'><strong class='bbc'>Closing</strong></span><br />
<br />
 Apart from the samples presented in this guide, do go through those which are included with the Daabli source code; they demonstrate most of the features of Daabli in a more complete manner. The source code is released under the MIT license<sup class='bbc'>[5]</sup> and has been tested on the following compilers: <br />
<br />
 StringConverter, which is specialized partially<ul class='bbc'><li>Microsoft Visual C++ 2005/2008</li><li>GCC 4.3.2</li></ul> I hope you enjoyed reading this guide as much as I did writing it, and that Daabli (or at least parts/concepts of it) will be useful to you. There is much potential for improvement; if you make changes to the code, improve it, or have some better ideas, I would love to know. I can be reached by email at francisxavierjp [at] gmail [dot] com. Comments and suggestions are always welcome! <br />
<br />
<span style='font-size: 12px;'><strong class='bbc'>References</strong></span><br />
<br />
[1] <a href='http://www.gamedev.net/reference/snippets/features/cppstringizing/' class='bbc_url' title=''>http://www.gamedev.n...cppstringizing/</a> <br />
[2] <a href='http://en.wikipedia.org/wiki/Trait_class' class='bbc_url' title='External link' rel='nofollow external'>http://en.wikipedia....iki/Trait_class</a> <br />
[3] <a href='http://en.wikipedia.org/wiki/Partial_template_specialization' class='bbc_url' title='External link' rel='nofollow external'>http://en.wikipedia...._specialization</a> <br />
[4] <a href='http://www.gamedev.net/reference/snippets/features/supFactory/' class='bbc_url' title=''>http://www.gamedev.n...res/supFactory/</a> <br />
[5] <a href='http://en.wikipedia.org/wiki/MIT_License' class='bbc_url' title='External link' rel='nofollow external'>http://en.wikipedia....iki/MIT_License</a>]]></description>
		<pubDate>Tue, 06 Oct 2009 06:19:15 +0000</pubDate>
		<guid isPermaLink="false">b31f0c758bb498b5d56b5fea80f313a7</guid>
	</item>
	<item>
		<title>Super Factory</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/super-factory-r2662</link>
		<description><![CDATA[<strong class='bbc'>Introduction</strong><br />
With object factory implementations already done to death, you might be wondering what on earth a "Super Factory" is. Simply put, it's an object factory which can create objects of any type and return them through any of their respective base class interfaces. This is a very useful feature indeed (especially for persistence frameworks, and especially for the one I'm writing (: ). Conventional object factory implementations require the user to either (a.) derive the objects to be created at runtime from a common base class, or (b.) use multiple factories to create heterogeneous objects. A Super Factory exposes a single unified interface to create heterogeneous objects at runtime.<br />
<br />
(Please note that the term "Super" here, is used to mean "very generic", or "broad in scope or content"; not a superior method/implementation.)<br />
<br />
<br />
<strong class='bbc'>How to use the code</strong><br />
Using <a href='http://members.gamedev.net/gaiiden/SuperFactory.zip' class='bbc_url' title='External link' rel='nofollow external'>the code</a> is quite easy. All we need do is add one file: SuperFactory.h (see the source code accompanying this article), to our project. To add Factory support to a class, we need to "register" it in the implementation file (.cpp) for that class. Macros have been included for the most common use cases.<br />
<br />
Macro syntax:<br />
SF_Register_Type[_Base(n)]( , , , , ...  );<br />
<br />
So, for e.g., to register an abstract class A, which doesn't have any base classes, we would use: SF_Register_Type( SF_Abstract, A );<br />
<br />
And to register a concrete class C, which has two base classes A and B, we would use: SF_Register_Type_Base2( SF_Concrete, C, A, B );<br />
<br />
Once the classes are registered, the Factory's Create method can then be used to create objects as required.<br />
<br />
<br />
<strong class='bbc'>A simple example</strong><br />
Consider the following classes defined in their respective header files:	  // A.h 	struct A 	{	 	virtual void Print() =0;	 	virtual ~A() { } 	}; 	// B.h 	struct B : public A 	{	 	virtual void Print() { cout << "In B::Print" << endl; }	 	virtual ~B() { } 	}; 	// C.h 	struct C : public B 	{	 	virtual void Print() { cout << "In C::Print" << endl; }	 	virtual ~C() { } 	};  To add Factory support, we register each class in it's respective implementation file:	  // A.cpp 	SF_Register_Type( SF_Abstract, A ); 	// B.cpp 	SF_Register_Type_Base1( SF_Concrete, B, A );	  // C.cpp 	SF_Register_Type_Base1( SF_Concrete, C, B );  Once that's done, we can use the Factory to create objects as required:	  A *pObj1; 	B *pObj2; 	A *pObj3; 	if( SuperFactory::Create( "B", pObj1 ) )	// Creates a B	 	pObj1->Print();					 	// In B::Print 	if( SuperFactory::Create( "C", pObj2 ) )	// Creates a C	 	pObj2->Print();					 	// In C::Print 	if( SuperFactory::Create( "C", pObj3 ) )	// Creates a C!	 	pObj3->Print();					 	// In C::Print!  Note that even though classes A and C might be implemented by two different people who know nothing of each other's implementations, the Factory has been indirectly "told" (by the registrations) that A is a base class of C. Hence, the Factory is able to create objects of type C and return them through an interface of type A, like pObj3 in the above example.<br />
<strong class='bbc'>Another simple example</strong><br />
Consider the following classes defined in their respective header files:	  // A.h 	struct A 	{	 	virtual void Print() =0;	 	virtual ~A() { } 	}; 	// B.h 	struct B 	{	 	virtual void Print() =0;	 	virtual ~B() { } 	}; 	// C.h 	struct C : public A, public B 	{	 	virtual void Print() { cout << "In C::Print" << endl; }	 	virtual ~C() { } 	};  To add Factory support, we register each class in it's respective implementation file:	  // A.cpp 	SF_Register_Type( SF_Abstract, A ); 	// B.cpp 	SF_Register_Type( SF_Abstract, B ); 	// C.cpp 	SF_Register_Type_Base2( SF_Concrete, C, A, B );  Once that's done, we can use the Factory to create objects as required:	  A *pA; 	B *pB; 	if( SuperFactory::Create( "C", pA ) )   // Creates a C	 	pA->Print();						// In C::Print 	if( SuperFactory::Create( "C", pB ) )   // Creates a C	 	pB->Print();						// In C::Print  Note that C derives from both A and B, so the Factory can create objects of type C and return them through either base interface, A or B.<br />
<strong class='bbc'>Working with primitive types</strong><br />
The Factory can also create primitive types (provided that they're registered). So, suppose the user wanted the ability to create float and unsigned int objects via the Factory, he would first register them as usual (in one of the implementation files):<br />
<br />
	  SF_Register_Type( SF_Concrete, float ); 	SF_Register_Type( SF_Concrete, unsigned int );  Once that's done, objects of these primitive types can be created as required:	  float *pFloat; 	if( SuperFactory::Create( "float", pFloat ) ) 	{	 	// Do something with pFloat 	} 	unsigned int *pUint; 	if( SuperFactory::Create( "unsigned int", pUint ) ) 	{	 	// Do something with pUint 	}  <br />
<strong class='bbc'>How does it work?</strong><br />
It's not required to know how it works in order to use it, so those who are not really interested can skip this section. The working of this Factory is very similar to the one described in the wonderful book "Modern C++ Design". For each specific product type (concrete/abstract), there exists an instance of a templated factory class to handle it. Each factory can only create objects of the product type it's designed to handle (provided that the product type it handles is not abstract). However, it also has the ability to delegate creation to the factories which handle derived Products.<br />
<br />
So when asked to create a product, given a product identifier, the factory first sees if the requested product is the one it's designed to handle. If so, it simply uses the user-supplied product creation function and returns whatever's created to the user. But if the product identifier is not recognized by the factory, it delegates creation to factories which handle derived product types, hoping that the product will be recognized by one of them. This happens recursively until one of the factories in the product hierarchy recognizes the identifier (which means that it knows how to create products of that type), or none of the factories in that product hierarchy recognize the identifier. If a product identifier is not recognized by any of the factories in that product hierarchy, then:<br />
 <ul class='bbcol decimal'><br /><li>the product to be created doesn't fall within that hierarchy (i.e., it's a base product, or an unrelated product).<br /></li><li>the product to be created was not (or incorrectly) registered with its associated factory.<br /></li><li>one of the products in the hierarchy was not (or incorrectly) registered, thereby breaking the "chain" of creation delegation.<br /></li></ul><br />
If one of the factories in the hierarchy recognizes the product identifier, it creates the product and returns it to its caller, which would be the immediate base product factory. The happens recursively until the product finally reaches the user. When the user registers a product and its immediate base classes, behind the scenes something tricky happens. The product gets registered straightforwardly with its factory (via the Factory::Register function). However, as described in the paragraphs above, each product needs information about its derived products (to delegate creation to); not about its base products. Hence there's no "RegisterBase" function; instead there's a "RegisterDerived" function. So, the product is instead registered with the factories of its base classes (via the Factory::RegisterDerived function). This is how each factory knows about its derived products (even though it appears to the user that he's registering classes and their bases).<br />
<br />
<br />
<strong class='bbc'>Pros/Cons</strong><br />
Pros:<ul class='bbc'><br /><li>Very easy to use and maintain.<br /></li><li>Exposes a single unified interface to create all objects.<br /></li><li>Doesn't require RTTI.<br /></li><li>Doesn't require the objects to be polymorphic.<br /></li><li>No overhead is added to objects (in terms of time/space).<br /></li><li>Supports primitive types (float, unsigned int, etc.)<br /></li><li>Supports (instantiated) templated types. (see Example4 in accompanying code)<br /></li><li>Can provide Factory services to existing classes without any modifications.<br /></li></ul><br />
Cons:<ul class='bbc'><br /><li>The implementation provided only has out of the box support for objects which have a public parameterless constructor. However, bypassing the normal registration macros allows you to provide your own functions to create objects, so you can use that to customize object creation. (Otherwise you can always hack the code to support your specific needs (: ).<br /></li><li>Requires some maintenence if class hierarchies change (but usually, that's not something which happens too frequently).<br /></li><li>A linear search is employed to find the required object type. The time taken depends on the hierarchical "distance" between the target type and the type to be returned. Normally this shouldn't be a problem, unless you have *extremely* deep class hierarchies.<br /></li></ul><br />
<strong class='bbc'>Closing</strong><br />
Please note that in the code examples above, for the purpose of clarity, I've left out deleting the objects allocated by the factory; in real world code you'll want to delete them after use, or use std::auto_ptr (or a smart pointer) to do it automatically for you. In the accompanying source code, there are two more examples. One shows the Factory working with a diamond inheritance hierarchy, and the other shows the Factory working with templated types.<br />
<br />
The source code is released under the MIT license and has been tested on thefollowing compilers:<br />
<br />
<ul class='bbc'><br /><li>Microsoft Visual C++ 2005/2008<br /></li><li>GCC 4.3.2<br /></li></ul><br />
There is much potential for improvement of the code; if you make changes to the code, improve it, or have some better ideas, I would love to know. I can be reached by email at francisxavierjp [at] gmail [dot] com. Comments/suggestions are always welcome!<br />
<strong class='bbc'>References</strong><br />
[1] "<a href='http://www.gamedev.net/columns/books/bookdetails.asp?productid=191' class='bbc_url' title=''>Modern C++ Design</a>"<br />
[2] <a href='http://loki-lib.sourceforge.net/' class='bbc_url' title='External link' rel='nofollow external'>Loki C++ library</a><br />
[3] "<a href='http://www.gamedev.net/reference/programming/features/objectfactory/' class='bbc_url' title=''>Creating a Generic Object Factory</a>"<br />
[4] <a href='http://en.wikipedia.org/wiki/Factory_method_pattern' class='bbc_url' title='External link' rel='nofollow external'>Factory method pattern</a>]]></description>
		<pubDate>Fri, 17 Jul 2009 12:20:49 +0000</pubDate>
		<guid isPermaLink="false">6e6dfb0bdbd1a0d2591c32e5959e0578</guid>
	</item>
	<item>
		<title>K-Means Heuristic for General Purpose Binary Se...</title>
		<link>http://www.gamedev.net/page/resources/_/technical/general-programming/k-means-heuristic-for-general-purpose-binary-se-r2607</link>
		<description><![CDATA[<br />
<strong class='bbc'>1 Introduction</strong><br />
 In image synthesis, as in many other fields, binary search trees are used to fulfil tasks that are often critical to applications. In image synthesis, examples of such operations are ray-tracing and frustum culling. A feature common to all these operations is that they consist in seeking information (the data set) contained in a certain volume (the search zone) of a k-dimension space (k ∈ N) and that they all use a binary search tree to perform this search as quickly as possible.<br />
<br />
 Assuming that the seeking information consist of points in space and that the search volume is a continuous and bounded interval, we shall see first of all that minimizing intra-class variance<sup class='bbc'>1</sup> (i.e. solving the problem of the k-means) is the ideal construction heuristic for a binary search tree.<br />
<br />
 We shall then see that Lloyd’s algorithm, which gives a local extremum to the k-means problem, is a very good heuristic, since it gives good results in practice, is very fast to compute, is robust, easy to implement and perfectly suited to animated data sets, common in image synthesis.<br />
<br />
 <br />
<strong class='bbc'>2 Related tasks</strong><br />
 For a 1-dimensional space, the binary search tree is a classic case studied by every IT student. The k-means tree is an ideal generalization of this algorithm for spaces with k dimensions.<br />
<br />
 In image synthesis, the structures used are Kd-trees and BVHs (Bounding Volume Hierarchy). We should note that Kd-tree is a special case of BSP (Binary Space Partition). The most popular construction heuristic for these structures at the moment is called SAH (Surface Area Heuristic).<br />
<br />
 <br />
<strong class='bbc'>3 Ideal heuristic for search algorithm</strong><br />
 <br />
<strong class='bbc'>3.1 Hypotheses and observations</strong><br />
 Our purpose here is to visit the smallest possible number of nodes in the traversal algorithm, subject to the hypothesis that we can only act on the binary tree construction heuristic.<br />
<br />
 We shall further assume that the data set φ is finite, that this set is wholly contained in a convex region Ω (continuous and bounded) and that the search zone is an interval of Ω, continuous and bounded, denoted Δ (convex or not). We note that on this hypothesis, a ray (used in ray tracing) bounded at Ω is an acceptable search zone.<br />
<br />
 We shall make the hypothesis that, whatever the heuristic used for the construction of the tree, each node is associated with the whole of φ or a non-empty part of it (φ is associated with the root node of the tree), and that each node possesses 0 or 2 child-nodes, depending on whether the node is associated with 1 or more than 1 of the points of φ. Thus each node which is not a leaf, splits its associated set of points into 2 non-empty subsets.<br />
<br />
 We shall observe that, if a node N possesses n points all contained in Δ then, whatever the heuristic used, the sub-tree corresponding to N possesses <em class='bbc'>2n-1</em> nodes which are all likely to be visited (except in the case of ancillary optimizations that may be added to the traversal algorithm). Similarly, if Δ contains a total of just <em class='bbc'>n</em> points, a minimum of <em class='bbc'>2n-1</em> nodes will be visited if <em class='bbc'>n</em> is the cardinal of φ, or a minimum of <em class='bbc'>2n</em> nodes if it is not, irrespective of the heuristic used (except in the case of ancillary optimizations that may be added to the traversal algorithm).<br />
<br />
 Furthermore, if all the points of φ are contained in Δ, then a total of exactly <em class='bbc'>2n-1</em> nodes will be visited, irrespective of the heuristic.<br />
<br />
 Every time that a part – non-empty, but not the entirety – of φ is sought, we can make the following observation: the ideal <em class='bbc'>magic</em> tree would be a tree which would immediately split the root node into two subsets: the set of points sought and the set of points not sought, whatever Δ might be. In such a case, only the minimum number of nodes would be visited, i.e. <em class='bbc'>2n</em> nodes.<br />
<br />
 <strong class='bbc'>From these observations, we can easily deduce the conditions to be fulfilled in order to minimize the number of nodes visited in the general case.</strong> We would have to:<br />
<br />
 <ul class='bbc'><li>refrain from visiting nodes having no associated point in Δ (i.e. <strong class='bbc'>3.3</strong>) </li><li>maximize the probability P<sub class='bbc'>ideal</sub> so that each node N splits its associated set into two subsets, one set entirely sought and one set entirely non-sought (i.e. <strong class='bbc'>3.2</strong>) </li><li>If the set of points associated with a node is sought, then we shall visit the same number of nodes in the sub-tree, irrespective of the heuristic (except in the case of ancillary optimizations that may be added to the traversal algorithm). Thus, this case is not significant.</li></ul> <br />
<strong class='bbc'>3.2 Maximizing P<sub class='bbc'>ideal</sub></strong><br />
 The hypothesis here, then, is that a node N is visited as long as at least one point in the set of points associated with it is sought, but not all of them.<br />
<br />
 The original problem of the k-means is to minimize intra-class variance (the sum of variances of each set is minimal). For each node N, solving the k-means problem with <em class='bbc'>k = 2</em> enables us to determine two subsets which are associated with the child-nodes of N.<br />
<br />
 Two observations must be made here. This first is that minimizing intra-class variance simultaneously maximizes inter-class variance, because the total variance of the set is equal to the sum of intra-class variance and inter-class variance [1].<br />
<br />
 The second observation is crucial to what follows. Taking two points P1 and P2: if P1 is in Δ, then the probability that P2 is also in Δ increases as the distance separating the two points diminishes. It is equal to 1 when the distance is zero, and to 0 when the distance separating the two points is greater than the maximum distance between P1 and any other point of Δ [2]. This can easily be visualized for a convex region Δ. It is sufficient to imagine a non-convex region as a convex region from which a piece has been removed, and it is clear that this also works for non-convex regions. <strong class='bbc'>Thus, from the general point of view, when nothing is known about Δ, the probability that P2 is also in Δ is maximized when the distance between P1 and P2 is minimized.</strong><br />
<br />
 The idea behind the k-means is that minimizing intra-class variance enables us to ensure that the points within one class are as close to each other as possible, while maximizing inter-class variance enables us to ensure that the classes themselves are as far from each other as possible.<br />
<br />
 Thus with the k-means heuristic and these hypotheses, it can be clearly seen that when a node N is visited, there exist i and j ∈ {0,1}, i!=j such that Ρ = ∏<sub class='bbc'>1</sub><sup class='bbc'><em class='bbc'>Ki</em></sup>ρ(Pik ∈ Δ | ∃m such that Pim ∈ Δ )*(1 - ∏<sub class='bbc'>1</sub><sup class='bbc'><em class='bbc'>Kj</em></sup>ρ(Pjk ∈ Δ | ∃m such that Pim ∈ Δ)) is maximized, where Pi is the set of points associated with the child-node i of N, Pj is the set of points associated with child-node j of N, Pik is the k<sup class='bbc'>th</sup> point of Pi, Pjk is the k<sup class='bbc'>th</sup> point of Pj, Ki is the cardinal number of Pi and Kj is the cardinal number of Pj.<br />
<br />
 Now P = P<sub class='bbc'>ideal</sub>; hence, tackling the k-means problem maximizes P<sub class='bbc'>ideal</sub>.<br />
<br />
 <br />
<strong class='bbc'>3.3 “Nodes visited where no associated point is sought”, or the bounding volumes problem</strong><br />
 A node visited that in reality possesses no point sought brings us straight to the problem of bounding volumes. It is possible to establish that a bounding volume intersects with Δ although no point φ associated with this volume belongs to Δ.<br />
<br />
 But here again, the k-means heuristic demonstrates its effectiveness. <strong class='bbc'>By definition, there cannot exist a bounding volume whose points are on average closer to the points of the set associated with it than the bounding volumes determined by sets of points determined by the k-means. Thus, the probability of the case arising where no point is in reality contained in Δ is minimized.</strong><br />
<br />
 <br />
<strong class='bbc'>4 Lloyd’s algorithm and animations</strong><br />
 The most popular algorithm for solving the k-means problem is Lloyd’s algorithm. But <strong class='bbc'>it only enables the determination of a local extremum</strong>. This algorithm nevertheless offers many advantages: it gives good results in practice, is very easy to implement, it converges rapidly, it is robust and... it is iterative.<br />
<br />
 The fact that it is iterative is especially interesting for the animated data sets frequently used in image synthesis. This is because it is possible to perform only a limited number of iterations each frame, so that there is no effect on frame-rate.<br />
<br />
 <br />
<strong class='bbc'>5 Conclusion</strong><br />
 It seems to us that the algorithms for solving the k-means problem ought to be employed in preference to very many heuristics used for the construction of binary search trees, because they correspond naturally to the problems of partition when seeking information in both bounded and continuous intervals.<br />
<br />
 In image synthesis, the most popular construction heuristic is SAH. Unfortunately, it is very slow to calculate, so that approximation methods are generally used. Although SAH acts on area-related information, in contrast to our k-means heuristic, we consider that it should be abandoned in favour of methods derived from our k-means heuristic, since although it is similar in principle, it does not possess all the theoretical and practical advantages of the latter.<br />
<br />
 <br />
<strong class='bbc'>References</strong><br />
 [1]   <a href='http://ljk.imag.fr/membres/Bernard.Ycart/smel/cours/ts/node14.html' class='bbc_url' title='External link' rel='nofollow external'>http://ljk.imag.fr/m.../ts/node14.html</a><br />
<br />
 [2]   <a href='http://en.wikipedia.org/wiki/Distance' class='bbc_url' title='External link' rel='nofollow external'>http://en.wikipedia.org/wiki/Distance</a><br />
<br />
 [3]   <a href='http://en.wikipedia.org/wiki/K-means_algorithm' class='bbc_url' title='External link' rel='nofollow external'>http://en.wikipedia....means_algorithm</a><br />
<br />
 [4]   State of the art in Ray Tracing Animated Scenes, Ingo Wald, William R. Mark, Johannes Günther, Solomon Boulos, Thiago Ize, Warren Hunt, Steven G. Parker, Peter Shirley, EUROGRAPHICS 2007<br />
<br />
  <sup class='bbc'>1</sup> A class is no more nor less than a set of points, and we shall use these terms interchangeably in this &#100;ocument.<br />
<br />
]]></description>
		<pubDate>Wed, 11 Feb 2009 17:38:35 +0000</pubDate>
		<guid isPermaLink="false">388ac20c845a327f97edece8acba6237</guid>
	</item>
</channel>
</rss>
