C++反思指南/教程
我发现无论如何很难找到在 C++ 中实现 Reflection 的方法。我只看过 AGM::LibReflection 和 Game Programming Gems 5 中的示例。有谁知道如何执行此操作的教程或不错的代码示例?
到目前为止,我知道 Boost/QT 的内置系统,但我不打算使用他们的系统(请不要妨碍这一点,我发现它臃肿,我想推出自己的系统,不要偏离主题)。
I am finding it difficult to find anyway to implement Reflection in C++. I have only seen examples from AGM::LibReflection and Game Programming Gems 5. Does anyone know of a tutorial or decent code sample for how to do this?
So far I know of Boost/QT's built in system, but I am not looking to use theirs (please do not hamper on this, I find it bloated and I want to roll my own, do not derail the topic).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
反射是检查程序属性的糟糕方法。您只能“反映”设计编译器的人将什么连接到反射机制中。这通常不是很多(您知道什么反射系统可以让您查看表达式内部?),这取决于语言。对于像 C++ 这样的东西,你试图在语言的顶部添加反射,本质上是作为 API 集,你将受到极大的限制,或者你将不得不以真正不自然的风格进行编码这实际上使您可以将反射数据声明为标准数据结构。
您可以改用程序转换引擎 (PTS)。这样的引擎操纵完整的程序表示,因此可以在技术上回答任何可回答的问题。特别是,它原则上可以回答你能想象到的所有反射问题,因为它充当编译器的替代者,并且可以看到编译器看到的一切。
(事实上,它能看到的比编译器看到的还要多;编译器一次只能看到一个编译单元,而一个好的 PTS 可以同时看到一组任意大的编译单元,因此可以回答有关整个集合的问题)。
我们的 DMS 软件重组工具包 可以解析完整的 C++(以及许多方言),构建AST 和准确的符号表。使用它,您可以实现任何静态可计算反射,然后使用它来生成分析结果或直接修改 AST。
DMS 与语言无关;它可以对多种语言执行此操作。
关于实际使用 DMS 进行“反射”:OP 想知道如何实现属性 getter 和 setter。使用 DMS 之类的 PTS,您可以解析感兴趣的类的源代码,然后遍历代码的 AST。对于类中的每个数据声明,您实际上可以通过构建表示 getter 代码的 AST 来为该数据创建一个 getter;对于 DMS 等工具,您可以通过编写 C++ 源代码模式来实现此目的,这些模式被解释为表示相应的 AST 片段,并带有可以用其他 AST 填充的占位符。然后,较小的转换可以修改原始 AST 以包含生成的 getter/setter;这会生成一个 AST,其中包含原始代码和生成的 getter/setter。最后一步是从 AST 重新生成代码,DMS 通过使用 AST 到源的漂亮打印机来实现,这些打印机是构成语言(例如 C++)前端的“域定义”(解析器、漂亮打印机、名称解析器)的一部分结尾。
Reflection is rotten way to inspect properties of a program. You can only "reflect" what the guy designing the compiler wired into the reflection machinery. That's usually not a lot (what reflection system do you know that will let you peer inside an expression?) and it depends on the language. And for something like C++, where you are trying to add reflection on top of the language essentially as set of APIs, you are going to be extremely limited, or you'll have to code in truly stilted style that lets you in effect declare the reflection data as standard data structures.
You could instead use a program transformation engine (PTS). Such an engine manipulates the complete program representation, and can thus technically answer any question which is answerable. In particular, it can in principle answer all the reflection questions you can imagine, because it acts as a substitute for the compiler, and can see everything the compiler sees.
(In fact, it can see more than the compiler sees; the compiler is limited to one compilation unit at a time, and a good PTS can see an arbitrarily big set of compilation units concurrently, and can thus answer questions about the set as a whole).
Our DMS Software Reengineering Toolkit can parse full (and many dialects of) C++, builts ASTs and accurate symbol tables. Using that, you can implement any static computable reflection, and then use that to produce analysis results or modify the ASTs directly.
DMS is language agnostic; it can do this for a large variety of languages.
Regarding actually using DMS to do "reflection": OP wanted to know how one might implement property getters and setters. With a PTS like DMS, you parse the source code of the class of interest, and then walk the AST for the code. For each data declaration inside the class, you literally manufacture a getter for that data, by building up the AST that represents the getter code; for tools like DMS, you can do this by composing patterns of C++ source code that are interpreted to represent the corresponding AST fragments, complete with placeholders you can fill in with other ASTs. Minor transformations can then modify the original AST to include the generated getters/setters; this produces an AST that contains the original code and the generated getters/setters. A final step is to regenerat code from the AST, which DMS does by using AST-to-source prettyprinters that are part of the "domain definition" (parser, prettyprinter, name resolver) that make up the language (e.g., C++) front end.
YMMV,但如果您正在寻找外部工具,还有 GCCXML (www.gccxml.org/) 和 OpenC++ (http://opencxx.sourceforge.net/)。标记库比比皆是......
YMMV, but there's GCCXML (www.gccxml.org/) and OpenC++ (http://opencxx.sourceforge.net/) if you're looking for an external tool. Mark-up libraries abound....