3 comments

[ 3.9 ms ] story [ 16.6 ms ] thread
Hi HN, this is a C container library I've been working on for a while. I've spent a long time trying to figure out the best way to make generic containers in C and this is what I've come up with.

Most container libraries in C are either weakly typed (with void pointers and function pointers) or they are heavy on macros (expanding to huge blocks of code wrapped in "do {} while(0)".) Pottery is different: it uses include files and renaming macros to create strongly typed composable templates. The templated code in Pottery looks sort of like normal C, and it preprocesses into ordinary C functions similar to what you would write by hand.

There's a bunch of stuff in the examples to demonstrate Pottery: a toy chess engine, yet another string library, etc. I'd love some feedback on it so please have a look or try it out and let me know what you think.

Looks great, is there a way to shorten those MACROs, a little too verbose/lengthy for readability, it's probably just me.
Not really. I originally used just a P_ prefix instead of POTTERY_ and tried to abbreviate some things (e.g. ITREE/ILIST for intrusive containers, etc.) I found it just made things ambiguous and required remembering more stuff (for example is ITREE a red-black tree or a weight-balanced tree? is it a map or a tree array?) I imagine a bunch of abbreviations would be less approachable for people that encounter it in a codebase without having seen Pottery before.

Thankfully it only affects the instantiation of the template. When you use the resulting code, it's only as verbose as your name for it. You can call, say, an int vector "iv"; the type will be iv_t and functions will be iv_init(), iv_destroy(), etc.

As for Pottery's internals, they're pretty verbose everywhere unfortunately. I did abbreviate a few things (like "open_hash_map" is "ohm" internally) but in the end I just decided to embrace the verbosity.