📄 message.h
字号:
// $Header: /home/CVS/configurator/base/message.h,v 1.3 2003/06/30 12:50:32 takayuki Exp $
#ifndef MESSAGE_H
#define MESSAGE_H
#ifdef _MSC_VER
# pragma warning(disable:4290) //C++ 偺椺奜偺巜掕偼柍帇偝傟傑偡丅娭悢偑 __declspec(nothrow) 偱側偄偙偲偺傒昞帵偝傟傑偡丅
# pragma warning(disable:4786) //僨僶僢僌忣曬偱幆暿巕偑255暥帤偵愗傝幪偰傜傟傑偟偨丅
#endif
#include <string>
#include <sstream>
#include <stdarg.h>
#include <stdexcept>
//#include "base/testsuite.h"
#include "base/singleton.h"
#include "base/except.h"
/*
* 暥帤楍惍宍僿儖僷乕僋儔僗
*/
class Formatter
{
protected:
std::string templatestring; //僥儞僾儗乕僩暥帤楍
std::stringstream content; //暥帤楍惍宍偲彂幃嵪傒暥帤楍壜擻応強梡僗僩儕乕儉
bool accept; //堷悢傪庴擖壜擻偐偳偆偐
//師偺堷悢偺憓擖愭傊偲堏摦
void shift(void) throw();
//枹愝掕偺堷悢傪 (null) 偱抲姺
void shift_all(void) throw();
public:
//僨僼僅儖僩僐儞僗僩儔僋僞
Formatter(void) throw();
//僐儞僗僩儔僋僞
Formatter(std::string src) throw();
//僐僺乕僐儞僗僩儔僋僞
Formatter(const Formatter & src) throw();
//僥儞僾儗乕僩暥帤楍偺愝掕 (拞恎偼徚偊傞)
void assign(std::string src) throw();
//撪梕偺僋儕傾
void clear(void) throw();
//戙擖墘嶼巕
Formatter & operator =(const Formatter & src) throw();
//堷悢偺愝掕
template<class T>
Formatter & operator << (const T & src) throw()
{
std::string::size_type pos;
//堷悢傪庴擖壜擻側傜
if(accept) {
pos = content.str().size();
content << src;
/* 嬻暥帤偱側偗傟偽師偺憓擖応強偵堏摦 */
if(pos != content.str().size())
shift();
}
return *this;
}
//撪晹儊僢僙乕僕偺庢傝弌偟
std::string str(void) throw();
//昗弨弌椡僗僩儕乕儉傊偺弌椡梡 operator << 掕媊
friend inline std::ostream & operator << (std::ostream & out, Formatter msg) throw()
{
out << msg.content.str();
return out;
}
// TESTSUITE_PROTOTYPE(main);
};
/*
* 儊僢僙乕僕弌椡梡偵2尵岅懳墳傪峴偭偨暥帤楍惍宍僿儖僷乕僋儔僗
*/
class Message : public Formatter
{
public:
enum tagLanguage
{
ENGLISH = 0,
JAPANESE = 1,
LAST_LANG, //嵟屻偺埵抲
NEUTRAL = ENGLISH, //昗弨尵岅
};
/* 弌椡儊僢僙乕僕尵岅惂屼僋儔僗 */
class MessageControl
{
protected:
int language; //偳偺尵岅傪巊梡偡傞偺偐
public:
//僐儞僗僩儔僋僞
SINGLETON_CONSTRUCTOR(MessageControl) throw() : language(NEUTRAL) {};
//傾僋僙僒
inline void setLanguage(int _language) throw() { language = _language; }
inline int getLanguage(void) const throw() { return language; }
const char * select(const char * msg1, const char * msg2, va_list vl ) throw();
};
protected:
//攈惗僋儔僗梡僐儞僗僩儔僋僞
void _initialize(const char * src1, const char * src2, va_list vl) throw();
public:
//僨僼僅儖僩僐儞僗僩儔僋僞
Message(void) throw();
//僐儞僗僩儔僋僞 (扨堦尵岅)
Message(std::string src) throw();
//僐儞僗僩儔僋僞 (暋悢尵岅)
Message(const char * src1, const char * src2, ... ) throw();
//僐僺乕僐儞僗僩儔僋僞
Message(const Message & src) throw();
//僥儞僾儗乕僩暥帤楍偺愝掕 (拞恎偼徚偊傞)
void assign(std::string src) throw() { Formatter::assign(src); }
void assign(const char * src1, const char * src2, ... ) throw();
//尵岅慖戰
static void selectLanguage(enum tagLanguage lang = NEUTRAL) throw();
//尵岅偺庢摼
static enum tagLanguage getCurrentLanguage(void) throw()
{ return static_cast<enum tagLanguage>(Singleton<MessageControl>::getInstance()->getLanguage()); }
//TESTSUITE_PROTOTYPE(main);
};
/*
* 忕挿弌椡梡暥帤楍惍宍僿儖僷乕僋儔僗
*/
class VerboseMessage : public Message
{
public:
//嫟捠偺忕挿弌椡忣曬傪帩偮僋儔僗
class VerboseControl
{
public:
bool verbose; //忕挿弌椡傪峴偆偐偳偆偐
std::ostream * out; //弌椡愭
//僐儞僗僩儔僋僞
SINGLETON_CONSTRUCTOR(VerboseControl) throw() : verbose(false), out(0)
{}
//忕挿弌椡惂屼
template<class T>
inline const VerboseControl & operator << (const T & src) const throw()
{
if(verbose && out != 0)
(*out) << src;
return *this;
}
//verbose傾僋僙僒
void setVerbose(bool _verbose) throw()
{ verbose = _verbose; }
bool getVerbose(void) const throw()
{ return verbose; }
//out傾僋僙僒
void setStream(std::ostream * _out) throw()
{ out = _out; }
std::ostream * getStream(void) throw()
{ return out; }
};
//忕挿弌椡僗僩儕乕儉 (扨弮側梷巭婡峔偮偒儔僢僷乕僋儔僗)
class VerboseStream
{
protected:
std::ostream * out;
public:
VerboseStream(std::ostream * _out = 0) throw() : out(_out) {};
template<class T>
VerboseStream & operator << (const T & src) throw()
{
if(out != 0)
(*out) << src;
return *this;
}
};
protected:
//僨僼僅儖僩僐儞僗僩儔僋僞 (僥僗僩梡)
VerboseMessage(void) throw();
public:
//僐儞僗僩儔僋僞
VerboseMessage(const char * src) throw();
VerboseMessage(const std::string & src) throw();
VerboseMessage(const char * src1, const char * src2, ... ) throw();
//僨僗僩儔僋僞
~VerboseMessage(void) throw();
//忕挿弌椡惂屼 (傾僋僙僒夞憲)
inline static void setVerbose(bool _verbose) throw()
{ Singleton<VerboseControl>::getInstance()->setVerbose(_verbose); }
//忕挿弌椡愭僗僩儕乕儉愝掕 (傾僋僙僒夞憲)
inline static void setStream(std::ostream * _stream) throw()
{ Singleton<VerboseControl>::getInstance()->setStream(_stream); }
//忕挿弌椡惂屼偺忬懺庢摼
inline static bool getVerbose(void) throw()
{ return Singleton<VerboseControl>::getInstance()->getVerbose(); }
//忕挿弌椡愭僗僩儕乕儉偺庢摼 (愝掕偟偨傕偺偲摨偠傕偺偼弌偰偙側偄)
inline static VerboseStream getStream(void) throw()
{ return VerboseStream(getVerbose() ? Singleton<VerboseControl>::getInstance()->getStream() : 0); }
//TESTSUITE_PROTOTYPE(main);
};
/*
* 椺奜儊僢僙乕僕梡暥帤楍惍宍僿儖僷乕僋儔僗
*/
class ExceptionMessage : public Message
{
public:
//椺奜偺婋尟搙儗儀儖
enum tagLevel {
DEFAULT = 0,
FATAL = 1,
WARNING = 2
};
#ifdef EXCEPT_H
class ExceptionMessageException : public Exception
{
friend class ExceptionMessage;
protected:
ExceptionMessageException(int _code, std::string _details) throw() : Exception("ExceptionMessage", _code, _details) {}
};
#endif
protected:
enum tagLevel level;
public:
//僐儞僗僩儔僋僞
ExceptionMessage(const char * src) throw();
ExceptionMessage(const std::string & src) throw();
ExceptionMessage(const char * src1, const char * src2, ...) throw();
ExceptionMessage(enum tagLevel level = DEFAULT) throw();
ExceptionMessage(enum tagLevel level, const char * src) throw();
ExceptionMessage(enum tagLevel level, const std::string & src) throw();
ExceptionMessage(enum tagLevel level, const char * src1, const char * src2, ...) throw();
ExceptionMessage(const ExceptionMessage & src) throw();
//婋尟搙儗儀儖嶲徠
inline bool operator == (enum tagLevel _level) const throw()
{ return level == _level; }
//婋尟搙儗儀儖嶲徠
inline bool operator != (enum tagLevel _level) const throw()
{ return !(operator ==(_level)); }
//堷悢偺愝掕 (曉媝抣偺宆傪Message & 偐傜 ExceptionMessage & 偵偡傞偨傔偺彫嵶岺)
template<class T>
inline ExceptionMessage & operator << (const T & src) throw()
{
Message::operator << ( src );
return *this;
}
#ifdef EXCEPT_H //Exception僋儔僗傪巊梡偡傞応崌
//椺奜偺敪惗
bool throwException(void) throw(Exception)
{
ExceptionMessageException exc(level, str());
return exc.throwException();
}
//儅僯僺儏儗乕僞
inline ExceptionMessage & operator << ( ExceptionMessage & (* func)(ExceptionMessage &) ) throw(Exception)
{ return (*func)(*this); }
#else //stdexcept傪巊梡偡傞応崌
//椺奜偺敪惗
template<class T>
bool throwException(void) throw(T)
{
throw T(str());
return true;
}
//儅僯僺儏儗乕僞
inline ExceptionMessage & operator << ( ExceptionMessage & (* func)(ExceptionMessage &) ) throw(T)
{ return (*func)(*this); }
#endif
//TESTSUITE_PROTOTYPE(main);
};
#ifdef EXCEPT_H //Exception僋儔僗傪巊梡偡傞応崌
//椺奜傪僗儘乕偡傞偨傔偺儅僯僺儏儗乕僞
inline ExceptionMessage & throwException(ExceptionMessage & excmsg) throw(Exception)
{
excmsg.throwException();
return excmsg;
}
#else
//椺奜傪僗儘乕偡傞偨傔偺儅僯僺儏儗乕僞
template<class T>
inline ExceptionMessage & throwException(ExceptionMessage & excmsg) throw(T)
{
excmsg.throwException<T>();
return excmsg;
}
#endif
/*
* 僨僶僢僌忣曬弌椡梡暥帤楍惍宍僿儖僷乕僋儔僗
*/
class DebugMessage : public Formatter
{
public:
//嫟捠偺忕挿弌椡忣曬傪帩偮僋儔僗 (僔儞僌儖僩儞偵偡傞偺偱VerboseControl傪摨偠傕偺傪僋儔僗壔偡傞)
class DebugControl : public VerboseMessage::VerboseControl
{ public: SINGLETON_CONSTRUCTOR_(DebugControl) throw() : VerboseMessage::VerboseControl(_singleton) {} };
protected:
//僨僼僅儖僩僐儞僗僩儔僋僞 (僥僗僩梡)
DebugMessage(void) throw();
public:
//僐儞僗僩儔僋僞
DebugMessage(std::string src) throw();
//僨僗僩儔僋僞
~DebugMessage(void) throw();
//忕挿弌椡惂屼 (傾僋僙僒夞憲)
inline static void setVerbose(bool _verbose) throw()
{ Singleton<DebugControl>::getInstance()->setVerbose(_verbose); }
//忕挿弌椡愭僗僩儕乕儉愝掕 (傾僋僙僒夞憲)
inline static void setStream(std::ostream * _stream) throw()
{ Singleton<DebugControl>::getInstance()->setStream(_stream); }
//忕挿弌椡惂屼偺忬懺庢摼
inline static bool getVerbose(void) throw()
{ return Singleton<DebugControl>::getInstance()->getVerbose(); }
//忕挿弌椡愭僗僩儕乕儉偺庢摼 (愝掕偟偨傕偺偲摨偠傕偺偼弌偰偙側偄)
inline static VerboseMessage::VerboseStream getStream(void) throw()
{ return VerboseMessage::VerboseStream(getVerbose() ? Singleton<DebugControl>::getInstance()->getStream() : 0); }
// TESTSUITE_PROTOTYPE(main);
};
#endif //MESSAGE_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -