OSDN Git Service

release_3.3.0
[toppersjsp4bf/jsp.git] / jsp / cfg / base / collection.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: collection.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
37  */
38
39 // $Header: /cvsroot/toppersjsp4bf/jsp/cfg/base/collection.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
40
41 #ifndef COLLECTION_H
42 #define COLLECTION_H
43
44 #include "base/testsuite.h"
45 #include "base/singleton.h"
46
47 #include <typeinfo>
48 #include <list>
49
50     //Collectionに登録したいオブジェクトのクラスの基底クラス
51 class Collectable
52 {
53 public:
54              Collectable(void) throw() {}   //特に何もしない
55     virtual ~Collectable(void) throw() {}   //特に何もしない
56 };
57
58
59
60     //ある型のインスタンスを登録して保持するためのクラス
61 class Collection
62 {
63 public:
64     struct Element
65     {
66         Collectable * instance;
67         bool          destruction;
68     };
69
70 protected:
71     std::list<Element> container;
72
73         //predecessorの次を指すイテレータを得る (getInstance)
74     std::list<Element>::const_iterator _findInstance(const Collectable * predecesor) const throw();
75
76 public:
77         //コンストラクタ & デストラクタ
78              Collection(void)  throw();
79     virtual ~Collection(void) throw();
80
81         //有効判定
82     inline bool isValid(void) const throw()
83     {   return this != 0;   }
84
85         //コレクションへの追加 (ポインタ用)
86     bool addInstance(Collectable * instance, bool destruction = true)  throw();
87     
88         //コレクションへの追加 (実体用)
89     inline bool addInstance(Collectable & instance, bool destruction = false) throw()
90     {   return addInstance(&instance, destruction);   }
91
92         //コレクションからの取得 (完全一致)
93     Collectable * getInstance(const std::type_info & type, const Collectable * predecessor = 0) const throw();
94
95         //コレクションからの取得 (派生したものもサーチ)
96     template<class T> T * getInstance(const Collectable * predecessor = 0) const throw()
97     {
98         T * result = 0;
99
100         if(isValid()) {
101             std::list<Element>::const_iterator iterator = _findInstance(predecessor);
102
103             while(iterator != container.end()) {
104                 result = dynamic_cast<T *>(iterator->instance);
105                 if(result != 0)
106                     break;
107                 ++ iterator;
108             }
109         }
110         return result;
111     }
112
113         //コレクションから派生先を含む全てのクラスインスタンスを削除 (破棄対象の場合は破棄も行う)
114     template<class T> void deleteInstance(void) throw()
115     {
116         if(isValid()) {
117             std::list<Element>::iterator iterator;
118
119             iterator = container.begin();
120             while(iterator != container.end()) {
121                 if(dynamic_cast<T *>(iterator->instance) != 0) {
122
123                         //削除対象ならインスタンスを削除
124                     if(iterator->destruction)
125                         delete iterator->instance;
126
127                         //リストから除外
128                     std::list<Element>::iterator target = iterator;
129                     ++ iterator;
130                     container.erase(target);
131                 }
132                 else
133                     ++ iterator;
134             }
135         }
136     }
137
138         //関連付けられたインスタンスを入れ替える
139     template<class T> inline bool replaceInstance(Collectable * instance, bool destruction = true) throw()
140     {
141             //自分と無関係なクラスはインデックスとして指定できない
142         if(dynamic_cast<T *>(instance) == 0)
143             return false;
144
145         deleteInstance<T>();
146         return addInstance(instance, destruction);
147     }
148
149         //関連付けられたインスタンスを入れ替える
150     template<class T> inline bool replaceInstance(Collectable & instance, bool destruction = false) throw()
151     {
152             //自分と無関係なクラスはインデックスとして指定できない
153         if(dynamic_cast<T *>(&instance) == 0)
154             return false;
155
156         deleteInstance<T>();
157         return addInstance(&instance, destruction);
158     }
159
160
161         //コレクションからインスタンスを除外 (破棄はしない)
162     bool removeInstance(const Collectable * instance) throw();
163
164         //全要素の破棄
165     void clear(void);
166
167     TESTSUITE_PROTOTYPE(main)
168 };
169
170
171     //実行時オブジェクト参照テーブル
172 class RuntimeObjectTable : protected Collection
173 {
174 friend class RuntimeObject;
175 public:
176     SINGLETON_CONSTRUCTOR(RuntimeObjectTable) throw() {}
177
178     static Collectable * getInstance(const std::type_info & type, const Collectable * predecessor = 0) throw()
179     {
180         RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance(std::nothrow);
181         Collectable * result = 0;
182
183         if(table != 0)
184             result = table->Collection::getInstance(type, predecessor);
185         return result;
186     }
187
188         //コレクションからの取得 (派生したものもサーチ)
189     template<class T> static T * getInstance(const Collectable * predecessor = 0) throw()
190     {
191         RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
192         T * result = 0;
193
194         if(table != 0) {
195             std::list<Element>::const_iterator iterator = table->_findInstance(predecessor);
196
197             while(iterator != table->container.end()) {
198                 result = dynamic_cast<T *>(iterator->instance);
199                 if(result != 0)
200                     break;
201                 ++ iterator;
202             }
203         }
204         return result;
205     }
206
207         /* Visual C++ 6.0 Fatal error C1001対策 */
208     template<class T> static T * getInstance(T ** dest, const Collectable * predecessor = 0) throw()
209     {
210         RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
211         T * result = 0;
212
213         if(table != 0) {
214             std::list<Element>::const_iterator iterator = table->_findInstance(predecessor);
215
216             while(iterator != table->container.end()) {
217                 result = dynamic_cast<T *>(iterator->instance);
218                 if(result != 0)
219                     break;
220                 ++ iterator;
221             }
222         }
223         if(dest != 0)
224             *dest = result;
225
226         return result;
227     }
228
229     TESTSUITE_PROTOTYPE(main)
230 };
231
232     //実行時オブジェクト
233 class RuntimeObject : public Collectable
234 {
235 public:
236     RuntimeObject(bool destruction = false) throw()
237     {
238         RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
239         if(table != 0)
240             table->addInstance(this, destruction);
241     }
242
243     virtual ~RuntimeObject(void) throw()
244     {
245         RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
246         if(table != 0)
247             table->removeInstance(this);
248     }
249 };
250
251     /* 呪 fatal error C1001: INTERNAL COMPILER ERROR (msc1.cpp:1794) */
252 #if _MSC_VER < 1300 
253 #  define getRuntimeObjectInstance(x) (dynamic_cast<x *>(RuntimeObjectTable::getInstance(typeid(x))))       //正確には透過ではないが、これで逃げるしかない
254 #else
255 #  define getRuntimeObjectInstance(x) (RuntimeObjectTable::getInstance<x>())
256 #endif
257
258
259 #endif
260
261
262