您将如何在预编译期间自动生成 class 实现?

How would you auto generate a class implementation during pre-compile?

我正在考虑创建一个服务定位器,我还想提供它提供的服务的“NULL”实现。您将如何解析接口并自动生成实现?你会使用什么工具 - 比如 CMake?基本上,我试图避免写类似这样的东西:

class IAudio
{
    virtual void playSound(SoundId Id) = 0;
    ....
};

***** Following is the boilerplate I would like to avoid  *****

class NullAudio : IAudio
{
     void playSound(SoundId) override { /* Does Nothing */ }
     ....
};

我似乎无法从我的 google 搜索自动代码生成中找到任何示例 - 它们都会在编辑器中找到引用代码完成的内容。我什至会考虑 运行 一个 python 脚本,它查找以 I 开头的文件 - 比如 IAudio.hpp,解析它并写出一个文件,如果这是执行此操作的常用方法。

谢谢!

我想我明白了。写一个小工具来做我想做的事,先配置CMake编译它,然后用add_custom_command.

让CMake 运行工具