Boost logo

Boost :

From: Dave Abrahams (abrahams_at_[hidden])
Date: 2000-10-25 09:06:06

--- In boost_at_[hidden], Anton Gluck <gluc_at_m...> wrote:
From: "Anton Gluck" <gluc_at_[hidden]>
> And also as usual, I ran into yet another prolem: I am trying to
> wrap a class that takes a reference to an ifstream as
> parameter. This is the C++ code for the constructor:
> TextRecordIterator::TextRecordIterator(std::ifstream& inputData) :
> fDataFile(inputData) {}
> This is how I tried to wrap it:
> TextRecordIterator_class.def(py::Constructor<std::ifstream&>());
> And these are the errors:


> I guess the problem here is that I/O streams are defined as
> templates with char_type and char_traits And I guess the solution
> to this is similar to the one for enumeration - to write a special
> from_python. But since the reference to the ifstream is what comes
> from Python but is not what's defining char_type and char_trail, I
> can't see how to do this. Can you help once again?

char_type and char_traits are a red herring, since std::ifstream is
just a typedef for std::basic_ifstream<char, std::char_traits<char>
>. All the information is there already. You are right about the
solution: you need to come up with this function

    X from_python(PyObject*, py::Type<std::ifstream&>)

Where X is a type convertible to std::ifstream&

The easy way to do that is to wrap std::ifstream as though it were
one of your own classes. This automatically provides the from_python
function. The downside to this approach in general is that the
ifstream must have originated in C++ (i.e. it can't just be a
wrapper around a Python FILE object). Of course, in this case it's
not an issue since an ifstream is a concrete type in C++.

If you want to do something like map a Python FILE object into a
stream, the solution would be quite a bit more complicated (and your
functions should probably be taking istream& parameters, not
ifstream& parameters).


Boost list run by bdawes at, gregod at, cpdaniel at, john at