-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathut.h
executable file
·171 lines (152 loc) · 5.81 KB
/
ut.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/////////////////////////////////////////////////
// Type-Name
template <class Type>
class TTypeNm: public TStr{
public:
static TStr GetNrTypeNm(const TStr& TypeNm){
if (TypeNm.IsPrefix("class ")){
return TypeNm.GetSubStr(6, TypeNm.Len()-1);}
else {return TypeNm;}}
public:
TTypeNm(): TStr(GetNrTypeNm((char*)(typeid(Type).name()))){}
};
template <class Type>
TStr GetTypeNm(const Type& Var){
TStr TypeNm=TStr(typeid(Var).name());
return TTypeNm<Type>::GetNrTypeNm(TypeNm);
}
/////////////////////////////////////////////////
// Notifications
inline void InfoNotify(const TStr& MsgStr){InfoNotify(MsgStr.CStr());}
inline void WarnNotify(const TStr& MsgStr){WarnNotify(MsgStr.CStr());}
inline void ErrNotify(const TStr& MsgStr){ErrNotify(MsgStr.CStr());}
inline void StatNotify(const TStr& MsgStr){StatNotify(MsgStr.CStr());}
typedef enum {ntInfo, ntWarn, ntErr, ntStat} TNotifyType;
ClassTP(TNotify, PNotify)//{
private:
TNotify(const TNotify&);
TNotify& operator=(const TNotify&);
public:
TNotify(){}
virtual ~TNotify(){}
virtual void OnNotify(const TNotifyType& /*Type*/, const TStr& /*MsgStr*/){}
virtual void OnStatus(const TStr& /*MsgStr*/){}
virtual void OnLn(const TStr& /*MsgStr*/){}
virtual void OnTxt(const TStr& /*MsgStr*/){}
static TStr GetTypeStr(
const TNotifyType& Type, const bool& Brief=true);
static void OnNotify(const PNotify& Notify,
const TNotifyType& Type, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnNotify(Type, MsgStr);}}
static void OnStatus(const PNotify& Notify, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnStatus(MsgStr);}}
static void OnLn(const PNotify& Notify, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnLn(MsgStr);}}
static void OnTxt(const PNotify& Notify, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnTxt(MsgStr);}}
static void DfOnNotify(const TNotifyType& Type, const TStr& MsgStr);
static const PNotify NullNotify;
static const PNotify StdNotify;
};
/////////////////////////////////////////////////
// Null-Notifier
class TNullNotify: public TNotify{
public:
TNullNotify(){}
static PNotify New(){return PNotify(new TNullNotify());}
void OnNotify(const TNotifyType& Type, const TStr& MsgStr){}
void OnStatus(const TStr& MsgStr){}
};
/////////////////////////////////////////////////
// Callback-Notifier
typedef void (__stdcall *TCallbackF)(const TNotifyType& Type, const TStr& MsgStr);
class TCallbackNotify : public TNotify
{
private:
TCallbackF CallbackF;
public:
TCallbackNotify(const TCallbackF& _CallbackF) : CallbackF(_CallbackF) {}
static PNotify New(const TCallbackF& CallbackF) { return PNotify(new TCallbackNotify(CallbackF)); }
void OnNotify(const TNotifyType& Type, const TStr& MsgStr)
{
Assert(CallbackF != NULL);
CallbackF(Type, MsgStr);
}
void OnStatus(const TStr& MsgStr)
{
Assert(CallbackF != NULL);
CallbackF(ntStat, MsgStr);
}
};
/////////////////////////////////////////////////
// Native-Callback-Notifier
typedef void (__stdcall *TNativeCallbackF)(int Type, const char* MsgStr);
class TNativeCallbackNotify : public TNotify
{
private:
TNativeCallbackF CallbackF;
public:
TNativeCallbackNotify(const TNativeCallbackF& _CallbackF) : CallbackF(_CallbackF) {}
static PNotify New(const TNativeCallbackF& CallbackF) { return PNotify(new TNativeCallbackNotify(CallbackF)); }
void OnNotify(const TNotifyType& Type, const TStr& MsgStr)
{
Assert(CallbackF != NULL);
CallbackF((int)Type, MsgStr.CStr());
}
void OnStatus(const TStr& MsgStr)
{
Assert(CallbackF != NULL);
CallbackF((int)ntStat, MsgStr.CStr());
}
};
/////////////////////////////////////////////////
// Standard-Notifier
class TStdNotify: public TNotify{
public:
TStdNotify(){}
static PNotify New(){return PNotify(new TStdNotify());}
void OnNotify(const TNotifyType& Type, const TStr& MsgStr);
void OnStatus(const TStr& MsgStr);
};
/////////////////////////////////////////////////
// Exception
ClassTP(TExcept, PExcept)//{
private:
TStr MsgStr;
TStr LocStr;
UndefDefaultCopyAssign(TExcept);
public:
TExcept(const TStr& _MsgStr): MsgStr(_MsgStr), LocStr(){}
TExcept(const TStr& _MsgStr, const TStr& _LocStr): MsgStr(_MsgStr), LocStr(_LocStr){}
virtual ~TExcept(){}
TStr GetMsgStr() const {return MsgStr;}
TStr GetLocStr() const {return LocStr;}
TStr GetStr() const {
if (LocStr.Empty()){return GetMsgStr();}
else {return GetLocStr()+": "+GetMsgStr();}}
// replacement exception handler
typedef void (*TOnExceptF)(const TStr& MsgStr);
static TOnExceptF OnExceptF;
static bool IsOnExceptF(){return OnExceptF!=NULL;}
static void PutOnExceptF(TOnExceptF _OnExceptF){OnExceptF=_OnExceptF;}
static TOnExceptF GetOnExceptF(){return OnExceptF;}
// throwing exception
static void Throw(const TStr& MsgStr){
if (IsOnExceptF()){(*OnExceptF)(MsgStr);}
else {throw PExcept(new TExcept(MsgStr));}}
static void Throw(const TStr& MsgStr, const TStr& ArgStr){
TStr FullMsgStr=MsgStr+" ("+ArgStr+")";
if (IsOnExceptF()){(*OnExceptF)(FullMsgStr);}
else {throw PExcept(new TExcept(FullMsgStr));}}
static void Throw(const TStr& MsgStr, const TStr& ArgStr1, const TStr& ArgStr2){
TStr FullMsgStr=MsgStr+" ("+ArgStr1+", "+ArgStr2+")";
if (IsOnExceptF()){(*OnExceptF)(FullMsgStr);}
else {throw PExcept(new TExcept(FullMsgStr));}}
static void ThrowFull(const TStr& MsgStr, const TStr& LocStr){
if (IsOnExceptF()){(*OnExceptF)(MsgStr);}
else {throw PExcept(new TExcept(MsgStr, LocStr));}}
};
#define Try try {
#define Catch } catch (PExcept Except){ErrNotify(Except->GetMsgStr());}
#define CatchFull } catch (PExcept Except){ErrNotify(Except->GetStr());}
#define CatchAll } catch (...){}