I opened the front cover of Design Patterns...

posted Feb 10, 2011, 9:14 PM by Tim Rodriguez   [ updated Mar 26, 2011, 10:07 AM ]

    ...and wrote a prototype framework that allows for this:

#include <cake/Cake.h>

    int x1,
    int x2,
    int y1,
    int y2
    (int, area,()),
    (int, perimeter,())
int $def_object_method(Rect, area) {
    return $abs((self->x2 - self->x1)*(self->y2 - self->y1));
int $def_object_method(Rect, perimeter) {
    return 2*$abs(self->x2 - self->x1) + 2*$abs(self->y2 - self->y1);

$main() {
    Rect my_r = $new(Rect);
    my_r->x1 = 0;
    my_r->x2 = 3;
    my_r->y1 = 0;
    my_r->y2 = 3;

    printf( "%d\n", $(my_r, area));
    return 0;

/* Output:

    And this:

/* In file "my_template.h" */
#ifdef MY_TPL_TYPE_1
#ifdef MY_TPL_TYPE_2

#include <cake/headers/utilities/template_defs.h>

#include <cake/headers/utilities/nested_template_prepare.h>
#include "my_nested_template.h"


    MY_TPL_TYPE_2 b
    (void, hello,())
void $def_template_method(Example, hello) {
    $cake_assert(self != NULLelse { return; }
    printf("Hello World from %s: %c!\n", $stringify(EXAMPLE), self->b);
    if (self->a) { $(self->a, hello) };

EXAMPLE $def_template_method(Example, init, size_t n, ...) {
    va_args args;
    va_start(args, n);
    $cake_assert(self != NULL) else { return NULL; }
    $unless(self->a != NULL) {
      self->a = $new(NESTED_EXAMPLE);
    if (n == 1) {
      self->b = va_arg(args, MY_TPL_TYPE_2);
    return self;

#include <cake/headers/utilities/template_undefs.h>

#undef EXAMPLE
#undef MY_TPL_TYPE_2
#undef MY_TPL_TYPE_1


/* In file "my_nested_template.h" */

#include <cake/headers/utilities/template_defs.h>

    (void, hello,())
void $def_template_method(Nested_Example, hello) {
    printf("Hello World from %s!\n",

#include <cake/headers/utilities/template_undefs.h>



/* In file "main.c" */
#include <cake/Cake.h>

#define MY_TPL_TYPE_1 int
#define MY_TPL_TYPE_2 char
#include "my_template.h"

$main() {
    Example_int_char e = $new(Example_int_char, 'c');

    $(e, hello);

    return 0;

/* Output:
   Hello World from Example_int_char_: c!
   Hello World from Nested_Example_int_!

    This framework won't be included in (and thus, won't delay) the initial release, v0.0.1. It is my hope to convert the code that I have written over to this framework by the full v0.1 release, so that people can create their own objects and use the preprocessing tools that I have provided. In order to get the full benefit, however, v0.2 will implement Interfaces, which will allow the developer to inform Cake of what additional preprocessor functions can be used on custom objects and templates.

GCC Limitations

posted Jul 20, 2010, 1:05 AM by Tim Rodriguez   [ updated Jul 20, 2010, 1:25 AM ]

1) No __attribute__ ((selectany)) support for non-Microsoft targets. While this isn't a necessity for Cake, it will remove the hassle of multiply-included templates, and simplify the use of Tuples.

2) No declarations of the form int i, long j.  Inclusion of this into the grammar as an extension would allow more than one variable to be declared for the scope of a for-loop, reducing overhead for things like $foreach and $map.

3) __builtin_choose_expr(x,y,z) currently will only evaluate either y or z for side-effects (based on the statically-decidable expression x), but at the moment will generate syntax errors for both y AND z. If it did not generate syntax errors, it would allow for greater flexibility of functions without defining nearly identical clones.

4) Minor inconvenience, but easily worked around: __builtin_classify_type(x) currently classifies arrays and pointers as the same thing although each have their own designated category.

    Cake and the developers who use it will benefit greatly from improving 1-3. I'm considering tinkering in the GCC source and patching them myself, because I imagine that these aren't exactly the highest priority fixes for the GCC team.

Working With the GCC Preprocessor

posted Jun 29, 2010, 5:13 PM by Tim Rodriguez   [ updated Jul 2, 2010, 4:21 PM ]

    GCC's CPP is astoundingly powerful. When at first I believed I needed recursive macros, I have discovered today that I actually do not. I'm only limited by the implementation limits of CPP (only 65,536 arguments to a macro, and no theoretical limit for line length, as long as you don't care about debug output). I wonder if the creators ever foresaw some of the metaprogramming capabilities that I'm discovering. While type safety is a key goal of mine, I must admit some disdain for how unsafe the "internal" macros are. It is highly recommended that anyone wishing to add in complicated functionality to Cake's macros correspond with me first.

    As of this post, the $map(iterable, function, iterable, ...) macro is working, with many more functional programming methods to follow. These macros should be much easier to swallow for the average developer than the Boost Preprocessor Library's (powerful though they may be). My hope is to make version 0.1 available by September this year.

Project Site Launched

posted Jun 3, 2010, 1:43 AM by Tim Rodriguez

    Finally, one centralized, public place to summarize the projects that I work on. I'll eventually make my own site, but for now, Google Site templates will suffice. There are currently only 2 project pages readily accessible: Cake, and Tabula Rasa; others will be added later.

    If you have any questions, feel free to e-mail me.

1-4 of 4