How can I do something like that (just an example):
any_struct *my_struct = create_struct(); add_struct_member(my_struct, 'a', int_member); add_struct_member(my_struct, 'b', float_member);
So that I could load and use a struct instance ‘from the outside’ (at the address addressOfMyStruct) with the given structure here?
any_struct_instance *instance = instance(my_struct, addressOfMyStruct); int a = instance_get_member(instance, 'a'); float b = instance_get_member(instance, 'b');
I would also like to be able to create struct instances dynamically this way.
I hope it’s clear what I want to do. I know that C/Invoke is able to do it, but is there a separate library to do that?
Actually demonstrating the code to make this work in C is a bit too involved for an SO post. But explaining the basic concept is doable.
What you’re really creating here is a templated property bag system. The one thing you’ll need a lot of to keep this going is some assiociative structure like a hash table. I’d say go with std::map but you mentioned this was a C only solution. For the sake of discussion I’m just going to assume you have some sort of hashtable available.
The ‘create_struct’ call will need to return a structure which contains a pointer to a hashtable which makes
const char*to essentially a size_t. This map defines what you need in order to create a new instance of the struct.The ‘insance’ method will essentially create a new hashtable with equal number of members as the template hashtable. Lets throw lazy evualation out the window for a second and assume you create all members up front. The method will need to loop over the template hashtable adding a member for every entry and malloc’ing a memory chunk of the specified size.
The implementation of instance_get_member will simply do a lookup in the map by name. The signature though and usage pattern will need to change though. C does not support templates and must chose a common return type that can represent all data. In this case you’ll need to chose
void*since that’s how the memory will need to be stored.You can make this a bit better by adding an envil macro to simulate templates