[C++-sig] How to hangle a NULL pointer?

Luiz Vitor Martinez Cardoso grabber at gmail.com
Tue Oct 2 21:28:22 CEST 2012


Stefan/Jeffrey,

Thank you, it's now working perfectly... but I have another issue.

I wrote:

object get_py_gender(const FaceObject &fobj)
> {
>     return fobj.gender ? object(*fobj.gender) : object();
> }
> object get_py_age(const FaceObject &fobj)
> {
>     return fobj.age ? object(*fobj.age) : object();
> }
> object get_py_agedeviation(const FaceObject &fobj)
> {
>     return fobj.ageDeviationDen ? object(*fobj.ageDeviationDen) : object();
> }
> object get_py_happy(const FaceObject &fobj)
> {
>     return fobj.happy ? object(*fobj.happy) : object();
> }
> object get_py_sad(const FaceObject &fobj)
> {
>     return fobj.sad ? object(*fobj.sad) : object();
> }
> object get_py_surprised(const FaceObject &fobj)
> {
>     return fobj.surprised ? object(*fobj.surprised) : object();
> }
> object get_py_angry(const FaceObject &fobj)
> {
>     return fobj.angry ? object(*fobj.angry) : object();
> }


Well... the functions are doing the same thing and don't like so much the
idea of repeating code unnecessarily, so I'm trying to write a template to
handle it.

template <class T>
> object get_py_wrapper(T obj)
> {
>     return obj ? object(*obj) : object();
> }


My problem is how to call the template function. The next code shows the
getAge function declaration.

inline quint8 getAge()
> {
>    return (quint8) qRound(*age / *ageDeviationDen);
> }


The class_ declaration (to bridge between languages) is:

void export_FaceObject()
> {
>     class_<FaceObject, boost::noncopyable>("FaceObject", init<>())
>        .
>        .
>        .
>        .add_property("age", get_py_age)  // <= It's working!
>        .add_property("age", get_py_wrapper<quint8>(&FaceObject::getAge))
>        .
>        .
>        .
>     ;
> }


And finally the error I'm getting is:

In file included from faceparser.cpp:10:0:
> ../../include/qttopyobjects.h: In function 'void export_FaceObject()':
> ../../include/qttopyobjects.h:369:71: error: no matching function for call
> to 'get_py_wrapper(quint8 (FaceObject::*)())'
> ../../include/qttopyobjects.h:369:71: note: candidate is:
> ../../include/qttopyobjects.h:353:8: note: template<class T>
> boost::python::api::object get_py_wrapper(T)
> ../../include/qttopyobjects.h:353:8: note:   template argument
> deduction/substitution failed:
> ../../include/qttopyobjects.h:369:71: note:   cannot convert
> '&FaceObject::getAge' (type 'quint8 (FaceObject::*)() {aka unsigned char
> (FaceObject::*)()}') to type 'unsigned char'



I'm sure that I need to use &FaceObject::getAge and quint8... but I can't
figure out what is going on. I already tried to do:

.add_property("age",
get_py_wrapper<quint8>(reinterpret_cast<quint8>(&FaceObject::getAge)))

I appreciate your support very much!

Best regards,
Luiz Vitor


On Tue, Oct 2, 2012 at 2:05 PM, Jeffrey Van Voorst <vanv0059 at umn.edu> wrote:

> You might want to use wrapper functions in C++
>
> boost::python::object
> gender_getter(const FaceObject& obj)
> {
>   if(obj.gender == NULL) return Py_None;
>   else return obj.gender;
> }
>
> Note: its been a bit since I used Boost.Python heavily.  The syntax could
> be incorrect, but I hope this gives you a general idea.
>
> --Jeff Van Voorst
>
> ______________________________**_________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/**mailman/listinfo/cplusplus-sig<http://mail.python.org/mailman/listinfo/cplusplus-sig>
>



-- 
Regards,

Luiz Vitor Martinez Cardoso
Celular: (11) 7351-7097 | Skype: grabberbr
engineer student at maua.br
 <http://maua.br>intern marketing engineer at geindustrial.com.br
entrepreneur at adboxnetwork.com

"If you wanna be successful, you need total dedication, go for your last
limit, give your best and love your love infinitely!"

"The only limits are the ones you place upon yourself"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20121002/c4235acf/attachment.html>


More information about the Cplusplus-sig mailing list