Function Delegate (C++)

In this code sample, the problem I solved (that sounds sort of pretentious, how about "attempted to solve") is that C++ has no function objects like C#, Lua or ActionScript. So I created the Delegate class, which can hold on to either member methods or global functions. It's typeless, so the end-user doesn't need to know the class or method that will be called, but type safe, so the end-user will get a run-time error if they pass in incorrectly typed parameters.

I became quite familiar with C++ Template Metaprogramming through this exercise, and it was a really cool thing to learn. I use it heavily throughout the project. Here's what each of the files/classes does:

  • Assert (in Assert.h and Assert.cpp) - Simple, just a more robust assert than the standard one.
  • SmartPointer (in SmartPointer.h, SmartPointer.inl and SmartPointer.cpp) - This class is a typical reference counted pointer and is used for the DelayedDelegate.
  • FunctionTraits.h and FunctionCast.h - These contain many metaprogramming structs to use to remove or add type modifiers (like const, pointers and such) as well as metamethods to convert function pointers to generic function pointers.
  • Delegate (in Delegate.h, Delegate.inl and Delegate.cpp) - This is the meat and potatoes (or brains) of the whole operation. The class comment shows a nice way of how to use this class. It leverages the structs FunctionTraits and FunctionCast a lot. Pretty much the basic idea of the delegate is to store a pointer to the object, a pointer to the function and then data on what types need to be passed to that particular function. Then the function is invoked, the types are checked and then the parameters are passed to the object and function pointer.
  • DelayedDelegate (in DelayedDelegate.h, DelayedDelegate.inl and DelayedDelegate.cpp) - This class allows the binder of the function delegate to set the arguments it will be invoked with at bind time. Then the invoker just has to invoke and the function will be called with the arguments set by the bind.

Component/Object Model (ActionScript 3.0)

An implementation of a Component/Object Model game engine core. I feel that actionscript, with it's anonymous function closures and dynamic objects, is an ideal language for this sort of engine.

String (C++)

A very simple string class (non-templatized). Just an example of my typical code style and quality.