Troy

Thanks .. your hints are most helpful .. I'll be back :-)

Tim

On 13/01/2010 19:10, troy d. straszheim wrote:
Tim Couper wrote:
OK. Here's what I've been stuck with all today .. I have a 3rd party C++ program function which returns a boost::variant (and its inverse)

my_variant my_variant_of_string(const std::string& str)

This one takes a string & returns a variant, and am trying to wrap this in python, so that there I can have

>>> my_variant_of_string('hello')
'hello'

>>> my_variant_of_string('1.2')
1.2

>>> my_variant_of_string('10')
10

Simple, eh? ...

How about something like (not tested):

using boost::python::object;

typedef variant<...> variant_t;

variant_t makes_variant_from(string s); // defined elsewhere

//
// visitor for converting contents of visitor to object
//
struct vc : boost::static_visitor<object>
{
  template <typename T>
  object operator()(const T& v) const
  {
     return v;
  }
};

bp::object
thunk(string s)
{
  variant_t v = makes_variant_from(s);
  return boost::apply_visitor(vc(), v);
}

BOOST_PYTHON_MODULE(mod)
{
  def("f", &thunk);
}


You could probably use this to just register a converter from variant to vanilla boost::python::object as well, if this is more convenient...

-t



_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig



No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.725 / Virus Database: 270.14.138/2618 - Release Date: 01/13/10 
07:35:00

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to