C ++反射指南/教程 [英] C++ Reflection Guide/Tutorials

查看:147
本文介绍了C ++反射指南/教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难找到在C ++中实现反射。我只看到了从AGM :: LibReflection和游戏编程宝石5的例子。有人知道如何做这个教程或体面的代码示例?

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?

到目前为止知道Boost / QT的内置系统,但我不想使用他们的(请不要阻碍这一点,我觉得it肿,我想滚自己的,不要脱轨的话题)。

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).

推荐答案

反射是一种腐烂的方法来检查程序的属性。你只能反映设计编译器的人连接到反射机器。这通常不是很多(你知道什么反射系统,让你同行在一个表达式?),它取决于语言。对于像C ++这样的东西,你试图在顶部添加反射本质上作为一组API,你将是非常有限的,或者你必须编写真正的stilted风格

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.

您可以改为使用程序转换引擎(PTS)。这样的引擎操纵完整的程序表示,并且因此可以在技术上回答任何可回答的问题。特别是,它可以在原则上回答所有你可以想象的反射问题,因为它作为编译器的替代品,并且可以看到编译器看到的一切。
(实际上,它可以看到比编译器看到的更多;编译器一次限制到一个编译单元,而良好的PTS可以同时看到任意大的编译单元集合,因此可以回答有关该集合的问题。

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).

我们的 DMS软件重构工具包可以解析C ++的完整(和许多方言),建立AST和精确的符号表。使用它,您可以实现任何静态可计算反射,然后使用它来产生分析结果或直接修改AST。

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是语言不可知的;它可以为各种各样的语言。

DMS is language agnostic; it can do this for a large variety of languages.

关于实际使用DMS做反射:OP想知道如何实现属性获取器和设置器。使用像DMS一样的PTS,你解析感兴趣的类的源代码,然后走AST的代码。对于类中的每个数据声明,你通过构建表示getter代码的AST,逐字地为该数据制作一个getter;对于诸如DMS之类的工具,您可以通过组合被解释为表示相应的AST片段的C ++源代码的模式来完成此操作,并使用可以用其他AST填充的占位符。小变换然后可以修改原始AST以包括生成的getter / setter;这将产生一个包含原始代码和生成的getter / setter的AST。最后一步是从AST重新生成代码,DMS通过使用构成语言(例如C ++)的域定义(解析器,prettyprinter,名称解析器)的一部分的AST到源的prettyprinter结束。

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.

这篇关于C ++反射指南/教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