📄 collection.cpp
字号:
/*
* 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.cpp,v 1.1 2006/04/14 02:46:02 9564907 Exp $
*/
// $Header: J:\\SapporoSoft\\usb20\\S1R72V05\\Debug\\Repository/S1R72V05_Toppers/Toppers/cfg/base/collection.cpp,v 1.1 2006/04/14 02:46:02 9564907 Exp $
#include "base/collection.h"
using namespace std;
//摿偵壗傕偟側偄僐儞僗僩儔僋僞
Collection::Collection(void) throw()
{}
//僨僗僩儔僋僞
Collection::~Collection(void) throw()
{
//擮偺偨傔偵椺奜偵傆偨傪偡傞
try { clear(); }
catch(...) {}
}
//僀儞僗僞儞僗傪僐儗僋僔儑儞偵捛壛 (弴彉偼搊榐弴)
bool Collection::addInstance(Collectable * _instance, bool _destruction) throw()
{
bool result = false;
if(isValid() && _instance != 0) {
// 摨偠僀儞僗僞儞僗傪2夞搊榐偟側偄傛偆偵偡傞
list<Element>::iterator scope;
scope = container.begin();
while(scope != container.end()) {
if(scope->instance == _instance)
break;
++ scope;
}
if(scope == container.end()) {
// 枛旜偵梫慺傪捛壛
struct Element element;
element.instance = _instance;
element.destruction = _destruction;
container.push_back(element);
}
else {
// 攋婞巜帵偼嵟怴偵捈偟偰偍偔
scope->destruction = _destruction;
}
result = true;
}
return result;
}
//巜掕偝傟偨Collectable僀儞僗僞儞僗偵憡摉偡傞応強傪巜偡僀僥儗乕僞偺庢摼 (getInstance)
list<Collection::Element>::const_iterator Collection::_findInstance(const Collectable * predecessor) const throw()
{
list<Element>::const_iterator result;
result = container.begin();
//predecessor偑巜掕偝傟偰偄偨応崌偼扵偡
if(predecessor != 0) {
while(result != container.end()) {
if(result->instance == predecessor) {
++ result; //専嶕偼predecessor偺師偐傜
break;
}
++ result;
}
}
return result;
}
//僐儗僋僔儑儞偐傜僀儞僗僞儞僗傪庢摼
Collectable * Collection::getInstance(const type_info & type, const Collectable * predecessor) const throw()
{
Collectable * result;
result = 0;
if(isValid()) {
list<Element>::const_iterator scope;
// 愭峴偡傞僀儞僗僞儞僗偺応強傪扵偡
scope = _findInstance(predecessor);
// 巜掕偝傟偨宆傪帩偮師偺僀儞僗僞儞僗傪扵偡
while(scope != container.end()) {
if(typeid(*scope->instance) == type) {
result = scope->instance;
break;
}
++ scope;
}
}
return result;
}
//僐儗僋僔儑儞偐傜僀儞僗僞儞僗傪彍奜 (攋婞偼偟側偄)
bool Collection::removeInstance(const Collectable * instance) throw()
{
bool result = false;
if(isValid() && instance != 0) {
list<Element>::iterator scope;
scope = container.begin();
while(scope != container.end()) {
if(scope->instance == instance) {
//梫慺偺嶍彍
container.erase(scope);
result = true;
break;
}
++ scope;
}
}
return result;
}
//慡梫慺偺攋婞
void Collection::clear(void)
{
if(isValid()) {
list<Element>::iterator scope;
list<Element>::iterator next;
//攋婞僼儔僌偑偮偄偰偄傞僀儞僗僞儞僗傪攋婞
scope = container.begin();
while(scope != container.end()) {
next = scope; //ROT宯偼delete偡傞偲帺暘傪徚偟偵偐偐傞偺偱丄僀僥儗乕僞傪曐懚偡傞偨傔偵愭偵師傪庢摼偟偰偍偔
++ next;
if(scope->destruction)
delete scope->instance;
scope = next;
}
container.clear();
}
}
/************************************************* 僥僗僩僗傿乕僩 *************************************************/
#ifdef TESTSUITE
#include "coverage_undefs.h"
class TestCollectable : public Collectable
{};
class TestCollectable2 : public Collectable
{ public: ~TestCollectable2(void) throw() { TestSuite::check("TestCollectable2::~TestCollectable2"); } };
#ifdef _MSC_VER
class TestCollectable3 : public Collectable
{ public: ~TestCollectable3(void) throw(int) { throw 0; } };
#endif
class TestCollectable4 : public TestCollectable2
{ public: ~TestCollectable4(void) throw() { TestSuite::check("TestCollectable4::~TestCollectable4"); } };
TESTSUITE(main, Collection)
{
BEGIN_CASE("addInstance","addInstance") {
BEGIN_CASE("1","僀儞僗僞儞僗偼惓偟偔搊榐偱偒傞") {
Collection col;
TestCollectable test;
TestCollectable2 test2;
BEGIN_CASE("1","搊榐偱偒TRUE偑曉傞 (1屄栚)") {
if(!col.addInstance(&test, false))
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2","搊榐偱偒true偑曉傞 (2屄栚)") {
if(!col.addInstance(&test2, false))
TEST_FAIL;
} END_CASE;
BEGIN_CASE("3","屄悢偑崌偭偰傞") {
if(col.container.size() != 2)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("4","弴斣偑偁偭偰傞") {
list<Element>::iterator scope;
scope = col.container.begin();
if(scope->instance != &test || scope->destruction != false)
TEST_FAIL;
++ scope;
if(scope->instance != &test2 || scope->destruction != false)
TEST_FAIL;
++ scope;
if(scope != col.container.end())
TEST_FAIL;
} END_CASE;
} END_CASE;
BEGIN_CASE("2","NULL僀儞僗僞儞僗偼搊榐偝傟側偄") {
Collection col;
BEGIN_CASE("1","NULL僀儞僗僞儞僗傪搉偡偲false偑曉傞") {
if(col.addInstance(0))
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2","壗傕搊榐偝傟偰偄側偄") {
if(col.container.size() != 0)
TEST_FAIL;
} END_CASE;
} END_CASE;
BEGIN_CASE("3","柍岠側僀儞僗僞儞僗偵懳偡傞憖嶌偱false偑曉傞") {
Collection col;
if(col.addInstance(0))
TEST_FAIL;
} END_CASE;
BEGIN_CASE("4","摨堦偺僀儞僗僞儞僗傪2夞搊榐偟偰傕1屄偩偗偟偐搊榐偝傟側偄") {
Collection col;
TestCollectable test;
TEST_CASE("0","[慜採] 堦夞栚偼惓偟偔惉岟偡傞", col.addInstance(&test, false));
TEST_CASE("1","娭悢偼惓偟偔廔椆偡傞", col.addInstance(&test, false));
TEST_CASE("2","僀儞僗僞儞僗悢偼憹偊偰偄側偄", col.container.size() == 1);
} END_CASE;
} END_CASE;
BEGIN_CASE("getInstance(type_info)","getInstance(type_info)") {
Collection col;
TestCollectable test;
TestCollectable test_2;
TestCollectable test_3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -