test if object instance is instance of extension class in C++

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

test if object instance is instance of extension class in C++

Holger Joukl

Hi,

what's the recommended way to check if an object instance is an instance of
my extension class
in C++ code?

I'm currently doing a very ugly

#define isMyExtensionClass(pyobj) (strcmp(pyobj->ob_type->tp_name,
"MyExtensionClass") == 0)

on PyObj* pyobj.

I bet there is a better way but just can't seem to find it.

Holger

Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: test if object instance is instance of extension class in C++

Stefan Seefeld-2
On 2012-02-23 11:50, Holger Joukl wrote:

> Hi,
>
> what's the recommended way to check if an object instance is an instance of
> my extension class
> in C++ code?
>
> I'm currently doing a very ugly
>
> #define isMyExtensionClass(pyobj) (strcmp(pyobj->ob_type->tp_name,
> "MyExtensionClass") == 0)
>
> on PyObj* pyobj.
>
> I bet there is a better way but just can't seem to find it.

bpl::extract<MyExtensionClass> e(pyobj);
if (e.check()) ...

Note that you may want to use a reference type as template argument to
the boost::python::extract type, if you want to retrieve the object by
reference instead of by value.

See http://www.boost.org/doc/libs/1_40_0/libs/python/doc/v2/extract.html
for details.


HTH,
        Stefan


--

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: test if object instance is instance of extension class in C++

Holger Joukl
Stefan,

many thanks, that of course did the trick. I'm not seeing the wood for the
trees
any more, I fear.

> > what's the recommended way to check if an object instance is an
instance of

> > my extension class
> > in C++ code?
> >
> > I'm currently doing a very ugly
> >
> > #define isMyExtensionClass(pyobj) (strcmp(pyobj->ob_type->tp_name,
> > "MyExtensionClass") == 0)
> >
> > on PyObj* pyobj.
> >
> > I bet there is a better way but just can't seem to find it.
>
> bpl::extract<MyExtensionClass> e(pyobj);
> if (e.check()) ...
>
> Note that you may want to use a reference type as template argument to
> the boost::python::extract type, if you want to retrieve the object by
> reference instead of by value.
>
> See http://www.boost.org/doc/libs/1_40_0/libs/python/doc/v2/extract.html
> for details.

Just for the record, this is what I do now:

inline
bool isMyExtensionClass(PyObject* &pyobj) {
    bp::extract<MyExtensionClass&> extractor(pyobj);
    return extractor.check();
}


inline
bool isMyExtensionClass(bp::object &obj) {
    bp::extract<MyExtensionClass&> extractor(obj);
    return extractor.check();
}

Thanks again & have a nice weekend
Holger

Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: test if object instance is instance of extension class in C++

Stefan Seefeld-2
On 02/24/2012 05:44 AM, Holger Joukl wrote:
> Just for the record, this is what I do now:
>
> inline
> bool isMyExtensionClass(PyObject* &pyobj) {
>     bp::extract<MyExtensionClass&> extractor(pyobj);
>     return extractor.check();
> }

I'm not sure why you use raw PyObject pointers in the first place. That
should all be hidden behind bpl::object instances (which will then take
care of the ref counting business for you).


> inline
> bool isMyExtensionClass(bp::object &obj) {
>     bp::extract<MyExtensionClass&> extractor(obj);
>     return extractor.check();
> }

bp::object itself has reference (smart pointer) semantics, so there is
no need to pass objects by reference.

    Stefan


--

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: test if object instance is instance of extension class in C++

Holger Joukl
Hi,

> On 02/24/2012 05:44 AM, Holger Joukl wrote:
> > Just for the record, this is what I do now:
> >
> > inline
> > bool isMyExtensionClass(PyObject* &pyobj) {
> >     bp::extract<MyExtensionClass&> extractor(pyobj);
> >     return extractor.check();
> > }
>
> I'm not sure why you use raw PyObject pointers in the first place. That
> should all be hidden behind bpl::object instances (which will then take
> care of the ref counting business for you).

We're porting a legacy boost.python v1-wrapped library to current
boost.python.
Some parts of its internal methods operate on raw PyObject pointers. While
we're probably going to change that for now I all I want is a running
version
and change as little as possible.

> > inline
> > bool isMyExtensionClass(bp::object &obj) {
> >     bp::extract<MyExtensionClass&> extractor(obj);
> >     return extractor.check();
> > }
>
> bp::object itself has reference (smart pointer) semantics, so there is
> no need to pass objects by reference.

I see. Just out of curiosity:
If I pass by reference I do save a copy constructor call, don't I?
But I circumvent the bp::object refcount safety, though(?).
But in a type check like above this shouldn't be dangerous - right?

Holger

Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: test if object instance is instance of extension class in C++

Stefan Seefeld-2
On 02/24/2012 09:33 AM, Holger Joukl wrote:
> bp::object itself has reference (smart pointer) semantics, so there is
> no need to pass objects by reference.
> I see. Just out of curiosity:
> If I pass by reference I do save a copy constructor call, don't I?

In principle, yes. I'm not sure how much of this the compiler would be
able to optimize away, though.

> But I circumvent the bp::object refcount safety, though(?).

Well, yes, but you don't need that as C++ language semantics ensure the
object lifetime.

> But in a type check like above this shouldn't be dangerous - right?

There is definitely no danger in doing this. It may just add a little
bit of overhead (since in certain cases passing by value is cheaper than
passing by reference, depending on the involved types and optimization
level.

    Stefan


--

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: test if object instance is instance of extension class in C++

Holger Joukl

> > bp::object itself has reference (smart pointer) semantics, so there is
> > no need to pass objects by reference.
> > I see. Just out of curiosity:
> > If I pass by reference I do save a copy constructor call, don't I?
>
> In principle, yes. I'm not sure how much of this the compiler would be
> able to optimize away, though.
>
> > But I circumvent the bp::object refcount safety, though(?).
>
> Well, yes, but you don't need that as C++ language semantics ensure the
> object lifetime.
>
> > But in a type check like above this shouldn't be dangerous - right?
>
> There is definitely no danger in doing this. It may just add a little
> bit of overhead (since in certain cases passing by value is cheaper than
> passing by reference, depending on the involved types and optimization
> level.

Ok, this sounds like its best to do

inline
bool isMyExtensionClass(PyObject* pyobj) {
    bp::extract<MyExtensionClass&> extractor(pyobj);
    return extractor.check();
}


inline
bool isMyExtensionClass(bp::object obj) {
    bp::extract<MyExtensionClass&> extractor(obj);
    return extractor.check();
}

Many thanks again Stefan,
Holger

Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig