Michele De Stefano wrote:

there is a much easier way to treat a FILE* as a C++ stream. The easy
way is to use my open source library (mds-utils,
http://code.google.com/p/mds-utils/).


Elegant use of boost::iostreams if I may say so.  I've been looking for
some usecases for some boost.python modifications I've been playing with
so I tried incorporating your FileObj.  Seems to work well.  One can
wrap a function that takes a std::ostream& like this:

using mds_utils::python::FileObj;

// c++ function we're wrapping
void sayhello(std::ostream& os)
{
  os << "*** hello ***\n";
}

//
// function object that converts object& and implements
// result_of protocol
//
struct conv {
  typedef FileObj result_type;

  result_type operator()(object& obj)
  {
     return FileObj(obj);
  }
};

BOOST_PYTHON_MODULE(mod)
{
  def("sayhello", as<void(conv(object&))>( &sayhello ));
  //  arg converter       ^^^^^^^^^^^^^
};

In this case python sees "sayhello" as taking a bp::object.

The as<> might need some clarification.... It takes a function type argument. Above, the argument is the type of a function that returns void, which takes one argument, which is a function that returns conv and takes one argument of type object&. In general, for a unary function 'fn' returning type R it is:

  as<R(T(U))>(&fn)

where T(U) means "convert the python object to C++ type U, then create a
temporary object T and use it to create result_of<T(U)>::type, then convert that type to R and return to python". This is a little boost::proto-ish. Maybe also a little like a C++1x concept map. You could also provide a generic 'conv' struct, I skipped this so as not to confuse things.

-t


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

Reply via email to