需要了解优秀的 C++反射 API(用于运行时类型识别 -RTTI 和运行时调用)
我需要一个好的 C++ Reflection API(如 Microsoft API),它使我能够确定在运行时识别的类型(类、结构、枚举、int、float、double 等)、声明它们并在运行时调用这些类型的方法。
问候,
乌斯曼
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在尝试使用插件式架构,http://pocoproject.org 上的 POCO 库有一些内容这可能会让你成功。它将允许您在运行时加载 .dll 或 .so 并创建其中包含的类。但是调用代码仍然需要一个描述接口(或抽象基类)的头文件,以便能够获取方法的签名。
If you are trying to get to a plugin-type architecture, the POCO Library at http://pocoproject.org has some pieces that might get you part of the way. It will allow you to load a .dll or .so at runtime and create the classes contained in it. But the calling code will still need a header file which describes an interface (or abstract base class) to be able to get the signatures of the methods.
C++ 是一种极其复杂的语言。 “反射”API 不是语言设计的一部分,因此基本上不存在。
如果您想要通用的“反射”和“元编程”,您可以通过外部语言并使用程序转换系统(PTS)。这样一个适合您目的的工具必须能够解析 C++(一次在多个编译单元中),为您提供对所有语言结构的访问,让您反映,即确定类型 (或其他属性)的任何构造(例如,变量、表达式或其他语法构造),并使您能够应用任意代码修改。显然,这不会在“运行时”发生(尽管我想如果你坚持的话,你可以花钱购买这样的机器)。
我们的 DMS 软件重组工具包及其 C++ 前端 在分析和转换大量 C++ 代码方面拥有良好的记录。请参阅技术论文了解一些详细的用例。我不认为维基百科网站上的其他工具可以处理 C++,尽管它们有正确的思维方式。
尽管它并不是真正的 PTS(没有源到源的转换),但 Clang 也可能有效。我不确定(因为我没有全部使用它),它如何收集类型信息并使用它来驱动源代码的转换。它显然非常擅长使用此类信息来进行 LLVM 代码生成。
C++ is an incredibly complex language. "Reflective" APIs weren't part of the language design and so basically it isn't there.
If you want general purpose "reflection" and "metaprogramming", you can get that by stepping outside the language and using a program transformation system (PTS). Such a tool for your purpose has to parse C++ (in more than one compilation unit at a time), provide you with access to all the language structures, let you reflect, that is, determine the type (or other properties) of any construct (e.g., variable, expression or other syntax construction) and enable you to apply arbitrary code modifications. Obviously, this won't happen at "runtime" (although I suppose you could shell out to such machinery if you insisted).
Our DMS Software Reengineering Toolkit with its C++ Front End has a proven track record at analyzing and transformating very large sets of C++ code. See the technical papers for some detailed use cases. I don't think the other tools at the Wikipedia site handle C++, although they have the right mindset.
Although it isn't really a PTS (no source-to-source transformations), Clang might work, too. I'm not sure (since I don't use it all), how it can collect type information and use it to drive transformations to the source code. Its clearly very good at using such information to do LLVM code generation.