[C++-sig] Boost tuple example?

Matt mjkeyes at sbcglobal.net
Fri Dec 9 05:58:27 CET 2005


Basically, I'm wanting to be able to build a Python list object in C++ and 
pass it into Python.

Similarly, I'd like to be able to receive one of these list objects back 
from Python and be able to extract values from it in C++.

I'm sure this is a simple operation, and (since these are simple types) it 
looks like I can do the following (thanks Ralf for the pointer to the test 
directory):

void SetValues(handle <> hObject)
{
     object oObj(hObject);

     tuple Values("value 1", 2, 3.0);
     //call a python function to set the values, etc... in oObj
}

void GetValues(handle <>hObject)
{
     object oObj(hObject);
     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
    {
          const char * szVal = extract<const char *>(oValues[0]);
          int nVal = extract<int>(oValues[1]);
          double nDblVal = extract<double>(oValues[2]);
    }
    catch(...)
    {
          PyErr_Print();
    }
}

Does that seem reasonable?

Also, is there an easy way to determine the length of a tuple in C++?

Thanks again you guys,
Matt

"David Abrahams" <dave at boost-consulting.com> wrote in message 
news:uu0dj5qms.fsf at boost-consulting.com...
> "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> writes:
>
>> --- Matt <mjkeyes at sbcglobal.net> wrote:
>>
>>> Does anyone have an example of boost's use of tuples?  For example, I am
>>> wanting to construct tuples in C++ and pass them on to Python (these 
>>> will
>>> consist of basic types (const char*'s, integers, etc.) or object values.
>>
>> Look in the file:
>>
>> boost/libs/python/test/tuple.cpp
>>
>> You can probably learn a lot of tricks by spending 30 minutes to scan 
>> through
>> all the *.cpp files in the boost/libs/python/test directory.
>
> I read the OP as asking about how to translate boost::tuple, a
> completely C++ generalization of std::pair (rather than
> boost::python::tuple, a wrapper for native Python tuples) to/from
> Python's tuples.
>
> Which is it?
>
> -- 
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com 






More information about the Cplusplus-sig mailing list