包含传递给 lua 的 std::string 的结构

发布于 2024-09-24 12:01:27 字数 1553 浏览 3 评论 0原文

我有使用 swig 的 C++ 代码,它创建一个结构体,将其传递给 lua (本质上是通过引用),并允许操作该结构体,以便在我返回到 C++ 函数后,在 lua 代码中所做的更改仍然保留。这一切都工作正常,直到我将 std::string 添加到结构中,如下所示:

struct stuff
{
    int x;
    int y;
    std::string z;
};

我无法修改 std::string 因为它显然是作为 const 引用传递的。如果我尝试在 lua 函数中为该字符串赋值,则会收到此错误:

str (arg 2) 中出现错误,应为“std::string const &”得到“字符串”

解决这个问题的正确方法是什么?我是否必须编写一些自定义 C++ 函数来设置 z 而不是使用 obj.z = "hi" 等常规语法?有什么方法可以使用 swig 进行此分配吗?

C++代码是


#include <stdio.h>
#include <string.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#include "example_wrap.hxx"

extern int luaopen_example(lua_State* L); // declare the wrapped module

int main()
{

    char buff[256];
    const char *cmdstr = "print(33)\n";
    int error;
    lua_State *L = lua_open();
    luaL_openlibs(L);
    luaopen_example(L);

    struct stuff b;

    b.x = 1;
    b.y = 2;

    SWIG_NewPointerObj(L, &b, SWIGTYPE_p_stuff, 0);
    lua_setglobal(L, "b");

     while (fgets(buff, sizeof(buff), stdin) != NULL) {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
                lua_pcall(L, 0, 0, 0);
        if (error) {
          fprintf(stderr, "%s", lua_tostring(L, -1));
          lua_pop(L, 1);  /* pop error message from the stack */
        }
      }

      printf("B.y now %d\n", b.y);
      printf("Str now %s\n", b.str.c_str());
      luaL_dostring(L, cmdstr);
      lua_close(L);
      return 0;

}

I have working C++ code using swig which creates a struct, passes it to lua (essentially by reference), and allows manipulation of the struct such that the changes made in the lua code remain once I've returned to the C++ function. This all works fine until I add a std::string to the struct, as shown here:

struct stuff
{
    int x;
    int y;
    std::string z;
};

I'm unable to modify the std::string because it's apparently passed as a const reference. If I attempt to assign a value to this string in my lua function I get this error:

Error in str (arg 2), expected 'std::string const &' got 'string'

What is the proper way to address this problem? Do I have to write some custom C++ function to set z rather than using normal syntax like obj.z = "hi"? Is there some way to allow this assignment using swig?

The C++ code is


#include <stdio.h>
#include <string.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#include "example_wrap.hxx"

extern int luaopen_example(lua_State* L); // declare the wrapped module

int main()
{

    char buff[256];
    const char *cmdstr = "print(33)\n";
    int error;
    lua_State *L = lua_open();
    luaL_openlibs(L);
    luaopen_example(L);

    struct stuff b;

    b.x = 1;
    b.y = 2;

    SWIG_NewPointerObj(L, &b, SWIGTYPE_p_stuff, 0);
    lua_setglobal(L, "b");

     while (fgets(buff, sizeof(buff), stdin) != NULL) {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
                lua_pcall(L, 0, 0, 0);
        if (error) {
          fprintf(stderr, "%s", lua_tostring(L, -1));
          lua_pop(L, 1);  /* pop error message from the stack */
        }
      }

      printf("B.y now %d\n", b.y);
      printf("Str now %s\n", b.str.c_str());
      luaL_dostring(L, cmdstr);
      lua_close(L);
      return 0;

}

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

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

发布评论

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

评论(1

牵你的手,一向走下去 2024-10-01 12:01:27

您需要将 %include 添加到您的 SWIG 模块中。否则,它不知道如何将 Lua string 映射到 C++ std::string


人们遇到的一个常见问题是包含 std 的类/结构::细绳。这可以通过定义类型映射来克服。例如:

%module example
%include "std_string.i"

%apply const std::string& {std::string* foo};

struct my_struct
{
  std::string foo;
};

You need to add %include <std_string.i> to your SWIG module. Otherwise, it does not know how to map a Lua string to an C++ std::string.


A common problem that people encounter is that of classes/structures containing a std::string. This can be overcome by defining a typemap. For example:

%module example
%include "std_string.i"

%apply const std::string& {std::string* foo};

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