Content

Not quite a Yegge long.

An impossible primitive for databinding

Tuesday 23 March 2010 - Filed under Uncategorized

Something that often comes up in UI programming is how to bind UI controls to backend data without writing a pile of code – and preferably without using reflection. Here’s an attempt (thwarted by the compiler, unfortunately) at capturing a C# ref parameter in closures so it can be passed around, stored, etc. It seems reasonable, and even useful, at a first glance:

class Binding<T>
{
    Action<T> SetValue;
    Func<T> GetValue;

    public T Value
    {
        get { return GetValue(); }
        set { SetValue(value); }
    }

    public Binding(ref T t)
    {
        SetValue = u => t = u;    // not allowed.
        GetValue = () => t;       // not allowed.
    }
}

Turns out that the compiler refuses to lift ref or out parameters into a closure – for good reason. If we were to allow this, then we’d have to rewrite the callers of Binding.ctor to lift their arbitrary ref expressions into the closure. That’s too much of a contortion, especially when those callers could be in another assembly…

So there you go: an interesting approach that doesn’t work.

 ::  Share or discuss  ::  2010-03-23  ::  admin

C macros: You can’t pre-lex this.

Wednesday 17 March 2010 - Filed under Uncategorized

For those of us who try to build our own C compilers, preprocessor macros are a significant stumbling-block, especially for standard C, where macros are token-oriented, rather than text-oriented.

The (old) naive approach is to just do a dirty text substitution, and then lex the result normally. This appears(!) to be what several production compilers actually do (MSVC, etc). Unfortunately, this gets really messy if you want to be fully compliant with the standard, since it’s really trying to force a text-centric substitution to pretend to be token-centric. The edge cases suck.

The (new) naive approach is to pre-lex the RHS of the macros (as far as pp_tokens). Unfortunately, it turns out *that* doesn’t work either, since the sequence of tokens depends on where it’s expanded. Here’s a quick example that’s sure to get you fired if you did it in production code, but the standard allows it:

#define FAIL_MACRO <stdio.h>

// here, it’s a header-name
#include FAIL_MACRO

// here, it’s a token sequence making up part of an expression
int main( void )
{
        struct { int h; } stdio = { 5 };
        return (2 FAIL_MACRO 3);
}

Contrived, but it’s valid. You have to know the context in order to correctly lex the RHS of a macro. Why am I messing around with C compilation again?

 ::  Share or discuss  ::  2010-03-17  ::  admin

The lameness of FizzBuzz

Tuesday 16 March 2010 - Filed under Uncategorized

I got sufficiently bored today that I decided to try out FizzBuzz. This is a common monkey-filter used to determine whether people can code at all. It’s stupidly trivial, and yet many supposed “programmers” can’t hack it. Here’s a quick (30 seconds) implementation in C:

#include <stdio.h>

