You are here: Home

Ruby on Rails provides an elegant and speedy framework for developing web applications.

But having a framework isn’t the only requirement for building a successful software product. More infrastructure is required. Like sensible revision control for distributed development. If you suspected that it isn’t necessary to archive all that boilerplate associated with a Rails project… you’re right. You don’t need to archive it. Which means building your subversion repository is a little more involved than svn add myrubydir/*. If you do that, you’re going to be committed megabytes of irrelevant log files.

[Note: I moved to Git and Github before finishing this article, but these links may yet be of interest to developers using Subversion.]

Links for in-depth information

If you’re already well-versed in subversion, none of this should have been a real stretch, just picking over the details of Rails. These were two of my primary sources of information for Ruby on Rails specific material:

{ 0 comments }

Here’s another installment on Object Oriented C programming. This time, we’re going to take a look at how inheritance allows you to simulate a generic function courtesy of the C preprocessor. If you have ever wondered how those spiffy objects in other programming languages always get “printed,” here’s one way to do it.

First, let’s write a little code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include < stdio .h>
 
typedef struct _object Object;
 
#ifndef OOCP_PRINTFUNC
typedef int  (*PrintFunc) (void * stream,
                                        const char * format,
                                        ...); 
#define OOCP_PRINTFUNC
#endif 
 
#ifndef OOCP_PRINTER 
typedef void (*Printer)   (Object * o,
                                       PrintFunc print_func,
                                       void * stream);
#define OOCP_PRINTER
#endif
 
#define object_print(o,p,s)   ((Object*)o)->print ((Object*)o,(PrintFunc)p,s);
 
struct _object {
  Printer print;
};
 
typedef struct _thing {
  Object o;
  int value;
} Thing;
 
void
thing_printer(Object * o, PrintFunc print, void * stream) {
 
  Thing * t = (Thing *)o;
  print(stream, "Value from thing_printer: %d\n", t->value);
}
 
int
main(int atgc, char ** argv) {
 
  Thing t;
  t.o.print = thing_printer;
  t.value = 1;
 
  object_print(&t, fprintf, stdout);
 
  return 0;
}

Ok, some 50 lines of code. Doesn’t look like much, but if you’re new to C, and haven’t been following along, rest assured: we’re starting to flex some muscle now.

That is, there’s a lot going on in this code.

Let’s start at the top:

3
4
5
6
7
8
9
10
typedef struct _object Object;
 
#ifndef OOCP_PRINTFUNC
typedef int  (*PrintFunc) (void * stream,
                                        const char * format,
                                        ...); 
#define OOCP_PRINTFUNC
#endif
  • Line 3 is familiar, but we would normally expect this in a header file rather than a source file. We’re keeping the typedef here to keep the listing simple.
  • But what’s this typedef on Line 6? And why these ugly #defines on lines 5, 9 and 10?

Easy stuff first. The #defines protect your code from a compiler redefinition error. The idea is you want to define something in one place only, and have that thing (whatever it is) defined throughout your program. Remember, we’re dealing with strong typing, and a compiled language. Interpreted languages, Ruby or PHP for example, have little limitation on how your data may be defined.

In those languages, if you want to declare something a function in one line of code, then that same thing as an array in another line of code, that’s your business.

But not with C.

Line 6 gets into the meat. Recall we typedef’ed structs in header files to create hidden implementations. The type checker just needs the declaration to compile properly, the implementation can be anywhere within the typedef’s scope. Here, we’re using typedef to create a pointer to type of function, which is called PrintFunc.

If you think the prototype looks familiar, perhap similar to functions defined in stdio.h, you’re thinking correctly.

Let’s continue…

12
13
14
15
16
17
18
19
20
21
22
23
#ifndef OOCP_PRINTER 
typedef void (*Printer)   (Object * o,
                                       PrintFunc print_func,
                                       void * stream);
#define OOCP_PRINTER
#endif
 
#define object_print(o,p,s)   ((Object*)o)->print ((Object*)o,(PrintFunc)p,s);
 
struct _object {
  Printer print;
};

With Line 13, we’re getting a bit more esoteric. Now we’re building our typedef’ed function from our own derived types instead of C’s primitives, or from types defined in the standard library. We’ve seen this stuff before, though, even if just a few lines before in the case of PrintFunc.

Now, any function which prints and takes the same arguments as Printer can be assigned to a Printer variable. More importantly, such functions can be assigned to member variables in any struct declaring a Printer member.

But line 19 is whackadoodle.

It’s a macro. Not a function.

Recall in Java you have Object.tostring? This is pretty close to that.

We now have all the tools we need, so let’s put it together.

25
26
27
28
29
30
31
32
33
34
35
typedef struct _thing {
  Object o;
  int value;
} Thing;
 
void
thing_printer(Object * o, PrintFunc print, void * stream) {
 
  Thing * t = (Thing *)o;
  print(stream, "Value from thing_printer: %d\n", t->value);
}

We’ve got our Thing (Line 25), and a way to print (Line 31) our Thing. Life is good.

Now let’s use our Thing:

37
38
39
40
41
42
43
44
45
46
47
int
main(int atgc, char ** argv) {
 
  Thing t;
  t.o.print = thing_printer;
  t.value = 1;
 
  object_print(&t, fprintf, stdout);
 
  return 0;
}

And there you have it, on Line 44: print that Thing out using it’s very own print “method.”

Exercise: Instead of declaring a Thing t, declare a Thing * t. Allocate its memory, print it, then remember to deallocate the memory afterwards. Run the program through valgrind to check your memory usage. You should have some memory left on the heap at the end of runtime, but nothing left over (no leaks).

The usual caveats…

You won’t want to use object_print for everything, but you might find that that a printing function defined in the same way works in a deeper class hierarchy. Suppose you have a Matrix class, which is the child of Object, and has children representing different kinds of matrices. In this case, having a matrix_print might be just the ticket.

Remember: object oriented anything is not a silver bullet for slaying design dragons. The simple framework we’re developing here is just that, simple. Instead of trying to bend your problem into this framework, use these concepts to create a framework that works best for your project.

Here’s some related material on function pointers:

  • The Function Pointer Tutorials: An excellent followup by Lars Haendel going into considerably more detail than what’s written here.
  • Declaring, Assigning, and Using Function Pointers: This is a classic piece of exposition on C, written by Steve Summit. I recall Steve from way back in Usenet days, before the World Wide Web. He’s the real deal and it’s worth studying this article in detail.

Stay tuned for an article specifically on how to typedef function pointers.

{ 0 comments }

Single Inheritance In C Programming Language I: Nesting structures

March 5, 2011

The C programming language lends itself well to single inheritance object-oriented schemes, provided a little care is taken in the class structure. Consider the following definition: 1 2 3 typedef struct _object { int foo; } Object; 1 2 3 4 typedef struct _thing { Object o; double bar; } Thing; Right here, I know [...]

0 comments Read the full article →

PHP on Snow Leopard: 5 ways to win

February 23, 2011

There’s at least five ways to install PHP on Macosx 10.6 Snow Leopard. Probably more. This becomes important when you as a developer want to add some code or a module to your application. You need to know: Where to install said module. How to configure the appropriate runtime to find and execute said module. [...]

0 comments Read the full article →

Turning down business: painful but sometimes necessary

January 17, 2011

Turning down business can be painful, but sometimes it’s the only way forward. I’ve recently had to turn down some business myself. I took on a project which didn’t quite feel right, and the deeper I got into it, the more I realized it just wasn’t the right project for me. Which really sucked because [...]

6 comments Read the full article →

Log your work: Using Trac to untangle unclear tasks

January 11, 2011

There’s an interesting hole in the task management and productivity literature. The situation is when you know more or less what needs to be done, but not exactly how to do it, and from where you’re starting, it’s not clear in what order and how long each particular task will be. And the whole thing [...]

2 comments Read the full article →

Simple Object Oriented Unit Testing For C Programming

January 3, 2011

Remember that giant blast of wind a few months ago? The wind that launched all my cool plants down the stairs? Well, here’s a picture of the re-potted collection. That’s about half of them, the other half are still languishing. Crassulae are tough as nails. I may re-pot the remaining… but these need to be [...]

3 comments Read the full article →

Rails 3 and Cucumber: getting started with outside-in testing

December 17, 2010

[Update July 16, 2001: This article is ranking very high on Google for "WordPress cucumber," a phrase I was searching for to see if anyone has developed Cucumber features and a test harness for WordPress. Turns out there is a Gem for it. If you are looking to test WordPress with Cucumber, please email me [...]

2 comments Read the full article →

Practical Git Tip #1: Handling differing repository and local names

October 29, 2010

Having repositories named uniquely and checked out into different locations under the same name helps with WordPress themes. Git is an excellent tool, but it was designed for a particular type of development practice, where every developer is working with the entire tree which is checked out into a dedicated local repository. In some cases, [...]

1 comment Read the full article →

Tracking Smith Barney investment accounts in Quickbooks

September 22, 2010

Short answer: Smith Barney’s statements are confusing. Longer answer… here’s what’s in progress: Created the following structure: 7410 Investment income (Other Income account) 7411 Long term gain 7412 Short term gain This solves part of the problem. I may need to create accounts for holding stock purchase, money funds, CDs, etc. I think each of [...]

0 comments Read the full article →