📄 collection.h
字号:
/*
* TOPPERS/JSP Kernel
* Toyohashi Open Platform for Embedded Real-Time Systems/
* Just Standard Profile Kernel
*
* Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
* Toyohashi Univ. of Technology, JAPAN
*
* 忋婰挊嶌尃幰偼丆埲壓偺 (1)乣(4) 偺忦審偐丆Free Software Foundation
* 偵傛偭偰岞昞偝傟偰偄傞 GNU General Public License 偺 Version 2 偵婰
* 弎偝傟偰偄傞忦審傪枮偨偡応崌偵尷傝丆杮僜僼僩僂僃傾乮杮僜僼僩僂僃傾
* 傪夵曄偟偨傕偺傪娷傓丏埲壓摨偠乯傪巊梡丒暋惢丒夵曄丒嵞攝晍乮埲壓丆
* 棙梡偲屇傇乯偡傞偙偲傪柍彏偱嫋戻偡傞丏
* (1) 杮僜僼僩僂僃傾傪僜乕僗僐乕僪偺宍偱棙梡偡傞応崌偵偼丆忋婰偺挊嶌
* 尃昞帵丆偙偺棙梡忦審偍傛傃壓婰偺柍曐徹婯掕偑丆偦偺傑傑偺宍偱僜乕
* 僗僐乕僪拞偵娷傑傟偰偄傞偙偲丏
* (2) 杮僜僼僩僂僃傾傪丆儔僀僽儔儕宍幃側偳丆懠偺僜僼僩僂僃傾奐敪偵巊
* 梡偱偒傞宍偱嵞攝晍偡傞応崌偵偼丆嵞攝晍偵敽偆僪僉儏儊儞僩乮棙梡
* 幰儅僯儏傾儖側偳乯偵丆忋婰偺挊嶌尃昞帵丆偙偺棙梡忦審偍傛傃壓婰
* 偺柍曐徹婯掕傪宖嵹偡傞偙偲丏
* (3) 杮僜僼僩僂僃傾傪丆婡婍偵慻傒崬傓側偳丆懠偺僜僼僩僂僃傾奐敪偵巊
* 梡偱偒側偄宍偱嵞攝晍偡傞応崌偵偼丆師偺偄偢傟偐偺忦審傪枮偨偡偙
* 偲丏
* (a) 嵞攝晍偵敽偆僪僉儏儊儞僩乮棙梡幰儅僯儏傾儖側偳乯偵丆忋婰偺挊
* 嶌尃昞帵丆偙偺棙梡忦審偍傛傃壓婰偺柍曐徹婯掕傪宖嵹偡傞偙偲丏
* (b) 嵞攝晍偺宍懺傪丆暿偵掕傔傞曽朄偵傛偭偰丆TOPPERS僾儘僕僃僋僩偵
* 曬崘偡傞偙偲丏
* (4) 杮僜僼僩僂僃傾偺棙梡偵傛傝捈愙揑傑偨偼娫愙揑偵惗偠傞偄偐側傞懝
* 奞偐傜傕丆忋婰挊嶌尃幰偍傛傃TOPPERS僾儘僕僃僋僩傪柶愑偡傞偙偲丏
*
* 杮僜僼僩僂僃傾偼丆柍曐徹偱採嫙偝傟偰偄傞傕偺偱偁傞丏忋婰挊嶌尃幰偍
* 傛傃TOPPERS僾儘僕僃僋僩偼丆杮僜僼僩僂僃傾偵娭偟偰丆偦偺揔梡壜擻惈傕
* 娷傔偰丆偄偐側傞曐徹傕峴傢側偄丏傑偨丆杮僜僼僩僂僃傾偺棙梡偵傛傝捈
* 愙揑傑偨偼娫愙揑偵惗偠偨偄偐側傞懝奞偵娭偟偰傕丆偦偺愑擟傪晧傢側偄丏
*
* @(#) $Id: collection.h,v 1.1 2006/04/14 02:46:02 9564907 Exp $
*/
// $Header: J:\\SapporoSoft\\usb20\\S1R72V05\\Debug\\Repository/S1R72V05_Toppers/Toppers/cfg/base/collection.h,v 1.1 2006/04/14 02:46:02 9564907 Exp $
#ifndef COLLECTION_H
#define COLLECTION_H
#include "base/testsuite.h"
#include "base/singleton.h"
#include <typeinfo>
#include <list>
//Collection偵搊榐偟偨偄僆僽僕僃僋僩偺僋儔僗偺婎掙僋儔僗
class Collectable
{
public:
Collectable(void) throw() {} //摿偵壗傕偟側偄
virtual ~Collectable(void) throw() {} //摿偵壗傕偟側偄
};
//偁傞宆偺僀儞僗僞儞僗傪搊榐偟偰曐帩偡傞偨傔偺僋儔僗
class Collection
{
public:
struct Element
{
Collectable * instance;
bool destruction;
};
protected:
std::list<Element> container;
//predecessor偺師傪巜偡僀僥儗乕僞傪摼傞 (getInstance)
std::list<Element>::const_iterator _findInstance(const Collectable * predecesor) const throw();
public:
//僐儞僗僩儔僋僞 & 僨僗僩儔僋僞
Collection(void) throw();
virtual ~Collection(void) throw();
//桳岠敾掕
inline bool isValid(void) const throw()
{ return this != 0; }
//僐儗僋僔儑儞傊偺捛壛 (億僀儞僞梡)
bool addInstance(Collectable * instance, bool destruction = true) throw();
//僐儗僋僔儑儞傊偺捛壛 (幚懱梡)
inline bool addInstance(Collectable & instance, bool destruction = false) throw()
{ return addInstance(&instance, destruction); }
//僐儗僋僔儑儞偐傜偺庢摼 (姰慡堦抳)
Collectable * getInstance(const std::type_info & type, const Collectable * predecessor = 0) const throw();
//僐儗僋僔儑儞偐傜偺庢摼 (攈惗偟偨傕偺傕僒乕僠)
template<class T> T * getInstance(const Collectable * predecessor = 0) const throw()
{
T * result = 0;
if(isValid()) {
std::list<Element>::const_iterator iterator = _findInstance(predecessor);
while(iterator != container.end()) {
result = dynamic_cast<T *>(iterator->instance);
if(result != 0)
break;
++ iterator;
}
}
return result;
}
//僐儗僋僔儑儞偐傜攈惗愭傪娷傓慡偰偺僋儔僗僀儞僗僞儞僗傪嶍彍 (攋婞懳徾偺応崌偼攋婞傕峴偆)
template<class T> void deleteInstance(void) throw()
{
if(isValid()) {
std::list<Element>::iterator iterator;
iterator = container.begin();
while(iterator != container.end()) {
if(dynamic_cast<T *>(iterator->instance) != 0) {
//嶍彍懳徾側傜僀儞僗僞儞僗傪嶍彍
if(iterator->destruction)
delete iterator->instance;
//儕僗僩偐傜彍奜
std::list<Element>::iterator target = iterator;
++ iterator;
container.erase(target);
}
else
++ iterator;
}
}
}
//娭楢晅偗傜傟偨僀儞僗僞儞僗傪擖傟懼偊傞
template<class T> inline bool replaceInstance(Collectable * instance, bool destruction = true) throw()
{
//帺暘偲柍娭學側僋儔僗偼僀儞僨僢僋僗偲偟偰巜掕偱偒側偄
if(dynamic_cast<T *>(instance) == 0)
return false;
deleteInstance<T>();
return addInstance(instance, destruction);
}
//娭楢晅偗傜傟偨僀儞僗僞儞僗傪擖傟懼偊傞
template<class T> inline bool replaceInstance(Collectable & instance, bool destruction = false) throw()
{
//帺暘偲柍娭學側僋儔僗偼僀儞僨僢僋僗偲偟偰巜掕偱偒側偄
if(dynamic_cast<T *>(&instance) == 0)
return false;
deleteInstance<T>();
return addInstance(&instance, destruction);
}
//僐儗僋僔儑儞偐傜僀儞僗僞儞僗傪彍奜 (攋婞偼偟側偄)
bool removeInstance(const Collectable * instance) throw();
//慡梫慺偺攋婞
void clear(void);
TESTSUITE_PROTOTYPE(main)
};
//幚峴帪僆僽僕僃僋僩嶲徠僥乕僽儖
class RuntimeObjectTable : protected Collection
{
friend class RuntimeObject;
public:
SINGLETON_CONSTRUCTOR(RuntimeObjectTable) throw() {}
static Collectable * getInstance(const std::type_info & type, const Collectable * predecessor = 0) throw()
{
RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance(std::nothrow);
Collectable * result = 0;
if(table != 0)
result = table->Collection::getInstance(type, predecessor);
return result;
}
//僐儗僋僔儑儞偐傜偺庢摼 (攈惗偟偨傕偺傕僒乕僠)
template<class T> static T * getInstance(const Collectable * predecessor = 0) throw()
{
RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
T * result = 0;
if(table != 0) {
std::list<Element>::const_iterator iterator = table->_findInstance(predecessor);
while(iterator != table->container.end()) {
result = dynamic_cast<T *>(iterator->instance);
if(result != 0)
break;
++ iterator;
}
}
return result;
}
/* Visual C++ 6.0 Fatal error C1001懳嶔 */
template<class T> static T * getInstance(T ** dest, const Collectable * predecessor = 0) throw()
{
RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
T * result = 0;
if(table != 0) {
std::list<Element>::const_iterator iterator = table->_findInstance(predecessor);
while(iterator != table->container.end()) {
result = dynamic_cast<T *>(iterator->instance);
if(result != 0)
break;
++ iterator;
}
}
if(dest != 0)
*dest = result;
return result;
}
TESTSUITE_PROTOTYPE(main)
};
//幚峴帪僆僽僕僃僋僩
class RuntimeObject : public Collectable
{
public:
RuntimeObject(bool destruction = false) throw()
{
RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
if(table != 0)
table->addInstance(this, destruction);
}
virtual ~RuntimeObject(void) throw()
{
RuntimeObjectTable * table = Singleton<RuntimeObjectTable>::getInstance();
if(table != 0)
table->removeInstance(this);
}
};
/* 庺 fatal error C1001: INTERNAL COMPILER ERROR (msc1.cpp:1794) */
#if _MSC_VER < 1300
# define getRuntimeObjectInstance(x) (dynamic_cast<x *>(RuntimeObjectTable::getInstance(typeid(x)))) //惓妋偵偼摟夁偱偼側偄偑丄偙傟偱摝偘傞偟偐側偄
#else
# define getRuntimeObjectInstance(x) (RuntimeObjectTable::getInstance<x>())
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -