📄 singleton.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: singleton.h,v 1.1 2006/04/14 02:46:07 9564907 Exp $
*/
// $Header: J:\\SapporoSoft\\usb20\\S1R72V05\\Debug\\Repository/S1R72V05_Toppers/Toppers/cfg/base/singleton.h,v 1.1 2006/04/14 02:46:07 9564907 Exp $
#include "base/testsuite.h"
#ifndef SINGLETON_H
#define SINGLETON_H
#ifdef _MSC_VER
# pragma warning(disable:4290) //C++ 偺椺奜偺巜掕偼柍帇偝傟傑偡丅娭悢偑 __declspec(nothrow) 偱側偄偙偲偺傒昞帵偝傟傑偡丅
# pragma warning(disable:4291) //弶婜壔僐亅僪偑椺奜傪僗儘亅偡傞偲丄'new' 墘嶼巕傪巊梡偟偰偄傞偲儊儌儕傪夝曻偟傑偣傫丅
# pragma warning(disable:4786) //僨僶僢僌忣曬偱幆暿巕偑 255 暥帤偵愗傝幪偰傜傟傑偟偨丅
#endif
#include <new>
#include <stdexcept>
/*
* 僔儞僌儖僩儞僷僞乕儞 幚憰
*/
//僔儞僌儖僩儞婎掙僋儔僗
// #gcc偑friend class Singleton傪怘偭偰偔傟側偄偺偱婎掙傪嶌傞
class SingletonBase {
public:
//僨僗僩儔僋僞偺僠僃乕儞
struct chain {
struct chain * next;
void (*destructor)(void);
};
//僔儞僌儖僩儞偱僀儞僗僞儞僗傪惗惉偟偨偙偲傪帵偡偨傔偺僋儔僗
// 偙偺僋儔僗偼Singleton偐傜偟偐幚懱壔偱偒側偄偺偱丄
// 傎偐偺僋儔僗偐傜偙傟傪堷悢偵傕偮僐儞僗僩儔僋僞傪巊梡偡傞偙偲偼偱偒側偄
class SingletonConstructionLocker
{
friend class SingletonBase;
protected:
SingletonConstructionLocker() {}
};
private:
static bool initialize; //弶婜壔嵪傒偐 (atexit傪屇傫偩偐偳偆偐)
static bool is_valid; //惗惉偵幐攕偟偨僔儞僌儖僩儞僈偁傞偐偳偆偐
static struct chain * top; //僨僗僩儔僋僞僠僃乕儞
//僨僗僩儔僋僞偺婲摦
static void final_release(void) throw()
{
//愭摢偐傜嶍彍 (愭擖傟屻弌偟)
while(top != 0) {
struct chain * node = top;
top = node->next;
(*node->destructor)();
delete node;
}
}
protected:
//僠僃乕儞偵僨僗僩儔僋僞傪捛壛
static bool addDestructor(void (* destructor)(void)) throw()
{
bool result = false;
struct SingletonBase::chain * node = new(std::nothrow) struct SingletonBase::chain;
if(node != 0) {
//怴偟偄梫慺傪愭摢偵捛壛
node->next = top;
node->destructor = destructor;
top = node;
result = true;
//梫慺攋婞偺偨傔偵嵟屻偵攋婞儖乕僠儞傪婲摦偝偣傞
if(!initialize) {
atexit(final_release);
initialize = true;
}
}
return result;
}
//SingletonConstructionLocker僼傽僋僩儕
inline static SingletonConstructionLocker getLocker(void)
{ return SingletonConstructionLocker(); }
public:
//惗惉偵幐攕偟偨僔儞僌儖僩儞偑側偄偙偲傪庢摼
static bool isValid(void) throw()
{ return is_valid; }
#ifdef TESTSUITE
/*
* 僐儞僥僉僗僩僠僃乕儞 (僥僗僩梡僔儞僌儖僩儞僐儞僥僉僗僩戅旔僠僃乕儞)
*/
class ContextChain {
protected:
struct chain {
struct chain * next;
void * context;
void (*restore)(void * & context) throw();
void (*renew)(void) throw();
void (*clear)(void);
} * top;
public:
ContextChain(void) throw() : top(0)
{}
~ContextChain(void) throw()
{ restoreContext(); }
/* 僐儞僥僉僗僩偺暅婣 */
void restoreContext(void) throw()
{
while(top != 0) {
struct chain * node = top;
top = top->next;
(*node->restore)(node->context);
delete node;
}
}
/* 慡偰偺僀儞僗僞儞僗傪嵞惗惉 */
void renewInstance(void) throw(std::bad_alloc)
{
struct chain * node = top;
while(node != 0) {
(*node->renew)();
node = node->next;
}
}
/* 慡偰偺僀儞僗僞儞僗傪攋婞 */
void clearInstance(void) throw(std::bad_alloc)
{
struct chain * node = top;
while(node != 0) {
(*node->clear)();
node = node->next;
}
}
/* 僐儞僥僉僗僩偺戅旔 */
template<class T> void saveContext(void) throw(std::bad_alloc)
{
struct chain * node = new struct chain;
node->next = top;
node->restore = reinterpret_cast<void(*)(void*&)>(Singleton<T>::restoreContext);
node->renew = Singleton<T>::renewInstance;
node->clear = Singleton<T>::clearInstance;
Singleton<T>::saveContext(reinterpret_cast<Singleton<T>::Context &>(node->context));
top = node;
}
};
#endif
TESTSUITE_PROTOTYPE(main)
};
template<class T>
class Singleton : SingletonBase
{
protected:
Singleton(void);
~Singleton(void);
static T * instance;
//僨僗僩儔僋僞
static void destructor(void) throw()
{
if(instance != 0) {
try{ delete instance; } catch(...) {}
instance = 0;
}
}
//僀儞僗僞儞僗偺惗惉 (僀儞僗僞儞僗惗惉 + 僨僗僩儔僋僞搊榐 + 僄儔乕張棟)
static T * createInstance(std::nothrow_t) throw()
{
T * instance = 0;
try {
instance = new(std::nothrow) T(getLocker());
if(instance != 0) {
if(!addDestructor(destructor))
throw false; //catch愡傪幚峴偝偣偨偄
}
}
catch(...) {
if(instance != 0) {
try { delete instance; } catch(...) {}
instance = 0;
}
}
return instance;
}
//僀儞僗僞儞僗惗惉 (bad_alloc椺奜傪僗儘乕偡傞幚憰)
static T * createInstance(void) throw(std::bad_alloc)
{
instance = createInstance(std::nothrow);
if(instance == 0)
throw std::bad_alloc();
return instance;
}
public:
//僀儞僗僞儞僗偺庢摼
inline static T * getInstance(void) throw(std::bad_alloc)
{
if(instance == 0)
instance = createInstance();
return instance;
}
//僀儞僗僞儞僗偺庢摼
inline static T * getInstance(std::nothrow_t) throw()
{
if(instance == 0)
instance = createInstance(std::nothrow);
return instance;
}
#ifdef TESTSUITE /* 僥僗僩梡 */
typedef T * Context; //戅旔梡
//怴偟偄僀儞僗僞儞僗偺惗惉
static void renewInstance(void) throw(std::bad_alloc)
{
destructor(); //崱偺僀儞僗僞儞僗傪攋婞
instance = new T(getLocker()); //怴偟偄僀儞僗僞儞僗傪惗惉 (僨僗僩儔僋僞偑搊榐偝傟偰偟傑偆偺偱createInstance傪屇傫偱偼偄偗側偄)
}
//僀儞僗僞儞僗攋婞
static void clearInstance(void) throw()
{ destructor(); }
//僐儞僥僉僗僩戅旔
static void saveContext(Context & context) throw()
{
context = instance;
instance = 0;
}
//僐儞僥僉僗僩暅婣
static void restoreContext(Context & context) throw()
{
destructor(); //崱偺僀儞僗僞儞僗傪攋婞
instance = context; //戅旔偟偨僀儞僗僞儞僗傪暅婣
}
#endif /* TESTSUITE */
};
//Singleton 惷揑曄悢
template<class T> T * Singleton<T>::instance = 0;
#define SINGLETON_CONSTRUCTOR(x) explicit x(const SingletonBase::SingletonConstructionLocker &)
#define SINGLETON_CONSTRUCTOR_(x) explicit x(const SingletonBase::SingletonConstructionLocker & _singleton)
#define SINGLETON_WRAPPER(derived, base) class derived : public base { public: SINGLETON_CONSTRUCTOR_(derived) throw() : base(_singleton) {} };
#endif /* SINGLETON_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -