source: anotherchoice/tags/jsp-1.4.4-full-UTF8/cfg/base/collection.h@ 363

Last change on this file since 363 was 363, checked in by ykominami, 5 years ago

add tags/jsp-1.4.4-full-UTF8

  • Property svn:executable set to *
File size: 9.4 KB
Line 
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 * 上記著作権者
10は,以下の (1)〜(4) の条件か,Free Software Foundation
11 * によってå…
12¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
13 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
14 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
15å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
16 * 利用と呼ぶ)することを無償で許諾する.
17 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
18 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
19 * スコード中に含まれていること.
20 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
21 * 用できる形で再é…
22å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
23å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
24 * 者
25マニュアルなど)に,上記の著作権表示,この利用条件および下記
26 * の無保証規定を掲載すること.
27 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
28 * 用できない形で再é…
29å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
30 * と.
31 * (a) 再é…
32å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
33マニュアルなど)に,上記の著
34 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
35 * (b) 再é…
36å¸ƒã®å½¢æ…
37‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
38 * 報告すること.
39 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
40 * 害からも,上記著作権者
41およびTOPPERSプロジェクトをå…
42è²¬ã™ã‚‹ã“と.
43 *
44 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
45お
46 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
47 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
48 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
49 *
50 * @(#) $Id: collection.h,v 1.6 2003/12/20 06:51:58 takayuki Exp $
51 */
52
53// $Header: /home/CVS/configurator/base/collection.h,v 1.6 2003/12/20 06:51:58 takayuki Exp $
54
55#ifndef COLLECTION_H
56#define COLLECTION_H
57
58#include "base/testsuite.h"
59#include "base/singleton.h"
60
61#include <typeinfo>
62#include <list>
63
64 //Collectionに登録したいオブジェクトのクラスの基底クラス
65class Collectable
66{
67public:
68 Collectable(void) throw() {} //特に何もしない
69 virtual ~Collectable(void) throw() {} //特に何もしない
70};
71
72
73
74 //ある型のインスタンスを登録して保持するためのクラス
75class Collection
76{
77public:
78 struct Element
79 {
80 Collectable * instance;
81 bool destruction;
82 };
83
84protected:
85 std::list<Element> container;
86
87 //predecessorの次を指すイテレータを得る (getInstance)
88 std::list<Element>::const_iterator _findInstance(const Collectable * predecesor) const throw();
89
90public:
91 //コンストラクタ & デストラクタ
92 Collection(void) throw();
93 virtual ~Collection(void) throw();
94
95 //有効判定
96 inline bool isValid(void) const throw()
97 { return this != 0; }
98
99 //コレクションへの追加 (ポインタ用)
100 bool addInstance(Collectable * instance, bool destruction = true) throw();
101
102 //コレクションへの追加 (実体用)
103 inline bool addInstance(Collectable & instance, bool destruction = false) throw()
104 { return addInstance(&instance, destruction); }
105
106 //コレクションからの取得 (完å…
107¨ä¸€è‡´)
108 Collectable * getInstance(const std::type_info & type, const Collectable * predecessor = 0) const throw();
109
110 //コレクションからの取得 (派生したものもサーチ)
111 template<class T> T * getInstance(const Collectable * predecessor = 0) const throw()
112 {
113 T * result = 0;
114
115 if(isValid()) {
116 std::list<Element>::const_iterator iterator = _findInstance(predecessor);
117
118 while(iterator != container.end()) {
119 result = dynamic_cast<T *>(iterator->instance);
120 if(result != 0)
121 break;
122 ++ iterator;
123 }
124 }
125 return result;
126 }
127
128 //コレクションから派生å…
129ˆã‚’含むå…
130¨ã¦ã®ã‚¯ãƒ©ã‚¹ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã‚’削除 (破棄対象の場合は破棄も行う)
131 template<class T> void deleteInstance(void) throw()
132 {
133 if(isValid()) {
134 std::list<Element>::iterator iterator;
135
136 iterator = container.begin();
137 while(iterator != container.end()) {
138 if(dynamic_cast<T *>(iterator->instance) != 0) {
139
140 //削除対象ならインスタンスを削除
141 if(iterator->destruction)
142 delete iterator->instance;
143
144 //リストから除外
145 std::list<Element>::iterator target = iterator;
146 ++ iterator;
147 container.erase(target);
148 }
149 else
150 ++ iterator;
151 }
152 }
153 }
154
155 //関連付けられたインスタンスをå…
156¥ã‚Œæ›¿ãˆã‚‹
157 template<class T> inline bool replaceInstance(Collectable * instance, bool destruction = true) throw()
158 {
159 //自分と無関係なクラスはインデックスとして指定できない
160 if(dynamic_cast<T *>(instance) == 0)
161 return false;
162
163 deleteInstance<T>();
164 return addInstance(instance, destruction);
165 }
166
167 //関連付けられたインスタンスをå…
168¥ã‚Œæ›¿ãˆã‚‹
169 template<class T> inline bool replaceInstance(Collectable & instance, bool destruction = false) throw()
170 {
171 //自分と無関係なクラスはインデックスとして指定できない
172 if(dynamic_cast<T *>(&instance) == 0)
173 return false;
174
175 deleteInstance<T>();
176 return addInstance(&instance, destruction);
177 }
178
179
180 //コレクションからインスタンスを除外 (破棄はしない)
181 bool removeInstance(const Collectable * instance) throw();
182
183 //å…
184¨è¦ç´ ã®ç ´æ£„
185 void clear(void);
186
187 TESTSUITE_PROTOTYPE(main)
188};
189
190
191 //実行時オブジェクト参ç…
192§ãƒ†ãƒ¼ãƒ–ル
193class RuntimeObjectTable : protected Collection
194{
195friend class RuntimeObject;
196public:
197 SINGLETON_CONSTRUCTOR(RuntimeObjectTable) throw() {}
198
199 static Collectable * getInstance(const std::type_info & type, const Collectable * predecessor = 0) throw()
200 {
201 RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance(std::nothrow);
202 Collectable * result = 0;
203
204 if(table != 0)
205 result = table->Collection::getInstance(type, predecessor);
206 return result;
207 }
208
209 //コレクションからの取得 (派生したものもサーチ)
210 template<class T> static T * getInstance(const Collectable * predecessor = 0) throw()
211 {
212 RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
213 T * result = 0;
214
215 if(table != 0) {
216 std::list<Element>::const_iterator iterator = table->_findInstance(predecessor);
217
218 while(iterator != table->container.end()) {
219 result = dynamic_cast<T *>(iterator->instance);
220 if(result != 0)
221 break;
222 ++ iterator;
223 }
224 }
225 return result;
226 }
227
228 /* Visual C++ 6.0 Fatal error C1001対策 */
229 template<class T> static T * getInstance(T ** dest, const Collectable * predecessor = 0) throw()
230 {
231 RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
232 T * result = 0;
233
234 if(table != 0) {
235 std::list<Element>::const_iterator iterator = table->_findInstance(predecessor);
236
237 while(iterator != table->container.end()) {
238 result = dynamic_cast<T *>(iterator->instance);
239 if(result != 0)
240 break;
241 ++ iterator;
242 }
243 }
244 if(dest != 0)
245 *dest = result;
246
247 return result;
248 }
249
250 TESTSUITE_PROTOTYPE(main)
251};
252
253 //実行時オブジェクト
254class RuntimeObject : public Collectable
255{
256public:
257 RuntimeObject(bool destruction = false) throw()
258 {
259 RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
260 if(table != 0)
261 table->addInstance(this, destruction);
262 }
263
264 virtual ~RuntimeObject(void) throw()
265 {
266 RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
267 if(table != 0)
268 table->removeInstance(this);
269 }
270};
271
272 /* 呪 fatal error C1001: INTERNAL COMPILER ERROR (msc1.cpp:1794) */
273#if _MSC_VER < 1300
274# define getRuntimeObjectInstance(x) (dynamic_cast<x *>(RuntimeObjectTable::getInstance(typeid(x)))) //正確には透過ではないが、これで逃げるしかない
275#else
276# define getRuntimeObjectInstance(x) (RuntimeObjectTable::getInstance<x>())
277#endif
278
279
280#endif
281
282
283
Note: See TracBrowser for help on using the repository browser.