SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ut.h
Go to the documentation of this file.
1 #include "bd.h"
2 
4 // Type-Name
5 template <class Type>
6 class TTypeNm: public TStr{
7 public:
8  static TStr GetNrTypeNm(const TStr& TypeNm){
9  if (TypeNm.IsPrefix("class ")){
10  return TypeNm.GetSubStr(6, TypeNm.Len()-1);}
11  else {return TypeNm;}}
12 public:
13  TTypeNm(): TStr(GetNrTypeNm((char*)(typeid(Type).name()))){}
14 };
15 template <class Type>
16 TStr GetTypeNm(const Type& Var){
17  TStr TypeNm=TStr(typeid(Var).name());
18  return TTypeNm<Type>::GetNrTypeNm(TypeNm);
19 }
20 
22 // Notifications
23 inline void InfoNotify(const TStr& MsgStr){InfoNotify(MsgStr.CStr());}
24 inline void WarnNotify(const TStr& MsgStr){WarnNotify(MsgStr.CStr());}
25 inline void ErrNotify(const TStr& MsgStr){ErrNotify(MsgStr.CStr());}
26 inline void StatNotify(const TStr& MsgStr){StatNotify(MsgStr.CStr());}
27 
29 
31 private:
32  TNotify(const TNotify&);
33  TNotify& operator=(const TNotify&);
34 public:
35  TNotify(){}
36  virtual ~TNotify(){}
37 
38  virtual void OnNotify(const TNotifyType& /*Type*/, const TStr& /*MsgStr*/){}
39  virtual void OnStatus(const TStr& /*MsgStr*/){}
40  virtual void OnLn(const TStr& /*MsgStr*/){}
41  virtual void OnTxt(const TStr& /*MsgStr*/){}
42 
43  // shortcuts for easier formationg
44  void OnNotifyFmt(const TNotifyType& Type, const char *FmtStr, ...);
45  void OnStatusFmt(const char *FmtStr, ...);
46  void OnLnFmt(const char *FmtStr, ...);
47  void OnTxtFmt(const char *FmtStr, ...);
48 
49  static TStr GetTypeStr(
50  const TNotifyType& Type, const bool& Brief=true);
51  static void OnNotify(const PNotify& Notify,
52  const TNotifyType& Type, const TStr& MsgStr){
53  if (!Notify.Empty()){Notify->OnNotify(Type, MsgStr);}}
54  static void OnStatus(const PNotify& Notify, const TStr& MsgStr){
55  if (!Notify.Empty()){Notify->OnStatus(MsgStr);}}
56  static void OnLn(const PNotify& Notify, const TStr& MsgStr){
57  if (!Notify.Empty()){Notify->OnLn(MsgStr);}}
58  static void OnTxt(const PNotify& Notify, const TStr& MsgStr){
59  if (!Notify.Empty()){Notify->OnTxt(MsgStr);}}
60  static void DfOnNotify(const TNotifyType& Type, const TStr& MsgStr);
61 
62  static const PNotify NullNotify;
63  static const PNotify StdNotify;
64  static const PNotify StdErrNotify;
65 };
66 
68 // Null-Notifier
69 class TNullNotify: public TNotify{
70 public:
72  static PNotify New(){return PNotify(new TNullNotify());}
73 
74  void OnNotify(const TNotifyType& Type, const TStr& MsgStr){}
75  void OnStatus(const TStr& MsgStr){}
76 };
77 
79 // Callback-Notifier
80 typedef void (__stdcall *TCallbackF)(const TNotifyType& Type, const TStr& MsgStr);
81 class TCallbackNotify : public TNotify
82 {
83 private:
85 public:
86  TCallbackNotify(const TCallbackF& _CallbackF) : CallbackF(_CallbackF) {}
87  static PNotify New(const TCallbackF& CallbackF) { return PNotify(new TCallbackNotify(CallbackF)); }
88 
89  void OnNotify(const TNotifyType& Type, const TStr& MsgStr)
90  {
91  Assert(CallbackF != NULL);
92  CallbackF(Type, MsgStr);
93  }
94  void OnStatus(const TStr& MsgStr)
95  {
96  Assert(CallbackF != NULL);
97  CallbackF(ntStat, MsgStr);
98  }
99 };
100 
102 // Native-Callback-Notifier
103 typedef void (__stdcall *TNativeCallbackF)(int Type, const char* MsgStr);
105 {
106 private:
108 public:
109  TNativeCallbackNotify(const TNativeCallbackF& _CallbackF) : CallbackF(_CallbackF) {}
110  static PNotify New(const TNativeCallbackF& CallbackF) { return PNotify(new TNativeCallbackNotify(CallbackF)); }
111 
112  void OnNotify(const TNotifyType& Type, const TStr& MsgStr)
113  {
114  Assert(CallbackF != NULL);
115  CallbackF((int)Type, MsgStr.CStr());
116  }
117  void OnStatus(const TStr& MsgStr)
118  {
119  Assert(CallbackF != NULL);
120  CallbackF((int)ntStat, MsgStr.CStr());
121  }
122 };
123 
125 // Standard-Notifier
126 class TStdNotify: public TNotify{
127 public:
129  static PNotify New(){return PNotify(new TStdNotify());}
130 
131  void OnNotify(const TNotifyType& Type, const TStr& MsgStr);
132  void OnStatus(const TStr& MsgStr);
133 };
134 
136 // Standard-Error-Notifier
137 class TStdErrNotify: public TNotify{
138 public:
140  static PNotify New(){return PNotify(new TStdErrNotify());}
141 
142  void OnNotify(const TNotifyType& Type, const TStr& MsgStr);
143  void OnStatus(const TStr& MsgStr);
144 };
145 
147 // Log-Notify
148 class TLogNotify : public TNotify {
149 private:
151 
152 public:
153  TLogNotify(const PNotify& _Notify): Notify(_Notify) { }
154  static PNotify New(const PNotify& Notify) { return new TLogNotify(Notify); }
155 
156  void OnStatus(const TStr& MsgStr);
157 };
158 
160 // Exception
162 private:
163  TStr MsgStr;
164  TStr LocStr;
166 public:
167  TExcept(const TStr& _MsgStr): MsgStr(_MsgStr), LocStr(){}
168  TExcept(const TStr& _MsgStr, const TStr& _LocStr): MsgStr(_MsgStr), LocStr(_LocStr){}
169  static PExcept New(const TStr& MsgStr, const TStr& LocStr = TStr()) {
170  return PExcept(new TExcept(MsgStr, LocStr)); }
171  virtual ~TExcept(){}
172 
173  TStr GetMsgStr() const {return MsgStr;}
174  TStr GetLocStr() const {return LocStr;}
175  TStr GetStr() const {
176  if (LocStr.Empty()){return GetMsgStr();}
177  else {return GetLocStr()+": "+GetMsgStr();}}
178 
179  // replacement exception handler
180  typedef void (*TOnExceptF)(const TStr& MsgStr);
181  static TOnExceptF OnExceptF;
182  static bool IsOnExceptF(){return OnExceptF!=NULL;}
183  static void PutOnExceptF(TOnExceptF _OnExceptF){OnExceptF=_OnExceptF;}
184  static TOnExceptF GetOnExceptF(){return OnExceptF;}
185 
186  // throwing exception
187  static void Throw(const TStr& MsgStr){
188  if (IsOnExceptF()){(*OnExceptF)(MsgStr);}
189  else {throw TExcept::New(MsgStr);}}
190  static void Throw(const TStr& MsgStr, const TStr& ArgStr){
191  TStr FullMsgStr=MsgStr+" ("+ArgStr+")";
192  if (IsOnExceptF()){(*OnExceptF)(FullMsgStr);}
193  else {throw TExcept::New(FullMsgStr);}}
194  static void Throw(const TStr& MsgStr, const TStr& ArgStr1, const TStr& ArgStr2){
195  TStr FullMsgStr=MsgStr+" ("+ArgStr1+", "+ArgStr2+")";
196  if (IsOnExceptF()){(*OnExceptF)(FullMsgStr);}
197  else {throw TExcept::New(FullMsgStr);}}
198  static void ThrowFull(const TStr& MsgStr, const TStr& LocStr){
199  if (IsOnExceptF()){(*OnExceptF)(MsgStr);}
200  else {throw TExcept::New(MsgStr, LocStr);}}
201 };
202 
203 #define Try try {
204 #define Catch } catch (PExcept Except){ErrNotify(Except->GetMsgStr());}
205 #define CatchFull } catch (PExcept Except){ErrNotify(Except->GetStr());}
206 #define CatchAll } catch (...){}
TStr GetTypeNm(const Type &Var)
Definition: ut.h:16
static void Throw(const TStr &MsgStr, const TStr &ArgStr1, const TStr &ArgStr2)
Definition: ut.h:194
static PExcept New(const TStr &MsgStr, const TStr &LocStr=TStr())
Definition: ut.h:169
TExcept(const TStr &_MsgStr, const TStr &_LocStr)
Definition: ut.h:168
int Len() const
Definition: dt.h:487
#define UndefDefaultCopyAssign(TNm)
Definition: bd.h:203
TPt< TExcept > PExcept
Definition: ut.h:161
static void PutOnExceptF(TOnExceptF _OnExceptF)
Definition: ut.h:183
bool IsPrefix(const char *Str) const
Definition: dt.cpp:1081
TCallbackF CallbackF
Definition: ut.h:84
void ErrNotify(const TStr &MsgStr)
Definition: ut.h:25
static void OnLn(const PNotify &Notify, const TStr &MsgStr)
Definition: ut.h:56
void OnStatus(const TStr &MsgStr)
Definition: ut.cpp:79
TPt< TNotify > PNotify
Definition: ut.h:30
bool Empty() const
Definition: bd.h:501
TStr GetLocStr() const
Definition: ut.h:174
void OnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.cpp:88
void InfoNotify(const TStr &MsgStr)
Definition: ut.h:23
void OnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.h:89
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
virtual ~TNotify()
Definition: ut.h:36
Definition: ut.h:69
TNativeCallbackF CallbackF
Definition: ut.h:107
static const PNotify StdErrNotify
Definition: ut.h:64
TLogNotify(const PNotify &_Notify)
Definition: ut.h:153
Definition: ut.h:28
static PNotify New()
Definition: ut.h:129
void StatNotify(const TStr &MsgStr)
Definition: ut.h:26
void(__stdcall * TNativeCallbackF)(int Type, const char *MsgStr)
Definition: ut.h:103
void(__stdcall * TCallbackF)(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.h:80
TTypeNm()
Definition: ut.h:13
static TOnExceptF GetOnExceptF()
Definition: ut.h:184
#define ClassTP(TNm, PNm)
Definition: bd.h:126
Definition: ut.h:6
virtual ~TExcept()
Definition: ut.h:171
void OnStatus(const TStr &MsgStr)
Definition: ut.cpp:106
Definition: ut.h:148
static void OnStatus(const PNotify &Notify, const TStr &MsgStr)
Definition: ut.h:54
static void Throw(const TStr &MsgStr)
Definition: ut.h:187
Definition: ut.h:28
void WarnNotify(const TStr &MsgStr)
Definition: ut.h:24
virtual void OnTxt(const TStr &)
Definition: ut.h:41
#define Assert(Cond)
Definition: bd.h:251
virtual void OnLn(const TStr &)
Definition: ut.h:40
TStdNotify()
Definition: ut.h:128
void OnStatus(const TStr &MsgStr)
Definition: ut.cpp:97
Definition: ut.h:30
static void OnNotify(const PNotify &Notify, const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.h:51
static PNotify New(const TNativeCallbackF &CallbackF)
Definition: ut.h:110
static PNotify New()
Definition: ut.h:140
static void ThrowFull(const TStr &MsgStr, const TStr &LocStr)
Definition: ut.h:198
virtual void OnNotify(const TNotifyType &, const TStr &)
Definition: ut.h:38
static PNotify New()
Definition: ut.h:72
static const PNotify StdNotify
Definition: ut.h:63
Definition: ut.h:28
Definition: ut.h:126
static bool IsOnExceptF()
Definition: ut.h:182
void OnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.h:112
static void Throw(const TStr &MsgStr, const TStr &ArgStr)
Definition: ut.h:190
static void OnTxt(const PNotify &Notify, const TStr &MsgStr)
Definition: ut.h:58
void OnStatus(const TStr &MsgStr)
Definition: ut.h:94
static const PNotify NullNotify
Definition: ut.h:62
Definition: ut.h:161
PNotify Notify
Definition: ut.h:150
TStr GetMsgStr() const
Definition: ut.h:173
static PNotify New(const TCallbackF &CallbackF)
Definition: ut.h:87
Definition: dt.h:412
enum TNotifyType_ TNotifyType
TCallbackNotify(const TCallbackF &_CallbackF)
Definition: ut.h:86
virtual void OnStatus(const TStr &)
Definition: ut.h:39
TStr GetStr() const
Definition: ut.h:175
Definition: ut.h:28
void OnStatus(const TStr &MsgStr)
Definition: ut.h:75
static TStr GetNrTypeNm(const TStr &TypeNm)
Definition: ut.h:8
void OnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.h:74
TNotifyType_
Definition: ut.h:28
TNativeCallbackNotify(const TNativeCallbackF &_CallbackF)
Definition: ut.h:109
static TOnExceptF OnExceptF
Definition: ut.h:181
char * CStr()
Definition: dt.h:476
void OnStatus(const TStr &MsgStr)
Definition: ut.h:117
static PNotify New(const PNotify &Notify)
Definition: ut.h:154
TNullNotify()
Definition: ut.h:71
TStdErrNotify()
Definition: ut.h:139
void OnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.cpp:70