[C++-sig] std::vector
David Abrahams
dave at boost-consulting.com
Wed Oct 2 16:44:39 CEST 2002
Hi Scott,
I think Ralf Grosse-Kunstleve is probably the one to answer your questions.
He's the one who implemented the import/export stuff for Boost.Python v1,
and has the most expertise in translating containers. He also has
implemented a much better mechanism for Boost.Python v2. He's been
travelling, but IIUC he might be able to get back to you by the end of the
week.
-Dave
-----------------------------------------------------------
David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com
----- Original Message -----
From: "Scott A. Smith" <ssmith at magnet.fsu.edu>
To: <c++-sig at python.org>
Sent: Wednesday, October 02, 2002 10:02 AM
Subject: [C++-sig] std::vector
> Hello,
>
> I have been reading the documentation on dealing with std::vector<> in
> Boost.Python, http://www.boost.org/libs/python/doc/cross_module.html, and
> this is exactly what I need for both vectors of built-in types and those
> using my own classes. I have used the class builder to expose such a
vector
> as a Python object and now need to be able to go back and export some C++
> functions that use them as arguments or as a return type.
>
> Is there a module already in Boost.Python that has "std_vector"? I cannot
> find anything on it and the html page above only supposes that one had
such
> a module. I think it would help me out quite a bit if I could play with
some
> vector<int> functions to try out what is suggested. Even more, if some of
> this is already in Boost.Python I would surely like to use it rather than
> begin doing so for all the class types I am using with std::vector.
>
> For example, I have this to expose my std::vector<std::string>:
>
> boost::python::class_builder<std::vector<std::string> >
> Vstring(PyModule, "Vstring");
> Vstring.def(boost::python::constructor<>());
> Vstring.def(Vstring_push_back, "push_back");
>
> and this allows me to make objects of Vstring in Python just fine. The
> push_back stuff is done as shown in the comprehensive test. Now, how do I
> get
>
> VS = Vstring()
> VS = MyFunctionReturningStdVectorOfStrings()
> MyFunctionUsingStdVectorOfStrings(VS)
>
> by exporting C++ functions that are something like
>
> std::vector<std::string> MyFunctionReturningStdVectorOfStrings();
> void MyFunctionUsingStdVectorOfStrings(std::vector<std::string>);
>
> to work in Python. Do I need import_converters/export_converters? Do I
need
> them even when everything is in the same module? Neither the
"vector_double"
> nor "string_map" in the comprehensive test show this and AFAIK the test
does
> have any code where C++ functions using and returning std::vector<double>
> are exported and making use of vector_double.
>
> Is there other documentation on this, or perhaps a place where I can
easily
> search for such info?
>
> Thanks,
> Scott
>
>
> =======================================
> Dr. Scott A. Smith
> Associate in Research
> National High Magnetic Field Laboratory
> 1800 East Paul Dirac Drive
> Tallahassee, FL 32310
>
> phone: (850) 644-6348
> FAX: (850) 644-1366
> email: ssmith at magnet.fsu.edu
> http://www.magnet.fsu.edu
> http://gamma.magnet.fsu.edu
>
>
>
> > -----Original Message-----
> > From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org]On
> > Behalf Of David Abrahams
> > Sent: Wednesday, October 02, 2002 8:05 AM
> > To: c++-sig at python.org
> > Subject: Re: [C++-sig] Pointer manipulation Boost.Python - C++
> >
> >
> > I'm going to answer this in terms of Boost.Python v2, since v1 is now
> > retired and won't be in the upcoming release.
> >
> > From: "Mathieu Tremblay" <mtremblay at golemlabs.com>
> >
> >
> > > Hello,
> > > I am taking a look at Boost.Python and I wonder if it is possible
> > > to convert pointers to existing classes to PyObjects* and then be
> > > able to pass an existing instance of an object directly to and from
> > > python:
> >
> > It is.
> >
> > > Example:
> > > In C++ I create a CWheel and I set its member m_diameter to 1233.
In
> > > python I have a function that receives a CWheel and displays the
> > > diameter (Lets suppose that Python knows the CWheel from
Boost.Python
> > > :-) )
> > > Ex : def printCWheelDiam(awheel):
> > > print awheel.m_diameter
> > >
> > > What I would like to do Is to create the CWheel in C++,
> > > pass its pointer to python so it can manipulate
> > > it (and see that its diameter is 1233) and then return a
> > > CWheel that I could map from a PyObject to a CWheel in C++.
> >
> > The safest thing to do is to create the CWheel by invoking its class
> > wrapper:
> >
> > // Declare the CWheel extension class
> > object wheel_class =
> > class_<CWheel>("CWheel")
> > .def_readonly("m_diameter", &CWheel::m_diameter)
> > .def("some_member_function", &CWheel::some_member_function)
> > ...
> > ;
> >
> > object wheel_obj = wheel_class(); // construct one
> >
> > Now you can pass wheel_obj back to python, and all reference counts are
> > nicely managed. You don't need to "map" anything between C++ and
Python;
> > the library takes care of that for you.
> >
> > If you really want to pass pointers around, it's certainly
> > possible to tell
> > the library to build a Python object around the pointer, but then you
need
> > to make sure the lifetime of the C++ object being referenced by
> > the pointer
> > extends past the lifetime of all Python references to the object or
your
> > program will crash.
> >
> >
> > -----------------------------------------------------------
> > David Abrahams * Boost Consulting
> > dave at boost-consulting.com * http://www.boost-consulting.com
> >
> >
> >
> > _______________________________________________
> > C++-sig mailing list
> > C++-sig at python.org
> > http://mail.python.org/mailman/listinfo/c++-sig
> >
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
More information about the Cplusplus-sig
mailing list