SNAP Library 2.1, User Reference  2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ut.h
Go to the documentation of this file.
00001 #include "bd.h"
00002 
00004 // Type-Name
00005 template <class Type>
00006 class TTypeNm: public TStr{
00007 public:
00008   static TStr GetNrTypeNm(const TStr& TypeNm){
00009     if (TypeNm.IsPrefix("class ")){
00010       return TypeNm.GetSubStr(6, TypeNm.Len()-1);}
00011     else {return TypeNm;}}
00012 public:
00013   TTypeNm(): TStr(GetNrTypeNm((char*)(typeid(Type).name()))){}
00014 };
00015 template <class Type>
00016 TStr GetTypeNm(const Type& Var){
00017   TStr TypeNm=TStr(typeid(Var).name());
00018   return TTypeNm<Type>::GetNrTypeNm(TypeNm);
00019 }
00020 
00022 // Notifications
00023 inline void InfoNotify(const TStr& MsgStr){InfoNotify(MsgStr.CStr());}
00024 inline void WarnNotify(const TStr& MsgStr){WarnNotify(MsgStr.CStr());}
00025 inline void ErrNotify(const TStr& MsgStr){ErrNotify(MsgStr.CStr());}
00026 inline void StatNotify(const TStr& MsgStr){StatNotify(MsgStr.CStr());}
00027 
00028 typedef enum {ntInfo, ntWarn, ntErr, ntStat} TNotifyType;
00029 
00030 ClassTP(TNotify, PNotify)//{
00031 private:
00032   TNotify(const TNotify&);
00033   TNotify& operator=(const TNotify&);
00034 public:
00035   TNotify(){}
00036   virtual ~TNotify(){}
00037 
00038   virtual void OnNotify(const TNotifyType& /*Type*/, const TStr& /*MsgStr*/){}
00039   virtual void OnStatus(const TStr& /*MsgStr*/){}
00040   virtual void OnLn(const TStr& /*MsgStr*/){}
00041   virtual void OnTxt(const TStr& /*MsgStr*/){}
00042 
00043   // shortcuts for easier formationg
00044   void OnNotifyFmt(const TNotifyType& Type, const char *FmtStr, ...);
00045   void OnStatusFmt(const char *FmtStr, ...);
00046   void OnLnFmt(const char *FmtStr, ...);
00047   void OnTxtFmt(const char *FmtStr, ...);
00048 
00049   static TStr GetTypeStr(
00050    const TNotifyType& Type, const bool& Brief=true);
00051   static void OnNotify(const PNotify& Notify,
00052    const TNotifyType& Type, const TStr& MsgStr){
00053     if (!Notify.Empty()){Notify->OnNotify(Type, MsgStr);}}
00054   static void OnStatus(const PNotify& Notify, const TStr& MsgStr){
00055     if (!Notify.Empty()){Notify->OnStatus(MsgStr);}}
00056   static void OnLn(const PNotify& Notify, const TStr& MsgStr){
00057     if (!Notify.Empty()){Notify->OnLn(MsgStr);}}
00058   static void OnTxt(const PNotify& Notify, const TStr& MsgStr){
00059     if (!Notify.Empty()){Notify->OnTxt(MsgStr);}}
00060   static void DfOnNotify(const TNotifyType& Type, const TStr& MsgStr);
00061 
00062   static const PNotify NullNotify;
00063   static const PNotify StdNotify;
00064   static const PNotify StdErrNotify;
00065 };
00066 
00068 // Null-Notifier
00069 class TNullNotify: public TNotify{
00070 public:
00071   TNullNotify(){}
00072   static PNotify New(){return PNotify(new TNullNotify());}
00073 
00074   void OnNotify(const TNotifyType& Type, const TStr& MsgStr){}
00075   void OnStatus(const TStr& MsgStr){}
00076 };
00077 
00079 // Callback-Notifier
00080 typedef void (__stdcall *TCallbackF)(const TNotifyType& Type, const TStr& MsgStr);
00081 class TCallbackNotify : public TNotify
00082 {
00083 private:
00084   TCallbackF CallbackF;
00085 public:
00086   TCallbackNotify(const TCallbackF& _CallbackF) : CallbackF(_CallbackF) {}
00087   static PNotify New(const TCallbackF& CallbackF) { return PNotify(new TCallbackNotify(CallbackF)); }
00088 
00089   void OnNotify(const TNotifyType& Type, const TStr& MsgStr)
00090   {
00091     Assert(CallbackF != NULL);
00092     CallbackF(Type, MsgStr); 
00093   }
00094   void OnStatus(const TStr& MsgStr)
00095   {
00096     Assert(CallbackF != NULL);
00097     CallbackF(ntStat, MsgStr); 
00098   }
00099 };
00100 
00102 // Native-Callback-Notifier
00103 typedef void (__stdcall *TNativeCallbackF)(int Type, const char* MsgStr);
00104 class TNativeCallbackNotify : public TNotify
00105 {
00106 private:
00107   TNativeCallbackF CallbackF;
00108 public:
00109   TNativeCallbackNotify(const TNativeCallbackF& _CallbackF) : CallbackF(_CallbackF) {}
00110   static PNotify New(const TNativeCallbackF& CallbackF) { return PNotify(new TNativeCallbackNotify(CallbackF)); }
00111 
00112   void OnNotify(const TNotifyType& Type, const TStr& MsgStr)
00113   {
00114     Assert(CallbackF != NULL);
00115     CallbackF((int)Type, MsgStr.CStr()); 
00116   }
00117   void OnStatus(const TStr& MsgStr)
00118   {
00119     Assert(CallbackF != NULL);
00120     CallbackF((int)ntStat, MsgStr.CStr()); 
00121   }
00122 };
00123 
00125 // Standard-Notifier
00126 class TStdNotify: public TNotify{
00127 public:
00128   TStdNotify(){}
00129   static PNotify New(){return PNotify(new TStdNotify());}
00130 
00131   void OnNotify(const TNotifyType& Type, const TStr& MsgStr);
00132   void OnStatus(const TStr& MsgStr);
00133 };
00134 
00136 // Standard-Error-Notifier
00137 class TStdErrNotify: public TNotify{
00138 public:
00139   TStdErrNotify(){}
00140   static PNotify New(){return PNotify(new TStdErrNotify());}
00141 
00142   void OnNotify(const TNotifyType& Type, const TStr& MsgStr);
00143   void OnStatus(const TStr& MsgStr);
00144 };
00145 
00147 // Log-Notify
00148 class TLogNotify : public TNotify {
00149 private:
00150         PNotify Notify;
00151 
00152 public:
00153         TLogNotify(const PNotify& _Notify): Notify(_Notify) { }
00154         static PNotify New(const PNotify& Notify) { return new TLogNotify(Notify); }
00155 
00156         void OnStatus(const TStr& MsgStr);
00157 };
00158 
00160 // Exception
00161 ClassTP(TExcept, PExcept)//{
00162 private:
00163   TStr MsgStr;
00164   TStr LocStr;
00165   UndefDefaultCopyAssign(TExcept);
00166 public:
00167   TExcept(const TStr& _MsgStr): MsgStr(_MsgStr), LocStr(){}
00168   TExcept(const TStr& _MsgStr, const TStr& _LocStr): MsgStr(_MsgStr), LocStr(_LocStr){}
00169   static PExcept New(const TStr& MsgStr, const TStr& LocStr = TStr()) {
00170           return PExcept(new TExcept(MsgStr, LocStr)); }
00171   virtual ~TExcept(){}
00172 
00173   TStr GetMsgStr() const {return MsgStr;}
00174   TStr GetLocStr() const {return LocStr;}
00175   TStr GetStr() const {
00176     if (LocStr.Empty()){return GetMsgStr();}
00177     else {return GetLocStr()+": "+GetMsgStr();}}
00178 
00179   // replacement exception handler
00180   typedef void (*TOnExceptF)(const TStr& MsgStr);
00181   static TOnExceptF OnExceptF;
00182   static bool IsOnExceptF(){return OnExceptF!=NULL;}
00183   static void PutOnExceptF(TOnExceptF _OnExceptF){OnExceptF=_OnExceptF;}
00184   static TOnExceptF GetOnExceptF(){return OnExceptF;}
00185 
00186   // throwing exception
00187   static void Throw(const TStr& MsgStr){
00188     if (IsOnExceptF()){(*OnExceptF)(MsgStr);}
00189     else {throw TExcept::New(MsgStr);}}
00190   static void Throw(const TStr& MsgStr, const TStr& ArgStr){
00191     TStr FullMsgStr=MsgStr+" ("+ArgStr+")";
00192     if (IsOnExceptF()){(*OnExceptF)(FullMsgStr);}
00193     else {throw TExcept::New(FullMsgStr);}}
00194   static void Throw(const TStr& MsgStr, const TStr& ArgStr1, const TStr& ArgStr2){
00195     TStr FullMsgStr=MsgStr+" ("+ArgStr1+", "+ArgStr2+")";
00196     if (IsOnExceptF()){(*OnExceptF)(FullMsgStr);}
00197     else {throw TExcept::New(FullMsgStr);}}
00198   static void ThrowFull(const TStr& MsgStr, const TStr& LocStr){
00199     if (IsOnExceptF()){(*OnExceptF)(MsgStr);}
00200     else {throw TExcept::New(MsgStr, LocStr);}}
00201 };
00202 
00203 #define Try try {
00204 #define Catch } catch (PExcept Except){ErrNotify(Except->GetMsgStr());}
00205 #define CatchFull } catch (PExcept Except){ErrNotify(Except->GetStr());}
00206 #define CatchAll } catch (...){}