Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过继承使用luabind和std::shared_ptr_C++_C++11_Lua_Shared Ptr_Luabind - Fatal编程技术网

C++ 通过继承使用luabind和std::shared_ptr

C++ 通过继承使用luabind和std::shared_ptr,c++,c++11,lua,shared-ptr,luabind,C++,C++11,Lua,Shared Ptr,Luabind,我有一个API(一个特定的GUI库),它非常依赖于std::shared_ptr,也就是说,它们经常被用作函数参数并存储在其他对象中。例如,容器小部件(如拆分器和框)将其子小部件存储在shared_ptrs中。现在我想通过luabind将这个API映射到Lua。在理想情况下,Luabind将在共享的\u ptr中创建新对象,并允许我将这些对象直接传递给使用共享的\u ptr参数的函数。这似乎适用于单个类,例如: luabind::class_<Button, std::shared_ptr

我有一个API(一个特定的GUI库),它非常依赖于
std::shared_ptr
,也就是说,它们经常被用作函数参数并存储在其他对象中。例如,容器小部件(如拆分器和框)将其子小部件存储在
shared_ptr
s中。现在我想通过luabind将这个API映射到Lua。在理想情况下,Luabind将在共享的\u ptr中创建新对象,并允许我将这些对象直接传递给使用共享的\u ptr参数的函数。这似乎适用于单个类,例如:

luabind::class_<Button, std::shared_ptr<Button>>("Button")
luabind::类(“按钮”)
虽然我这样声明它,但我可以公开和使用类似于
voidfoo(std::shared_ptr const&)
的函数

现在,luabind手册提到,为了使用类的层次结构,我必须对层次结构中的所有类使用相同的共享\u ptr模板实例,例如

luabind::class_<BaseWidget, std::shared_ptr<BaseWidget>>("BaseWidget"),
luabind::class_<Button, BaseWidget, std::shared_ptr<BaseWidget>>("Button")
luabind::class_uzy(“BaseWidget”),
luabind::类uu(“按钮”)

现在我不能再调用
foo
——它将无法从Lua中找到函数。我可以让luabind在
shared\u ptr
s中仍然支持传递按钮吗?另外,我想知道为什么luabind要求对层次结构中的所有类使用相同的智能指针,而不是将它们转换为基类指针。

我认为要实现这一点,必须像这样绑定派生类:

luabind::class_<Button, BaseWidget, std::shared_ptr<Button>> ("Button")
原因是luabind使用带有整数的类型id系统(更准确地说,是在继承.hpp中定义的std::size\t)。
您可以通过以下函数获得任何注册类型的类型id:

luabind::detail::static_class_id<T>(nullptr);
luabind::detail::static_class_id(nullptr);
其中T是注册类。
在我的演示程序中,它们是:

  • BaseWidget=3
  • std::shared_ptr=6
  • 按钮=4
  • 标准::共享按钮>=7
  • 行动=5
因此,当您从lua调用DoClick时,它将调用实例\u holder.hpp中模板类指针\u holder的get成员:

std::pair<void*, int> get(class_id target) const
{
    if (target == registered_class<P>::id)
        return std::pair<void*, int>(&this->p, 0);

    void* naked_ptr = const_cast<void*>(static_cast<void const*>(
        weak ? weak : get_pointer(p)));

    if (!naked_ptr)
        return std::pair<void*, int>((void*)0, 0);

    return get_class()->casts().cast(
        naked_ptr
      , static_class_id(false ? get_pointer(p) : 0)
      , target
      , dynamic_id
      , dynamic_ptr
    );
}
std::pair get(class_id target)const
{
if(target==registered_class

::id) 返回std::pair(&this->p,0); void*裸铸=常数铸(静态铸)( 弱?弱:获取_指针(p)); 如果(!裸体) 返回std::pair((void*)0,0); 返回get_class()->casts().cast( 裸体 ,静态类id(false?获取指针(p):0) ,目标 ,动态识别码 ,动态 ); }

如您所见,如果目标类与注册的类不同,它将尝试执行强制转换。
这就是有趣的地方。 如果您将Button类声明为

luabind::class_<Button, BaseWidget,std::shared_ptr<BaseWidget>>("Button")
luabind::class_<Button, BaseWidget, std::shared_ptr<Button>> ("Button")
luabind::类(“按钮”)
然后该实例将作为共享的\u ptr保存到BaseWidget,因此cast函数将尝试从BaseWidget(3)转换到std::shared\u ptr