Boost Python and iostreams

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Boost Python and iostreams

Michele De Stefano
Good Morning.

I'm new to Python and Boost Python.
I read the Boost Python documentation and some Python tutorials and
now I'd like to extend Python with some C++ classes
I already have.

The problem is that these classes take (and return), at some point, a
reference to an istream or an ostream.

I've googled a lot and I also searched within this list but I've not
found a clear explanation yet.

The problem is this (I will simplify the code a lot): I have a class A
representing my data and a friend class B which has the "read" and
"write" methods for reading and writing A from and to a stream

class A {
    // Some uninteresting code here
    friend class B;
    // Some uninteresting code here

class B {
    // Some uninteresting code here

    std::istream& read(std::istream &is, A *pd);

    std::ostream& write(std::ostream &os, const A &d);

If I have to read an A object from file, I instantiate an object of class A (a),
I open an ifstream (ifs), I instantiate a B object (b) and then, I use,&a).

I would like to extend Python with this behavior.

While it is quite easy to wrap A into Python (using Boost Python) it
seems to me that wrapping B is very difficult, first of all because
the "read" and "write" methods take (and return) references to
std::istream and std::ostream.

So, first of all, I tried to wrap a std::ifstream using Boost.Python
(for starting, I defined the open and close methods only):

#include <boost/python.hpp>
#include <fstream>

using namespace boost::python;
using namespace std;

BOOST_PYTHON_MODULE(libifstream_ext) {

When I try to compile, g++ exits with an error that says that the
copy-constructor of basic_ios<char, std::char_traits<char> > is
(obviously) private.

So I have two questions:

1) Is it possible to wrap into Python classes like B, using
Boost.Python ? If yes, how ?

2) Is it possible to wrap std::ifstream and std::ofstream into Python,
using Boost.Python ? If yes, how ?

Thank you in advance.

This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
Boost-langbinding mailing list
[hidden email]