broadcaster.hh

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #ifndef lestes__std___broadcaster_hh___included
00029 #define lestes__std___broadcaster_hh___included
00030 
00031 #include <lestes/common.hh>
00032 #include <lestes/std/broadcast_listener.hh>
00033 #include <lestes/std/list.hh>
00034 
00035 package(lestes);
00036 package(std);
00037 
00038 /*!
00039   \brief A template for broadcasting class.
00040 
00041   The concept of broadcasting is very similar to the concept of events.  At
00042   a certain point of the program's run there occurs an important event.  Such
00043   events can be divided into several categories.  The first category of events,
00044   and by far the most numerous, is the category that encompasses all events
00045   that spring from a single source.  These events are represented by the class
00046   event, q.v.  The second category consists of events, that affect an object of
00047   a given type.  These events could represent the creation / alteration
00048   / destruction of an object.  The second category of events need not
00049   necesarily come from a single source.  The remaining kinds of events are not
00050   discussed here.
00051 
00052   The second category of events, i.e. the events affecting a single object, can
00053   be handled by a simple means: all parties interested in the event would
00054   enqueue themselves in a designated place.  Then, at the moment of the event,
00055   the designated place will be accessed and all enqueued listeners would be
00056   notified.  The mechanics involved are the same as for the first category.
00057   However, the affected object shall be announced to the listeners as well.
00058 
00059  */
00060 template <typename T, typename Y = T> class broadcaster : public object {
00061 public:
00062         //! Inform the listeners, that the event has occured on the parameter.
00063         void broadcast(ptr < T > what)
00064         {
00065                 typename list < srp < broadcast_listener < Y > > >::iterator it = listeners->begin(),
00066                      end = listeners->end();
00067                 for ( ; it != end ; ++it) {
00068                         (*it)->run(what);
00069                 }
00070         }
00071 
00072         //! Register with the broadcaster to listen for the events.
00073         void attach(ptr < broadcast_listener < Y > > listener)
00074         {
00075                 listeners->push_back(listener);
00076         }
00077 
00078         //! Create the broadcaster/event.
00079         static ptr < broadcaster < T, Y > > create()
00080         {
00081                 return new broadcaster < T, Y >();
00082         }
00083 protected:
00084         void gc_mark()
00085         {
00086                 listeners.gc_mark();
00087                 object::gc_mark();
00088         }
00089 
00090         broadcaster < T, Y >()
00091                 : listeners(list < srp < broadcast_listener < Y > > >::create())
00092         {}
00093 private:
00094         srp < list < srp < broadcast_listener < Y > > > > listeners;
00095 };
00096 
00097 
00098 end_package(std);
00099 end_package(lestes);
00100 
00101 #endif  // lestes__std___broadcaster_hh___included

Generated on Mon Feb 12 18:22:32 2007 for lestes by doxygen 1.5.1-20070107