Adonthell  0.4
event.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000/2001/2002/2003 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 /**
20  * @file event.cc
21  * @author Kai Sterker <kai.sterker@gmail.com>
22  *
23  * @brief Defines the base event class.
24  */
25 
26 #include "event.h"
27 #include "event_handler.h"
28 
29 // constructor
31 {
32  Action = ACTION_NOTHING;
33  Registered = false;
34  Paused = false;
35  Repeat = -1;
36  Script = NULL;
37  PyFunc = NULL;
38  Args = NULL;
39  List = NULL;
40  Id = "";
41 }
42 
43 // destructor
45 {
46  // automatically remove myself from the event_handler
48 
49  // ... and from the event_list
50  if (List) List->remove_event (this);
51 
52  clear ();
53 }
54 
55 // cleanup
56 void event::clear ()
57 {
58  switch (Action)
59  {
60  // script attached
61  case ACTION_SCRIPT:
62  {
63  delete Script;
64  Py_XDECREF (Args);
65  Args = NULL;
66  Script = NULL;
67 
68  break;
69  }
70 
71  // python callback attached
72  case ACTION_PYFUNC:
73  {
74  delete PyFunc;
75  PyFunc = NULL;
76 
77  break;
78  }
79 
80  default: break;
81  }
82 
83  Action = ACTION_NOTHING;
84 }
85 
86 // set a script as event's action
87 void event::set_script (std::string filename, PyObject * args)
88 {
89  // cleanup
90  clear ();
91 
92  if (filename == "") return;
93 
94  Py_XINCREF (args);
95  Args = args;
96 
97  u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1;
98  PyObject *theargs = PyTuple_New (argssize);
99 
100  // We can pass_instance directly 'cause PyTuple_SetItem steals a
101  // reference to the result of pass_instance.
102  PyTuple_SetItem (theargs, 0, python::pass_instance (this, "event"));
103  for (u_int16 i = 1; i < argssize; i++)
104  {
105  PyObject *intref = PyTuple_GetItem (args, i - 1);
106  Py_INCREF (intref);
107  PyTuple_SetItem (theargs, i, intref);
108  }
109 
110  Script = new py_object;
111  Script->create_instance (EVENTS_DIR + filename, filename, theargs);
112  Py_DECREF (theargs);
113 
114  Action = ACTION_SCRIPT;
115 }
116 
117 // set a python callback as event's action
118 void event::set_callback (PyObject *callback, PyObject *args)
119 {
120  // cleanup
121  clear ();
122 
123  // create the callback
124  PyFunc = new py_callback (callback, args);
125 
126  // tell the event what to do
127  Action = ACTION_PYFUNC;
128 }
129 
130 // set a C/C++ callback as event's action
131 void event::set_callback (const Functor0 & callback)
132 {
133  // cleanup
134  clear ();
135 
136  Callback = callback;
137  Action = ACTION_CPPFUNC;
138 }
139 
140 // save the state of the script associated with the event
141 void event::put_state (ogzstream & file) const
142 {
143  Type >> file;
144  // Id >> file;
145  Repeat >> file;
146  Paused >> file;
147  Action >> file;
148 
149  switch (Action)
150  {
151  // save script
152  case ACTION_SCRIPT:
153  {
154  Script->class_name () >> file;
155 
156  if (Args)
157  {
158  true >> file;
159  python::put_tuple (Args, file);
160  }
161  else false >> file;
162 
163  return;
164  }
165 
166  // save python callback
167  case ACTION_PYFUNC:
168  {
169  PyFunc->put_state (file);
170  return;
171  }
172 
173  default: return;
174  }
175 }
176 
177 // load the state of the script associated with the event
179 {
180  // Note that �Type� is already read by event_list::load to
181  // determine what event subclass to instanciate
182  // Id << file;
183  Repeat << file;
184  Paused << file;
185  Action << file;
186 
187  switch (Action)
188  {
189  // load script from file
190  case ACTION_SCRIPT:
191  {
192  std::string name;
193  bool has_args;
194  PyObject * args = NULL;
195 
196  name << file;
197  has_args << file;
198 
199  if (has_args) args = python::get_tuple (file);
200 
201  set_script (name, args);
202  Py_XDECREF (args);
203 
204  return true;
205  }
206 
207  // load python callback from file
208  case ACTION_PYFUNC:
209  {
210  PyFunc = new py_callback;
211  return PyFunc->get_state (file);
212  }
213 
214  default: return true;
215  }
216 
217  return false;
218 }
219 
220 // the event_list this event is kept in
222 {
223  List = l;
224 }
225 
226 // disable the event temporarily
228 {
230  Paused = true;
231 }
232 
233 // resume a disabled event
235 {
237  Paused = false;
238 }
239 
240 // repeat an event
242 {
243  if (Repeat > 0) Repeat--;
244 
245  return Repeat;
246 }
event::Id
string Id
(Optional) Id of the event
Definition: event.h:314
py_callback::get_state
bool get_state(igzstream &in)
Restores the callback from a file.
Definition: py_callback.cc:120
event::List
event_list * List
The event_list this event is kept in.
Definition: event.h:363
event::put_state
virtual void put_state(ogzstream &out) const
Saves the basic event data (such as the type or script data) to a file.
Definition: event.cc:141
igzstream
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
event_handler.h
Declares the event_handler class.
event::Args
PyObject * Args
The arguments passed to the script.
Definition: event.h:348
event_list
Base class for objects that want to register events.
Definition: event_list.h:56
event::Repeat
s_int32 Repeat
Defines how often the event should be repeated.
Definition: event.h:336
s_int32
#define s_int32
32 bits long signed integer
Definition: types.h:50
event::set_callback
void set_callback(PyObject *callback, PyObject *args=NULL)
Sets a python function/method to be executed whenever the event occurs.
Definition: event.cc:118
py_object::create_instance
bool create_instance(string file, string classname, PyObject *args=NULL)
Creates an instance of a Python class.
Definition: py_object.cc:57
event::Action
u_int8 Action
What happens if the event occurs - see enum above.
Definition: event.h:319
py_object
Python object class.
Definition: py_object.h:45
event::set_script
void set_script(string filename, PyObject *args=NULL)
Sets a script to be executed whenever the event occurs.
Definition: event.cc:87
ogzstream
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
event::Registered
bool Registered
Whether the event is registered with the event handler.
Definition: event.h:324
event::~event
virtual ~event()
Destructor.
Definition: event.cc:44
event::Paused
bool Paused
Whether the event temporarily disabled or not.
Definition: event.h:329
py_callback::put_state
void put_state(ogzstream &out) const
Saves the callback and it's arguments to file.
Definition: py_callback.cc:94
event::pause
virtual void pause()
Disable the event temporarily.
Definition: event.cc:227
EVENTS_DIR
#define EVENTS_DIR
Directory where event scripts reside.
Definition: event.h:39
event::get_state
virtual bool get_state(igzstream &in)
Loads the basic event date from a file.
Definition: event.cc:178
event::PyFunc
py_callback * PyFunc
Python callback that may be executed instead of the script.
Definition: event.h:353
event_handler::register_event
static void register_event(event *ev)
Registers an event.
Definition: event_handler.h:79
python::get_tuple
static PyObject * get_tuple(igzstream &file)
Loads a Python tuple previously saved with put_tuple ().
Definition: python_class.cc:134
python::put_tuple
static void put_tuple(PyObject *tuple, ogzstream &file)
Save a Python tuple into a file.
Definition: python_class.cc:167
event::resume
virtual void resume()
Re-enable an event that has been paused.
Definition: event.cc:234
event::set_list
void set_list(event_list *list)
Tell the whether it is kept in an event_list.
Definition: event.cc:221
event.h
Declares the event class.
event::Type
u_int8 Type
Event type - see enum above.
Definition: event.h:309
event::do_repeat
s_int32 do_repeat()
Decrease the event's repeat count and return the number of repeats left.
Definition: event.cc:241
event::Script
py_object * Script
The Python script accociated with this event.
Definition: event.h:342
u_int16
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
event_list::remove_event
void remove_event(event *ev)
Removes an event from the list.
Definition: event_list.cc:75
python::pass_instance
static PyObject * pass_instance(void *instance, const char *class_name)
Magic function that makes any C object available to Python!
Definition: python_class.cc:128
py_object::class_name
std::string class_name() const
Returns the class name of this object.
Definition: py_object.h:223
event_handler::remove_event
static void remove_event(event *ev)
Unregister an event.
Definition: event_handler.h:57
event::Callback
Functor0 Callback
C++ callback that may be executed when the event gets triggered.
Definition: event.h:358
event::clear
void clear()
Cleanup.
Definition: event.cc:56
event::event
event()
Constructor.
Definition: event.cc:30
py_callback
Stores the C++ <-> Python callback binding.
Definition: py_callback.h:41