5

我已经断断续续地解决这个问题几个月了,现在想真正想出一个合适的解决方案来处理使用成员函数创建新的用户定义类(和这些类的实例)的情况/在 C++11 项目中运行时的属性。

到目前为止,我一直在使用 SWIG(以前使用 Python,现在使用 Lua,探索 Squirrel)。就像我迄今为止遇到的所有 C++ 绑定/嵌入库(Luna*、luabinder、luabind、OOLua、Sqrat/Sqext、Squall)一样,都希望您的类在代码执行之前在 C++ 中预定义,因为它们要么依赖于预处理器指令或模板。

所以我的问题是,是否有任何库使用更程序化的方法来包装语言,或者是否有任何像 Lua 或 Squirrel 这样的好的教程/示例,可以推荐用于处理自定义命名类的创建具有自定义成员和功能?一些方向将不胜感激。

即使是一个简单的例子,展示如何在 Lua 和 Squirrel 中通过各自的 C++ API 而不使用宏/模板/动态生成的代码来创建具有函数和属性的自定义类,也会非常有帮助。

编辑:我已经创建了一个Instance包含std::vector成员键/值对的类,以及一个标识类型的成员,以便可以查找函数。然而,关于在 Lua/Squirrel 中创建简单类而不使用静态代码的文档很少。

编辑 2:我想要一个适用于任何平台且无需动态链接的解决方案。

4

3 回答 3

2

创建从某个现有 C++ 类派生的类是将新类引入正在运行的 C++ 程序的唯一方法(我知道)。如果没有动态编译实际的 C++ 源代码并加载生成的库,就无法物理添加新类。下一个最好的事情是在 C++ 中创建一个包装 Python(Lua 等)对象的代理对象,并使该 Python(Lua)对象成为一个类的实例,该类扩展镜像到 Python(Lua)端的现有 C++ 类。

        C++

     +---------+         mirrors                   +--------------+
     | class X |  ...............................> | class X      |
     +---------+                                   | mirrored to  |
          |                                        | Python       |
          | inherits                               +--------------+
          v                                      inherits  |
     +-----------------+                                   v
     | class X_Wrapper |        references         +--------------+
     |    | python obj ------------------------->  | class CX(X): |
     +-----------------+                           |    def met() |
                                                   +--------------+

这是一个使用 Python 扩展 C++ 类的示例,使用 boost::python 作为桥梁。

C++ 方面:

#include <boost/python.hpp>
#include <iostream>

using namespace boost::python;
// this is the interface we will implement in Python
struct World
{
    virtual std::string greet() = 0;
    virtual ~World() {}
};

// this is a helper class needed to access Python-overrided methods
struct WorldWrap : World, wrapper<World>
{
    std::string greet()
    {
        return this->get_override("greet")();
    }
};

// This function tests our class implemented in Python
std::string test(World* w)
{
    std::cout << "Calling w->greet() on some World-derived object\n";
    return w->greet();
}

// This is what the Python side will see
BOOST_PYTHON_MODULE(hello)
{
    class_<WorldWrap, boost::noncopyable>("World")
            .def("greet", pure_virtual(&World::greet));

    def("test", test);
}

蟒蛇方面:

import hello


class HomeWorld(hello.World):
    """ Implements a function defined in C++ as pure virtual """
    def greet(self):
        return "howdy"

home = HomeWorld()
print (hello.test(home))
于 2016-11-08T22:24:22.783 回答
1

考虑遵循 Lua 多图示例。

Multimap = {};

