# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

agent_string.h

Go to the documentation of this file.
00001 
00006 /* Copyright, 2000 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */
00024 #ifndef NL_AGENT_STRING_H
00025 #define NL_AGENT_STRING_H
00026 #include <list>
00027 
00028 #include "nel/ai/c/abstract_interface.h"
00029 
00030 namespace NLAIAGENT
00031 {       
00039         class IVarName: public NLAIC::IBasicInterface
00040         {
00041         public:
00042 
00043                 IVarName()
00044                 {
00045                 }
00046 
00048                 virtual const char *getString() const = 0;              
00049 
00051 
00052                 bool operator < (const IVarName &v) const
00053                 {                       
00054                         const char *name1 = getString();
00055                         const char *name2 = v.getString();
00056                         if(name1[0] < name2[0]) return true;
00057                         else return strcmp(name1,name2) < 0;                    
00058                 }
00059 
00060                 bool operator > (const IVarName &v) const
00061                 {                       
00062                         const char *name1 = getString();
00063                         const char *name2 = v.getString();
00064                         if(name1[0] > name2[0]) return true;
00065                         else return strcmp(name1,name2) > 0;
00066                 }
00067 
00068 
00069                 bool operator == (const IVarName &v) const
00070                 {
00071                         const char *name1 = getString();
00072                         const char *name2 = v.getString();
00073                         if(name1[0] == name2[0]) return strcmp(name1,name2) == 0;
00074                         
00075                         return false;
00076                 }
00078 
00080                 const char *addString(const IVarName &s) const 
00081                 {
00082                         char *nameTmp,*str = (char *)s.getString();             
00083                         nameTmp = (char *)getString();  
00084                         char *name = new char [strlen(str) + strlen(nameTmp) + 2];
00085                         sprintf(name,"%s%s",nameTmp,str);                       
00086                         return name;
00087                 }
00088 
00090                 const char *subString(const IVarName &s) const 
00091                 {
00092                         char *nameTmp,*str = (char *)s.getString();             
00093                         nameTmp = (char *)getString();  
00094 
00095                         sint32 k = strlen(nameTmp);
00096 
00097                         for(sint32 i = 0; i < k; i ++)
00098                         {
00099                                 if(!strcmp(&nameTmp[i],str))
00100                                 {
00101                                         sint32 l,n = strlen(str);
00102                                         char *name = new char [(l = (k - n + 1))];
00103                                         memcpy(name,nameTmp,i);
00104                                         memcpy(&name[i],nameTmp + n,l);
00105                                         return name;
00106                                 }
00107                         }
00108                         return NULL;
00109                 }
00110 
00112 
00113                 virtual IVarName &operator += (const IVarName &s) = 0;
00114                 virtual IVarName &operator -= (const IVarName &s) = 0;
00115                 virtual IVarName &operator = (const IVarName &v) = 0;           
00117 
00118                 virtual ~IVarName()
00119                 {
00120                 }       
00121                 //virtual typeVarName getEnumType() const = 0;
00122                 
00123         };              
00124 
00132         class CStringVarName : public IVarName
00133         {
00134         public:
00135                 static const NLAIC::CIdentType IdStringVarName;
00136         private:
00137                 char *_Name;
00138         public:
00139                 CStringVarName(const char *name)
00140                 {
00141                         sint32 i = strlen(name) + 1;
00142                         _Name =  new char [i];
00143                         if(i == 1)
00144                         {
00145                                 _Name[0] = 0;
00146                         }                       
00147                         else memcpy(_Name,name,i);
00148                 }
00149 
00150                 CStringVarName(const CStringVarName &s)
00151                 {
00152                         sint32 i = strlen(s._Name) + 1;
00153                         _Name =  new char [i];
00154                         if(i == 1)
00155                         {
00156                                 _Name[0] = 0;
00157                         }                       
00158                         else memcpy(_Name,s._Name,i);
00159                 }
00160 
00161                 CStringVarName(NLMISC::IStream &is):_Name(NULL)
00162                 {
00163                         load(is);
00164                 }
00165                 
00166 
00167                 virtual ~CStringVarName()
00168                 {
00169                         delete []_Name;
00170                 }       
00171 
00172                 virtual  const char *getString() const
00173                 {
00174                         return _Name;
00175                 }               
00176 
00177                 const NLAIC::CIdentType &getType() const;
00178                 
00179 
00180                 void save(NLMISC::IStream &os)
00181                 {                       
00182                         uint32 size = strlen(_Name) + 1;
00183                         os.serial( size );
00184                         std::string x = std::string(_Name);
00185                         os.serial( x );
00186                 }
00187 
00188                 void load(NLMISC::IStream &is)
00189                 {
00190                         uint32 i;               
00191                         is.serial(i);
00192                         if(_Name != NULL) delete []_Name;
00193                         _Name =  new char [i];
00194                         std::string name;
00195                         is.serial( name );      
00196                         strcpy(_Name, name.c_str() );
00197                 }               
00198 
00199                 IVarName &operator += (const IVarName &s)
00200                 {
00201                         char *nameTmp = (char *)addString(s);
00202                         delete _Name;
00203                         _Name = nameTmp;                        
00204                         return *this;
00205                 }               
00206                 
00207                 IVarName &operator -= (const IVarName &s)
00208                 {                       
00209                         char *n = (char *)subString(s);
00210                         if(n)
00211                         {
00212                                 delete _Name;
00213                                 _Name = n;
00214                         }                               
00215                         return *this;
00216                 }
00217                 
00218                 IVarName &operator = (const IVarName &v)
00219                 {
00220                         delete _Name;
00221                         sint32 i = strlen(v.getString()) + 1;
00222                         _Name =  new char [i];
00223                         memcpy(_Name,v.getString(),i);
00224                         return *this;
00225                 }
00226 
00227                 CStringVarName &operator = (const CStringVarName &v)
00228                 {
00229                         delete _Name;
00230                         sint32 i = strlen(v.getString()) + 1;
00231                         _Name =  new char [i];
00232                         memcpy(_Name,v.getString(),i);
00233                         return *this;
00234                 }
00235 
00236                 const NLAIC::IBasicType *clone() const
00237                 {
00238                         NLAIC::IBasicInterface *m = new CStringVarName(_Name);
00239                         return m;
00240                 }
00241 
00242                 const NLAIC::IBasicType *newInstance() const
00243                 {                       
00244                         return clone();
00245                 }
00246 
00247                 void getDebugString(std::string &text) const
00248                 {
00249                         text = NLAIC::stringGetBuild("'%s'",_Name);                     
00250                 }
00251         };      
00252 
00261         class CIndexedVarName : public IVarName
00262         {
00263 
00264         public:
00265                 static const NLAIC::CIdentType IdIndexedVarName;
00266         private:
00268                 struct CNameStruc
00269                 {
00271                         sint32 Count;
00273                         sint32 Index;
00275                         CStringVarName *Name;
00276                         
00277 
00278                         CNameStruc():Count(0),Index(0),Name(NULL) {}
00279                         CNameStruc(CStringVarName *name,sint32 index):Count(0),Index(index),Name(name){}
00281                         const sint32  &inc()
00282                         {
00283                                 return ++Count;
00284                         }
00286                         const sint32  &dec()
00287                         {
00288                                 return --Count;
00289                         }
00290 
00291                         virtual void save(NLMISC::IStream &)
00292                         {
00293                                 // TODO
00294 /*                              os.serial( Count );
00295                                 os.serial( Index );
00296                                 os.serial( *Name );*/
00297                         }
00298 
00299                         virtual void load(NLMISC::IStream &)
00300                         {
00301 /*                              is.serial(Count);
00302                                 is.serial(Index);                               
00303                                 if(Name == NULL) 
00304                                         Name = new CStringVarName(is);
00305                                 is.serial(Name);*/
00306                         }
00307                 };              
00308                 
00310                 typedef std::map<CStringVarName ,CNameStruc *> tMapName;
00311 
00312         private:
00313 
00315                 static tMapName *_Map;
00317                 static CNameStruc *_TableName;
00322                 static const sint32 _Bank;
00323 
00325                 static sint32 _Count;
00329                 static std::list<CNameStruc *> *_Empty;
00330 
00331         private:
00333                 sint32 _Index;
00334 
00335         public:
00336                 CIndexedVarName(const char *name);
00337                 CIndexedVarName(const CIndexedVarName &name);
00338                 CIndexedVarName(NLMISC::IStream &is);
00339 
00340 
00341                 sint32 newIndex();
00342                 
00343                 const char *getString() const;
00344 
00345                 const NLAIC::CIdentType &getType() const;       
00346 
00347                 const sint32 &getIndex() const
00348                 {
00349                         return _Index;
00350                 }
00351 
00352                 IVarName &operator += (const IVarName &s);              
00353 
00354                 IVarName &operator -= (const IVarName &s);
00355 
00356                 IVarName &operator = (const IVarName &v);               
00357 
00358                 void save(NLMISC::IStream &os);
00359                 void load(NLMISC::IStream &is);
00360                 const NLAIC::IBasicType *clone() const;
00361                 const NLAIC::IBasicType *newInstance() const;
00362                 void getDebugString(std::string &text) const;
00363 
00364                 virtual ~CIndexedVarName();
00365 
00366         private:
00368                 void insert(const CStringVarName &name);
00370                 void clear();
00371 
00372         public:
00373                 static void initClass();
00374                 static void saveClass(NLMISC::IStream &is);
00375                 static void loadClass(NLMISC::IStream &is);
00376                 static void releaseClass();
00377         };
00378 }
00379 #endif