在 compile-time 处动态创建地图

Dynamically creating a map at compile-time

我正在游戏引擎中实现 Lua。导出到 Lua 的所有函数都有以 luavoidluaintluabool 开头的 headers,只是为了快速参考预期参数,等等一眼就看出是导出了这个函数

#define luavoid(...) void

luavoid(std::string s) TextMsg()
{
    std::string s;
    ExtractLuaParams(1, s);
    ::TextMsg(s.c_str());
}

为了将函数实际导出到 Lua,它们被添加到字典中。启动时,地图用于调用 lua_register.

std::unordered_map<std::string, ScriptCall> _callMap = {
    { "TextMsg", TextMsg },
    ...
}

会有很多函数导出。我不想手动维护这张地图,而是想自动创建它。

我的第一直觉是 compile-time 上的宏。我最初放弃了它并开始编写一个程序来解析代码(作为 pre-build 事件),因为所有函数都可以 text-matched 与 luaX 宏。它会创建一个 header 文件,其中包含自动生成的地图。

然后我在 compile-time 找到了一种方法后又回去做这件事。在我最终在游戏中实现它之前,我想出了这个解决方案作为示例:

using MapType = std::unordered_map<std::string, int>;

template <MapType& m>
struct MapMaker
{
    static int MakePair(std::string s, int n)
    {
        m[s] = n;
        return n;
    }
};

#define StartMap(map) MapType map
#define AddMapItem(map, s, n) int map##s = MapMaker<map>::MakePair(#s, n)

StartMap(myMap);
AddMapItem(myMap, abc, 1);
AddMapItem(myMap, def, 2);
AddMapItem(myMap, ghi, 3);

void main()
{
    for (auto& x : myMap)
    {
        std::cout << x.first.c_str() << "->" << x.second << std::endl;
    }
}

有效。

我的问题是,这有多可怕,是否可以改进?最后我想要的只是一个将字符串映射到函数的列表。有没有更好的方法来创建地图,还是我应该使用 text-parsing 方法?

要温柔(-ish)。这是我第一次尝试使用这样的模板进行编码。我假设这属于模板元编程。

how horrible is this and can it be improved?

介于可怕和可怕之间。 (有些问题最好不要问。)是的...

All I want in the end is a list mapping a a string to a function. Is there a better way to create a map or should I just go with the text-parsing method?

最简单的做法是:

#define ADDFN(FN) { #FN, FN }

std::unordered_map<std::string, ScriptCall> _callMap = {
    ADDFN(TextMsg),
    ...
};

这使用宏来自动重复字符串文字函数名称和标识符 - 您的实现没有进一步添加任何实质性内容。

就是说,您 可以 试验比您的实施更进一步的自动化,也许是这样的:

#define LUAVOID(FN, ...) \
    void FN(); \
    static auto addFN ## __LINE__ = myMap.emplace(#FN, FN); \
    void FN()

LUAVOID(TextMsg, string s)
{
    ...
}

看到了运行here.

这里的想法是宏生成一个函数声明,以便它可以注册函数,然后再定义。 __LINE__ 可能足以保证标识符的唯一性 - 假设您有一个文件执行此操作,并且您的编译器替换为数字文字(我使用过的所有编译器都这样做,但我不记得标准是否强制要求). emplace 函数具有非 void return 类型,因此可以直接用于插入 map.

Be gentle(-ish). This is my first attempt at coding with templates like this.

对不起。

I assume this falls under template metaprogramming.

这是有争议的。许多 C++ 程序员(包括我自己)认为 "metaprogramming" 涉及更高级的模板用法——例如可变长度参数列表、递归实例化和专门化——但许多其他人认为所有模板用法都是 "metaprogramming"由于模板提供了如何创建实例的说明,这在技术上足以构成 metaprogramming.