本文整理汇总了C++中Sequence::createNewItemFromObj方法的典型用法代码示例。如果您正苦于以下问题:C++ Sequence::createNewItemFromObj方法的具体用法?C++ Sequence::createNewItemFromObj怎么用?C++ Sequence::createNewItemFromObj使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sequence
的用法示例。
在下文中一共展示了Sequence::createNewItemFromObj方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: seq_ass_item
//-------------------------------------------------------------------------------------
int Sequence::seq_ass_item(PyObject* self, Py_ssize_t index, PyObject* value)
{
Sequence* seq = static_cast<Sequence*>(self);
std::vector<PyObject*>& values = seq->getValues();
if (uint32(index) >= values.size())
{
PyErr_SetString(PyExc_IndexError, "Sequence assignment index out of range");
PyErr_PrintEx(0);
return -1;
}
if(value)
{
// 检查类别是否正确
if(seq->isSameItemType(value))
{
values[index] = seq->createNewItemFromObj(value);
}
else
{
PyErr_SetString(PyExc_IndexError, "Sequence set to type is error!");
PyErr_PrintEx(0);
return -1;
}
}
else
{
values.erase(values.begin() + index);
}
return 0;
}
示例2: seq_ass_slice
//-------------------------------------------------------------------------------------
int Sequence::seq_ass_slice(PyObject* self, Py_ssize_t index1, Py_ssize_t index2, PyObject* oterSeq)
{
Sequence* seq = static_cast<Sequence*>(self);
std::vector<PyObject*>& values = seq->getValues();
// 是否是删除元素
if (!oterSeq)
{
if (index1 < index2)
values.erase(values.begin() + index1, values.begin() + index2);
return 0;
}
// oterSeq必须是一个 sequence
if (!PySequence_Check(oterSeq))
{
PyErr_Format(PyExc_TypeError, "Sequence slices can only be assigned to a sequence");
PyErr_PrintEx(0);
return -1;
}
if (oterSeq == seq)
{
PyErr_Format(PyExc_TypeError, "Sequence does not support assignment of itself to a slice of itself");
PyErr_PrintEx(0);
return -1;
}
int sz = values.size();
int osz = PySequence_Size(oterSeq);
// 保证index不会越界
if (index1 > sz) index1 = sz;
if (index1 < 0) index1 = 0;
if (index2 > sz) index2 = sz;
if (index2 < 0) index2 = 0;
// 检查一下 看看有无错误类别
for (int i = 0; i < osz; ++i)
{
PyObject* pyVal = PySequence_GetItem(oterSeq, i);
bool ok = seq->isSameItemType(pyVal);
Py_DECREF(pyVal);
if (!ok)
{
PyErr_Format(PyExc_TypeError, "Array elements must be set to type %s (setting slice %d-%d)", "ss", index1, index2);
PyErr_PrintEx(0);
return -1;
}
}
if (index1 < index2)
values.erase(values.begin() + index1, values.begin() + index2);
// 先让vector分配好内存
values.insert(values.begin() + index1, osz, (PyObject*)NULL);
for(int i = 0; i < osz; ++i)
{
PyObject* pyTemp = PySequence_GetItem(oterSeq, i);
if(pyTemp == NULL)
{
PyErr_Format(PyExc_TypeError, "Sequence::seq_ass_slice::PySequence_GetItem %d is NULL.", i);
PyErr_PrintEx(0);
}
values[index1 + i] = seq->createNewItemFromObj(pyTemp);
Py_DECREF(pyTemp);
}
return 0;
}