C++ 中对 C 数据结构的通用访问通过某种形式的反思?

发布于 2024-12-27 13:07:21 字数 439 浏览 2 评论 0原文

是否有一个反射框架用于以通用方式访问 C/C++ 数据结构(嵌套结构、指针数组、各种其他基本数据类型)?

如果没有,有什么建议可以最好地解决这个问题吗?

我正在为 大型 MS Windows C API 编写测试软件,其中包含数十到数百个不同的结构体,其中一些包含指向结构体的嵌套指针数组,最多可达多个级别。

对于测试,需要根据以字符串形式给出的测试描述来预设数据结构的值并随后进行检查。目前,这是以一种相当扁平和乏味(并且不一致且容易出错)的方式编程的,我想改变这种方式。

我正在考虑使用一些用于所有数据类型构造的模板,这些模板允许通常通过某种路径访问数据结构中的值。

我不需要完全自动化,为结构中的每个特定字段做一些工作(比如一两行代码)就可以了。

Is there a reflection framework for accessing C/C++ data structures (nested structs, arrays of pointers, various other basic data types) in a generic way?

If not, any hints to best approach this?

I'm writing test software for a large MS Windows C API with tens to hundreds of different structs, some of which contain nested array of pointers to structs up to several levels.

For the tests the data structures need to be preset with values and checked afterwards, according to test descriptions given as strings. Currently this is programmed in a rather flat and tedious (and inconsistent and error-prone) way, which I'd like to change.

I'm thinking of some templates for all the data type constructions used that allow to access the values in the data structures generically via some kind of paths.

I don't need this to be fully automated, it would be OK to do some work (say one or two lines of code) for each specific field in a struct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2025-01-03 13:07:21

您正在描述一种称为反射的功能,C/C++ 不支持该功能。

在 C++ 中执行此操作的方法是解析源文件,然后生成更多源代码来执行所需的操作,例如查找每个类和嵌套类、查找每个成员函数等。

编写 C++ 解析器很困难,但有许多现有的免费实现可用。

You're describing a feature called reflection, which C/C++ don't support.

The way to do this in C++ would be to parse the source files and then generate more source code to perform the operations you require, e.g. find each class, and nested classes, find each member function, etc.

Writing a C++ parser is hard but there are lots of existing free implementations available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文