[C++-sig] exposing iterator for the values of std::map

Luca Sbardella luca.sbardella at gmail.com
Tue May 22 12:53:28 CEST 2007


Hi,
I'm trying to expose to python the iterator of a specific class with this
skeleton

class bla;
typedef boost::shared_ptr<bla> BLA;

class bla  {
public:
  typedef std::map<std:string,BLA> container;
  typedef container::iterator  iterator;

  static BLA  get(const std::string& code);

  static iterator  begin() {return s_db.begin();}
  static iterator  end()   {return s_db.end();}

  std::string code() const {return ...}

private:
  static container s_db;
};

the bla class is exposed to Python in the usual way. my question is how do I
expose the iterator such that in Python I can iterate through the elements
in s_db as it was a list (I want to see the BLA elements not the keys of the
map).
In other words I would like to be able to do this

b = bla(...)
for  i in b:
  print i.code()

Naively, I tried to use .def("__iter__", range(&bla::begin,&bla::end)) but,
not surprisingly, it doesn't work (something to do with the implementation
of NextPolicies I assume).

Best
Luca
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070522/e51738f7/attachment.htm>


More information about the Cplusplus-sig mailing list