Macros

Since all of the structures here use a void pointer as their data type it would make sense to have a macro for it that turns the given in value into a void pointer. It could look something like this

#define T(v) (void*)v

(I just called it T since we use it as a replacement for the generic T)

And other macros for stuff like turning a void pointer to "string" could also be useful but I won't be discussing that here.


And since we have that we can carry on with that and use it for stuff like hashmap_put. So that we don't need to manually cast it to a void pointer. A example macro would look like this

#define HASHMAP_PUT(map, key, value) hashmap_put(map, key, T(value))

And here is a example piece of code where it shows off the usefulness of this example macro

hashmap* example = hashmap_create(10);
HASHMAP_PUT(example, "test", "Hello world");
HASHMAP_PUT(example, "test2", 2);

// Will all work without needing to manually cast it directly 

Last updated