Michele De Stefano's C++ Utilities
fileobj.hpp
Go to the documentation of this file.
1 // fileobj.hpp
2 //
3 // Copyright (c) 2009 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
7 #ifndef MDS_UTILS_PYTHON_FILEOBJ_HPP_INCLUDED
8 #define MDS_UTILS_PYTHON_FILEOBJ_HPP_INCLUDED
9 
32 #include <mds_utils/python/obj.hpp>
33 #include <iostream>
34 #include <stdexcept>
36 #include <boost/iostreams/stream_buffer.hpp>
37 #include <boost/shared_ptr.hpp>
38 
39 namespace mds_utils {
40 
47  namespace python {
48 
62 class FileObj : public Obj {
63 
64  boost::shared_ptr<boost::iostreams::stream_buffer<mds_utils::file_utils::cFile_Device> >
65  piosbuf;
66 
68 
69  boost::shared_ptr<std::iostream> pios;
70 
71 public:
72 
74  FileObj() {}
75 
88  FileObj(PyObject *po) : Obj(po) {
89  if (!PyFile_Check(m_po)) throw std::runtime_error("Not a Python file object");
90 
91  fdevice.set_FILEp(PyFile_AsFile(m_po));
92  piosbuf.reset(new boost::iostreams::stream_buffer<mds_utils::file_utils::cFile_Device>());
93  piosbuf->open(fdevice);
94  pios.reset(new std::iostream(piosbuf.get()));
95  pios->exceptions(std::ios::badbit | std::ios::failbit);
96  }
97 
99  operator std::iostream& () {
100  return *pios;
101  }
102 };
103 
104 
113 class iFileObj : public Obj {
114 
115  boost::shared_ptr<boost::iostreams::stream_buffer<mds_utils::file_utils::cFile_Source> >
116  pisbuf;
117 
119 
120  boost::shared_ptr<std::istream> pis;
121 
122 public:
123 
125  iFileObj() {}
126 
139  iFileObj(PyObject *po) : Obj(po) {
140  if (!PyFile_Check(m_po)) throw std::runtime_error("Not a Python file object");
141 
142  fsource.set_FILEp(PyFile_AsFile(m_po));
143  pisbuf.reset(new boost::iostreams::stream_buffer<mds_utils::file_utils::cFile_Source>());
144  pisbuf->open(fsource);
145  pis.reset(new std::istream(pisbuf.get()));
146  pis->exceptions(std::ios::badbit | std::ios::failbit);
147  }
148 
150  operator std::istream& () { return *pis; }
151 };
152 
153 
162 class oFileObj : public Obj {
163 
164  boost::shared_ptr<boost::iostreams::stream_buffer<mds_utils::file_utils::cFile_Sink> >
165  posbuf;
166 
168 
169  boost::shared_ptr<std::ostream> pos;
170 
171 public:
172 
174  oFileObj() {}
175 
188  oFileObj(PyObject *po) : Obj(po) {
189  if (!PyFile_Check(m_po)) throw std::runtime_error("Not a Python file object");
190 
191  fsink.set_FILEp(PyFile_AsFile(m_po));
192  posbuf.reset(new boost::iostreams::stream_buffer<mds_utils::file_utils::cFile_Sink>());
193  posbuf->open(fsink);
194  pos.reset(new std::ostream(posbuf.get()));
195  pos->exceptions(std::ios::badbit | std::ios::failbit);
196  }
197 
199  operator std::ostream& () { return *pos; }
200 };
201  }
202 }
203 #endif
Source device for FILE* pointers.
Sink device for FILE* pointers.
FileObj()
Default constructor.
Definition: fileobj.hpp:74
Contains some Boost.Iostreams devices to use a C FILE* as a C++ std::stream.
Class for easily managment of Python input file objects from C++ code.
Definition: fileobj.hpp:113
iFileObj()
Default constructor.
Definition: fileobj.hpp:125
Main namespace of all Michele De Stefano&#39;s C++ utilities.
Definition: endian.hpp:30
Utilities and workarounds for all the extension codes.
Device for FILE* pointers.
PyObject * m_po
Underlying pointer to the wrapped Python object.
Definition: obj.hpp:165
Contains a wrapper class for the PyObject* datatype.
Class for easily managment of Python file objects from C++ code.
Definition: fileobj.hpp:62
iFileObj(PyObject *po)
Constructor from a Python object.
Definition: fileobj.hpp:139
oFileObj(PyObject *po)
Constructor from a Python object.
Definition: fileobj.hpp:188
Class for easily managment of Python output file objects from C++ code.
Definition: fileobj.hpp:162
FileObj(PyObject *po)
Constructor from a Python object.
Definition: fileobj.hpp:88
oFileObj()
Default constructor.
Definition: fileobj.hpp:174
This is a simple wrapper around the PyObject* datatype.
Definition: obj.hpp:68