char const * fmts[] = { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" };

int main( void )
{
        int i;
        for( i = 1; i <= 100; i++ )
                printf( fmts[ !(i%3) | (!(i%5)<<1) ], i );
}

1 comment  ::  Share or discuss  ::  2010-03-16  ::  admin

OpenRA gets C&C support

Saturday 27 February 2010 - Filed under Uncategorized

Thanks to Paul Chote (github/pchote) we’ve got support for the original Command & Conquer being added to OpenRA as well. Here’s an early screenshot for the Nod side. Most things work, except for Ore to Tiberium conversion:

openra-cnc

 ::  Share or discuss  ::  2010-02-27  ::  admin

Knitted TF2 Scout

Saturday 13 February 2010 - Filed under Uncategorized

 

048

1 comment  ::  Share or discuss  ::  2010-02-13  ::  admin

I need a beer glass.

Sunday 17 January 2010 - Filed under Uncategorized

IMG_0989[1]

An odd combination, but the beer is rather good.

 ::  Share or discuss  ::  2010-01-17  ::  admin

First flying lesson

Sunday 17 January 2010 - Filed under Uncategorized

My wife got me flying lessons for Christmas; I had my first flight today. Flew (more or less) one loop of the traffic pattern for Feilding aerodrome, with a few extra turns to get used to the controls.

IMG_0973 Me with ZK-TOD, a Cessna 152, before doing the preflight checklist.

IMG_0975 Taxiing out to the runway. It’s… interesting, driving with your feet.

IMG_0978Nice takeoff, eastward toward the ranges.
Can’t see much because of the cloud.

IMG_0981I definitely didn’t taxi onto the grass a bit here. Hmmn.

IMG_0985… And back to where we started.

Was awesome, I’m definitely hooked. Back again on Saturday for another lesson.

 ::  Share or discuss  ::  2010-01-17  ::  admin

Red Alert Cursors, so we don’t lose them again…

Sunday 3 January 2010 - Filed under Uncategorized

Here’s a capture from a hacked OpenRA build:

ra-cursors

 ::  Share or discuss  ::  2010-01-03  ::  admin

Using Github with MsysGit

Saturday 19 December 2009 - Filed under Uncategorized

I’ve been happily using Git for all my personal projects for the last couple of months, and everything is great – except for the performance of Github for pulling and pushing. I’ll outline here the steps I took to fix it.

First, I noticed that only the authenticated access to Github is pathetically slow. Pulling from the public URI is fast. I tried exploiting this by adding a read-only copy of each remote:

$ git remote add origin-ro -f git://github.com/chrisforbes/OpenRA.git

Then I just use the appropriate remote for pulls and pushes:

$ git pull origin-ro master                 # grab upstream changes
$ git push origin master                    # push changes back

Now this works, but it has some undesirable side-effects. For example, one of my projects is fairly active, with others contributing to the upstream Github repo. This is normally fine, except that git doesn’t know the two remotes are actually the same repo (how could it?) so they get their heads out of sync. I find myself having to do this several times a day to get a sane view of upstream:

$ git fetch origin-ro

That’s not ideal, so I did a bit of digging around, and noticed interesting output from the git-remote command:

$ git remote –v
... snip ...
origin  git@github.com:chrisforbes/OpenRA.git (fetch)
origin  git@github.com:chrisforbes/OpenRA.git (push)

This seems to suggest that you can set different fetch & push URLs. Yet the git-remote manual page says nothing about this – in fact, it’s pretty clear that there are two config options that control this stuff. Unhelpful.

It turns out that if you read the git-push man page, or the git-config man page (which is useful, but incredibly long), there’s another option: remote.<remote-name>.pushurl. Time to fix this mess:

$ git config remote.origin.url git://github.com/chrisforbes/OpenRA.git
$ git config remote.origin.pushurl git@github.com:chrisforbes/OpenRA.git
$ git remote rm origin-ro

... repeat for every other remote i have ...

So we’ve switched the fetch URL over to use anonymous access, and set pushurl to something that allows writing. Because it’s all within the same remote now, git (esp gitk, which is very dumb!) doesn’t get confused.

Hopefully this is useful to someone :D

 ::  Share or discuss  ::  2009-12-19  ::  admin

Climate Change – What Are We Aiming For?

Monday 14 December 2009 - Filed under Uncategorized

While we conveniently don’t often hear of opinions other than the shrill mainstream environmental activist’s (and now politician’s) cry of “oh crap, the climate is changing and it’s our fault for, uh, breathing and driving cars and stuff, so, uh, pay this new tax” it should be plainly obvious that the Earth’s climate has not historically been stable – long before the human population was anywhere near large enough to have any impact whatsoever, no matter how outlandish a model of the impact of human CO2 production you might use.

There have been considerably warmer times, and there have been considerably cooler times, and these variations are clearly perfectly natural. This is true independent of whether the current round of political scaremongering is backed up by correct theory or not.

So my question, to which I have never heard an adequate answer from anyone is this:

Given that the temperature record shows substantial ongoing changes in the absence of significant human tampering, what is the goal? If we were to jump wholeheartedly on board the anti-climate-change bandwagon, what rate of residual change after removing our contributions is an acceptable natural level?”

(more…)

 ::  Share or discuss  ::  2009-12-14  ::  admin