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

<channel>
	<title>chrishowie.com &#187; Programming</title>
	<atom:link href="http://www.chrishowie.com/category/computer/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrishowie.com</link>
	<description>The best laid plans are in my other pants</description>
	<lastBuildDate>Tue, 15 Nov 2011 20:20:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>What I dislike about C++, part 1: References</title>
		<link>http://www.chrishowie.com/2011/09/27/what-i-dislike-about-c-plus-plus-part-1-references/</link>
		<comments>http://www.chrishowie.com/2011/09/27/what-i-dislike-about-c-plus-plus-part-1-references/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 17:46:18 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=468</guid>
		<description><![CDATA[I&#8217;ve started a new job, for those of you who didn&#8217;t know. I&#8217;m now coding C++ daily. My relationship with C++ has been distant, simply because I haven&#8217;t really ever had a need to use it. However, C and C# are both strong languages of mine, and C++ sits somewhere in the middle: C with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started a new job, for those of you who didn&#8217;t know.  I&#8217;m now coding C++ daily.  My relationship with C++ has been distant, simply because I haven&#8217;t really ever had a need to use it.  However, C and C# are both strong languages of mine, and C++ sits somewhere in the middle: C with classes, C# without garbage collection.  (These are rough approximations, and not without caveats.)</p>
<p>There are bound to be things in every programming language that get in the way of productivity.  In this post I&#8217;ll highlight one of those things in C++: references.  References are obfuscating and mostly useless.  But first, why would you use a reference?</p>
<p>A reference is effectively a pointer, but this is hidden by the language.  You use it like it&#8217;s <i>not</i> a pointer, and the compiler turns direct accesses into indirect accesses.  For example:</p>
<pre>#include &lt;iostream&gt;

int main() {
	int a = 5;
	int &amp;b = a;

	std::cout &lt;&lt; "a is " &lt;&lt; a &lt;&lt; std::endl;

	b = 6;

	std::cout &lt;&lt; "a is " &lt;&lt; a &lt;&lt; std::endl;
}</pre>
<p>The output will be:</p>
<pre>a is 5
a is 6</pre>
<p>Note that I did not say <code>*b = 6;</code>.  The reference is treated as though it were not a pointer.  Cool&#8230; but what&#8217;s the benefit?</p>
<p>The solitary benefit I&#8217;ve heard from others is that references cannot be null.  When you declare a function/method that accepts an argument typed as a reference, it&#8217;s not possible to make that reference equivalent to a null pointer.</p>
<p>Ok, so we trade a bit of clarity for a compile-time guarantee that we won&#8217;t be dereferencing a null pointer.  That&#8217;s a good trade, right?</p>
<p>Maybe.  Consider this excerpt:</p>
<pre>class Foo;

void do_something() {
    Foo *foo = new Foo();
    use_foo(*foo);
    delete foo;
}</pre>
<p>This is contrived, yes, and there&#8217;s a better way to write this code.  But I&#8217;m illustrating something here.  I&#8217;ve told you that <code>Foo</code> is a class, but I haven&#8217;t told you the prototype for <code>use_foo()</code>.  That&#8217;s on purpose.  Now, you tell me if the <code>Foo</code> instance is going to be copied.  I&#8217;ll even tell you that <code>Foo</code> doesn&#8217;t overload <code>operator*</code>.</p>
<p>Do you have your answer yet?  If you said yes &#8212; the logical choice &#8212; you&#8217;re wrong.  If you said no, you&#8217;re also wrong.  Well, actually, if you said yes or no, you <i>might</i> be wrong.  It&#8217;s impossible to tell.  If <code>use_foo()</code>&#8216;s declaration is <code>use_foo(Foo foo)</code> then a copy will be made using <code>Foo</code>&#8216;s copy constructor (if possible, otherwise it will be a compile-time error).  But if the function&#8217;s prototype is <code>use_foo(Foo &amp;foo)</code> then we are actually passing in the value stored in the <code>foo</code> variable &#8212; a memory address.  The object will not be copied.  In other words, while it looks like we are dereferencing <code>foo</code>, we are actually doing no such thing.</p>
<p>In C, you can tell pretty much everything you need to know from a call site.  In C++, you must know how the function you&#8217;re calling is defined too, simply because you have to know when things are references and when they are not.  The treatment of what is fundamentally a pointer type as a value type (at the language level) is what causes the uncertainty.  You use value-type grammar around references, even though they are not really a value type.</p>
<p>If it weren&#8217;t for the existence references, the code above would be perfectly clear.  (Well, unless you didn&#8217;t have me to tell you that <code>Foo</code> doesn&#8217;t implement <code>operator*</code>&#8230;)</p>
<p>Don&#8217;t get me wrong, C++ still has a lot of good features.  It&#8217;s just a bit irritating that because of a bad feature (and some other features too, such as some forms of operator overloading), I must know the details of every type and function on a line of code to be absolutely sure what it&#8217;s doing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2011/09/27/what-i-dislike-about-c-plus-plus-part-1-references/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mutable strings in Mono</title>
		<link>http://www.chrishowie.com/2010/11/24/mutable-strings-in-mono/</link>
		<comments>http://www.chrishowie.com/2010/11/24/mutable-strings-in-mono/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 19:50:57 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=434</guid>
		<description><![CDATA[Update 2010-12-17: Those of you who saw this post appear and then vanish were not seeing things. The Mono community identified the contents of this blog post as a serious security vulnerability in Moonlight that, through violation of the type system, allows the CoreCLR security layer to be bypassed. Attackers could potentially run arbitrary code [...]]]></description>
			<content:encoded><![CDATA[<p style="border-bottom: 1px solid #fff"><b>Update 2010-12-17:</b> Those of you who saw this post appear and then vanish were not seeing things.  The Mono community identified the contents of this blog post as a serious security vulnerability in Moonlight that, through violation of the type system, allows the CoreCLR security layer to be bypassed.  Attackers could potentially run arbitrary code with the permissions of the user running Moonlight.  This entry was therefore temporarily removed until a patch was made available.  See <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-4254">CVE-2010-4254</a> and <a href="http://secunia.com/advisories/42373">SA42373</a>.  If you are using Moonlight, <a href="http://www.go-mono.com/moonlight/download.aspx">update to 2.3.0.1 or later</a> ASAP.  The original and unedited blog post follows:</p>
<p>So I was messing around with generic methods and discovered that <a href="https://bugzilla.novell.com/show_bug.cgi?id=654136">generic constraints can be bypassed on Mono 2.6.7 and 2.8 using reflection</a> (with the exception of the <code>new()</code> constraint).  One of the fun results of this bug is that the <code>String</code> class can be made mutable <i>without using reflection to set private members!</i></p>
<p>The following code demonstrates this; it is legal and will run on Mono up to and including version 2.8:</p>
<pre>using System;
using System.Reflection;

public class FakeString {
    public int length;
    public char start_char;
}

public class TestCase {
    private static FakeString UnsafeConversion&lt;T&gt;(T thing)
        where T : FakeString
    {
        return thing;
    }

    public static void Main() {
        var a = "foo";
        var b = MakeMutable(a);

        Console.WriteLine(a);
        b.start_char = 'b';
        Console.WriteLine(a);
    }

    private static FakeString MakeMutable(string s)
    {
        var m = typeof(TestCase).GetMethod("UnsafeConversion", BindingFlags.NonPublic | BindingFlags.Static);
        var m2 = m.MakeGenericMethod(typeof(string));

        var d = (Func&lt;string, FakeString&gt;)Delegate.CreateDelegate(typeof(Func&lt;string, FakeString&gt;), null, m2);

        return d(s);
    }
}</pre>
<p>This code outputs:</p>
<pre>foo
boo</pre>
<p>Smells like some fun exploits could be written taking advantage of this.  Should Moonlight users be afraid?  I&#8217;m not absolutely certain, but I think there might just be a way to do some damage.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2010/11/24/mutable-strings-in-mono/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenVP has landed</title>
		<link>http://www.chrishowie.com/2010/08/20/openvp-has-landed/</link>
		<comments>http://www.chrishowie.com/2010/08/20/openvp-has-landed/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 17:53:26 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Banshee]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[OpenVP]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=430</guid>
		<description><![CDATA[Well, if you&#8217;ve been waiting for some kind of stable release of OpenVP for Banshee, you will love this. OpenVP is part of the Banshee Community Extensions 1.7.4 release! Go get it, and be sure to file any bugs you come across.]]></description>
			<content:encoded><![CDATA[<p>Well, if you&#8217;ve been waiting for some kind of stable release of OpenVP for Banshee, you will love this.  <a href="http://mail.gnome.org/archives/banshee-list/2010-August/msg00072.html">OpenVP is part of the Banshee Community Extensions 1.7.4 release!</a>  <a href="http://download.banshee.fm/banshee-community-extensions/1.7.4/">Go get it</a>, and be sure to <a href="http://code.google.com/p/openvisualizationplatform/issues/list">file any bugs</a> you come across.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2010/08/20/openvp-has-landed/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>git-svn in the workplace</title>
		<link>http://www.chrishowie.com/2010/04/01/git-svn-in-the-workplace/</link>
		<comments>http://www.chrishowie.com/2010/04/01/git-svn-in-the-workplace/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 16:42:22 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=365</guid>
		<description><![CDATA[At work, we use Subversion for source control. This is quite the popular VCS, but I&#8217;ve grown accustomed to (and much prefer) Git. Don&#8217;t get me wrong, SVN has its advantages, but since using Git my workflow has changed quite radically, and it&#8217;s difficult to revert to the rather inflexible and tedious SVN workflow. Anyway, [...]]]></description>
			<content:encoded><![CDATA[<p>At work, we use Subversion for source control.  This is quite the popular VCS, but I&#8217;ve grown accustomed to (and much prefer) Git.  Don&#8217;t get me wrong, SVN has its advantages, but since using Git my workflow has changed quite radically, and it&#8217;s difficult to revert to the rather inflexible and tedious SVN workflow.  Anyway, I&#8217;ve been using git-svn for the past month or so, and thought I&#8217;d share some of my practices.</p>
<p>In my clone, master is my &#8220;local&#8221; storage branch.  I use it to version things like my .gitignore, and my commit message template.  I would also use it for my dcommit/rebase scripts too, but since this is on Windows, Git becomes angry when it attempts to remove scripts that are executing.</p>
<p>Master is then the common root for my topic branches.  I&#8217;ll do some work and commit, then do more work, as the usual Git workflow goes.  The ability to create local branches and commits is great for several reasons:</p>
<p>First, I can commit much more often, without fearing that I will break somebody else&#8217;s working copy &#8212; I do frequently commit broken code now, because the commits don&#8217;t get sent to SVN automatically.</p>
<p>Second, and a side effect of the above, I am much more agile.  Sometimes I&#8217;ll be working on two projects at once, and keeping separate branches for them means that the broken state of one branch does not affect my ability to build/debug another.  This means I can even drop everything (after a commit or stash) and help out with an urgent QA or support issue, without either having lots of uncommitted work interfering or committing broken code to a production repository first.</p>
<p>Third, I can version changes I make to code that might make my life easier, but that would require approval to commit.  I have not yet done this, but thanks to my local repository, it&#8217;s an option.</p>
<p>Fourth, I can develop against a stable codebase.  If I need a specific fix from SVN to work, I can cherry-pick it into my topic branch &#8212; fetching commits from SVN does not mandate that my code must be merged with them.  Along the same lines, I also have the ability to rebase all my topic branches against a specific SVN commit, which is great when somebody commits broken code that causes build errors or incorrect runtime behavior.  If I were using SVN, I would either have to fix it myself, wait for the owner of the code to fix it, or revert to an earlier commit.  And since commits are linear, that might mean I would lose some of <i>my</i> code.  Git allows me to retain my commits while backing out changes made by others.  And if I <i>did</i> already commit some of my changes after the broken code, I can still rebase master and cherry-pick my subsequent commits onto a new branch and continue my work.</p>
<p>And finally, I can review all my commits before pushing them out.  I&#8217;ve had several occasions to fix up commit messages already.</p>
<p>Of course, I do eventually need to commit to SVN.  I have a few scripts to help here.  Obviously I don&#8217;t want to commit my local-only stuff in master.  So I have one script that simply does <code>git svn fetch &#038;&#038; git rebase --onto git-svn master</code>.  For the Git-impaired, this fetches any new commits from SVN and adds corresponding Git commits on the git-svn branch.  The rebase command then takes all the commits on the current branch since master&#8217;s latest commit, and applies them as patches to the tip of the git-svn branch, creating a new commit for each.  This effectively removes the local changes I&#8217;ve made in master from the commits, as well as merges the changes in SVN into my commits.  (If one of my commits fails to apply as a patch, then I have to resolve the conflicts manually, just like an SVN update.  But in this case, I still retain all my individual commits.)</p>
<p>After running this script, I then <code>git svn dcommit</code>, which commits my local commits to SVN, one by one.</p>
<p>At this point, I usually run my post-commit script, which rebases master on to git-svn, then rebases all my other branches on top of master.  I might not do this if I don&#8217;t want to rebase some of my other topic branches yet.</p>
<p>In closing, I leave you with my trick for changing commit messages.  I used to use git filter-branch for this, which is like using a sledgehammer for surgery.  (My similes are awesome, I know.)  Now I use this process:</p>
<p><code>git checkout -b temp $COMMIT_TO_CHANGE</code> (Create a new branch called &#8220;temp&#8221; at the commit I&#8217;m changing, and switch to it.)</p>
<p><code>git commit --amend</code> (Open an editor to amend my commit.  I change to message here, then save and quit.  The temp branch now has the new commit, whose parent commit is the same as it was before being amended.)</p>
<p><code>git checkout $ORIGINAL_BRANCH</code> (Switch back to the branch we are amending.)</p>
<p><code>git rebase temp</code> (Rebase the branch on top of the amended commit.  Since the original commit will be applied on top of the amended commit, it is dropped during the rebase.  The other commits will apply with no conflicts.  The history is now corrected.)</p>
<p><code>git branch -d temp</code> (Remove our temporary branch.)</p>
<p>And there you have it.  In a history with many thousands of commits, this is much faster than git filter-branch, and for those relatively fluent in Git, is also easier to remember how to do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2010/04/01/git-svn-in-the-workplace/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>When FarmVille == Productivity</title>
		<link>http://www.chrishowie.com/2010/01/27/when-farmville-productivity/</link>
		<comments>http://www.chrishowie.com/2010/01/27/when-farmville-productivity/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 00:27:11 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Computer]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=348</guid>
		<description><![CDATA[Update 2010-02-08: Jonathan Pryor has merged many of my extension methods into Cadenza. I&#8217;d strongly suggest checking it out. It&#8217;s no secret to my friends that I love to program&#8230; even more so as I&#8217;ve been developing a FarmVille client in C# and having them test it. (As much as you might hate FarmVille, you [...]]]></description>
			<content:encoded><![CDATA[<p><b>Update 2010-02-08:</b> Jonathan Pryor has merged many of my extension methods into <a href="http://gitorious.org/cadenza">Cadenza</a>.  I&#8217;d strongly suggest checking it out.</p>
<p>It&#8217;s no secret to my friends that I love to program&#8230; even more so as I&#8217;ve been developing a <a href="http://www.farmville.com">FarmVille</a> client in C# and having them test it.  (As much as you might hate FarmVille, you must agree that there&#8217;s a certain awesome factor in LINQ-to-FarmVille: <code>service.Plow(from i in service.World.Objects.OfType&lt;Plot&gt;() where i.State == "fallow" || i.State == "withered" select i</code>)</p>
<p>Well, among this project and others I consistently find myself writing the same code over and over.  I know of many programmers who have developed personal toolkits for the languages they use frequently, but for some reason I&#8217;ve been programming for about 13-15 years now and haven&#8217;t ever built my own library.  This application gave me an excuse to do so, and so I&#8217;ve started on the Cdh.Toolkit suite of libraries.</p>
<p>Here is a summary of the classes available:</p>
<ul>
<li><b>Cdh.Toolkit.Collections</b>: Some useful collection types, all designed to be derived.
<ul>
<li><b>ReadOnlyCollection&lt;T&gt;</b>, <b>ReadOnlyDictionary&lt;TKey, TValue&gt;</b>, and <b>ReadOnlyList&lt;T&gt;</b>: Wrappers around the corresponding interface types ICollection&lt;T&gt;, IDictionary&lt;TKey, TValue&gt;, and IList&lt;T&gt;, throwing exceptions on all write attempts.  While there is a ReadOnlyCollection&lt;T&gt; as part of the .NET framework, it is not designed to be derived, and the other two classes do not have a counterpart at all.</li>
<li><b>SynchronizedCollection&lt;T&gt;</b>, <b>SynchronizedDictionary&lt;TKey, TValue&gt;</b>, and <b>SynchronizedList&lt;T&gt;</b>: Wrappers around the corresponding interfaces.  All accesses are synchronized against a ReaderWriterLockSlim, allowing for multiple concurrent read operations.  The enumeration behavior can be specified as either lock, which holds a read lock for the duration of the enumeration, or copy, which creates a copy of the collection and enumerates it instead.</li>
<li><b>ObservableCollection&lt;T&gt;</b>: A collection that fires events when modified.  ObservableDictionary and ObservableList are currently not provided, due to some implementation complexities.  However, the interfaces <b>IObservableCollection&lt;T&gt;</b>, <b>IObservableDictionary&lt;TKey, TValue&gt;</b>, and <b>IObservableList&lt;T&gt;</b> and some EventArgs classes are provided to allow developers to implement their own observable collections easily.</li>
<li><b>ObservableHashSet&lt;T&gt;</b>: An observable and API-compatible wrapper around HashSet&lt;T&gt;.</li>
<li><b>ReadOnlyObservableCollection&lt;T&gt;</b>, <b>ReadOnlyObservableDictionary&lt;TKey, TValue&gt;</b>, and <b>ReadOnlyObservableList&lt;T&gt;</b>: Wrappers around the IObservable* interfaces mentioned above.  Events from the wrapped collections are forwarded.  This allows one to have a read only observable collection without sacrificing the IObservable* interface, which would happen if such a collection were wrapped in one of the normal read only classes listed above.</li>
</ul>
</li>
<li><b>Cdh.Toolkit.Extensions</b>: Extension libraries designed to ease the use of many classes in the .NET framework.
<ul>
<li><b>Collections</b>: Extensions specific to collection classes.
<ul>
<li><b>TValue IDictionary&lt;TKey, TValue&gt;.GetOrDefault(TKey key)</b>: Returns default(TValue) if the key is not present in the dictionary.</li>
<li><b>TValue IDictionary&lt;TKey, TValue&gt;.GetOrValue(TKey key, TValue fallback)</b>: Returns fallback if the key is not present in the dictionary.</li>
</ul>
</li>
<li><b>ComponentModel</b>: Extensions specific to the System.ComponentModel namespace.
<ul>
<li><b>void ISynchronizeInvoke.AutoInvoke(Action action)</b>: Executes the action delegate on the ISynchronizeInvoke object if required, otherwise does so on the current thread.</li>
<li><b>object ISynchronizeInvoke.AutoInvoke(Delegate method, params object[] args)</b>: Executes the delegate on the ISynchronizeInvoke object if required, otherwise does so on the current thread, and returns the return value of that method in either case.</li>
<li><b>AsyncCallback AsyncCallback.Invoked(ISynchronizeInvoke obj)</b>: Returns a wrapper around the AsyncCallback delegate that will invoke it using the AutoInvoke extension above.  Useful for async callbacks that need to operate on a Winforms GUI.</li>
</ul>
</li>
<li><b>Enumerable</b>: Extensions to enumerable objects.
<ul>
<li><b>IEnumerable&lt;T&gt; IEnumerable&lt;T?&gt;.NotNull() where T : struct</b>: Returns all values from the non-null nullable objects in the sequence.</li>
<li><b>void IEnumerable&lt;T&gt;.Walk()</b>: Enumerates the sequence, discarding all values obtained.  Useful as an alternative to .ToList() when you need to make sure that a query executes, but do not need to use the result.</li>
<li><b>void IEnumerable&lt;T&gt;.CopyInto(IList&lt;T&gt; list)</b>: Copies a sequence into a list.</li>
</ul>
</li>
<li><b>Events</b>: Extensions that make writing event logic easier.  All of these extensions return if the event handler in question is null, making event-firing code simpler and easier to read.
<ul>
<li><b>void EventHandler.Fire(object sender)</b>: Uses EventArgs.Empty as the event arguments.</li>
<li><b>void EventHandler.Fire(object sender, EventArgs args)</b></li>
<li><b>void EventHandler.Fire(object sender, Func&lt;EventArgs&gt; argsFactory)</b>: Calls the factory function only if the event handler is not null.  Useful when construction of the event arguments can take a long time.</li>
<li><b>void EventHandler&lt;T&gt;.Fire(object sender, T args)</b></li>
<li><b>void EventHandler&lt;T&gt;.Fire(object sender, Func&lt;T&gt; argsFactory)</b>: Calls the factory function only if the event handler is not null.  Useful when construction of the event arguments can take a long time.</li>
</ul>
</li>
<li><b>ReaderWriterLockSlim</b>: Allows these kind of locks to be used in a using() block, which makes code easier to read and maintain.  They will also return a no-op IDisposable if a compatible lock is already held by the current thread, which makes non-recursive lock objects simpler to code with.  (The return type is actually a value type that implements IDisposable, which means that usage of these methods does not incur any object allocation overhead.)
<ul>
<li><b>IDisposable ReaderWriterLockSlim.Read()</b>: Returns an IDisposable that will release the read lock when disposed.  This method returns a no-op IDisposable instead if the current thread already holds a read, upgradeable read, or write lock.</li>
<li><b>IDisposable ReaderWriterLockSlim.UpgradeableRead()</b>: Returns an IDisposable that will release the upgradeable read lock when disposed.  This method returns a no-op IDisposable instead if the current thread already holds an upgradeable read or write lock.</li>
<li><b>IDisposable ReaderWriterLockSlim.Write()</b>: Returns an IDisposable that will release the write lock when disposed.  This method returns a no-op IDisposable instead if the current thread already holds a write lock.</li>
</ul>
</li>
<li><b>Reflection</b>
<ul>
<li><b>T ICustomAttributeProvider.GetCustomAttribute&lt;T&gt;(bool inherit) where T : Attribute</b>: Returns a typed attribute, or null if there is no attribute of type T.</li>
<li><b>IEnumerable&lt;T&gt; ICustomAttributeProvider.GetCustomAttributes&lt;T&gt;(bool inherit) where T : Attribute</b>: Returns a sequence of attributes of type T present on the attribute provider.</li>
</ul>
</li>
<li><b>Reflection.Emit</b>
<ul>
<li><b>void ILGenerator.EmitTypeOf(Type type)</b>: Emits the IL sequence that will leave the same Type object on the execution stack.</li>
</ul>
</li>
</ul>
<p>The amount of code is slim, but I&#8217;ve found at least one of the classes or extensions invaluable in every project I&#8217;ve worked on since starting the toolkit.  It&#8217;s an interesting case where coding for a game actually winds up improving my productivity working on other software too.</p>
<p>Eventually these libraries will be released under the MIT license, so stay tuned for another blog post with a link to the Git repository.</p>
<p>(And yes, the above list will be converted into real documentation.  Someday.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2010/01/27/when-farmville-productivity/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Covariant IEnumerables, pre-.NET-4.0</title>
		<link>http://www.chrishowie.com/2009/11/05/covariant-ienumerables/</link>
		<comments>http://www.chrishowie.com/2009/11/05/covariant-ienumerables/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 18:22:57 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Computer]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=338</guid>
		<description><![CDATA[One of the nice things that .NET 3.5 gives us is LINQ, which gives new life to the often-neglected IEnumerable generic interface. Sequence processing is now a first-class citizen in the C# world, and this is a good thing. However, it can be very tricky to design a usable API around enumerables. Today I present [...]]]></description>
			<content:encoded><![CDATA[<p>One of the nice things that .NET 3.5 gives us is LINQ, which gives new life to the often-neglected IEnumerable generic interface.  Sequence processing is now a first-class citizen in the C# world, and this is a good thing.  However, it can be very tricky to design a usable API around enumerables.  Today I present my solution to an annoying (but not showstopping) hurdle.</p>
<p>Consider the case where you have several types implementing an interface.  In my case, these types all have a common ancestor, but this is beside the point.  We&#8217;ll call the interface <code>IFoo</code>, and the classes implementing this interface <code>ThingOne</code>, <code>ThingTwo</code>, and <code>ThingThree</code>.</p>
<p>If I have a method that acts on a series of <code>IFoo</code> objects, it is tempting to accept <code>IEnumerable&lt;IFoo&gt;</code> as an argument.  It makes sense, right?  Well, sort of.  Your users will not like this, because <code>IEnumerable&lt;ThingOne&gt;</code>, <code>IEnumerable&lt;ThingTwo&gt;</code>, and <code>IEnumerable&lt;ThingThree&gt;</code> are not convertible to <code>IEnumerable&lt;IFoo&gt;</code>.  While it&#8217;s not too annoying, your users will have to cope by invoking <code>Cast&lt;IFoo&gt;()</code> on their enumerables for them to work with your API.  This not only adds code overhead (read: more code to maintain) but a minor amount of CPU and memory overhead to create an object that is going to cast objects to an interface that they explicitly implement.</p>
<p>The solution is rather simple, but not very obvious at first glance.  Instead of using the signature <code>void OperateOnFoos(IEnumerable&lt;IFoo&gt; foos)</code>, use this instead: <code>void OperateOnFoos&lt;T&gt;(IEnumerable&lt;T&gt; foos) where T : IFoo</code>.  It is a simple change, and the method will work exactly the same as before, except your users will no longer be required to cast their enumerables to <code>IFoo</code>.</p>
<p>This technique applies just as well to situations where you take an enumerable to a class that is designed to be subclassed.</p>
<p>Now depending on how generics are implemented in your runtime of choice, you&#8217;re still probably going to see a small memory hit for each different <code>T</code> you use when calling this method.  But it&#8217;s not likely to be anywhere near the cost of creating a bunch of cast-enumerables that you really could do without.  And that aside, the convenience of not having to <code>Cast&lt;IFoo&gt;()</code> enumerables is totally worth changing one line of code.</p>
<p>.NET 4.0 will likely render this mechanism obsolete with the introduction of covariant interfaces, but in the meantime let&#8217;s all do something nice for our users!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2009/11/05/covariant-ienumerables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cdh.SimpleRpc</title>
		<link>http://www.chrishowie.com/2009/10/15/cdh-simplerpc/</link>
		<comments>http://www.chrishowie.com/2009/10/15/cdh-simplerpc/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 21:39:36 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=329</guid>
		<description><![CDATA[I&#8217;ve got this idea to code some game servers for a series of cooperative games my brother and I used to play as kids. I get similar ideas all the time&#8230; how about a game server for this card game or that board game? The problem I run into is pretty much always exactly the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got this idea to code some game servers for a series of cooperative games my brother and I used to play as kids.  I get similar ideas all the time&#8230; how about a game server for this card game or that board game?  The problem I run into is pretty much always exactly the same: what communication protocol do I use?</p>
<p>I decided on a list of criteria that this protocol, whatever it is, must meet:</p>
<ol>
<li>It must be portable across programming languages and runtimes.  If somebody else wants to write a better client using a different environment, that should be straightforward &#8212; perhaps not necessarily <i>easy</i>, but at least straightforward.</li>
<li>It must be relatively efficient on the wire.  Protocol chatter should be minimal in comparison to the data being exchanged.</li>
<li>The object library should be simple and elegant to code against.  When writing my game, the last thing I want to worry about is silly protocol details.  Just get my message to the other computer please.</li>
</ol>
<p>And here are the existing protocols I considered:</p>
<ul>
<li>.NET remoting.  Since I code most in C# these days, it seemed like a logical choice.  But it very blatantly breaks criterion 1 when using the binary formatter, and breaks both 1 and 2 when using the SOAP formatter.</li>
<li>SOAP web service.  Criterion 3 is satisfied, until you get to session persistence details.  Criterion 1 is satisfied, and criterion 2&#8230; not so much.</li>
<li>XML-RPC.  Criterion 1 is met, and 2 is somewhat met.  But criterion 3 is not &#8212; XML-RPC does not define any mechanism for dealing with persistent sessions.  I would have to spend time writing a session manager with expiration and whatnot.  No thanks.</li>
</ul>
<p>And I&#8217;m sure I looked at others.  The point is, for something as simple as message-passing between a game client and server, there doesn&#8217;t appear to be much out there that satisfies my requirements.  And this is something I&#8217;ve come back to frequently.</p>
<p>Well, after several years of mulling the problem over in my subconscious, I knuckled down and coded.  I have a usable library after two days of development.  (And we&#8217;re talking maybe a few hours per day.)  Written in C#, it allows any CIL-based language to write simple message-based client/server programs in very small amounts of code.  For a quick example, let&#8217;s create a server that will convert strings to uppercase, with tracing back to the client.</p>
<p>First, we need to create an interface library so that the client and server know what each other&#8217;s methods are:</p>
<pre>using System;
using Cdh.SimpleRpc;

namespace ServiceTest.Interfaces {
    public interface IServer {
        [RpcMethod] string ToUppercase(string str);
    }

    public interface IClient {
        [RpcMethod] void Trace(string message);
    }
}</pre>
<p>Now, here is the client:</p>
<pre>using System;
using System.IO;
using Cdh.SimpleRpc;
using ServiceTest.Interfaces;

namespace ServiceTest.Client {
    public class MainClass {
        public static void Main() {
            Stream serverStream = ConnectToServer();

            var service = new RpcService&lt;IClient, IServer&gt;(new Client(), serverStream);

            new Thread(delegate { while(service.Read()); }).Start();

            IServer server = service.RemoteServerProxy;
            Console.WriteLine("ToUppercase result: " + server.ToUppercase("this is a test"));

            serverStream.Close();
        }

        private Stream ConnectToServer() {
            // Here is your code to connect to the server endpoint.
        }
    }

    internal class Client : IClient {
        public void Trace(string message) {
            Console.WriteLine("Server trace: " + message);
        }
    }
}</pre>
<p>Note that the RpcService object generates a typed object that will transparently proxy calls to the remote service.  The server program is almost as simple:</p>
<pre>using System;
using System.IO;
using Cdh.SimpleRpc;
using ServiceTest.Interfaces;

namespace ServiceTest.Server {
    public class MainClass {
        public static void Main() {
            Stream clientStream = AcceptConnection();

            Server server = new Server();
            var service = new RpcService&lt;IServer, IClient&gt;(server, clientStream);
            server.client = service.RemoteServerProxy;

            while(service.Read());

            clientStream.Close();
        }

        private Stream AcceptConnection() {
            // Here is your code to accept a client connection.
        }
    }

    internal class Server : IServer {
        public IClient client;

        public string ToUppercase(string str) {
            client.Trace("Entering ToUppercase");
            str = str.ToUpper();
            client.Trace("Leaving ToUppercase");

            return str;
        }
    }
}</pre>
<p>Ta-da.  Some closing notes about this library:</p>
<ul>
<li>It should be completely thread-safe, and will allow you to place calls using the proxy objects from multiple threads.  The calls will block until a response is returned from the remote service.</li>
<li>Yes, you can throw exceptions in a service method, and yes, it will cause an exception to be thrown remotely from the proxy object.</li>
<li>In the future it may be possible to flag service methods that return void as &#8220;no response&#8221; calls, which will cause the proxy call to return immediately.  Of course, you will not be notified if an exception is thrown remotely.</li>
<li>This API doesn&#8217;t do any complex serialization, and will only operate on the primitive types, excluding IntPtr.  It will probably allow transmission of arrays at some point, and perhaps allow custom objects too.</li>
</ul>
<p>Looking back on the list of criteria, this library, even in the early stages of development, easily meets all three.  I&#8217;ll be hacking on it some more I&#8217;m sure, and may even publish the Git repository somewhere, when I&#8217;m confident that the code doesn&#8217;t totally suck.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2009/10/15/cdh-simplerpc/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Splitting a Git repository</title>
		<link>http://www.chrishowie.com/2009/07/09/splitting-a-git-repository/</link>
		<comments>http://www.chrishowie.com/2009/07/09/splitting-a-git-repository/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 18:47:46 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=291</guid>
		<description><![CDATA[First some backstory&#8230; I had a public and a private Subversion repository on my web server, and when I started a new project I&#8217;d import it into one of them. This is nice because I get versioning and history, plus I get implicit synchronization between my various development boxen. It&#8217;s not unusual to have one [...]]]></description>
			<content:encoded><![CDATA[<p>First some backstory&#8230;</p>
<p>I had a public and a private Subversion repository on my web server, and when I started a new project I&#8217;d import it into one of them.  This is nice because I get versioning and history, plus I get implicit synchronization between my various development boxen.</p>
<p>It&#8217;s not unusual to have one monolithic Subversion repository for many different projects, mainly because setting up a Subversion repository can take a small bit of work, especially if you are serving it from HTTP.  However, Git makes it so easy to create new repositories that there is no excuse not to create per-project repositories, not to mention that you should anyway since Git doesn&#8217;t support checking out a subdirectory of a repository.  You simply can&#8217;t use a monolithic Git repository because you have to clone the whole big tree, even if you only plan on working on one subdirectory.</p>
<p>Since Git is so much more awesome, I plan on converting my Subversion repositories over.  But what do I do with the multi-project ones?  There&#8217;s no built-in mechanism for pulling out just one directory, with history, into a new repository.  So I <a href="/files/git-pluck.sh">wrote one</a>.</p>
<p>The usage is <code>git-pluck src-repo dest-repo path/to/directory</code>.  The script copies the repository at <code>src-repo</code> to a new directory, <code>dest-repo</code>, and then does its magic.  It rewrites all of the commits so that all of the files in <code>path/to/directory</code> are moved into the root of the repository, and everything else is deleted.  Commits that do not introduce changes to that directory are removed from the history, potentially including the first commit to the repository.  Finally, the reflogs and backups are removed and the repository is compacted, leaving you with a small, single-project repository.</p>
<p>This script was written and tested using Git 1.5.6.5.  Feedback is welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2009/07/09/splitting-a-git-repository/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Git and Banshee.OpenVP fun</title>
		<link>http://www.chrishowie.com/2009/05/07/git-and-bansheeopenvp-fun/</link>
		<comments>http://www.chrishowie.com/2009/05/07/git-and-bansheeopenvp-fun/#comments</comments>
		<pubDate>Thu, 07 May 2009 21:43:39 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Banshee]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Computer]]></category>
		<category><![CDATA[OpenVP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=244</guid>
		<description><![CDATA[Well it&#8217;s hacking season again. With GNOME&#8217;s switch from Subversion to Git complete, which means Banshee now uses Git too, it gave me an excuse to finally learn it. This was not fun. But having toughed it out, I can definitely say that I love it. Now that Banshee is using Git, Aaron is starting [...]]]></description>
			<content:encoded><![CDATA[<p>Well it&#8217;s hacking season again.  With GNOME&#8217;s switch from Subversion to Git complete, which means <a href="http://mail.gnome.org/archives/banshee-list/2009-April/msg00120.html">Banshee now uses Git too</a>, it gave me an excuse to finally learn it.  <a href="http://twitter.com/cdhowie/status/1664570385">This was not fun.</a>  But having toughed it out, I can definitely say that <a href="http://twitter.com/cdhowie/status/1729199925">I love it</a>.</p>
<p>Now that Banshee is using Git, Aaron is starting work on a branch off of the 1.4 series to incorporate my visualization patch.  Wielding my new Git tool belt, I was off and hacking.  Taking Gabriel&#8217;s branch allowing replacement of source widgets, I rebased that from master to stable-vis, fixed the merge conflicts, and started hacking away at Banshee.OpenVP.  Unfortunately, not all the pieces I needed were there yet.  So I <a href="http://gitorious.org/projects/banshee/repos/cdhowie-clone/commits/327f490aa06f5af59b48d6a4832fd894f351fedb">added</a> <a href="http://gitorious.org/projects/banshee/repos/cdhowie-clone/commits/fd9cf79d6f8f57fabdd0ca58afb722c28756038c">them</a> and pushed them up to <a href="http://gitorious.org">Gitorious</a>.  Neat.</p>
<p>Now Banshee.OpenVP looks like this:
<div style="text-align:center;"><a href="http://picasaweb.google.com/lh/photo/xeCOFVbEQXnGVR0Qy5tU_A?feat=embedwebsite"><img src="http://lh5.ggpht.com/_1U-UwfPfZ6A/SgMW44Vm8cI/AAAAAAAAAe8/BWH-RTov3ZM/s400/Banshee.OpenVP%20-%20Now%20Playing%20integration.png" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2009/05/07/git-and-bansheeopenvp-fun/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>The new Gazebo: a Gtk# interface to FICS</title>
		<link>http://www.chrishowie.com/2009/04/17/the-new-gazebo-a-gtk-interface-to-fics/</link>
		<comments>http://www.chrishowie.com/2009/04/17/the-new-gazebo-a-gtk-interface-to-fics/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 19:33:25 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Chess]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.chrishowie.com/?p=231</guid>
		<description><![CDATA[I&#8217;ve abandoned my idea of creating an AJAX interface for the time being. It is a cool idea but I think I can do much better by writing a proper application. The Linux FICS interface scene is rather weak. eboard is about the best there is in terms of usability, and it has its share [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve abandoned my idea of creating an AJAX interface for the time being.  It is a cool idea but I think I can do much better by writing a proper application.</p>
<p>The Linux FICS interface scene is rather weak.  eboard is about the best there is in terms of usability, and it has its share of problems.  xboard is there for the minimalists who want their interface to provide a chess board only.  When compared with the powerful and extensible interfaces available for Windows it&#8217;s rather a shame there&#8217;s nothing similar for Linux.</p>
<p>So I&#8217;ve decided to take the name I was going to use for my web interface and apply it to a new Linux interface.  Building on Mono.Addins, I&#8217;ve already got an interface that can be extended in several key ways.  Addins can, for example, provide new text highlighting classes or classes that can manipulate the console text buffer in interesting ways.</p>
<p>Using a tip from jonp in #mono, I have now come up with an extensible preferences system that does not suck, based on XLinq.  Addins can simply subclass PreferenceContainer, slap on a few attributes, and they have an easy-to-use set of strongly typed preferences that get automatically serialized to XML.  Right now only the basic primitive types and strings are supported, but this will be extended later to include things like arrays, lists, and XLinq objects.  Thanks to Mono.Addins and some more support classes I wrote, addins can also provide Gtk# widgets that get embedded into the application preferences window.  By writing very little code, addin authors can persist their settings <i>and</i> provide the user with a convenient way to change them.</p>
<p>I&#8217;m still lacking a chess board though.  If anyone likes writing custom widgets and feels comfortable working on this project let me know.  The sources will be made public in the coming weeks after I&#8217;ve had a chance to polish them and set consistent style guidelines.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrishowie.com/2009/04/17/the-new-gazebo-a-gtk-interface-to-fics/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

