add swig::PyItem and all the helper code
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7787 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
07ff83fdc3
commit
40a65ea681
7 changed files with 206 additions and 6 deletions
|
|
@ -24,8 +24,64 @@
|
|||
|
||||
%include std_except.i
|
||||
|
||||
|
||||
|
||||
namespace swig {
|
||||
%ignore PyItem;
|
||||
struct PyItem {};
|
||||
%apply PyObject * {PyItem};
|
||||
%apply PyObject * const& {PyItem const&};
|
||||
}
|
||||
|
||||
%fragment(SWIG_Traits_frag(swig::PyItem),"header",fragment="StdTraits") {
|
||||
namespace swig {
|
||||
template <> struct traits<PyItem > {
|
||||
typedef value_category category;
|
||||
static const char* type_name() { return "PyItem"; }
|
||||
};
|
||||
|
||||
template <> struct traits_from<PyItem> {
|
||||
typedef PyItem value_type;
|
||||
static PyObject *from(const value_type& val) {
|
||||
return static_cast<PyObject *>(val);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct traits_check<PyItem, value_category> {
|
||||
static bool check(PyObject *obj) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct traits_asval<PyItem > {
|
||||
typedef PyItem value_type;
|
||||
static int asval(PyObject *obj, value_type *val) {
|
||||
if (val) *val = obj;
|
||||
return SWIG_OK;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
%fragment(SWIG_Traits_frag(PyItem),"header")
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
%fragment("PySequence_Base","header")
|
||||
{
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct less <PyObject *>: public binary_function<PyObject *, PyObject *, bool>
|
||||
{
|
||||
bool
|
||||
operator()(PyObject * v, PyObject *w) const
|
||||
{ return PyObject_Compare(v, w) < 0; }
|
||||
};
|
||||
}
|
||||
|
||||
namespace swig {
|
||||
template <> struct traits<PyObject *> {
|
||||
typedef value_category category;
|
||||
|
|
@ -45,6 +101,59 @@ namespace swig {
|
|||
return val;
|
||||
}
|
||||
};
|
||||
|
||||
class PyItem {
|
||||
PyObject *_obj;
|
||||
public:
|
||||
PyItem(const PyItem& item) : _obj(item._obj)
|
||||
{
|
||||
Py_XINCREF(_obj);
|
||||
}
|
||||
|
||||
PyItem(PyObject *obj = 0) :_obj(obj)
|
||||
{
|
||||
Py_XINCREF(obj);
|
||||
}
|
||||
|
||||
PyItem & operator=(PyObject *obj) {
|
||||
Py_XINCREF(obj);
|
||||
Py_XDECREF(_obj);
|
||||
_obj = obj;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PyItem & operator=(const PyItem& item) {
|
||||
this->operator=(static_cast<PyObject *>(item));
|
||||
return *this;
|
||||
}
|
||||
|
||||
~PyItem()
|
||||
{
|
||||
Py_XDECREF(_obj);
|
||||
}
|
||||
|
||||
operator PyObject *() const
|
||||
{
|
||||
return _obj;
|
||||
}
|
||||
|
||||
PyObject *operator->() const
|
||||
{
|
||||
return _obj;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct less <swig::PyItem>: public binary_function<swig::PyItem, swig::PyItem, bool>
|
||||
{
|
||||
bool
|
||||
operator()(const swig::PyItem& v, const swig::PyItem& w) const
|
||||
{ return PyObject_Compare(v, w) < 0; }
|
||||
};
|
||||
}
|
||||
|
||||
namespace swig {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue