Boost logo

Boost Users :

Subject: Re: [Boost-users] Exposing std::pair to Python
From: Micha³ Nowotka (mmmnow_at_[hidden])
Date: 2009-04-16 19:18:14


I think I can partially answer your question. Although I don't know
how to expose std::pair using indexing suite, thanks to Roman
Yakovenko and his Py++ package, I can show you working example of
exporting pair using different approach:

This is *.h file with class containing variable of type std::pair<int,
int> to be exposed (this file is also available here:
http://pygccxml.svn.sourceforge.net/viewvc/pygccxml?view=rev):

// Copyright 2004-2008 Roman Yakovenko.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef __std_pair_to_be_exported_hpp__
#define __std_pair_to_be_exported_hpp__

#include <utility>

struct tester_t{
    tester_t(int a, int b){
       pair_.first = a;
       pair_.second = b;
    }

    int compute(){return pair_.first + pair_.second;}

    std::pair<int, int> pair_;
};
#endif//__std_pair_to_be_exported_hpp__

And here is wrapper code generated by Py++:

// This file has been generated by Py++.

// Copyright 2004-2008 Roman Yakovenko.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "boost/python.hpp"

#include "std_pair_to_be_exported.hpp"

namespace bp = boost::python;

BOOST_PYTHON_MODULE(std_pair){
    bp::class_< std::pair< int, int > >(
"pair_less__int_comma__int__greater_", "documentation", bp::init<
>("documentation") )
        .def( bp::init< int const &, int const & >(( bp::arg("__a"),
bp::arg("__b") ), "documentation") )
        .def_readwrite( "first", &std::pair< int, int >::first,
"documentation" )
        .def_readwrite( "second", &std::pair< int, int >::second,
"documentation" );

    bp::class_< tester_t >( "tester_t", "documentation", bp::init<
int, int >(( bp::arg("a"), bp::arg("b") ), "documentation") )
        .def(
            "compute"
            , (int ( ::tester_t::* )( ) )( &::tester_t::compute )
            , "documentation" )
        .def_readwrite( "pair_", &tester_t::pair_, "documentation" );
}

You should notice that in python:
pairA = module.pair_less__int_comma__int__greater_(2,3)
pairB = module.pair_less__int_comma__int__greater_(2,3)
pairA.first == pairB.first

>True

pairA.second == pairB.second

>True

BUT:

pairA == pairB

>False

I'm also interested in exposing std::pair using indexing suite because
it perhaps make statement pairA == pairB become true.
Especially if that code would be auto-generated by Py++ just like
other STL containers it could be very useful. Roman Yakovenko wrote
there is a chance this functionality would be added in next release of
Py++.

Also you can ask this question on Cplusplus-sig mailing list:
http://mail.python.org/mailman/listinfo/cplusplus-sig

If you find accurate answer to your question, please let me know.

-- 
Regards
Micha³ Nowotka

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net