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

时间:2013-01-28 15:29:33

标签: c parsing code-generation deserialization

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

#ifndef SEARCHRESULTS_H
#define SEARCHRESULTS_H

typedef struct {
    char *url;
} SearchResult;

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

#endif

由于C缺少反射,所有函数都必须手动编写,但我相信应该有一种很好的方法来解析标题,找到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);
}

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

2 个答案:

答案 0 :(得分:1)

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

// 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 =]

有效!

答案 1 :(得分:0)

我建议将解析后的数据作为键值对存储在hash table中,以便您查找实体。

当然,您必须针对要求的属性不可用的情况采取措施(例如,缺少url属性)。根据您在哈希表中存储信息的方式,还存在类型安全性问题。但也许值得将键值对存储为字符串,并在需要时将它们转换为数字或其他。