gpt4 book ai didi

c - 如何从结构定义生成函数?

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:11 25 4
gpt4 key购买 nike

我正在编写一个用于反序列化数据的库。我有这样的结构定义:

#ifndef SEARCHRESULTS_H
#define SEARCHRESULTS_H

typedef struct {
char *url;
} SearchResult;

typedef struct {
int count;
SearchResult *searchResults;
} SearchResults;

#endif

由于 C 缺少反射,所有函数都必须手动编写,但我相信应该有一个很好的方法来解析 header ,找到 struct 名称和字段,并生成函数 (伪 C):

#include "SearchResults.h"
#include "pdata_serialization.h"

void SearchResult_parse(pdata *data, SearchResult *obj) {
obj->url = strdup(data->values["url"]);
}

void SearchResult_free(SearchResult *obj) {
free(obj->url);
free(obj);
}

void SearchResults_parse(pdata *data, SearchResults *obj) {
obj->count = data->values["count"];
obj->searchResults = malloc(sizeof(SearchResult) * obj->count);
for (int i = 0; i < obj->count; i++)
SearchResult_parse(data->values["searchResults"][i], &obj->searchResults[i]);
}

void SearchResults_free(SearchResults *obj) {
for (int i = 0; i < obj->count; i++)
SearchResult_free(&obj->searchResults[i]);
free(obj);
}

我不是在寻找一个完整的解决方案,而是在寻找一个理智的、最小的想法和一个小例子。

最佳答案

GNU Autogen 解决了这个问题.我将 header 转换为定义文件,每个结构一个:

// SearchResult.def
AutoGen Definitions struct;
struct = {
type = "char *";
name = "url";
};

// SearchResults.def
AutoGen Definitions struct;
struct = {
type = "SearchResult *";
name = "searchResults";
};

并编写了以下不太漂亮的模板:

[= AutoGen5 Template h c =][=
(define model (string-substitute (def-file) ".def" ""))
=][=
CASE (suffix)
=][=
== h
=][=
(make-header-guard "")
=]

typedef struct _[= (. model) =] {[=
FOR struct =]
[=
(get "type")
=][=
(get "name")
=];[=
IF (and (*== (get "type") "*") (not (== (get "type") "char *"))) =]
size_t [= (get "name") =]Count;[=
ENDIF =][=
ENDFOR struct
=]
} [= (. model) =];

#endif /* [= (. header-guard) =] */
[=
== c
=]#include "[= (. header-file) =]"
#include "pdata_serialization.h"

void [= (. model) =]_parse(pdata *data, [= (. model) =] *obj) {[=
FOR struct =][=
IF (== (get "type") "char *")
=]
obj->[= (get "name") =] = strdup(data->values["[= (get "name") =]"]);[=
ELIF (*== (get "type") "*")
=]
obj->[= (get "name") =]Count = data->values["[= (get "name") =]Count"];
obj->[= (get "name") =] = malloc(sizeof([= (string-substitute (get "type") " *" "") =]) * obj->[= (get "name") =]Count);
for (size_t i = 0; i < obj->[= (get "name") =]Count; i++)
[= (string-substitute (get "type") " *" "") =]_parse(data->values["[= (get "name") =]"][i], &obj->[= (get "name") =][i]);[=
ENDIF
=][=
ENDFOR struct
=]
}

void [= (. model) =]_free([= (. model) =] *obj) {[=
FOR struct =][=
IF (== (get "type") "char *")
=]
free(obj->[= (get "name") =]);[=
ELIF (*== (get "type") "*")
=]
for (size_t i = 0; i < obj->[= (get "name") =]Count; i++)
[= (string-substitute (get "type") " *" "") =]_free(&obj->[= (get "name") =][i]);
free(obj->[= (get "name") =]);[=
ENDIF
=][=
ENDFOR struct
=]
free(obj);
}
[= ESAC =]

有效!

关于c - 如何从结构定义生成函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14565345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com