Richard's coding blog

My favourite C++11 thing so far!

See http://akrzemi1.wordpress.com/2012/03/19/meta-functions-in-c11/ for more details:

constexpr int factorial(int i) {
    return i == 0 ? 1 : i * factorial(i - 1);
}

constexpr int safe_factorial(int i) {
    return i < 0 ? throw int() : factorial(i);
}

int main() {
    constexpr int i = safe_factorial(-2);
    return i;
}

This kind of thing gets me excited!

Getting DHCP configuration information from Windows Store apps

I was recently asked via e-mail from a user of my Lanscan app (http://lanscan.rcook.org/) how to obtain DHCP information from a Windows Store app.

Fortunately, DhcpRequestParams and related Win32 APIs are part of the Windows Store partition of the Win32 API and so are callable from C#-based Windows Store apps (and probably other places too).

I thought I'd give Pastebin a try and so here are the links to the relevant p/invoke and interop declarations you'll need in order to call these functions from your C# program:

Living the dream, starting tomorrow...

Writing compilers for a living starting tomorrow. Huzzah!

Last day at Microsoft

Friday 5 April 2013 was my last day in Engineering Excellence and at Microsoft. On Monday 15 April, I start at Coverity. Woot!

Test posting

Posted on 5 September 2011 at 11:23am Pacific time.

Fun with contracts and anonymous delegates

I officially blew my own mind today. I discovered System.Diagnostics.Contracts and inline declaration and invocation of anonymous delegates all in the same day...

namespace FunWithContracts
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;

    internal static class Program
    {
        private static void MySort<T>(T[] array, int index, int length, IComparer<T> comparer)
        {
            Contract.Requires<ArgumentNullException>(array != null);

Binding "this" in JavaScript: I finally get it!

var obj = {
  name: "Richard",
  callback: function(event) {
    print("event = " + event);
    print("name  = " + this.name);
  }
};
var element = {
  name: "Element",
  callback: null,
  invoke: function() {
    print("invoke");
    if (this.callback != null) {
      this.callback("hello");
    }
  }
};
function bind(context, func) {
  return function() {
    return func.apply(context, arguments);
  }
}
element.callback = bind(obj, obj.callback);
element.invoke();

Simulating packet loss with iptables/ipfw

I need to simulate packet loss for a little programming assignment I'm working on which involves implementing a multicast chat application using UDP sockets. I need to build reliability into the system (sequence numbers and acknowledgements etc.) and thought it might be sensible to test the behaviour of my program under WAN-style network conditions. Originally I was just going to randomly drop packets in my server, but then I thought to myself that there ought to be an OS-level way of doing it. Indeed, there is and here is my bash script that demonstrates it.

Syndicate content