I've been fiddling with Template::Toolkit a bit lately and came up with
something odd and interesting (to me anyway).

I created a module called Stlgen.pm which uses templates to generate C
code. The C code implements a linked list. The perl code and the template
controls what records are in linked list elements (i.e. characters,
integers, unsigned longs, strings, whatever you want). It even supports
multiple records per element, such as "name" and "age" and "height" and
"weight". Each with their own types.

The templates handle sorting (with a default numeric compare that can be
overridden by the user in c code to be string compare or whatever they
want). It implements it with a merged sort, which has been confirmed to
work, at least with unsigned integers as an example.

The templates also support a "dumper" function, which will dump out
whatever records/types you decide to put in the linked list element.

Basically, it's the Standard Template Library (STL) container class for
linked lists, but instead of using the template functionality of C++,
Stlgen.pm uses the Template::Toolkit from perl and generates instances of
the class in plain C code.

I have no idea if its been done before, but it was an intriguing exercise
that piqued my interest and I decided to see if it was possible. Mostly it
turned into a learning exercise for Template::Toolkit and how to look at a
linked list in C in completely generic (templatable) terms.

Anyway, I just got it to the point where the sort function successfully
sorts a list and decided to archive a somewhere other than my harddrive so
I'd have a backup. And also introduce it to the public and see if this was
at all interesting to anyone else.

Like I said, I have no idea if someone already has some sort of solution
for having a configurable linked list in C or not. If I've reinvented the
wheel, then if nothing else, I learned how to do a merged-sort algorithm.

I just uploaded the tarball to cpan, it should show up here somewhere:

http://www.cpan.org/modules/by-authors/id/G/GS/GSLONDON/

It is an alpha version. I'm calling it rev 0.012. So it may have some
bugs. But it creates compilable c code and includes an example main.c
which fills a list with numbers, sorts it, and dumps the contents.

The Stlgen.pm module is coded in a fairly generic way such that other
container templates could be added to it and the same user interface would
work regardless of whether it was a linked list in C or a hash in C or
some other container in some language that doesn't support those
containers naitively.

If anyone does a lot of C coding would be willing to take a peek at my
module, generate a linked list, and play around with it a bit, I'd
appreciate any feedback. It is alpha, so don't put it in your
deliverables.

Greg



_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to