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

2012-10-02 Thread Stefan Seefeld
On 10/02/2012 12:29 PM, Luiz Vitor Martinez Cardoso wrote:
>
> All the members are initialized to 0 or NULL if it's a pointer.
>
> The problem is that I'm getting a "Segmentation Fault" message when I
> try to access the pointers members from Python.

Can you elaborate on what you want to happen in that case ? Python has
no notion of pointers, so the pointers would need to be dereferenced. If
that isn't what you want, you need to add some logic to return something
else if the members are not initialized yet.

Stefan


-- 

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

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


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

2012-10-02 Thread Luiz Vitor Martinez Cardoso
Stefan,

Thank you!

To clarify my explanation... the pointer declarations are members from a
class called FaceObject.

qint32 * gender;
float * age;
float * ageDeviationDen;
quint32 * happy;
quint32 * sad;
quint32 * surprised;
quint32 * angry;

Depending on the software flow these pointers may or may not be initialized.

The problem occurs when the pointers are not initialized (poiting to NULL)
and I try to read their value inside Python.

# faceobject_poc.py

t = faceparser.FaceObject()
t.mergeCount <= It's okay!
t.gender <= I got an Seg. Fault... because qint32 * gender = NULL


I of course could try a qint32 * gender = new quint32()... but do you know
any way to handle the non-intialized members?

Thank you!


On Tue, Oct 2, 2012 at 1:35 PM, Stefan Seefeld  wrote:

> On 10/02/2012 12:29 PM, Luiz Vitor Martinez Cardoso wrote:
> >
> > All the members are initialized to 0 or NULL if it's a pointer.
> >
> > The problem is that I'm getting a "Segmentation Fault" message when I
> > try to access the pointers members from Python.
>
> Can you elaborate on what you want to happen in that case ? Python has
> no notion of pointers, so the pointers would need to be dereferenced. If
> that isn't what you want, you need to add some logic to return something
> else if the members are not initialized yet.
>
> Stefan
>
>
> --
>
>   ...ich hab' noch einen Koffer in Berlin...
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Regards,

Luiz Vitor Martinez Cardoso
Celular: (11) 7351-7097 | Skype: grabberbr
engineer student at 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"
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

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

2012-10-02 Thread Luiz Vitor Martinez Cardoso
Stefan,

I would like to do on the Python side:

t = faceparser.FaceObject()
if (t.gender = None):
   print 'gender is not defined'
else:
   print gender

Best regards,
Luiz Vitor.

On Tue, Oct 2, 2012 at 1:44 PM, Luiz Vitor Martinez Cardoso <
[email protected]> wrote:

> Stefan,
>
> Thank you!
>
> To clarify my explanation... the pointer declarations are members from a
> class called FaceObject.
>
> qint32 * gender;
> float * age;
> float * ageDeviationDen;
> quint32 * happy;
> quint32 * sad;
> quint32 * surprised;
> quint32 * angry;
>
> Depending on the software flow these pointers may or may not be
> initialized.
>
> The problem occurs when the pointers are not initialized (poiting to NULL)
> and I try to read their value inside Python.
>
> # faceobject_poc.py
>
> t = faceparser.FaceObject()
> t.mergeCount <= It's okay!
> t.gender <= I got an Seg. Fault... because qint32 * gender = NULL
>
>
> I of course could try a qint32 * gender = new quint32()... but do you know
> any way to handle the non-intialized members?
>
> Thank you!
>
>
> On Tue, Oct 2, 2012 at 1:35 PM, Stefan Seefeld wrote:
>
>> On 10/02/2012 12:29 PM, Luiz Vitor Martinez Cardoso wrote:
>> >
>> > All the members are initialized to 0 or NULL if it's a pointer.
>> >
>> > The problem is that I'm getting a "Segmentation Fault" message when I
>> > try to access the pointers members from Python.
>>
>> Can you elaborate on what you want to happen in that case ? Python has
>> no notion of pointers, so the pointers would need to be dereferenced. If
>> that isn't what you want, you need to add some logic to return something
>> else if the members are not initialized yet.
>>
>> Stefan
>>
>>
>> --
>>
>>   ...ich hab' noch einen Koffer in Berlin...
>>
>> ___
>> Cplusplus-sig mailing list
>> [email protected]
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>
>
>
> --
> Regards,
>
> Luiz Vitor Martinez Cardoso
> Celular: (11) 7351-7097 | Skype: grabberbr
> engineer student at 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"
>
>


-- 
Regards,

Luiz Vitor Martinez Cardoso
Celular: (11) 7351-7097 | Skype: grabberbr
engineer student at 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"
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

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

2012-10-02 Thread Stefan Seefeld

I would write little wrapper functions that check the pointer, and if
it's NULL, return None instead of the dereferenced value.

To illustrate:

  object get_age(FaceObject &f) { return f.getAge() ?
object(*f.getAge()) : object();}

  ...

  class_(...).add_property("age", get_age);

Thus, if the pointer is initialized, the value pointed to will be
returned, and if not, None.

-- 

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

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


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

2012-10-02 Thread Jeffrey Van Voorst

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
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


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

2012-10-02 Thread Luiz Vitor Martinez Cardoso
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 
> 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", init<>())
>.
>.
>.
>.add_property("age", get_py_age)  // <= It's working!
>.add_property("age", get_py_wrapper(&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
> 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(reinterpret_cast(&FaceObject::getAge)))

I appreciate your support very much!

Best regards,
Luiz Vitor


On Tue, Oct 2, 2012 at 2:05 PM, Jeffrey Van Voorst  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
> [email protected]
> http://mail.python.org/**mailman/listinfo/cplusplus-sig
>



-- 
Regards,

Luiz Vitor Martinez Cardoso
Celular: (11) 7351-7097 | Skype: grabberbr
engineer student at 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"
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig