[C++-sig] Boost tuple example?

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Fri Dec 9 13:42:29 CET 2005


--- Matt <mjkeyes at sbcglobal.net> wrote:
> void SetValues(handle <> hObject)
> {
>      object oObj(hObject);

I'd make this

  void SetValues(object& oObj)
  {

> 
>      tuple Values("value 1", 2, 3.0);

Does this work? I was thinking you need

       tuple Values = make_tuple("value 1", 2, 3.0);

but I am not sure.

>      //call a python function to set the values, etc... in oObj
> }
> 
> void GetValues(handle <>hObject)
> {
>      object oObj(hObject);

Same as above:

  void GetValues(object& oObj)
  {

Or if you are expecting a tuple this may be even better:

  void GetValues(tuple oValues)

>      tuple oValues;
>      //call Python function to retrieve a tuple object into oValues
>      //not quite sure what this would look like - i know
>      //tuple is an object, but should i try/catch a call to a python
>      //function into a tuple variable?
> 
>     try
>     {

I don't think it is advisable to use try + catch() here. Just let the exception
propagate through to Python.

>           const char * szVal = extract<const char *>(oValues[0]);
>           int nVal = extract<int>(oValues[1]);
>           double nDblVal = extract<double>(oValues[2]);

I believe this works on most compilers, but not all. I always do it like this:

            const char * szVal = extract<const char *>(oValues[0])();

I.e. with an additional "()" at the end. Similar for the two other extract<>
statements.
            
>     }
>     catch(...)
>     {
>           PyErr_Print();
>     }
> }
> 
> Does that seem reasonable?
> 
> Also, is there an easy way to determine the length of a tuple in C++?

David insisted on not giving us .size(), much to my dismay. Therefore you have
to say:

  #include <boost/python/detail/api_placeholder.hpp>

and then:

  boost::python::len(obj)

David: I still believe we should add .size() to tuple, list, str. The current
solution is a real productivity killer; you won't believe how much time I
spent/wasted over the years digging out that silly include above again and
again. Purity is not always a virtue.

Cheers,
        Ralf


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list