Page MenuHomeHEPForge

vec_string.i
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

vec_string.i

%include std_vector.i
%include std_string.i
%{
#include <vector>
#include <string>
#include <cassert>
%}
%typecheck(SWIG_TYPECHECK_STRING_ARRAY) (const std::vector<std::string>&)
{
$1 = 0;
if (PySequence_Check($input))
{
int allStrings = 1;
const Py_ssize_t size = PySequence_Size($input);
for (Py_ssize_t i=0; i<size && allStrings; ++i)
{
PyObject *o = PySequence_GetItem($input, i);
assert(o);
if (!PyUnicode_Check(o))
allStrings = 0;
Py_DECREF(o);
}
$1 = allStrings;
}
}
%typemap(in) (const std::vector<std::string>&)
{
if (PySequence_Check($input))
{
$1 = new std::vector<std::string>();
const Py_ssize_t size = PySequence_Size($input);
$1->reserve(size);
for (Py_ssize_t i=0; i<size; ++i)
{
PyObject *o = PySequence_GetItem($input, i);
assert(o);
if (PyUnicode_Check(o))
{
PyObject* pyStr = PyUnicode_AsEncodedString(o, "utf-8", "Error -");
assert(pyStr);
const char* contents = PyBytes_AsString(pyStr);
assert(contents);
$1->push_back(std::string(contents));
Py_DECREF(pyStr);
Py_DECREF(o);
}
else
{
Py_DECREF(o);
delete $1;
SWIG_exception(SWIG_TypeError, "sequence must contain only strings");
return NULL;
}
}
}
else
{
SWIG_exception(SWIG_TypeError, "expected a sequence of strings");
return NULL;
}
}
%typemap(freearg) (const std::vector<std::string>&)
{
delete $1;
}
%typemap(out) std::vector<std::string>
{
const unsigned long vsize = $1.size();
$result = PyList_New(vsize);
if ($result)
{
bool ok = true;
for (unsigned long i=0; i<vsize && ok; ++i)
{
PyObject* copy = PyUnicode_FromString(($1[i]).c_str());
if (copy == NULL)
ok = false;
else
PyList_SET_ITEM($result, i, copy);
}
if (!ok)
{
Py_DECREF($result);
$result = NULL;
}
}
}
%typemap(out) (const std::vector<std::string>&)
{
const unsigned long vsize = $1->size();
$result = PyTuple_New(vsize);
if ($result)
{
bool ok = true;
for (unsigned long i=0; i<vsize && ok; ++i)
{
PyObject* copy = PyUnicode_FromString((*$1)[i].c_str());
if (copy == NULL)
ok = false;
else
PyTuple_SET_ITEM($result, i, copy);
}
if (!ok)
{
Py_DECREF($result);
$result = NULL;
}
}
}

File Metadata

Mime Type
text/x-c
Expires
Tue, Sep 30, 4:41 AM (1 d, 14 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
6560885
Default Alt Text
vec_string.i (2 KB)

Event Timeline