[C++-sig] map of vectors with indexing suites

Nicolas Rougier Nicolas.Rougier at loria.fr
Wed May 21 18:37:39 CEST 2008


Hello,


I've been using the vector_indexing_suite and the map_indexing_suite to
interface a map of vector and it is almost working. My problem is when I
want to iterate through the map: the type of elements is something like

map_indexing_suite_MyMapClass_Entry

while I would have expected a tuple of key/value. Do I do something
wrong or is it the expected behavior ? What I would like to do in the
end is to iterate through the map then iterate through each vector.

Also, I would like to have a nice __repr__ method for the map and the
vectors but I did not find how to add easily such methods (currently, I
create a class that inherit the std::map only to add the repr method).


Nicolas


module.py
----------------------------------------------------------------------
from _module import *

u1 = Unit()
u2 = Unit()
u1.connect (u2, 'type1')
u1.links['type1'].append (Link(u2))
u1.links['type2'] = LinkVec()

print type (u1.links['type1'])
print type (u1.links['type2'])
for lst in u1.links:
    print lst, type(lst)
    # for l in ??




module.cc
----------------------------------------------------------------------
// g++ module.cc -I/usr/include/python2.5 -fPIC -shared -lboost_python
-lpython2.5 -o _module.so

#include <string>
#include <vector>
#include <map>
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>

typedef boost::shared_ptr<class Link>     LinkPtr;
typedef boost::shared_ptr<class Unit>     UnitPtr;
typedef std::vector<LinkPtr>              LinkVec;
typedef boost::shared_ptr<LinkVec>        LinkVecPtr;
typedef std::map<std::string, LinkVecPtr> LinkMap;
typedef boost::shared_ptr<LinkMap>        LinkMapPtr;

class Link {
public:
    Link (UnitPtr src) : source(src) {};
    virtual ~Link (void) {};
protected:
    UnitPtr source;
};

class Unit {
public:
    Unit (void) {};
    virtual ~Unit (void) {};
    void connect (UnitPtr other, std::string type) {
        if (not links[type])
            links[type] = LinkVecPtr (new LinkVec);
        links[type]->push_back (LinkPtr (new Link (other)));
    }

public:
    LinkMap links;
};

BOOST_PYTHON_MODULE(_module)
{
    using namespace boost::python;

    register_ptr_to_python <UnitPtr> ();
    register_ptr_to_python <LinkPtr> ();
    register_ptr_to_python <LinkVecPtr> ();
    register_ptr_to_python <LinkMapPtr> ();

    class_<LinkVec >("LinkVec")
        .def(vector_indexing_suite <LinkVec, true >())
        ;

    class_<LinkMap >("LinkMap")
        .def(map_indexing_suite <LinkMap, true >())
        ;

    class_<Unit> ("Unit", init<>())
        .def ("connect", &Unit::connect)
        .def_readwrite ("links", &Unit::links)
    ;

    class_<Link> ("Link", init<UnitPtr>())
    ;

}





More information about the Cplusplus-sig mailing list