[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: singleton.h,v 1.12 2004/09/06 15:00:49 honda Exp $
|
---|
| 51 | */
|
---|
| 52 |
|
---|
| 53 | // $Header: /home/CVS/configurator/base/singleton.h,v 1.12 2004/09/06 15:00:49 honda Exp $
|
---|
| 54 |
|
---|
| 55 | #include "base/testsuite.h"
|
---|
| 56 |
|
---|
| 57 | #ifndef SINGLETON_H
|
---|
| 58 | #define SINGLETON_H
|
---|
| 59 |
|
---|
| 60 | #ifdef _MSC_VER
|
---|
| 61 | # pragma warning(disable:4290) //C++ ã®ä¾å¤ã®æå®ã¯ç¡è¦ããã¾ããé¢æ°ã __declspec(nothrow) ã§ãªããã¨ã®ã¿è¡¨ç¤ºããã¾ãã
|
---|
| 62 | # pragma warning(disable:4291) //åæåã³âããä¾å¤ãã¹ãâããã¨ã'new' æ¼ç®åã使ç¨ãã¦ããã¨ã¡ã¢ãªã解æ¾ãã¾ããã
|
---|
| 63 | # pragma warning(disable:4786) //ãããã°æ
|
---|
| 64 | å ±ã§èå¥åã 255 æåã«åãæ¨ã¦ããã¾ããã
|
---|
| 65 | #endif
|
---|
| 66 |
|
---|
| 67 | #include <new>
|
---|
| 68 | #include <stdexcept>
|
---|
| 69 | #include <cstdlib>
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | * ã·ã³ã°ã«ãã³ãã¿ã¼ã³ å®è£
|
---|
| 73 |
|
---|
| 74 | */
|
---|
| 75 |
|
---|
| 76 | //ã·ã³ã°ã«ãã³åºåºã¯ã©ã¹
|
---|
| 77 | // #gccãfriend class Singletonãé£ã£ã¦ãããªãã®ã§åºåºãä½ã
|
---|
| 78 | class SingletonBase {
|
---|
| 79 | public:
|
---|
| 80 | //ãã¹ãã©ã¯ã¿ã®ãã§ã¼ã³
|
---|
| 81 | struct chain {
|
---|
| 82 | struct chain * next;
|
---|
| 83 | void (*destructor)(void);
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | //ã·ã³ã°ã«ãã³ã§ã¤ã³ã¹ã¿ã³ã¹ãçæãããã¨ã示ãããã®ã¯ã©ã¹
|
---|
| 87 | // ãã®ã¯ã©ã¹ã¯Singletonããããå®ä½åã§ããªãã®ã§ã
|
---|
| 88 | // ã»ãã®ã¯ã©ã¹ãããããå¼æ°ã«ãã¤ã³ã³ã¹ãã©ã¯ã¿ã使ç¨ãããã¨ã¯ã§ããªã
|
---|
| 89 | class SingletonConstructionLocker
|
---|
| 90 | {
|
---|
| 91 | friend class SingletonBase;
|
---|
| 92 | protected:
|
---|
| 93 | SingletonConstructionLocker() {}
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | private:
|
---|
| 97 | static bool initialize; //åæåæ¸ã¿ã (atexitãå¼ãã ãã©ãã)
|
---|
| 98 | static bool is_valid; //çæã«å¤±æããã·ã³ã°ã«ãã³ã¬ãããã©ãã
|
---|
| 99 | static struct chain * top; //ãã¹ãã©ã¯ã¿ãã§ã¼ã³
|
---|
| 100 |
|
---|
| 101 | //ãã¹ãã©ã¯ã¿ã®èµ·å
|
---|
| 102 | static void final_release(void) throw()
|
---|
| 103 | {
|
---|
| 104 | //å
|
---|
| 105 | é ããåé¤ (å
|
---|
| 106 | å
|
---|
| 107 | ¥ãå¾åºã)
|
---|
| 108 | while(top != 0) {
|
---|
| 109 | struct chain * node = top;
|
---|
| 110 | top = node->next;
|
---|
| 111 |
|
---|
| 112 | (*node->destructor)();
|
---|
| 113 |
|
---|
| 114 | delete node;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | protected:
|
---|
| 119 | //ãã§ã¼ã³ã«ãã¹ãã©ã¯ã¿ã追å
|
---|
| 120 | static bool addDestructor(void (* destructor)(void)) throw()
|
---|
| 121 | {
|
---|
| 122 | bool result = false;
|
---|
| 123 | struct SingletonBase::chain * node = new(std::nothrow) struct SingletonBase::chain;
|
---|
| 124 | if(node != 0) {
|
---|
| 125 |
|
---|
| 126 | //æ°ããè¦ç´ ãå
|
---|
| 127 | é ã«è¿½å
|
---|
| 128 | node->next = top;
|
---|
| 129 | node->destructor = destructor;
|
---|
| 130 |
|
---|
| 131 | top = node;
|
---|
| 132 |
|
---|
| 133 | result = true;
|
---|
| 134 |
|
---|
| 135 | //è¦ç´ ç ´æ£ã®ããã«æå¾ã«ç ´æ£ã«ã¼ãã³ãèµ·åããã
|
---|
| 136 | if(!initialize) {
|
---|
| 137 | atexit(final_release);
|
---|
| 138 | initialize = true;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | return result;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | //SingletonConstructionLockerãã¡ã¯ããª
|
---|
| 145 | inline static SingletonConstructionLocker getLocker(void)
|
---|
| 146 | { return SingletonConstructionLocker(); }
|
---|
| 147 |
|
---|
| 148 | public:
|
---|
| 149 | //çæã«å¤±æããã·ã³ã°ã«ãã³ããªããã¨ãåå¾
|
---|
| 150 | static bool isValid(void) throw()
|
---|
| 151 | { return is_valid; }
|
---|
| 152 |
|
---|
| 153 | #ifdef TESTSUITE
|
---|
| 154 | /*
|
---|
| 155 | * ã³ã³ããã¹ããã§ã¼ã³ (ãã¹ãç¨ã·ã³ã°ã«ãã³ã³ã³ããã¹ãéé¿ãã§ã¼ã³)
|
---|
| 156 | */
|
---|
| 157 | class ContextChain {
|
---|
| 158 | protected:
|
---|
| 159 | struct chain {
|
---|
| 160 | struct chain * next;
|
---|
| 161 | void * context;
|
---|
| 162 | void (*restore)(void * & context) throw();
|
---|
| 163 | void (*renew)(void) throw();
|
---|
| 164 | void (*clear)(void);
|
---|
| 165 | } * top;
|
---|
| 166 |
|
---|
| 167 | public:
|
---|
| 168 | ContextChain(void) throw() : top(0)
|
---|
| 169 | {}
|
---|
| 170 |
|
---|
| 171 | ~ContextChain(void) throw()
|
---|
| 172 | { restoreContext(); }
|
---|
| 173 |
|
---|
| 174 | /* ã³ã³ããã¹ãã®å¾©å¸° */
|
---|
| 175 | void restoreContext(void) throw()
|
---|
| 176 | {
|
---|
| 177 | while(top != 0) {
|
---|
| 178 | struct chain * node = top;
|
---|
| 179 | top = top->next;
|
---|
| 180 |
|
---|
| 181 | (*node->restore)(node->context);
|
---|
| 182 | delete node;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /* å
|
---|
| 187 | ¨ã¦ã®ã¤ã³ã¹ã¿ã³ã¹ãåçæ */
|
---|
| 188 | void renewInstance(void) throw(std::bad_alloc)
|
---|
| 189 | {
|
---|
| 190 | struct chain * node = top;
|
---|
| 191 | while(node != 0) {
|
---|
| 192 | (*node->renew)();
|
---|
| 193 | node = node->next;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /* å
|
---|
| 198 | ¨ã¦ã®ã¤ã³ã¹ã¿ã³ã¹ãç ´æ£ */
|
---|
| 199 | void clearInstance(void) throw(std::bad_alloc)
|
---|
| 200 | {
|
---|
| 201 | struct chain * node = top;
|
---|
| 202 | while(node != 0) {
|
---|
| 203 | (*node->clear)();
|
---|
| 204 | node = node->next;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* ã³ã³ããã¹ãã®éé¿ */
|
---|
| 209 | template<class T> void saveContext(void) throw(std::bad_alloc)
|
---|
| 210 | {
|
---|
| 211 | struct chain * node = new struct chain;
|
---|
| 212 |
|
---|
| 213 | node->next = top;
|
---|
| 214 | node->restore = reinterpret_cast<void(*)(void*&)>(Singleton<T>::restoreContext);
|
---|
| 215 | node->renew = Singleton<T>::renewInstance;
|
---|
| 216 | node->clear = Singleton<T>::clearInstance;
|
---|
| 217 |
|
---|
| 218 | Singleton<T>::saveContext(reinterpret_cast<Singleton<T>::Context &>(node->context));
|
---|
| 219 |
|
---|
| 220 | top = node;
|
---|
| 221 | }
|
---|
| 222 | };
|
---|
| 223 | #endif
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | TESTSUITE_PROTOTYPE(main)
|
---|
| 227 | };
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 | template<class T>
|
---|
| 231 | class Singleton : SingletonBase
|
---|
| 232 | {
|
---|
| 233 | protected:
|
---|
| 234 | Singleton(void);
|
---|
| 235 | ~Singleton(void);
|
---|
| 236 |
|
---|
| 237 | static T * instance;
|
---|
| 238 |
|
---|
| 239 | //ãã¹ãã©ã¯ã¿
|
---|
| 240 | static void destructor(void) throw()
|
---|
| 241 | {
|
---|
| 242 | if(instance != 0) {
|
---|
| 243 | try{ delete instance; } catch(...) {}
|
---|
| 244 | instance = 0;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | //ã¤ã³ã¹ã¿ã³ã¹ã®çæ (ã¤ã³ã¹ã¿ã³ã¹çæ + ãã¹ãã©ã¯ã¿ç»é² + ã¨ã©ã¼å¦ç)
|
---|
| 249 | static T * createInstance(std::nothrow_t) throw()
|
---|
| 250 | {
|
---|
| 251 | T * instance = 0;
|
---|
| 252 | try {
|
---|
| 253 | instance = new(std::nothrow) T(getLocker());
|
---|
| 254 |
|
---|
| 255 | if(instance != 0) {
|
---|
| 256 | if(!addDestructor(destructor))
|
---|
| 257 | throw false; //catchç¯ãå®è¡ãããã
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | catch(...) {
|
---|
| 261 | if(instance != 0) {
|
---|
| 262 | try { delete instance; } catch(...) {}
|
---|
| 263 | instance = 0;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | return instance;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | //ã¤ã³ã¹ã¿ã³ã¹çæ (bad_allocä¾å¤ãã¹ãã¼ããå®è£
|
---|
| 270 | )
|
---|
| 271 | static T * createInstance(void) throw(std::bad_alloc)
|
---|
| 272 | {
|
---|
| 273 | instance = createInstance(std::nothrow);
|
---|
| 274 | if(instance == 0)
|
---|
| 275 | throw std::bad_alloc();
|
---|
| 276 | return instance;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | public:
|
---|
| 280 | //ã¤ã³ã¹ã¿ã³ã¹ã®åå¾
|
---|
| 281 | inline static T * getInstance(void) throw(std::bad_alloc)
|
---|
| 282 | {
|
---|
| 283 | if(instance == 0)
|
---|
| 284 | instance = createInstance();
|
---|
| 285 | return instance;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | //ã¤ã³ã¹ã¿ã³ã¹ã®åå¾
|
---|
| 289 | inline static T * getInstance(std::nothrow_t) throw()
|
---|
| 290 | {
|
---|
| 291 | if(instance == 0)
|
---|
| 292 | instance = createInstance(std::nothrow);
|
---|
| 293 | return instance;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | #ifdef TESTSUITE /* ãã¹ãç¨ */
|
---|
| 297 |
|
---|
| 298 | typedef T * Context; //éé¿ç¨
|
---|
| 299 |
|
---|
| 300 | //æ°ããã¤ã³ã¹ã¿ã³ã¹ã®çæ
|
---|
| 301 | static void renewInstance(void) throw(std::bad_alloc)
|
---|
| 302 | {
|
---|
| 303 | destructor(); //ä»ã®ã¤ã³ã¹ã¿ã³ã¹ãç ´æ£
|
---|
| 304 | instance = new T(getLocker()); //æ°ããã¤ã³ã¹ã¿ã³ã¹ãçæ (ãã¹ãã©ã¯ã¿ãç»é²ããã¦ãã¾ãã®ã§createInstanceãå¼ãã§ã¯ãããªã)
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | //ã¤ã³ã¹ã¿ã³ã¹ç ´æ£
|
---|
| 308 | static void clearInstance(void) throw()
|
---|
| 309 | { destructor(); }
|
---|
| 310 |
|
---|
| 311 | //ã³ã³ããã¹ãéé¿
|
---|
| 312 | static void saveContext(Context & context) throw()
|
---|
| 313 | {
|
---|
| 314 | context = instance;
|
---|
| 315 | instance = 0;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | //ã³ã³ããã¹ã復帰
|
---|
| 319 | static void restoreContext(Context & context) throw()
|
---|
| 320 | {
|
---|
| 321 | destructor(); //ä»ã®ã¤ã³ã¹ã¿ã³ã¹ãç ´æ£
|
---|
| 322 | instance = context; //éé¿ããã¤ã³ã¹ã¿ã³ã¹ã復帰
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | #endif /* TESTSUITE */
|
---|
| 326 |
|
---|
| 327 | };
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | //Singleton éçå¤æ°
|
---|
| 332 | template<class T> T * Singleton<T>::instance = 0;
|
---|
| 333 |
|
---|
| 334 | #define SINGLETON_CONSTRUCTOR(x) explicit x(const SingletonBase::SingletonConstructionLocker &)
|
---|
| 335 | #define SINGLETON_CONSTRUCTOR_(x) explicit x(const SingletonBase::SingletonConstructionLocker & _singleton)
|
---|
| 336 |
|
---|
| 337 | #define SINGLETON_WRAPPER(derived, base) class derived : public base { public: SINGLETON_CONSTRUCTOR_(derived) throw() : base(_singleton) {} };
|
---|
| 338 |
|
---|
| 339 | #endif /* SINGLETON_H */
|
---|
| 340 |
|
---|