如何动态地创建和使用C阅读结构? [英] How to dynamically create and read structs in C?
问题描述
我如何做这样的事情(只是一个例子):
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);
这样我可以加载并使用从外面一个结构实例(在地址 addressOfMyStruct
),这里的给定结构?
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.
我希望这是清楚我想做的事情。我知道, C /调用是能够做到这一点,但有一个独立的库这样做呢?
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?
推荐答案
其实证明了code键使用C这项工作是有点太涉及一个SO职位。但解释的基本概念是可行的。
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.
你真正创建这里是一个模板化的属性包系统。有一件事你需要大量的保持这个会是一些assiociative结构如哈希表。我说一起去的std ::地图,你所提到的,这是一个C唯一的解决办法。为了便于讨论,我只是将假设你有某种可用的哈希表。
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.
在create_struct的通话将需要返回,其中包含一个指向一个哈希表,这使得为const char *
来基本上是一个为size_t的结构。这张地图定义你为了创建结构的新实例所需要的。
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.
在insance的方法将主要创建具有同等数量的成员为模板哈希表的哈希表新。让我们扔掉懒惰evualation窗外一秒钟,并假设您创建的所有成员锋线。该方法将需要循环模板哈希表增加了对每个项目的成员,malloc'ing指定大小的内存块。
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.
instance_get_member的实施将根本就在地图的名称进行查找。虽然签名和使用模式需要改变,虽然。 C不支持模板,必须选择一个共同的返回类型,可以重新present的所有数据。在这种情况下,你需要选择无效*
因为这是内存将如何需要存储。
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.
void* instance_get_member(any_struct_instance* inst, const char* name);
您可以通过添加envil宏来模拟模板,让这个好一点
You can make this a bit better by adding an envil macro to simulate templates
#define instance_get_member2(inst, name, type) \
*((type*)instance_get_member((inst),(name)))
...
int i = instance_get_member2(pInst,"a", int);
这篇关于如何动态地创建和使用C阅读结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!