00001
00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023
00024 #ifndef NL_AGENT_STRING_H
00025 #define NL_AGENT_STRING_H
00026 #include <list>
00027
00028 namespace NLAIAGENT
00029 {
00037 class IVarName: public NLAIC::IBasicInterface
00038 {
00039 public:
00040
00041 IVarName()
00042 {
00043 }
00044
00046 virtual const char *getString() const = 0;
00047
00049
00050 bool operator < (const IVarName &v) const
00051 {
00052 const char *name1 = getString();
00053 const char *name2 = v.getString();
00054 if(name1[0] < name2[0]) return true;
00055 else return strcmp(name1,name2) < 0;
00056 }
00057
00058 bool operator > (const IVarName &v) const
00059 {
00060 const char *name1 = getString();
00061 const char *name2 = v.getString();
00062 if(name1[0] > name2[0]) return true;
00063 else return strcmp(name1,name2) > 0;
00064 }
00065
00066
00067 bool operator == (const IVarName &v) const
00068 {
00069 const char *name1 = getString();
00070 const char *name2 = v.getString();
00071 if(name1[0] == name2[0]) return strcmp(name1,name2) == 0;
00072
00073 return false;
00074 }
00076
00078 const char *addString(const IVarName &s) const
00079 {
00080 char *nameTmp,*str = (char *)s.getString();
00081 nameTmp = (char *)getString();
00082 char *name = new char [strlen(str) + strlen(nameTmp) + 2];
00083 sprintf(name,"%s%s",nameTmp,str);
00084 return name;
00085 }
00086
00088 const char *subString(const IVarName &s) const
00089 {
00090 char *nameTmp,*str = (char *)s.getString();
00091 nameTmp = (char *)getString();
00092
00093 sint32 k = strlen(nameTmp);
00094
00095 for(sint32 i = 0; i < k; i ++)
00096 {
00097 if(!strcmp(&nameTmp[i],str))
00098 {
00099 sint32 l,n = strlen(str);
00100 char *name = new char [(l = (k - n + 1))];
00101 memcpy(name,nameTmp,i);
00102 memcpy(&name[i],nameTmp + n,l);
00103 return name;
00104 }
00105 }
00106 return NULL;
00107 }
00108
00110
00111 virtual IVarName &operator += (const IVarName &s) = 0;
00112 virtual IVarName &operator -= (const IVarName &s) = 0;
00113 virtual IVarName &operator = (const IVarName &v) = 0;
00115
00116 virtual ~IVarName()
00117 {
00118 }
00119
00120
00121 };
00122
00130 class CStringVarName : public IVarName
00131 {
00132 public:
00133 static const NLAIC::CIdentType IdStringVarName;
00134 private:
00135 char *_Name;
00136 public:
00137 CStringVarName(const char *name)
00138 {
00139 sint32 i = strlen(name) + 1;
00140 _Name = new char [i];
00141 if(i == 1)
00142 {
00143 _Name[0] = 0;
00144 }
00145 else memcpy(_Name,name,i);
00146 }
00147
00148 CStringVarName(const CStringVarName &s)
00149 {
00150 sint32 i = strlen(s._Name) + 1;
00151 _Name = new char [i];
00152 if(i == 1)
00153 {
00154 _Name[0] = 0;
00155 }
00156 else memcpy(_Name,s._Name,i);
00157 }
00158
00159 CStringVarName(NLMISC::IStream &is):_Name(NULL)
00160 {
00161 load(is);
00162 }
00163
00164
00165 virtual ~CStringVarName()
00166 {
00167 delete []_Name;
00168 }
00169
00170 virtual const char *getString() const
00171 {
00172 return _Name;
00173 }
00174
00175 const NLAIC::CIdentType &getType() const;
00176
00177
00178 void save(NLMISC::IStream &os)
00179 {
00180 uint32 size = strlen(_Name) + 1;
00181 os.serial( size );
00182 std::string x = std::string(_Name);
00183 os.serial( x );
00184 }
00185
00186 void load(NLMISC::IStream &is)
00187 {
00188 uint32 i;
00189 is.serial(i);
00190 if(_Name != NULL) delete []_Name;
00191 _Name = new char [i];
00192 std::string name;
00193 is.serial( name );
00194 strcpy(_Name, name.c_str() );
00195 }
00196
00197 IVarName &operator += (const IVarName &s)
00198 {
00199 char *nameTmp = (char *)addString(s);
00200 delete _Name;
00201 _Name = nameTmp;
00202 return *this;
00203 }
00204
00205 IVarName &operator -= (const IVarName &s)
00206 {
00207 char *n = (char *)subString(s);
00208 if(n)
00209 {
00210 delete _Name;
00211 _Name = n;
00212 }
00213 return *this;
00214 }
00215
00216 IVarName &operator = (const IVarName &v)
00217 {
00218 delete _Name;
00219 sint32 i = strlen(v.getString()) + 1;
00220 _Name = new char [i];
00221 memcpy(_Name,v.getString(),i);
00222 return *this;
00223 }
00224
00225 CStringVarName &operator = (const CStringVarName &v)
00226 {
00227 delete _Name;
00228 sint32 i = strlen(v.getString()) + 1;
00229 _Name = new char [i];
00230 memcpy(_Name,v.getString(),i);
00231 return *this;
00232 }
00233
00234 const NLAIC::IBasicType *clone() const
00235 {
00236 NLAIC::IBasicInterface *m = new CStringVarName(_Name);
00237 return m;
00238 }
00239
00240 const NLAIC::IBasicType *newInstance() const
00241 {
00242 return clone();
00243 }
00244
00245 void getDebugString(char *text) const
00246 {
00247 sprintf(text,"'%s'",_Name);
00248 }
00249 };
00250
00259 class CIndexedVarName : public IVarName
00260 {
00261
00262 public:
00263 static const NLAIC::CIdentType IdIndexedVarName;
00264 private:
00266 struct CNameStruc
00267 {
00269 sint32 Count;
00271 sint32 Index;
00273 CStringVarName *Name;
00274
00275
00276 CNameStruc():Count(0),Index(0),Name(NULL) {}
00277 CNameStruc(CStringVarName *name,sint32 index):Count(0),Index(index),Name(name){}
00279 const sint32 &inc()
00280 {
00281 return ++Count;
00282 }
00284 const sint32 &dec()
00285 {
00286 return --Count;
00287 }
00288
00289 virtual void save(NLMISC::IStream &)
00290 {
00291
00292 00293 00294
00295 }
00296
00297 virtual void load(NLMISC::IStream &)
00298 {
00299 00300 00301 00302 00303
00304 }
00305 };
00306
00308 typedef std::map<CStringVarName ,CNameStruc *> tMapName;
00309
00310 private:
00311
00313 static tMapName *_Map;
00315 static CNameStruc *_TableName;
00320 static const sint32 _Bank;
00321
00323 static sint32 _Count;
00327 static std::list<CNameStruc *> *_Empty;
00328
00329 private:
00331 sint32 _Index;
00332
00333 public:
00334 CIndexedVarName(const char *name);
00335 CIndexedVarName(const CIndexedVarName &name);
00336 CIndexedVarName(NLMISC::IStream &is);
00337
00338
00339 sint32 newIndex();
00340
00341 const char *getString() const;
00342
00343 const NLAIC::CIdentType &getType() const;
00344
00345 const sint32 &getIndex() const
00346 {
00347 return _Index;
00348 }
00349
00350 IVarName &operator += (const IVarName &s);
00351
00352 IVarName &operator -= (const IVarName &s);
00353
00354 IVarName &operator = (const IVarName &v);
00355
00356 void save(NLMISC::IStream &os);
00357 void load(NLMISC::IStream &is);
00358 const NLAIC::IBasicType *clone() const;
00359 const NLAIC::IBasicType *newInstance() const;
00360 void getDebugString(char *text) const;
00361
00362 ~CIndexedVarName();
00363
00364 private:
00366 void insert(const CStringVarName &name);
00368 void clear();
00369
00370 public:
00371 static void initClass();
00372 static void saveClass(NLMISC::IStream &is);
00373 static void loadClass(NLMISC::IStream &is);
00374 static void releaseClass();
00375 };
00376 }
00377 #endif