Michele De Stefano's C++ Utilities
fileobj_usage.i
1 // fileobj_usage.i
2 //
3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
7 /*
8  * Instructions for generating and building the extension:
9  *
10  * 1. swig -c++ -Wall -python -I../../../include -o fileobj_usage_wrap.cpp fileobj_usage.i
11  *
12  * 2. python setup.py build
13  */
14 
15 /**
16  * \example fileobj_usage.i
17  *
18  * A simple SWIG interface for a Python extension module that shows the
19  * mds_utils::python::FileObj usage.
20  *
21  * \remarks Here I've used SWIG for convenience only. The
22  * mds_utils::python::FileObj class does not impose you
23  * this choice.
24  */
25 
26 %module fileobj_usage
27 
28 // Include the following interface files for having the proper typemaps
29 %include "mds_utils/python/fileobj.i"
30 
31 %header %{
32 #include <mds_utils/python/conversion.hpp>
33 #include <mds_utils/python/fileobj.hpp>
34 #include <iostream>
35 
36 namespace mdspy = mds_utils::python;
37 %}
38 
39 
40 %inline %{
41 
42 double read_double(mds_utils::python::FileObj fobj) {
43 
44  using namespace std;
45 
46  istream &ifs(fobj);
47 
48  double val;
49 
50  ifs.read(reinterpret_cast<char*>(&val),sizeof(double));
51 
52  return val;
53 }
54 
55 
56 
57 void write_double(mds_utils::python::FileObj fobj,double val) {
58 
59  using namespace std;
60 
61  ostream &ofs(fobj);
62 
63  ofs.write(reinterpret_cast<char*>(const_cast<double*>(&val)),sizeof(double));
64 }
65 
66 %}