OSDN Git Service

Change the directry name to hirado
[trx-305dsp/dsp.git] / hirado / kernel / cfg / base / event.h
1 /*
2  *  TOPPERS/JSP Kernel
3  *      Toyohashi Open Platform for Embedded Real-Time Systems/
4  *      Just Standard Profile Kernel
5  * 
6  *  Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
7  *                              Toyohashi Univ. of Technology, JAPAN
8  * 
9  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
10  *  によって公表されている GNU General Public License の Version 2 に記
11  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
12  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
13  *  利用と呼ぶ)することを無償で許諾する.
14  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16  *      スコード中に含まれていること.
17  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
20  *      の無保証規定を掲載すること.
21  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
23  *      と.
24  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
26  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
27  *        報告すること.
28  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
30  * 
31  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
32  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
33  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
34  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
35  * 
36  *  @(#) $Id: event.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
37  */
38
39 // $Header: /cvsroot/toppersjsp4bf/jsp/cfg/base/event.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
40
41 #ifndef EVENT_H
42 #define EVENT_H
43
44 #include "base/testsuite.h"
45 #include "base/singleton.h"
46
47 #include <list>
48
49     //イベントフックの取りまとめ役
50 template<typename T>
51 class Event
52 {
53 public:
54     typedef void (*handler_type)(T &);
55
56     class handler_list_type : public std::list<handler_type>
57     {   public: SINGLETON_CONSTRUCTOR(handler_list_type) throw() {}   };
58
59     /*
60      *   クラスの中のハンドラ用
61      */
62
63     class Handler
64     {
65     friend class Event;
66     public:
67         class instance_list_type : public std::list<Handler *>
68         {   public: SINGLETON_CONSTRUCTOR(instance_list_type) throw() {}   };
69
70         virtual void handler(T & ev) = 0;
71
72     protected:
73             //登録
74         Handler(void) throw()
75         {   Singleton<instance_list_type>::getInstance()->push_back(this);   }
76         
77             //登録解除
78         virtual ~Handler(void) throw()
79         {
80             typename Event<T>::Handler::instance_list_type * list = Singleton<instance_list_type>::getInstance(std::nothrow);
81             typename Event<T>::Handler::instance_list_type::iterator scope;
82
83             if(list != 0) {
84                 scope = list->begin();
85                 while(scope != list->end()) {
86                     if(*scope == this) {
87                         typename Event<T>::Handler::instance_list_type::iterator target = scope;
88                         ++ scope;
89                         list->erase(target);
90                     }
91                     else
92                         ++ scope;
93                 }
94             }
95         }
96     };
97
98 public:
99     typedef T value_type;
100
101     Event(void) {}
102     virtual ~Event(void) {}
103
104     /*
105      *  スタティックなハンドラ用
106      */
107
108         //イベントにハンドラを追加
109     inline static void add(handler_type func) throw(std::bad_alloc)
110     {   Singleton<handler_list_type>::getInstance()->push_back(func);   }
111
112         //イベントからハンドラを削除
113     static void remove(handler_type func) throw()
114     {
115         typename Event<T>::handler_list_type * list;
116         typename Event<T>::handler_list_type::iterator scope;
117
118         list = Singleton<handler_list_type>::getInstance(std::nothrow);
119         if(list != 0) {
120             scope = list->begin();
121             while(scope != list->end()) {
122                 if(*scope == func) {
123                     typename handler_list_type::iterator target = scope;
124                     ++ scope;
125                     list->erase(target);
126                 }
127                 else
128                     ++ scope;
129             }
130         }
131     }
132
133     /*
134      *  共通操作
135      */
136         //イベントの発生
137     static void raise(T & ev)
138     {
139             typedef typename Event<T>::handler_list_type handler_list_type_t; 
140             typedef typename Event<T>::Handler::instance_list_type instance_list_type_t;
141
142         /* 登録された静的な関数にイベントを発行 */ {
143            handler_list_type_t * list;
144             typename handler_list_type_t::iterator scope;
145
146             list = Singleton<handler_list_type_t>::getInstance();   //throw(bad_cast)
147
148             scope = list->begin();
149             while(scope != list->end()) {
150                 (**scope)(ev);
151                 ++ scope;
152             }
153         }
154
155         /* 登録されたクラスインスタンスにイベントを発行 */ {
156             instance_list_type_t * list;
157             typename instance_list_type_t::iterator scope;
158
159             list = Singleton<instance_list_type_t>::getInstance();  //throw(bad_cast)
160
161             scope = list->begin();
162             while(scope != list->end()) {
163                 (*scope)->handler(ev);
164                 ++ scope;
165             }
166         }
167     }
168
169         //イベントの発生 (引数なし)
170     inline static void raise(void)
171     {
172         T ev;
173         raise(ev);
174     }
175 };
176
177
178     /*
179      *  汎用イベント
180      */
181
182     //コンストラクタが起動し終わったあと、真っ先に起るイベント
183 class StartupEvent {};
184
185
186     //成功失敗を問わず、プログラムが終わるときに起るイベント
187 struct ShutdownEvent
188 {
189     int exit_code;
190 };
191
192 #endif
193
194
195