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