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
pp.h
Go to the documentation of this file.
00001 
00002 // Property-Tag
00003 typedef enum {
00004   ptUndef, ptBool, ptInt, ptFlt, ptStr, ptValV, ptSel, ptSet} TPpTagVal;
00005 
00006 class TPpTag{
00007 public:
00008   static TStr GetStr(const int& Tag);
00009   static TPpTagVal GetTag(const TStr& Str);
00010   static TPpTagVal LoadTxt(TILx& Lx){
00011     return GetTag(Lx.GetIdStr());}
00012   static void SaveTxt(TOLx& Lx, const TInt& Tag){SaveTxt(Lx, int(Tag));}
00013   static void SaveTxt(TOLx& Lx, const int& Tag){
00014     Lx.PutIdStr(GetStr(Tag));}
00015 };
00016 
00018 // Property-Value
00019 class TPpVal{
00020 private:
00021   TInt Tag;
00022   TBool Bool; TInt Int; TFlt Flt; TStr Str; TVec<TPpVal> ValV;
00023 public:
00024   TPpVal(): Tag(ptUndef){}
00025   TPpVal(const TPpVal& PpVal): Tag(PpVal.Tag), Bool(PpVal.Bool),
00026     Int(PpVal.Int), Flt(PpVal.Flt), Str(PpVal.Str), ValV(PpVal.ValV){}
00027   TPpVal(const bool& b): Tag(ptBool), Bool(b){}
00028   TPpVal(const TBool& b): Tag(ptBool), Bool(b){}
00029   TPpVal(const int& _Int): Tag(ptInt), Int(_Int){}
00030   TPpVal(const TInt& _Int): Tag(ptInt), Int(_Int){}
00031   TPpVal(const double& _Flt): Tag(ptFlt), Flt(_Flt){}
00032   TPpVal(const TFlt& _Flt): Tag(ptFlt), Flt(_Flt){}
00033   TPpVal(const TStr& _Str): Tag(ptStr), Str(_Str){}
00034   TPpVal(const char* CStr): Tag(ptStr), Str(TStr(CStr)){}
00035   TPpVal(const TVec<TPpVal>& _ValV): Tag(ptValV), ValV(_ValV){}
00036   TPpVal(TSIn& SIn):
00037     Tag(SIn), Bool(SIn), Int(SIn), Flt(SIn), Str(SIn), ValV(SIn){}
00038   void Save(TSOut& SOut){
00039     Tag.Save(SOut); Bool.Save(SOut); Int.Save(SOut);
00040     Flt.Save(SOut); Str.Save(SOut); ValV.Save(SOut);}
00041 
00042   TPpVal& operator=(const TPpVal& PpVal){
00043     Tag=PpVal.Tag; Bool=PpVal.Bool; Int=PpVal.Int;
00044     Flt=PpVal.Flt; Str=PpVal.Str; ValV=PpVal.ValV; return *this;}
00045   bool operator==(const TPpVal& PpVal) const;
00046   bool operator<(const TPpVal& PpVal) const {
00047     if ((Tag==PpVal.Tag)&&(int(Tag)==ptInt)){return Int<PpVal.Int;}
00048     else if ((Tag==PpVal.Tag)&&(int(Tag)==ptFlt)){return Flt<PpVal.Flt;}
00049     else {Fail; return false;}}
00050 
00051   int GetTag() const {return (int)Tag;}
00052   TBool GetBool() const {IAssert(int(Tag)==ptBool); return Bool;}
00053   TInt GetInt() const {IAssert(int(Tag)==ptInt); return Int;}
00054   TFlt GetFlt() const {IAssert(int(Tag)==ptFlt); return Flt;}
00055   TStr GetStr() const {IAssert(int(Tag)==ptStr); return Str;}
00056   TVec<TPpVal> GetValV() const {IAssert(int(Tag)==ptValV); return ValV;}
00057 
00058   TStr GetValStr(const bool& DoAddTag=false) const;
00059   static TPpVal LoadTxt(TILx& Lx);
00060   void SaveTxt(TOLx& Lx) const;
00061 };
00062 typedef TVec<TPpVal> TPpValV;
00063 
00065 // Property
00066 class TPp;
00067 typedef TPt<TPp> PPp;
00068 typedef TVec<PPp> TPpV;
00069 
00070 class TPp{
00071 private:
00072   TCRef CRef;
00073 private:
00074   static TStr PpNm;
00075 private:
00076   TStr IdNm, UcIdNm, DescNm;
00077   TInt Tag, ValVTag;
00078   TPpVal Val, DfVal;
00079   TPpVal MnVal, MxVal;
00080   TPpValV CcValV;
00081   TPpV PpV;
00082   void IAssertSubPp() const {IAssert((int(Tag)==ptSel)||(int(Tag)==ptSet));}
00083   int GetPpN(const TStr& IdNm) const;
00084   void GetChA(const int& Lev, const bool& Brief, TChA& ChA) const;
00085 public:
00086   TPp():
00087     IdNm(), UcIdNm(), DescNm(),
00088     Tag(ptUndef), ValVTag(),
00089     Val(), DfVal(), MnVal(), MxVal(),
00090     CcValV(), PpV(){}
00091   TPp(const TPp& Pp):
00092     IdNm(Pp.IdNm), UcIdNm(Pp.IdNm.GetUc()), DescNm(Pp.DescNm),
00093     Tag(Pp.Tag), ValVTag(Pp.ValVTag),
00094     Val(Pp.Val), DfVal(Pp.DfVal), MnVal(Pp.MnVal), MxVal(Pp.MxVal),
00095     CcValV(Pp.CcValV), PpV(Pp.PpV){}
00096   TPp(const TStr& _IdNm, const TStr& _DescNm,
00097    const TPpTagVal& _Tag=ptUndef, const TPpTagVal& _ValVTag=ptUndef):
00098     IdNm(_IdNm), UcIdNm(_IdNm.GetUc()), DescNm(_DescNm),
00099     Tag(_Tag), ValVTag(_ValVTag),
00100     Val(), DfVal(), MnVal(), MxVal(),
00101     CcValV(), PpV(){IAssert(int(ValVTag)!=ptValV);}
00102   TPp(TSIn& SIn):
00103     IdNm(SIn), UcIdNm(SIn), DescNm(SIn),
00104     Tag(SIn), ValVTag(SIn),
00105     Val(SIn), DfVal(SIn), MnVal(SIn), MxVal(SIn),
00106     CcValV(SIn), PpV(SIn){}
00107   static PPp Load(TSIn& SIn){return new TPp(SIn);}
00108   void Save(TSOut& SOut){
00109     IdNm.Save(SOut); UcIdNm.Save(SOut); DescNm.Save(SOut);
00110     Tag.Save(SOut); ValVTag.Save(SOut);
00111     Val.Save(SOut); DfVal.Save(SOut); MnVal.Save(SOut); MxVal.Save(SOut);
00112     CcValV.Save(SOut); PpV.Save(SOut);}
00113 
00114   TPp& operator=(const TPp& Pp){
00115     if (this!=&Pp){
00116       IdNm=Pp.IdNm; UcIdNm=Pp.UcIdNm; DescNm=Pp.DescNm;
00117       Tag=Pp.Tag; ValVTag=Pp.ValVTag;
00118       Val=Pp.Val; DfVal=Pp.DfVal; MnVal=Pp.MnVal; MxVal=Pp.MxVal;
00119       CcValV=Pp.CcValV; PpV=Pp.PpV;}
00120     return *this;}
00121 
00122   // basic operations
00123   TStr GetIdNm() const {return IdNm;}
00124   TStr GetUcIdNm() const {return UcIdNm;}
00125   TStr GetDescNm() const {return DescNm;}
00126   int GetTag() const {return Tag;}
00127   int GetValVTag() const {return ValVTag;}
00128   void PutVal(const TPpVal& _Val);
00129   TPpVal GetVal() const {if (Val==TPpVal()){return DfVal;} else {return Val;}}
00130 
00131   // property definition
00132   void PutDfVal(const TPpVal& _DfVal){PutVal(_DfVal); DfVal=Val;}
00133   TPpVal GetDfVal() const {return DfVal;}
00134   void PutMnMxVal(const TPpVal& _MnVal, const TPpVal& _MxVal){
00135     IAssert((int(Tag)==ptInt)||(int(Tag)==ptFlt));
00136     IAssert((Tag==_MnVal.GetTag())&&(Tag==_MxVal.GetTag()));
00137     MnVal=_MnVal; MxVal=_MxVal;}
00138   TPpVal GetMnVal() const {IAssert((int(Tag)==ptInt)||(int(Tag)==ptFlt)); return MnVal;}
00139   TPpVal GetMxVal() const {IAssert((int(Tag)==ptInt)||(int(Tag)==ptFlt)); return MxVal;}
00140   void AddCcVal(const TPpVal& _Val){
00141     IAssert((int(Tag)==ptStr)||(int(Tag)==_Val.GetTag()));
00142     IAssert(CcValV.SearchForw(_Val)==-1); CcValV.Add(_Val);}
00143   void PutCcValV(const TPpValV& CcValV){
00144     for (int CcValN=0; CcValN<CcValV.Len(); CcValN++){AddCcVal(CcValV[CcValN]);}}
00145   int GetCcVals() const {IAssert(int(Tag)==ptStr); return CcValV.Len();}
00146   TPpVal GetCcVal(const int& CcValN) const {
00147     IAssert(int(Tag)==ptStr); return CcValV[CcValN];}
00148 
00149   // property put/get
00150   bool IsPp(const TStr& IdNm) const {IAssertSubPp(); return GetPpN(IdNm)!=-1;}
00151   bool IsPp(const TStr& IdNm, PPp& Pp) const {
00152     IAssertSubPp(); int PpN=GetPpN(IdNm);
00153     if (PpN==-1){return false;} else {Pp=GetPp(PpN); return true;}}
00154   int GetPps() const {IAssertSubPp(); return PpV.Len();}
00155   int AddPp(const PPp& Pp){IAssertSubPp(); return PpV.Add(Pp);}
00156   void AddPpV(const TPpV& PpV){
00157     for (int PpN=0; PpN<PpV.Len(); PpN++){AddPp(PpV[PpN]);}}
00158   PPp GetPp(const int& PpN) const {IAssertSubPp(); return PpV[PpN];}
00159   PPp GetPp(const TStr& IdPath) const;
00160   PPp GetSelPp() const {
00161     IAssert(int(Tag)==ptSel); return GetPp(GetVal().GetStr());}
00162 
00163   // add&create property shortcuts
00164   int AddPpInt(const TStr& IdNm, const TStr& DescNm=TStr()){
00165     PPp Pp=PPp(new TPp(IdNm, DescNm, ptInt)); return AddPp(Pp);}
00166   int AddPpInt(const TStr& IdNm, const TStr& DescNm,
00167    const int& MnVal, const int& MxVal, const int& DfVal){
00168     PPp Pp=PPp(new TPp(IdNm, DescNm, ptInt));
00169     Pp->PutMnMxVal(MnVal, MxVal); Pp->PutDfVal(DfVal); return AddPp(Pp);}
00170   int AddPpFlt(const TStr& IdNm, const TStr& DescNm=TStr()){
00171     PPp Pp=PPp(new TPp(IdNm, DescNm, ptFlt)); return AddPp(Pp);}
00172   int AddPpFlt(const TStr& IdNm, const TStr& DescNm,
00173    const double& MnVal, const double& MxVal, const double& DfVal){
00174     PPp Pp=PPp(new TPp(IdNm, DescNm, ptFlt));
00175     Pp->PutMnMxVal(MnVal, MxVal); Pp->PutDfVal(DfVal); return AddPp(Pp);}
00176   int AddPpStr(const TStr& IdNm, const TStr& DescNm=TStr(), const TStr& DfVal=TStr()){
00177     PPp Pp=PPp(new TPp(IdNm, DescNm, ptStr));
00178     Pp->PutDfVal(DfVal); return AddPp(Pp);}
00179   static PPp GetSetPp(const TStr& IdNm, const TStr& DescNm=TStr()){
00180     return PPp(new TPp(IdNm, DescNm, ptSet));}
00181 
00182   // boolean property put/get shortcuts
00183   void PutValBool(const bool& Val){PutVal(TPpVal(Val));}
00184   TBool GetValBool() const {return GetVal().GetBool();}
00185   void PutValBool(const TStr& IdPath, const bool& Val){
00186     GetPp(IdPath)->PutVal(TPpVal(Val));}
00187   TBool GetValBool(const TStr& IdPath) const {
00188     return GetPp(IdPath)->GetVal().GetBool();}
00189 
00190   // integer property put/get shortcuts
00191   void PutValInt(const int& Val){PutVal(TPpVal(Val));}
00192   TInt GetValInt() const {return GetVal().GetInt();}
00193   void PutValInt(const TStr& IdPath, const int& Val){
00194     GetPp(IdPath)->PutVal(TPpVal(Val));}
00195   TInt GetValInt(const TStr& IdPath) const {
00196     return GetPp(IdPath)->GetVal().GetInt();}
00197 
00198   // float property put/get shortcuts
00199   void PutValFlt(const double& Val){PutVal(TPpVal(Val));}
00200   TFlt GetValFlt() const {return GetVal().GetFlt();}
00201   void PutValFlt(const TStr& IdPath, const double& Val){
00202     GetPp(IdPath)->PutVal(TPpVal(Val));}
00203   TFlt GetValFlt(const TStr& IdPath) const {
00204     return GetPp(IdPath)->GetVal().GetFlt();}
00205 
00206   // string property put/get shortcuts
00207   void PutValStr(const TStr& Val){PutVal(TPpVal(Val));}
00208   TStr GetValStr() const {return GetVal().GetStr();}
00209   void PutValStr(const TStr& IdPath, const TStr& Val){
00210     GetPp(IdPath)->PutVal(TPpVal(Val));}
00211   TStr GetValStr(const TStr& IdPath) const {
00212     return GetPp(IdPath)->GetVal().GetStr();}
00213 
00214   // value property vector put/get shortcuts
00215   void PutValValV(const TPpValV& Val){PutVal(TPpVal(Val));}
00216   TPpValV GetValValV() const {return GetVal().GetValV();}
00217   void PutValValV(const TStr& IdPath, const TPpValV& Val){
00218     GetPp(IdPath)->PutVal(TPpVal(Val));}
00219   TPpValV GetValValV(const TStr& IdPath) const {
00220     return GetPp(IdPath)->GetVal().GetValV();}
00221 
00222   // string
00223   TStr GetStr(const bool& Brief=true) const {
00224     TChA ChA; GetChA(0, Brief, ChA); return ChA;}
00225 
00226   // file
00227   static PPp LoadTxt(const PSIn& SIn){
00228     TILx Lx(SIn, TFSet()|iloCmtAlw|iloSigNum|oloCsSens); return LoadTxt(Lx);}
00229   void SaveTxt(const PSOut& SOut) const {
00230     TOLx Lx(SOut, TFSet()|oloCmtAlw|oloFrcEoln|oloSigNum|oloCsSens); SaveTxt(Lx);}
00231   static PPp LoadTxt(TILx& Lx);
00232   void SaveTxt(TOLx& Lx, const int& Lev=0) const;
00233 
00234   friend class TPt<TPp>;
00235 };
00236 
00237 inline PPp MkClone(const PPp& Pp){return new TPp(*Pp);}
00238