# 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.cpp

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 #include "nel/ai/agent/agent.h"
00025 
00026 namespace NLAIAGENT
00027 {
00028                 const NLAIC::CIdentType &CStringVarName::getType() const
00029         {               
00030                 return IdStringVarName;
00031         }
00032 
00033         const NLAIC::CIdentType &CIndexedVarName::getType() const
00034         {
00035                 
00036                 return IdIndexedVarName;
00037         }
00038 
00039         void CIndexedVarName::initClass()
00040         {
00041                 if(_Empty == NULL) 
00042                 {
00043                         _Empty = new std::list<CIndexedVarName::CNameStruc *>;
00044                         
00045                 }
00046 
00047                 if(_Map == NULL) 
00048                 {
00049                         _Map = new CIndexedVarName::tMapName;
00050                 }
00051                 
00052                 if(_TableName != NULL)
00053                 {
00054                         _Map->clear();
00055                         for(sint32 i =0; i < _Count; i++)
00056                         {
00057                                 if(_TableName[i].Name != NULL) delete _TableName[i].Name;
00058                         }
00059                         delete [] _TableName;
00060                 }
00061                 _TableName = new CNameStruc [_Bank];
00062                 _Count = _Bank;
00063                 
00064                 for(sint32 i = 0; i < _Count; i++)
00065                 {
00066                         _TableName[i].Name = NULL;
00067                         _TableName[i].Count = 0;
00068                         _TableName[i].Index = i;
00069                         _Empty->push_back(&_TableName[i]);
00070                 }
00071                 
00072         }
00073         
00074         void CIndexedVarName::saveClass(NLMISC::IStream &os)
00075         {
00076                 sint32 count = (sint32) _Count;
00077                 os.serial( count );
00078                 for(sint32 i = 0; i < count; i++)
00079                 {
00080                         if(_TableName[i].Name != NULL)
00081                         {
00082                                 sint32 s = 1;
00083                                 os.serial(  s  );
00084                                 _TableName[i].save(os);
00085                         }
00086                         else 
00087                         {
00088                                 sint32 s = 0;
00089                                 os.serial(  s  );
00090                         }
00091                 }
00092 
00093         }
00094 
00095         void CIndexedVarName::loadClass(NLMISC::IStream &is)
00096         {
00097                 sint32 i;
00098                 is.serial( i );
00099                 if(i != 0)
00100                 {                       
00101                         releaseClass();
00102                         _TableName = new CNameStruc [i];
00103                         _Count = i;
00104                         sint32 k;
00105                         for(i = 0;i < _Count;i ++)
00106                         {
00107                                 is.serial( k );
00108                                 if(k)
00109                                 {                                                                       
00110                                         _TableName[i].load(is);
00111                                         _Map->insert(tMapName::value_type(*_TableName[i].Name, &_TableName[i]));
00112                                 }
00113                                 else
00114                                 {
00115                                         _TableName[i].Count = 0;
00116                                         _TableName[i].Index = i;
00117                                         _TableName[i].Name = NULL;
00118                                         _Empty->push_back(&_TableName[i]);
00119                                 }
00120                         }
00121                 }
00122 
00123         }
00124 
00125         void CIndexedVarName::releaseClass()
00126         {
00127                 if(_TableName != NULL)
00128                 {
00129                         _Map->clear();
00130                         _Empty->clear();
00131                         for(sint32 i =0; i < _Count; i++)
00132                         {
00133                                 if(_TableName[i].Name != NULL) delete _TableName[i].Name;
00134                         }
00135                         delete [] _TableName;
00136                         _TableName = NULL;
00137                 }
00138                 if(_Empty != NULL) 
00139                 {
00140                         delete _Empty;
00141                         _Empty = NULL;
00142                 }
00143                 if(_Map != NULL) 
00144                 {
00145                         delete _Map;
00146                         _Map = NULL;
00147                 }
00148         }       
00149 
00150         CIndexedVarName::CIndexedVarName(const char *name)
00151         {
00152                 insert(CStringVarName(name));
00153         }       
00154 
00155         CIndexedVarName::CIndexedVarName(const CIndexedVarName &name)
00156         {               
00157                 tMapName::iterator i = _Map->find(*_TableName[name.getIndex()].Name);           
00158                 if(i != _Map->end())
00159                 {
00160                         _Index = name.getIndex();
00161                         (*i).second->Count ++;
00162                 }
00163                 else
00164                 {
00165                         _Index = -1;
00166                         throw NLAIE::CExceptionIndexError();
00167                 }
00168                 
00169         }
00170         
00171         CIndexedVarName::CIndexedVarName(NLMISC::IStream &is)
00172         {
00173                 CStringVarName str(is);
00174                 insert(str);            
00175         }
00176         
00177         CIndexedVarName::~CIndexedVarName()
00178         {               
00179                 clear();
00180         }
00181 
00182         IVarName &CIndexedVarName::operator = (const IVarName &s)
00183         {
00184                 insert(CStringVarName(s.getString()));
00185                 return *this;
00186         }
00187 
00188         void CIndexedVarName::clear()
00189         {               
00190                 if(_TableName && !(--_TableName[_Index].Count) )
00191                 {
00192                         tMapName::iterator i = _Map->find(*_TableName[_Index].Name);
00193                         if(i != _Map->end()) 
00194                         {
00195                                 _Map->erase(i);
00196                                 delete _TableName[_Index].Name;
00197                                 _TableName[_Index].Name = NULL;
00198                                 _Empty->push_back(&_TableName[_Index]);
00199                         }
00200                 }
00201         }
00202 
00203 
00204         void CIndexedVarName::insert(const CStringVarName &str)
00205         {               
00206                 tMapName::iterator i = _Map->find(str);         
00207                 if(i != _Map->end())
00208                 {
00209                         _Index = (*i).second->Index;
00210                         (*i).second->Count ++;
00211                 }
00212                 else 
00213                 {
00214                         _Index = newIndex();
00215                         _TableName[_Index].Name = (CStringVarName *)str.clone();
00216                         _Map->insert(tMapName::value_type(*_TableName[_Index].Name, &_TableName[_Index]));
00217                 }
00218         }
00219 
00220         
00221 
00222         IVarName &CIndexedVarName::operator += (const IVarName &s)
00223         {
00224                 char *n = (char *)addString(s);
00225                 insert(n);
00226                 return *this;
00227         }
00228 
00229         IVarName &CIndexedVarName::operator -= (const IVarName &s)
00230         {                       
00231                 char *n = (char *)subString(s);
00232                 if(n != NULL) insert(n);
00233                 return *this;
00234         }
00235 
00236         
00237         sint32 CIndexedVarName::newIndex()
00238         {
00239 
00240                 if(_Empty->size())
00241                 {
00242                         CNameStruc *o = _Empty->front();
00243                         _Empty->pop_front();
00244                         o->Count++;
00245                         return o->Index;
00246                 }
00247                 else
00248                 {
00249                         CNameStruc *tmp = _TableName;
00250                         _TableName = new CNameStruc [_Count + _Bank];
00251                         
00252                         for(sint32 i = _Count; i < (_Count + _Bank); i++)
00253                         {
00254                                 _TableName[i].Count = 0;
00255                                 _TableName[i].Index = i;
00256                                 _TableName[i].Name = NULL;
00257                                 _Empty->push_back(&_TableName[i]);
00258                         }
00259                         
00260                         memcpy(_TableName, tmp, _Count*sizeof(CNameStruc));
00261                         _Count += _Bank;
00262                         return newIndex();
00263                 }
00264         }
00265 
00266         void CIndexedVarName::save(NLMISC::IStream &os)
00267         {
00268                 os.serial( *_TableName[getIndex()].Name );
00269         }
00270         
00271         void CIndexedVarName::load(NLMISC::IStream &is)
00272         {
00273                 clear();
00274                 CStringVarName str(is);
00275                 insert(str);
00276         }
00277         
00278         const NLAIC::IBasicType *CIndexedVarName::clone() const
00279         {
00280                 NLAIC::IBasicInterface *x = new CIndexedVarName(*this);
00281                 return x;
00282         }
00283 
00284         const NLAIC::IBasicType *CIndexedVarName::newInstance() const
00285         {
00286                 NLAIC::IBasicInterface *x = new CIndexedVarName("Inst");
00287                 return x;
00288         }
00289 
00290         void CIndexedVarName::getDebugString(char *text) const
00291         {
00292                 
00293         }
00294 
00295         const char *CIndexedVarName::getString() const
00296         {
00297                 return _TableName[_Index].Name->getString();
00298         }       
00299 }