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