A
Andrew Poelstra
I have a simple dictionary (right now it is implemented as a
linked list, which is more than fine for its current usage),
which accepts generic objects via void pointers, as well as
a delete function of the form void()(void *).
To make a dictionary I have:
typedef void (delete)(void *);
Dictionary *dict_new(const char *name, dict_deleter delete);
For simple objects, I might then create a dictionary and
simply give it free() as the delete function. This is a
generic container but I still only use each dictionary
for a single kind of object, so this works and makes my
dict_delete() function a lot cleaner, since it knows how
to delete all of its objects.
For dictionaries of more complex objects, though, I might
make my own delete function like:
void delete_token(Token *)
The problem is that this does not convert to
void delete_token(void *)
automatically. So either I have to cast my delete_token()
function when passing it to my dict_new() function, or
define it as taking a void* in the first place, which is
Bad because it is designed to only handle Token objects.
Neither of those are very nice solutions, IMHO. What do
you guys recommend?
linked list, which is more than fine for its current usage),
which accepts generic objects via void pointers, as well as
a delete function of the form void()(void *).
To make a dictionary I have:
typedef void (delete)(void *);
Dictionary *dict_new(const char *name, dict_deleter delete);
For simple objects, I might then create a dictionary and
simply give it free() as the delete function. This is a
generic container but I still only use each dictionary
for a single kind of object, so this works and makes my
dict_delete() function a lot cleaner, since it knows how
to delete all of its objects.
For dictionaries of more complex objects, though, I might
make my own delete function like:
void delete_token(Token *)
The problem is that this does not convert to
void delete_token(void *)
automatically. So either I have to cast my delete_token()
function when passing it to my dict_new() function, or
define it as taking a void* in the first place, which is
Bad because it is designed to only handle Token objects.
Neither of those are very nice solutions, IMHO. What do
you guys recommend?