Adonthell  0.4
py_callback.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001/2002 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef PY_CALLBACK_H__
20 #define PY_CALLBACK_H__
21 
22 
23 /**
24  * @file py_callback.h
25  * @author Kai Sterker <kai.sterker@gmail.com>
26  *
27  * @brief Declares the py_callback class.
28  *
29  *
30  */
31 
32 
33 #include <locale>
34 #include "Python.h"
35 #include "fileops.h"
36 
37 /**
38  * Stores the C++ <-> Python callback binding
39  *
40  */
42 {
43 public:
44 
45  /**
46  * Default ctor,
47  */
48  py_callback ();
49 
50  /**
51  * Constructor that assigns a function and its arguments to the callback.
52  *
53  * @param func function assigned to this callback.
54  * @param args Arguments passed to the function.
55  */
56  py_callback (PyObject * func, PyObject * args);
57 
58  /**
59  * Destructor.
60  */
61  ~py_callback ();
62 
63 
64  /**
65  * @name Executing the callback
66  */
67  //@{
68 
69  /**
70  * Calls the python function without arguments.
71  */
72  void callback_func0 ();
73 
74  /**
75  * Calls the python function and returns bool.
76  */
77  bool callback_func0ret ();
78 
79  /**
80  * Calls the python function with an integer.
81  *
82  * @param arg Integer value to pass to the callback
83  */
84  void callback_func1 (int arg);
85 
86  //@}
87 
88 
89  /**
90  * @name Loading / Saving
91  */
92  //@{
93 
94  /**
95  * Saves the callback and it's arguments to file.
96  * @note Currently, arguments have to be a tuple containing only
97  * integers and/or strings.
98  *
99  * @param out file where to save the callback.
100  */
101  void put_state (ogzstream& out) const;
102 
103  /**
104  * Restores the callback from a file. For that to work, the static
105  * py_callback::instance member has to point to the python instance
106  * containing the callback.
107  *
108  * @param in file to load the callback from.
109  *
110  * @return \e true if the callback could be restored, \e false otherwise
111  *
112  * @sa instance
113  */
114  bool get_state (igzstream& in);
115 
116  /**
117  * When restoring a callback from file, instance has to point to the
118  * python instance (module or class) containing the callback.
119  *
120  * @sa get_state
121  */
122  static PyObject *instance;
123  //@}
124 
125 private:
126 
127  /**
128  * The actual function call.
129  *
130  * @param args The arguments passed to the callback.
131  */
132  PyObject *make_call (PyObject *args);
133 
134  /**
135  * The function to be called.
136  */
137  PyObject *function;
138 
139  /**
140  * Additional arguments passed to the function.
141  */
142  PyObject *arguments;
143 };
144 
145 #endif // PY_CALLBACK_H__
void callback_func1(int arg)
Calls the python function with an integer.
Definition: py_callback.cc:82
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
void put_state(ogzstream &out) const
Saves the callback and it&#39;s arguments to file.
Definition: py_callback.cc:94
static PyObject * instance
When restoring a callback from file, instance has to point to the python instance (module or class) c...
Definition: py_callback.h:122
bool callback_func0ret()
Calls the python function and returns bool.
Definition: py_callback.cc:68
bool get_state(igzstream &in)
Restores the callback from a file.
Definition: py_callback.cc:120
~py_callback()
Destructor.
Definition: py_callback.cc:53
void callback_func0()
Calls the python function without arguments.
Definition: py_callback.cc:60
Stores the C++ <-> Python callback binding.
Definition: py_callback.h:41
Declares the igzstream, ogzstream and fileops classes.
py_callback()
Default ctor,.
Definition: py_callback.cc:37