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
如何使用SWIG在PHP中使用std::vector 我正在使用SWIG在PHP中封装C++ API。我在这方面做了很多工作,但是我对一个返回向量的函数有问题。标题如下所示: #include <vector> namespace STF { class MyClass { public: const std::vector<MyOtherClass> &getList(); }; } %include <std_vector.i> %import "STF_MyOtherClass.i" %{ #include "STF_MyOtherClass.h" #include "STF_MyClass.h" %} %include "STF_MyClass.h" #包括 名称空间STF { 类MyClass { 公众: const std::vector&getList(); }; }_Php_C++_Swig - Fatal编程技术网

如何使用SWIG在PHP中使用std::vector 我正在使用SWIG在PHP中封装C++ API。我在这方面做了很多工作,但是我对一个返回向量的函数有问题。标题如下所示: #include <vector> namespace STF { class MyClass { public: const std::vector<MyOtherClass> &getList(); }; } %include <std_vector.i> %import "STF_MyOtherClass.i" %{ #include "STF_MyOtherClass.h" #include "STF_MyClass.h" %} %include "STF_MyClass.h" #包括 名称空间STF { 类MyClass { 公众: const std::vector&getList(); }; }

如何使用SWIG在PHP中使用std::vector 我正在使用SWIG在PHP中封装C++ API。我在这方面做了很多工作,但是我对一个返回向量的函数有问题。标题如下所示: #include <vector> namespace STF { class MyClass { public: const std::vector<MyOtherClass> &getList(); }; } %include <std_vector.i> %import "STF_MyOtherClass.i" %{ #include "STF_MyOtherClass.h" #include "STF_MyClass.h" %} %include "STF_MyClass.h" #包括 名称空间STF { 类MyClass { 公众: const std::vector&getList(); }; },php,c++,swig,Php,C++,Swig,接口文件如下所示: #include <vector> namespace STF { class MyClass { public: const std::vector<MyOtherClass> &getList(); }; } %include <std_vector.i> %import "STF_MyOtherClass.i" %{ #include "STF_MyOtherClass.h" #i

接口文件如下所示:

#include <vector>

namespace STF
{
class MyClass
{
    public:
        const std::vector<MyOtherClass> &getList();
};
}
%include <std_vector.i>
%import "STF_MyOtherClass.i"

%{
    #include "STF_MyOtherClass.h"
    #include "STF_MyClass.h"
%}

%include "STF_MyClass.h"
%include
%导入“STF_MyOtherClass.i”
%{
#包括“STF_MyOtherClass.h”
#包括“STF_MyClass.h”
%}
%包括“STF_MyClass.h”
我似乎能够很好地调用该函数,但它返回的是一个PHP资源,而不是一个对象。具体来说,它是一种类型的资源:“\u p\u std\u vecort\u STF\u MyClass\u t”

我如何让它返回一个可以迭代的对象(最好是使用foreach循环),或者如何迭代这个资源

更新:

我一直在根据我在这里读到的内容制定解决方案:

基本上,我正在尝试将向量转换为python数组:

%typemap(out) std::vector<STF::MyOtherClass>
{
    array_init( return_value );

    std::vector<STF::MyOtherClass>::iterator itr;

    itr = $1.begin();

    for( itr; itr != $1.end(); itr++ )
    {
        zval* tmp;

        MAKE_STD_ZVAL( tmp );
        SWIG_SetPointerZval( tmp, &*itr, $descriptor(STF::MyOtherClass*), 2 );

        add_next_index_zval( return_value, tmp );
    }
}
%typemap(out)std::vector
{
数组_init(返回_值);
std::vector::迭代器itr;
itr=$1.begin();
对于(itr;itr!=$1.end();itr++)
{
zval*tmp;
制定标准(tmp);
SWIG_SetPointerZval(tmp,&*itr,$descriptor(STF::MyOtherClass*),2);
添加下一个索引(返回值,tmp);
}
}

这是非常接近工作。我在SWIG_ZTS_SetPointerZval的包装器代码中放置了一个断点。当初始化对象时,它为“stf\uuu myotherclass”执行zend\u lookup\u类,但失败(找不到类)。我不确定它为什么找不到类。

您就快到了,但是除了
%include
之外,您还需要以下内容:

%template (MyVector) std::vector<MyOtherClass>;
%模板(MyVector)std::vector;
这指示SWIG将
MyOtherClass
的向量作为名为
MyVector
的类型公开给目标语言。如果不这样做,SWIG就不知道要为哪些类型实例化
std::vector
,因此它被简化为默认包装

旁注:


const std::vector getList()中设置
const
有什么原因吗当它不是引用时?我要么将它作为一个引用,并使方法
const
也(
const std::vector&getList()const;
),要么完全删除
const
,因为它在那里什么也不做。

最后,我将向量转换成PHP数组(将它放在MyOtherClass的接口文件中):

%typemap(out)const std::vector&
{
数组_init(返回_值);
std::vector::const_迭代器itr;
itr=$1->begin();
对于(itr;itr!=$1->end();itr++)
{
zval*tmp;
STF::MyOtherClass*res=新STF::MyOtherClass(*itr);
制定标准(tmp);
swig_type_info type=*$descriptor(STF::MyOtherClass*);
type.name=(char*)“\u p\u CustomNamespace\\MyOtherClass”;
开关设定值ZVAL(tmp、res和类型,2);
添加下一个索引(返回值,tmp);
}
}

awoodland不适用于我的%模板。我认为这可能是因为我没有将PHP类放在不同的自定义名称空间中。相反,我手动完成了这项工作,并传入了我希望它使用的php类。

tl;dr:您需要显式实例化
std::vector
,以向PHP公开特定类型。我尝试了此解决方案,但仍然得到了返回给我的资源。也许这是因为我的类位于命名空间中?(我更新了上面的帖子以添加名称空间)。我试图查看生成的包装器代码,但看不到它试图在哪里创建MyVector对象。函数的存在就是为了实现这一点,但它们似乎不在其他任何地方引用。另外,getList函数应该是通过引用的,我只是打错了。@drewag-您需要在
%template
指令中明确提到名称空间。您显示的类型映射将改变行为,使
%include
中的某些类型映射无法应用。我将尝试组合一个更完整的示例。我尝试显式地提到名称空间,但这对我仍然不起作用。我认为这可能是因为我将PHP类放在了一个不同于默认名称空间的名称空间中。最后,我使用了typemap解决方案。谢谢你的帮助!