Skip to content

Commit

Permalink
@2336839 Handle std::forward_list data type conversion. Currently SWI…
Browse files Browse the repository at this point in the history
…G doesn't provide std_forward_list.i for data type conversion between Python and C++.
  • Loading branch information
yongchen1999 authored and tsuna committed May 18, 2015
1 parent 2e48d90 commit a06685a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions SwigUtils.i
@@ -1,6 +1,8 @@
// Copyright (c) 2014 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

%include exception.i

// Based on http://www.swig.org/Doc2.0/SWIGDocumentation.html#Python_nn60
%typemap(in) (int argc, char **argv) {
if (!PyList_Check($input)) {
Expand Down Expand Up @@ -52,3 +54,58 @@ namespace std {
size_t size() const;
};
}

%define STD_FORWARD_LIST(data_type)

// Python --> C
%typemap(in) std::forward_list<eos::##data_type> const & {
PyObject *t = $input;
if (PyTuple_Check(t) != true) {
PyErr_SetString(PyExc_TypeError, "argument must be a tuple");
return NULL;
}

std::forward_list<eos::##data_type> *a = new std::forward_list<eos::##data_type>();
auto it = a->before_begin();
for (int i=0; i<PyTuple_Size(t); i++) {
PyObject *fv = PyTuple_GetItem(t, i);
eos::##data_type *fv_c;
if (SWIG_ConvertPtr(fv, (void **)&fv_c,
SWIGTYPE_p_eos__##data_type,
SWIG_POINTER_EXCEPTION) == -1) {
char buf[256];
delete a;
snprintf(buf, 256, "expected data type is eos::%s.", #data_type);
SWIG_exception(SWIG_TypeError, buf);
return NULL;
} else {
it = a->insert_after(it, *fv_c);
}
}

$1 = a;
}

// C --> Python
%typemap(out) std::forward_list<eos::##data_type> const & {
std::forward_list<eos::##data_type> *a = $1;
std::forward_list<eos::##data_type>::const_iterator it;
int count = 0;
for (it=a->cbegin(); it!=a->cend(); ++it) {
count += 1;
}
PyObject *t = PyTuple_New(count);
int i = 0;
for (it=a->cbegin(); it!=a->cend(); ++it) {
PyObject *fv = SWIG_NewPointerObj((void *)&(*it),
SWIGTYPE_p_eos__##data_type,
0);
PyTuple_SetItem(t, i, fv);
i++;
}
$result = t;
}
%enddef

STD_FORWARD_LIST(fib_via_t)
STD_FORWARD_LIST(mpls_label_t)

0 comments on commit a06685a

Please sign in to comment.