如何编写给定自定义 IDL 的代码生成器?

发布于 2024-07-12 23:06:12 字数 122 浏览 5 评论 0原文

如果您有一个足够简单的架构,其中包含基本的布尔字符串字段,那么如何使用 C++ 编写代码生成器。 如果你曾经写过,你是怎么开始的? 欢迎文章/推荐。

编辑:请注意,这不是标准的 CORBA idl。

If you have a simple enough schema with basic boolean string fields, how to go about writing a code generator in C++.
If you ever wrote , how did you start. Articles/recommendation welcome.

EDIT: Note that this is not the standard CORBA idl.

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

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

发布评论

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

评论(3

合约呢 2024-07-19 23:06:13

总之:

  1. 手动转换某些内容,
  2. 将输出复制到字符串文字中,然后
  3. 使用循环和变量而不是固定名称进行概括。

这是一个用 TDD 解决的特别好的问题 - 首先建立一个框架,例如 CPPUnit(或其中之一)许多替代方案)以及愚蠢的简单测试,这些测试只是确保您可以加载文件并通过写出文件来迭代其内容。

以渐进的方式接近解决方案 - 获得一些简单且通用的输出,通过测试确认其有效,然后添加更复杂的变体。 例如:首先处理单参数函数。

对于中等复杂度的代码,只需使用标准 C++ ostream 类有一长串输出语句,这将允许您编写大量混合文字与任何变量的代码。

In summary:

  1. Convert something by hand,
  2. copy that output into a string literal and then
  3. generalise with loops and variables instead of fixed names.

This is a particularly good problem to tackle with TDD - start by setting up a framework such as CPPUnit (or one of many alternatives) with stupidly simple tests that just ensure you can load a file and iterate its contents by writing them out.

Approach the solution very incrementally - get something simple and general output, with a test to confirm it works, then add a more sophisticated variation. eg: handle single parameter functions first.

For code up to a moderate range of complexity, just have a long set of output statements using the standard C++ ostream classes which will allow you to write lots of code mixing literals with any variables.

转身泪倾城 2024-07-19 23:06:13

Comet 是实现 COM 的非常好的库。

Comet is pretty good library to implement COM.

意中人 2024-07-19 23:06:13

Unfortunately your question lacks details about what exactly you are trying to generate. However I will take a stab and recommend that you look at the C++ boost library spirit (http://spirit.sourceforge.net). With Spirit you can create parsers. So assuming you have written a spirit parser for your DSL (domain specific language - your IDL) you will get an AST (abstract syntax tree) after parsing your IDL file(s) that you can walk with the visitor pattern and by creating appropriate visitors you can generate whatever output you need, e.g. generating code.

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