# 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                 return;
00128 
00129                 if(_TableName != NULL)
00130                 {
00131                         _Map->clear();
00132                         _Empty->clear();
00133                         for(sint32 i =0; i < _Count; i++)
00134                         {
00135                                 if(_TableName[i].Name != NULL) delete _TableName[i].Name;
00136                         }
00137                         delete [] _TableName;
00138                         _TableName = NULL;
00139                 }
00140                 if(_Empty != NULL) 
00141                 {
00142                         delete _Empty;
00143                         _Empty = NULL;
00144                 }
00145                 if(_Map != NULL) 
00146                 {
00147                         delete _Map;
00148                         _Map = NULL;
00149                 }
00150         }       
00151 
00152         CIndexedVarName::CIndexedVarName(const char *name)
00153         {
00154                 insert(CStringVarName(name));
00155         }       
00156 
00157         CIndexedVarName::CIndexedVarName(const CIndexedVarName &name)
00158         {               
00159                 tMapName::iterator i = _Map->find(*_TableName[name.getIndex()].Name);           
00160                 if(i != _Map->end())
00161                 {
00162                         _Index = name.getIndex();
00163                         (*i).second->Count ++;
00164                 }
00165                 else
00166                 {
00167                         _Index = -1;
00168                         throw NLAIE::CExceptionIndexError();
00169                 }
00170                 
00171         }
00172         
00173         CIndexedVarName::CIndexedVarName(NLMISC::IStream &is)
00174         {
00175                 CStringVarName str(is);
00176                 insert(str);            
00177         }
00178         
00179         CIndexedVarName::~CIndexedVarName()
00180         {               
00181                 clear();
00182         }
00183 
00184         IVarName &CIndexedVarName::operator = (const IVarName &s)
00185         {
00186                 insert(CStringVarName(s.getString()));
00187                 return *this;
00188         }
00189 
00190         void CIndexedVarName::clear()
00191         {               
00192                 if(_TableName && !(--_TableName[_Index].Count) )
00193                 {
00194                         tMapName::iterator i = _Map->find(*_TableName[_Index].Name);
00195                         if(i != _Map->end()) 
00196                         {
00197                                 _Map->erase(i);
00198                                 delete _TableName[_Index].Name;
00199                                 _TableName[_Index].Name = NULL;
00200                                 _Empty->push_back(&_TableName[_Index]);
00201                         }
00202                 }
00203         }
00204 
00205 
00206         void CIndexedVarName::insert(const CStringVarName &str)
00207         {               
00208                 tMapName::iterator i = _Map->find(str);         
00209                 if(i != _Map->end())
00210                 {
00211                         _Index = (*i).second->Index;
00212                         (*i).second->Count ++;
00213                 }
00214                 else 
00215                 {
00216                         _Index = newIndex();
00217                         _TableName[_Index].Name = (CStringVarName *)str.clone();
00218                         _Map->insert(tMapName::value_type(*_TableName[_Index].Name, &_TableName[_Index]));
00219                 }
00220         }
00221 
00222         
00223 
00224         IVarName &CIndexedVarName::operator += (const IVarName &s)
00225         {
00226                 char *n = (char *)addString(s);
00227                 insert(n);
00228                 return *this;
00229         }
00230 
00231         IVarName &CIndexedVarName::operator -= (const IVarName &s)
00232         {                       
00233                 char *n = (char *)subString(s);
00234                 if(n != NULL) insert(n);
00235                 return *this;
00236         }
00237 
00238         
00239         sint32 CIndexedVarName::newIndex()
00240         {
00241 
00242                 if(_Empty->size())
00243                 {
00244                         CNameStruc *o = _Empty->front();
00245                         _Empty->pop_front();
00246                         o->Count++;
00247                         return o->Index;
00248                 }
00249                 else
00250                 {
00251                         CNameStruc *tmp = _TableName;
00252                         _TableName = new CNameStruc [_Count + _Bank];
00253                         
00254                         for(sint32 i = _Count; i < (_Count + _Bank); i++)
00255                         {
00256                                 _TableName[i].Count = 0;
00257                                 _TableName[i].Index = i;
00258                                 _TableName[i].Name = NULL;
00259                                 _Empty->push_back(&_TableName[i]);
00260                         }
00261                         
00262                         memcpy(_TableName, tmp, _Count*sizeof(CNameStruc));
00263                         _Count += _Bank;
00264                         return newIndex();
00265                 }
00266         }
00267 
00268         void CIndexedVarName::save(NLMISC::IStream &os)
00269         {
00270                 os.serial( *_TableName[getIndex()].Name );
00271         }
00272         
00273         void CIndexedVarName::load(NLMISC::IStream &is)
00274         {
00275                 clear();
00276                 CStringVarName str(is);
00277                 insert(str);
00278         }
00279         
00280         const NLAIC::IBasicType *CIndexedVarName::clone() const
00281         {
00282                 NLAIC::IBasicInterface *x = new CIndexedVarName(*this);
00283                 return x;
00284         }
00285 
00286         const NLAIC::IBasicType *CIndexedVarName::newInstance() const
00287         {
00288                 NLAIC::IBasicInterface *x = new CIndexedVarName("Inst");
00289                 return x;
00290         }
00291 
00292         void CIndexedVarName::getDebugString(std::string &text) const
00293         {
00294                 
00295         }
00296 
00297         const char *CIndexedVarName::getString() const
00298         {
00299                 return (*(_TableName + _Index)).Name->getString();
00300                 //return _TableName[_Index].Name->getString();
00301         }       
00302 }