[C++-sig] boost::any
Christophe de VIENNE
cdevienne at alphacent.com
Thu Feb 23 12:37:27 CET 2006
Ralf W. Grosse-Kunstleve wrote:
[...]
> Couldn't you just do this?
>
> class_<boost::any>("boost_any", no_init);
> class<std::vector<boost::any> >("stl_vector_boost_any", no_init);
Probably :-) I didn't event thought about it !
> You couldn't manipulate these types from Python, but at least you can
> return a boost::any from one function and use it as an argument in
> another. If you want to be fancy, you could give boost_any some methods,
> e.g. to return the value type name as a string or to convert certain types
> you know/care about to a boost::python::object. E.g. (untested):
>
> boost::python::object
> any_extract(boost::any const& self)
> {
> if (self.empty()) return boost::python::object(); // None
> if (self.type() == typeid(int)) {
> return boost::python::object(*boost::any_cast<int>(self));
> }
> if (self.type() == typeid(double)) {
> return boost::python::object(*boost::any_cast<double>(self));
> }
> throw std::runtime_error("boost::any: unkown value type");
> }
Could I, with this method, define converter::arg_to_python, arg_from_python
and return_from_python ? I can't see why not for now, so I'll try to play
with that.
> class_<boost::any>("boost_any", no_init)
> .def("empty", &boost::any::empty)
> .def("extract", any_extract)
> ;
>
> Note that you can wrap an unbound C++ function as a Python method if the
> first argument of the function is of the wrapped type.
>
> You could even enable construction of any objects from Python by injecting
> your custom constructor using boost::python::make_constructor.
>
> If you want to manipulate the std::vector<boost::any> from Python you
> could most likely use the vector indexing suite that comes with
> Boost.Python.
>
> Unless I am overlooking something fundamental (I've never worked with
> boost::any) I think you can make this work really nicely.
I'll have a try and will come back with my eventual solution.
Thanks for the suggestions !
Regards,
Christophe
More information about the Cplusplus-sig
mailing list