[363] | 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ã«ç»é²ããããªãã¸ã§ã¯ãã®ã¯ã©ã¹ã®åºåºã¯ã©ã¹
|
---|
| 65 | class Collectable
|
---|
| 66 | {
|
---|
| 67 | public:
|
---|
| 68 | Collectable(void) throw() {} //ç¹ã«ä½ãããªã
|
---|
| 69 | virtual ~Collectable(void) throw() {} //ç¹ã«ä½ãããªã
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | //ããåã®ã¤ã³ã¹ã¿ã³ã¹ãç»é²ãã¦ä¿æããããã®ã¯ã©ã¹
|
---|
| 75 | class Collection
|
---|
| 76 | {
|
---|
| 77 | public:
|
---|
| 78 | struct Element
|
---|
| 79 | {
|
---|
| 80 | Collectable * instance;
|
---|
| 81 | bool destruction;
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | protected:
|
---|
| 85 | std::list<Element> container;
|
---|
| 86 |
|
---|
| 87 | //predecessorã®æ¬¡ãæãã¤ãã¬ã¼ã¿ãå¾ã (getInstance)
|
---|
| 88 | std::list<Element>::const_iterator _findInstance(const Collectable * predecesor) const throw();
|
---|
| 89 |
|
---|
| 90 | public:
|
---|
| 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 | §ãã¼ãã«
|
---|
| 193 | class RuntimeObjectTable : protected Collection
|
---|
| 194 | {
|
---|
| 195 | friend class RuntimeObject;
|
---|
| 196 | public:
|
---|
| 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 | //å®è¡æãªãã¸ã§ã¯ã
|
---|
| 254 | class RuntimeObject : public Collectable
|
---|
| 255 | {
|
---|
| 256 | public:
|
---|
| 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 |
|
---|