【问题标题】:Convert types via string using SWIG typemaps使用 SWIG 类型映射通过字符串转换类型
【发布时间】:2014-09-08 20:29:01
【问题描述】:

我正在使用 swig 为某些类生成包装器。一个类需要一个 QUuid (std::list) 列表(见下文)。有一个 toString 给出了一个 std::string 并且 python 也可以通过使用这个字符串来实例化它的 uuid。如何生成使用上述转换为 python 的类型映射(或者其他可能更好的类型),反之亦然。如果这也与目标语言无关(至少我还需要 ruby​​ 包装器),那最好是这样。

谢谢

IRPICurrentPositionsCommand.h:

class IRPICurrentPositionsCommand : public CommandBase
{
  Q_OBJECT

  COMMAND_IMPLEMENTATION_BASICS(IRPICurrentPositionsCommand, ir)

public:
  std::list<QUuid> GetAxisIDs() const
  {
    return m_AxisIDs;
  }

  void SetAxisIDs(std::list<QUuid> arg)
  {
    m_AxisIDs = arg;
  }

  std::list<double> GetAxisPostions() const
  {
    return m_AxisPostions;
  }

  void SetAxisPostions(std::list<double> arg)
  {
    m_AxisPostions = arg;
  }

private:
  std::list<QUuid> m_AxisIDs;
  std::list<double> m_AxisPostions;

protected:

  /** @see ora::CommandBase::Serialize(QDataStream &stream) **/
  virtual void Serialize(QDataStream &stream) const;

  /** @see ora::CommandBase::Deserialize(QDataStream &stream) **/
  virtual void Deserialize(QDataStream &stream);
};

【问题讨论】:

  • 你的问题不是很清楚。你能发布你的.h吗?
  • @MasterMind 我添加了一个示例标题。
  • 所以基本上您想知道如何为您的std::list&lt;QUuid&gt; 对象生成模板?
  • @MasterMind 这正是我想要的。

标签: python c++ ruby type-conversion swig


【解决方案1】:

这是std::list&lt;QUuid&gt; 对象的 SWIG/Python 模板。我不熟悉 Ruby,但希望这能让您对类型映射有所了解。

// To use std::string
%include "std_string.i"

%typemap(out) std::list<QUuid>
{
    PyObject* outList = PyList_New(0);

    int error;

    std::list<QUuid>::iterator it;
    for ( it=$1.begin() ; it != $1.end(); it++ )
    {
        PyObject* pyQUuid = SWIG_NewPointerObj(new QUuid(*it), SWIGTYPE_p_QUuid, SWIG_POINTER_OWN );

        error = PyList_Append(outList, pyQUuid);
        Py_DECREF(pyQUuid);
        if (error) SWIG_fail;       
    }

    $result = outList;
}

%typemap(in) std::list<QUuid>
{
    //$input is the PyObject
    //$1 is the parameter

    if (PyList_Check($input))
    {
        std::list<QUuid> listTemp;

        for(int i = 0; i<PyList_Size($input); i++)
        {
            PyObject* pyListItem = PyList_GetItem($input, i);

            QUuid* arg2 = (QUuid *) 0 ;

            int res1 = 0;
            void *argp1;

            res1 = SWIG_ConvertPtr(pyListItem, &argp1, SWIGTYPE_p_QUuid,  0  | 0);
            if (!SWIG_IsOK(res1))
            {
                PyErr_SetString(PyExc_TypeError,"List must only contain QUuid objects");
                return NULL;
            }  
            if (!argp1)
            {
                PyErr_SetString(PyExc_TypeError,"Invalid null reference for object QUuid");
                return NULL;
            }

            arg2 = reinterpret_cast< QUuid * >(argp1);
            listTemp.push_back(*arg2);
        }

        $1 = listTemp;
    }
    else
    {
        PyErr_SetString(PyExc_TypeError,"Wrong argument type, list expected");
        return NULL;
    }
}

【讨论】:

  • 非常感谢。我会尝试将它添加到我的包装器中。
  • 两个问题: 1 .如何获得SWIGTYPE_p_QUuid的定义?只有当我有一个以 Quuid 作为参数的函数时才会显示。 2.我告诉python它应该使用UUID的部分在哪里?
猜你喜欢
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
  • 2013-05-15
  • 2023-03-18
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
相关资源
最近更新 更多