/usr/include/crystalspace-2.0/csutil/cssubscription.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | /*
Crystal Space 3D engine - Event Subscription internals
Copyright (C) 2005 by Adam D. Bradley <artdodge@cs.bu.edu>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CS_CSEVENTSUBSCRIPTION_H__
#define __CS_CSEVENTSUBSCRIPTION_H__
#include "csutil/partialorder.h"
#include "csutil/tree.h"
#include "csutil/list.h"
#include "csutil/eventhandlers.h"
#include "iutil/eventh.h"
class csEventQueue;
/**
* This class is used to represent the event namespace (tree).
* Each node represents an event name (e.g., "crystalspace.input.mouse")
* and contains two data structures: a partial order graph representing
* subscribers to this event (including those who subscribed to parent
* event names) along with their ordering constraints, and a queue of
* subscribers representing a valid total order of that graph (used to
* speed up the common case).
*/
class csEventTree : public csTreeNode
{
public:
// forward declarator
class SubscriberIterator;
/**
* Find a node with a given name in the event tree owned by q .
* If no such node yet exists, create it (along with any necessary
* parent nodes, including the name root).
*/
csEventTree *FindNode (csEventID name, csEventQueue *q);
/**
* Subscribe a given handler to a given event name subtree via
* a given event queue. This is wrapped by csEventQueue::Subscribe
* which may be easier to use in some situations.
* \sa csEventQueue::Subscribe
*/
bool Subscribe (csHandlerID, csEventID, csEventQueue *q);
/**
* Unubscribe a given handler to a given event name subtree via
* a given event queue. This is wrapped by csEventQueue::Unsubscribe
* which may be easier to use in some situations.
* Note that unsubscribing is reentrant (an event handler can
* unsubscribe itself) but NOT thread-safe (only event handlers
* in the same thread as the event queue can unsubscribe).
* \sa csEventQueue::Unsubscribe
*/
void Unsubscribe(csHandlerID, csEventID, csEventQueue *q);
/**
* Send the provided event to all subscribers, regardless of their
* return values.
*/
void Notify();
/**
* Send the provided event to all subscribers, using the normal
* return and Broadcast rules.
*/
void Dispatch(iEvent &e);
#ifdef ADB_DEBUG
// Dump the event tree and subscriptions to stderr.
void Dump();
#endif
static inline csEventTree *CreateRootNode(csRef<iEventHandlerRegistry> ®1,
csRef<iEventNameRegistry> ®2,
csEventQueue *q)
{
return new csEventTree (reg1, reg2, reg2->GetID(""), 0, q);
}
static inline void DeleteRootNode(csEventTree *node)
{
CS_ASSERT(node);
CS_ASSERT(node->self == node->name_reg->GetID(""));
delete node;
}
private:
csRef<iEventHandlerRegistry> handler_reg;
csRef<iEventNameRegistry> name_reg;
#ifdef ADB_DEBUG
void Dump(int depth);
#endif
csEventTree *FindNodeInternal(csEventID &name, csEventQueue *q);
/**
* Use the static csEventTree::CreateRootNode() method to get a
* root node, and use (root_node)->FindNode() to fill out the
* csEventTree. It will handle all of the internals that you don't
* understand.
*/
csEventTree (csRef<iEventHandlerRegistry> &,
csRef<iEventNameRegistry> &,
csEventID name, csEventTree *parent, csEventQueue *q);
~csEventTree ();
csEventID self;
csEventQueue *queue;
bool SubscribeInternal (csHandlerID, csEventID);
void UnsubscribeInternal (csHandlerID);
/**
* We use copy-on-write (COW) semantics to avoid making an
* unnecessary number of copies of the PO graph and the
* subscriber queue. If this is true, then we are simply
* using a reference to the parent's graph and SubscriberQueue;
* if a change must be made locally, you need to invoke the
* ForceFatCopy() method to break the connection.
*/
bool fatNode;
/**
* The object of COW in the event tree. Having a single consolidated
* control record allows us to couple the management of the two
* subscriber data structures (graph/queue), their regeneration, and
* the queue iteration process. This makes it a lot easier to catch
* all of the nasty corner cases (particularly, reentrant Unsubscribe).
*/
class FatRecordObject
{
private:
csRef<iEventHandlerRegistry> handler_reg;
csRef<iEventNameRegistry> name_reg;
public:
FatRecordObject(csEventTree *root,
csRef<iEventHandlerRegistry> &h_reg,
csRef<iEventNameRegistry> &n_reg,
csPartialOrder<csHandlerID> *new_sg,
csList<iEventHandler *> *new_sq) :
handler_reg (h_reg), name_reg (n_reg),
SubscriberGraph (new_sg), SubscriberQueue (new_sq),
my_root (root), iterator (0), iterating_for (0)
{
/* If there's no SQ, mark it for creation */
StaleSubscriberQueue = (SubscriberQueue==0);
}
~FatRecordObject()
{
delete SubscriberGraph;
if (SubscriberQueue)
delete SubscriberQueue;
CS_ASSERT (iterator == 0);
CS_ASSERT (iterating_for == 0);
}
/**
* Re-builds csEventTree::SubscriberQueue from csEventTree::SubscriberGraph.
*/
void RebuildQueue();
/**
* Handle subscription nitty-gritty
*/
csPartialOrder<csHandlerID> *SubscribeInternal(csHandlerID, csEventID);
/**
* Handle unsubscribe nitty-gritty
*/
void UnsubscribeInternal(csHandlerID);
/**
* The current partial-order graph.
*/
csPartialOrder<csHandlerID> *SubscriberGraph;
/**
* The (usually) current subscriber queue, derived from the PO graph
* to improve common-case performance.
*/
csList<iEventHandler *> *SubscriberQueue;
/**
* Flag set when the graph is manipulated without re-generating the
* SubscriberQueue. This allows us to handle the rare "adding a
* subscriber to a current in-process event" case efficiently.
*/
bool StaleSubscriberQueue;
/**
* The root of the event name subtree which this record represents
*/
csEventTree *my_root;
/**
* When this is set, there is an iterator operating upon this particular
* event subscription record. Any subscription record may have at most
* one iterator at any given time.
*/
SubscriberIterator *iterator;
/**
* This points to the particular node in the event name tree for which
* the iterator is currently running.
*/
csEventTree *iterating_for;
};
FatRecordObject *fatRecord;
/**
* Ensure we are using a local fat copy of the PO graph and
* the delivery queue. Also descends the tree so our children
* aren't referencing our parents instead of us.
* If we already are, this has no effect.
*/
void ForceFatCopy();
/**
* Revert a fat node to a shallow node.
* Used to throw away failed modifications.
*/
void KillFatCopy();
/**
* Propagate current copy pointers down the tree, overwriting
* existing shallow node pointers until other fat nodes are
* found.
*/
void PushFatCopy(FatRecordObject *);
public:
/**
* The SubscriberIterator is a wrapper for the messy internals
* of figuring out which event handler to call next. In the
* common case, this is simply iterating over a pre-existing
* list (csEventTree::SubscriberQueue). There are degenerative
* cases where it must fall back on progressively solving the
* partial order graph (csEventTree::SubscriberGraph).
*
* Only one iterator may exist for a given event node at a time.
*/
class SubscriberIterator
{
public:
/**
* Constructor. Establishes the csEventTree reference to this
* iterator to ensure there can be only one.
*/
SubscriberIterator (iEventHandlerRegistry* r, csEventTree *t,
csEventID bevent) : handler_reg(r), record(t->fatRecord),
baseevent(bevent), mode(SI_LIST), qit(0)
{
CS_ASSERT(record->iterator == 0);
record->iterator = this;
record->iterating_for = t;
if (t->fatRecord->SubscriberQueue)
qit = new csList<iEventHandler *>::Iterator (
*t->fatRecord->SubscriberQueue);
else
GraphMode();
}
/**
* Destructor. Remove the csEventTree reference to us.
*/
~SubscriberIterator ()
{
CS_ASSERT(record->iterator == this);
record->iterator = 0;
record->iterating_for = 0;
delete qit;
}
/// Test if there is another available handler
inline bool HasNext ()
{
switch(mode)
{
case SI_LIST:
return qit->HasNext ();
case SI_GRAPH:
do
{
csHandlerID id = record->SubscriberGraph->GetEnabled (
CS_HANDLER_INVALID);
if (id == CS_HANDLER_INVALID)
break;
else if (handler_reg->IsInstance(id))
return true;
else
record->SubscriberGraph->Mark(id);
}
while (true);
return false;
default:
CS_ASSERT((mode == SI_LIST) ||
(mode == SI_GRAPH));
return false;
}
}
/// Return an available handler and mark it as "done"
inline iEventHandler *Next ()
{
switch(mode)
{
case SI_LIST:
/* DOME : see if the current element has been deleted. */
return qit->Next ();
case SI_GRAPH:
/* see if the current element has been flagged for deletion. */
do
{
csHandlerID id = record->SubscriberGraph->GetEnabled (
CS_HANDLER_INVALID);
if (id == CS_HANDLER_INVALID)
break;
else if (handler_reg->IsInstance (id))
{
record->SubscriberGraph->Mark (id);
return handler_reg->GetHandler (id);
}
else
record->SubscriberGraph->Mark(id);
}
while (true);
return 0;
default:
CS_ASSERT((mode == SI_LIST) ||
(mode == SI_GRAPH));
return 0;
}
}
/* SI_GRAPH mode data structures and methods */
void GraphMode (); // change to graph (SI_GRAPH) iterator mode.
private:
friend class csEventTree;
friend class csEventQueueTest;
csRef<iEventHandlerRegistry> handler_reg;
FatRecordObject *record;
csEventID baseevent;
enum
{
SI_LIST,
SI_GRAPH
} mode;
/* SI_LIST mode data structures */
csList<iEventHandler *>::Iterator* qit;
};
friend class SubscriberIterator;
friend class csEventQueueTest;
/**
* Return a csEventTree::SubscriberIterator for all subscribers
* to this event name (and to its parents).
*/
SubscriberIterator *GetIterator();
};
#endif // __CS_CSEVENTSUBSCRIPTION_H__
|