function Multimap:__index(key)
    if (key == 'keys') then
        local ret = {}
        for k,_ in pairs(self) do
            ret[#ret+1] = k;
        end
        return ret;
    else
        return rawget(getmetatable(self), key)
    end
end

function Multimap.Create()
    local self = {};
    setmetatable(self, Multimap);
    return self;
end

function Multimap:Insert(key, value)
    local list = self[key];
    if (list == nil) then
        list = {};
        self[key] = list;
    end
    table.insert(list, value);
end

function Multimap:Remove(key, value)
    local list = self[key];
    assert(list ~= nil, "key not found");
    for i = 1,#list do
        if (list[i] == value) then
            table.remove(list, i);
            if (#list == 0) then
                self[key] = nil;
            end
            return;
        end
    end
    error("value not found");
end


-- testing

m = Multimap.Create()
m:Insert(1,5)
m:Insert(2,6)
m:Insert(3,7)
m:Insert(1,8)
m:Remove(2,6)
print(pcall(function() 
    m:Remove(2,6)   -- will produce assert exception
end))

print("keys left: ", table.concat(m.keys, ','))

您可以通过多种方式在 C++ 中实现这一点。

  1. 使用繁重的 Lua API。下面的代码几乎与 Lua 完全相同。
#include <Lua/src/lua.hpp>

int Multimap_Index(lua_State* L) {
    lua_settop(L, 2);       // force 2 arguments

    const char *key_value = "key";
    size_t key_len;
    const char *key = lua_tolstring(L, 2, &key_len);
    if (!strncmp(key, key_value, strlen(key_value))) {
        int i = 0;
        lua_newtable(L);                // stack : self, key, ret = {}
        int ret = lua_gettop(L);
        lua_pushnil(L);                 // stack : self, key, ret, nil
        while (lua_next(L, 1) != 0) {   // stack : self, key, ret, k, v
            lua_pop(L, 1);              // stack : self, key, ret, k
            lua_len(L, ret);            // stack : self, key, ret, k, #ret
            lua_pushvalue(L, -2);       // stack : self, key, ret, k, #ret, k
            lua_rawseti(L, ret, lua_tointeger(L, -2)+1); // ret[#ret+1] = k ; || stack : self, key, ret, k, #ret
            lua_pop(L, 1);              // stack : self, key, ret, k
        }
        // stack : self, key, ret
        return 1;
    }
    else {
        lua_getmetatable(L, 1);     // stack : self, key, metatable(self)
        lua_pushvalue(L, 2);        // stack : self, key, metatable(self), key
        lua_rawget(L, -2);          // stack : self, key, metatable(self), rawget(metatable(self), key)
        return 1;
    }
}

int Multimap_Remove(lua_State* L) {
    lua_settop(L, 3);               // force 3 arguments: self, key, value
    lua_checkstack(L, 12);          // reserve 12 arguments on stack (just in case)
    lua_pushvalue(L, 2);            // stack: self, key, value, key
    lua_gettable(L, 1);             // stack: self, key, value, list = self[key]
    if (lua_isnil(L, -1))
        luaL_error(L, "key not found");
    lua_len(L, -1);                 // stack: self, key, value, list, #list
    int count = lua_tointeger(L, -1);
    lua_pop(L, 1);                  // stack: self, key, value, list
    for (int i = 1; i <= count; ++i) {
        lua_rawgeti(L, -1, i);      // stack: self, key, value, list, v = list[i]
        if (lua_compare(L, 3, 5, LUA_OPEQ)) {   // if (list[i] == value)
            lua_getglobal(L, "table");      // stack : self, key, value, list, v, table
            lua_getfield(L, -1, "remove");  // stack : self, key, value, list, v, table, table.remove
            lua_pushvalue(L, 4);
            lua_pushinteger(L, i);          // stack : self, key, value, list, v, table, table.remove, list, i
            lua_call(L, 2, 0);              // table.remove(list, i); || stack : self, key, value, list, v, table
            lua_pushnil(L);
            if (lua_next(L, 4) == 0) {      // if list is empty table
                lua_pushvalue(L, 2);
                lua_pushnil(L);
                lua_settable(L, 1);         // self[key] = nil
            }
            return 0;
        }
    }
    luaL_error(L, "value not found");
}

int main() {
    auto L = luaL_newstate();
    luaL_openlibs(L);

    lua_newtable(L);
    int Multimap = lua_gettop(L);           // Multimap = {}
    lua_pushvalue(L, Multimap);
    lua_setglobal(L, "Multimap");           // _G.Multimap = Multimap;

    // option 1: create a C function for operation
    // Multimap.__index = &Multimap_Index
    lua_pushcfunction(L, Multimap_Index);
    lua_setfield(L, Multimap, "__index");

    // option 2: compile Lua code and use it
    luaL_loadstring(L,
        "local self = {};\n"
        "setmetatable(self, Multimap);\n"
        "return self;\n"
    );
    lua_setfield(L, Multimap, "Create");    // Multimap.Create = &Multimap_Create

    luaL_loadstring(L,
        "local self, key, value = ...;\n"   // initialize local variables from parameters here
        "local list = self[key];\n"
        "if (list == nil) then\n"
        "   list = {};\n"
        "   self[key] = list;\n"
        "end\n"
        "table.insert(list, value);\n"
    );
    lua_setfield(L, Multimap, "Insert");    // Multimap.Create = &Multimap_Insert

    lua_pushcfunction(L, Multimap_Remove);
    lua_setfield(L, Multimap, "Remove");    // Multimap.Create = &Multimap_Remove

    lua_getfield(L, Multimap, "Create");
    lua_call(L, 0, 1);
    int m = lua_gettop(L);
    lua_getfield(L, m, "Insert");           // stack : m, m.insert
    int Insert = lua_gettop(L);

    // m.Insert(m, 1, 5)
    lua_pushvalue(L, Insert);
    lua_pushvalue(L, m);
    lua_pushinteger(L, 1);
    lua_pushinteger(L, 5);
    lua_call(L, 3, 0);

    // m.Insert(m, 2, 6)
    lua_pushvalue(L, Insert);
    lua_pushvalue(L, m);
    lua_pushinteger(L, 2);
    lua_pushinteger(L, 6);
    lua_call(L, 3, 0);

    // m.Insert(m, 3, 7)
    lua_pushvalue(L, Insert);
    lua_pushvalue(L, m);
    lua_pushinteger(L, 3);
    lua_pushinteger(L, 7);
    lua_call(L, 3, 0);

    // m.Insert(m, 1, 8)
    lua_pushvalue(L, Insert);
    lua_pushvalue(L, m);
    lua_pushinteger(L, 1);
    lua_pushinteger(L, 8);
    lua_call(L, 3, 0);

    // m.Remove(m, 2, 6)
    lua_getfield(L, m, "Remove");
    lua_pushvalue(L, m);
    lua_pushinteger(L, 2);
    lua_pushinteger(L, 6);
    lua_call(L, 3, 0);

    // m.Remove(m, 2, 6)
    lua_getfield(L, m, "Remove");
    lua_pushvalue(L, m);
    lua_pushinteger(L, 2);
    lua_pushinteger(L, 6);
    lua_pcall(L, 3, 0, 0);
    printf("%s\n", lua_tostring(L, -1));

    lua_getglobal(L, "table");
    lua_getfield(L, -1, "concat");
    lua_getfield(L, m, "keys");
    lua_pushstring(L, ",");
    lua_call(L, 2, 1);
    printf("keys left: %s\n", lua_tostring(L, -1));

    lua_close(L);

    return 0;
}
  1. 或者您可以使用使用 std::multimap 的 Lua 用户数据(我需要另一个小时来实现这一点,所以请问您是否真的需要它——这并没有从您的问题中得出)
于 2016-11-12T01:53:37.163 回答
-1

免责声明: 我发布此贡献作为答案,因为我没有足够的声望点来添加评论。

评论: 抛开与特定脚本语言绑定的问题,您似乎面临着该C++语言的一个基本限制:它不是“动态的”(正如其他评论所指出的那样)。也就是说,该语言不提供任何扩展或修改已编译程序的功能。

不过,也许所有的希望都没有失去。在网上搜索“c++ 动态加载”显示某些系统(如 Linux 和 Windows)似乎确实实现了动态加载机制。

以下是两篇讨论该主题的(旧)文章的链接。

  1. Linux Journal 中的 Linux上 C++ 的动态类加载。
  2. Dr.Dobb's 中动态加载的 C++ 对象

乍一看,它们似乎很有趣。不过,我不确定它们是否仍然相关。

这只是在黑暗中的一个镜头。

于 2016-11-15T21:07:44.517 回答