Adonthell  0.4
event_list.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 /**
21  * @file event_list.cc
22  * @author Kai Sterker <kai.sterker@gmail.com>
23  *
24  * @brief Implements the event_list class.
25  *
26  */
27 
28 #include <algorithm>
29 #include "event_list.h"
30 #include "event_handler.h"
31 
32 // Array with callbacks to return a newly instanciated event
33 new_event event_list::instanciate_event[MAX_EVENTS];
34 
35 // constructor
37 {
38  Paused = false;
39 }
40 
41 // destructor
43 {
44  clear ();
45 }
46 
47 // Unregisters and deletes all events.
49 {
50  event *ev;
51 
52  while (!Events.empty ())
53  {
54  ev = Events.back ();
55  ev->set_list (NULL);
56  Events.pop_back ();
57  delete ev;
58  }
59 }
60 
61 // Adds an event to the list and register it with the event_handler.
63 {
64  ev->set_list (this);
65  Events.push_back (ev);
66 
67  // if the event list is paused, also pause new events
68  if (Paused) ev->pause ();
69 
70  // only register event if not paused
71  else if (!ev->is_paused ()) event_handler::register_event (ev);
72 }
73 
74 // Remove an event from the list
76 {
77  vector<event*>::iterator i;
78 
79  // Search for the event we want to remove
80  i = find (Events.begin (), Events.end (), ev);
81 
82  // found? -> get rid of it :)
83  if (i != Events.end ()) Events.erase (i);
84 }
85 
86 // retrieve event by its id
87 event *event_list::get_event (const string & id)
88 {
89  vector<event*>::iterator i;
90 
91  for (i = Events.begin (); i != Events.end (); i++)
92  if ((*i)->id () == id) return *i;
93 
94  return NULL;
95 }
96 
97 // disable all events in the list
99 {
100  Paused = true;
101  for (vector<event*>::iterator i = Events.begin (); i != Events.end (); i++)
102  (*i)->pause ();
103 }
104 
105 // enable all events in the list
107 {
108  Paused = false;
109  for (vector<event*>::iterator i = Events.begin (); i != Events.end (); i++)
110  (*i)->resume ();
111 }
112 
113 // Register an event for loading
115 {
116  if (type < MAX_EVENTS)
117  instanciate_event[type] = e;
118 }
119 
120 // Save an event_list to file
122 {
123  std::vector <event *>::iterator i;
124  u_int32 nbr_events = Events.size ();
125 
126  nbr_events >> out;
127 
128  for (i = Events.begin (); i != Events.end (); i++)
129  (*i)->put_state (out);
130 }
131 
132 // Loads an event_list from file
134 {
135  u_int32 nbr_events;
136  u_int8 type;
137  event *ev;
138 
139  nbr_events << in;
140 
141  while (nbr_events--)
142  {
143  ev = NULL;
144  type << in;
145 
146  // Instanciate an event of the given type
147  if (type < MAX_EVENTS && instanciate_event[type] != NULL)
148  ev = instanciate_event[type]();
149 
150  // try to load it, ...
151  if (ev != NULL && ev->get_state (in))
152  add_event (ev);
153 
154  // ... otherwise fail.
155  else
156  {
157  fprintf (stderr, "Could not load event #%i. Aborting ...\n", type);
158  return false;
159  }
160  }
161 
162  return true;
163 }
event_list::clear
void clear()
Unregisters and deletes all events owned by this list.
Definition: event_list.cc:48
new_event
event *(* new_event)()
Pointer to a function returning a newly allocated event.
Definition: event_list.h:41
igzstream
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
event_handler.h
Declares the event_handler class.
u_int32
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
ogzstream
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
event_list::add_event
void add_event(event *ev)
Adds an event to this list.
Definition: event_list.cc:62
u_int8
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
event::pause
virtual void pause()
Disable the event temporarily.
Definition: event.cc:227
event_list::get_state
bool get_state(igzstream &in)
Loads the event_list from a file and registers all loaded events.
Definition: event_list.cc:133
event_list::event_list
event_list()
Constructor - creates an empty, unpaused event_list.
Definition: event_list.cc:36
event_list::resume
void resume()
Re-enable the events associated with the event_list, thus 'awaking' the object to life again.
Definition: event_list.cc:106
event_list::Events
std::vector< event * > Events
List of events.
Definition: event_list.h:175
event_list::~event_list
virtual ~event_list()
Destructor - unregisters and deletes all events owned by this list.
Definition: event_list.cc:42
event_handler::register_event
static void register_event(event *ev)
Registers an event.
Definition: event_handler.h:79
event_list::get_event
event * get_event(const string &id)
Try to retrieve the event with given id from the list.
Definition: event_list.cc:87
event::set_list
void set_list(event_list *list)
Tell the whether it is kept in an event_list.
Definition: event.cc:221
event
Base class for events.
Definition: event.h:75
event_list::remove_event
void remove_event(event *ev)
Removes an event from the list.
Definition: event_list.cc:75
event_list.h
Declares the event_list class.
event_list::register_event
static void register_event(u_int8 type, new_event e)
Register an event for loading.
Definition: event_list.cc:114
event::is_paused
bool is_paused() const
Check whether the event is temporarily disabled or not.
Definition: event.h:260
event_list::pause
void pause()
Disable any events associated with this event_list.
Definition: event_list.cc:98
event_list::put_state
void put_state(ogzstream &out) const
Save the event_list to a file.
Definition: event_list.cc:121