Boost logo

Boost :

From: Anton Gluck (gluc_at_[hidden])
Date: 2000-10-26 00:23:01


> <snip>
>
> > 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++.

I'm still chewing on this one. I _think_ I have ifstream wrapped properly
with these lines:

py::ClassWrapper<std::ifstream> ifstream_class(ClusterForPy, "ifstream");
ifstream_class.def(py::Constructor<const char*>

But the line "TextRecordIterator_class.def(py::Constructor<std::ifstream&>());"
still gives my problems, because now "const class std::basic_ifstream<..>"
can't be converted to "class std::basic_ifstream<..>". (The exact error is:
d:\py_cpp/extclass.h(475) : error C2664: '__thiscall
TextRecordIterator::TextRecordIterator(class
std::basic_ifstream<char,struct std::char_traits<char> > &)' : cannot
convert parameter 1 from 'const class std::basic_ifstream<char,struct
std::char_traits<char> >' to 'class std::basic_ifstream<char,struct
std::char_traits<char> > &'
        Conversion loses qualifiers
.)
However, in the constructor of ifstream_class the const qualifier is
needed (or else I get conversion errors there). Am I at least on the right
track?

Another question, about the use of this in Python: Do I understand you
correctly if I say that this is how it would work in Python:

>>> inputfilestream = ifstream("filename")
>>> iterator = TextRecordIterator(inputfilestream)

Thanks again for your help.

Toni


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk