NLMISC::CStaticStringMapper Class Reference

#include <string_mapper.h>


Detailed Description

After endAdd you cannot add strings anymore or it will assert
Author:
Matthieu Besson

Nevrax France

Date:
November 2003

Definition at line 112 of file string_mapper.h.

Public Member Functions

TSStringId add (const std::string &str)
 Globaly map a string into a unique Id.

void clear ()
 CStaticStringMapper ()
TSStringId emptyId ()
 Return the global id for the empty string (helper function).

const char * get (TSStringId stringId)
 Globaly unmap a string.

uint32 getCount ()
void memoryCompress ()
void memoryUncompress ()
void serial (NLMISC::IStream &f, std::vector< TSStringId > &strIdVect) throw (EStream)
void serial (NLMISC::IStream &f, TSStringId &strId) throw (EStream)
 ~CStaticStringMapper ()

Private Attributes

char * _AllStrings
uint32 _IdCounter
std::vector< char * > _IdToStr
bool _MemoryCompressed
std::map< TSStringId, std::string > _TempIdTable
std::map< std::string, TSStringId_TempStringTable


Constructor & Destructor Documentation

NLMISC::CStaticStringMapper::CStaticStringMapper  )  [inline]
 

Definition at line 125 of file string_mapper.h.

References _IdCounter, and add().

00126         {
00127                 _IdCounter = 0;
00128                 _AllStrings = NULL;
00129                 _MemoryCompressed = false;
00130                 add("");
00131         }

NLMISC::CStaticStringMapper::~CStaticStringMapper  )  [inline]
 

Definition at line 133 of file string_mapper.h.

References clear().

00134         {
00135                 clear();
00136         }


Member Function Documentation

TSStringId NLMISC::CStaticStringMapper::add const std::string &  str  ) 
 

Globaly map a string into a unique Id.

Definition at line 99 of file string_mapper.cpp.

References _IdCounter, _TempIdTable, _TempStringTable, nlassert, and NLMISC::TSStringId.

Referenced by clear(), and CStaticStringMapper().

00100 {
00101         nlassert(!_MemoryCompressed);
00102         std::map<std::string, TSStringId>::iterator it = _TempStringTable.find(str);
00103         if (it == _TempStringTable.end())
00104         {
00105                 _TempStringTable.insert(pair<string,TSStringId>::pair(str,_IdCounter));
00106                 _TempIdTable.insert(pair<TSStringId,string>::pair(_IdCounter,str));
00107                 _IdCounter++;
00108                 return _IdCounter-1;
00109         }
00110         else
00111         {
00112                 return it->second;
00113         }
00114 }

void NLMISC::CStaticStringMapper::clear  ) 
 

Definition at line 185 of file string_mapper.cpp.

References _IdCounter, _IdToStr, _TempIdTable, _TempStringTable, add(), and NLMISC::contReset().

Referenced by ~CStaticStringMapper().

00186 {
00187         contReset(_TempStringTable);
00188         contReset(_TempIdTable);
00189         delete [] _AllStrings;
00190         contReset(_IdToStr);
00191 
00192         _IdCounter = 0;
00193         _AllStrings = NULL;
00194         _MemoryCompressed = false;
00195         add("");
00196 }

TSStringId NLMISC::CStaticStringMapper::emptyId  )  [inline]
 

Return the global id for the empty string (helper function).

Definition at line 147 of file string_mapper.h.

References NLMISC::TSStringId.

00147 { return 0; }

const char * NLMISC::CStaticStringMapper::get TSStringId  stringId  ) 
 

Globaly unmap a string.

Definition at line 167 of file string_mapper.cpp.

References _IdToStr, _TempIdTable, nlassert, and NLMISC::TSStringId.

Referenced by NLMISC::CPath::getFileList(), NLMISC::CPath::memoryCompress(), and NLMISC::CPath::remapExtension().

00168 {
00169         if (_MemoryCompressed)
00170         {
00171                 nlassert(stringId < _IdToStr.size());
00172                 return _IdToStr[stringId];
00173         }
00174         else
00175         {
00176                 std::map<TSStringId, std::string>::iterator it = _TempIdTable.find(stringId);
00177                 if (it != _TempIdTable.end())
00178                         return it->second.c_str();
00179                 else
00180                         return NULL;
00181         }
00182 }

uint32 NLMISC::CStaticStringMapper::getCount  )  [inline]
 

Definition at line 151 of file string_mapper.h.

References _IdCounter, and uint32.

00151 { return _IdCounter; }

void NLMISC::CStaticStringMapper::memoryCompress  ) 
 

Definition at line 135 of file string_mapper.cpp.

References _IdToStr, _TempIdTable, _TempStringTable, NLMISC::contReset(), uint, and uint32.

00136 {
00137         _MemoryCompressed = true;
00138         std::map<TSStringId, std::string>::iterator it = _TempIdTable.begin();
00139 
00140         uint nTotalSize = 0;
00141         uint32 nNbStrings = 0;
00142         while (it != _TempIdTable.end())
00143         {
00144                 nTotalSize += it->second.size() + 1;
00145                 nNbStrings++;
00146                 it++;
00147         }
00148         
00149         _AllStrings = new char[nTotalSize];
00150         _IdToStr.resize(nNbStrings);
00151         nNbStrings = 0;
00152         nTotalSize = 0;
00153         it = _TempIdTable.begin();
00154         while (it != _TempIdTable.end())
00155         {
00156                 strcpy(_AllStrings + nTotalSize, it->second.c_str());
00157                 _IdToStr[nNbStrings] = _AllStrings + nTotalSize;
00158                 nTotalSize += it->second.size() + 1;
00159                 nNbStrings++;
00160                 it++;
00161         }
00162         contReset(_TempStringTable);
00163         contReset(_TempIdTable);
00164 }

void NLMISC::CStaticStringMapper::memoryUncompress  ) 
 

Definition at line 117 of file string_mapper.cpp.

References _IdToStr, _TempIdTable, _TempStringTable, NLMISC::contReset(), NLMISC::TSStringId, and uint.

00118 {
00119         std::map<std::string, TSStringId>       tempStringTable;
00120         std::map<TSStringId, std::string>       tempIdTable;
00121         for(uint k = 0; k < _IdToStr.size(); ++k)
00122         {
00123                 tempStringTable[_IdToStr[k]] = (TSStringId) k;
00124                 tempIdTable[(TSStringId) k] = _IdToStr[k];
00125         }
00126         delete [] _AllStrings;
00127         _AllStrings = NULL;
00128         contReset(_IdToStr);    
00129         _TempStringTable.swap(tempStringTable);
00130         _TempIdTable.swap(tempIdTable);
00131         _MemoryCompressed = false;
00132 }

void NLMISC::CStaticStringMapper::serial NLMISC::IStream f,
std::vector< TSStringId > &  strIdVect
throw (EStream)
 

Definition at line 215 of file string_mapper.cpp.

References uint.

00216 {
00217         std::vector<std::string> vsTmp;
00218         std::string sTmp;
00219         // Serialize class components.
00220         if (f.isReading())
00221         {                               
00222                 f.serialCont(vsTmp);            // FXs played only when attacking and according to the power.
00223                 strIdVect.resize(vsTmp.size());
00224                 for(uint i = 0; i < vsTmp.size(); ++i)
00225                         strIdVect[i] = add(vsTmp[i]);
00226                         }
00227         else
00228         {               
00229                 vsTmp.resize(strIdVect.size());
00230                 for (uint i = 0; i < vsTmp.size(); ++i)
00231                         vsTmp[i] = get(strIdVect[i]);
00232                 f.serialCont(vsTmp);            // FXs played only when attacking and according to the power.
00233                                 
00234         }
00235 }

void NLMISC::CStaticStringMapper::serial NLMISC::IStream f,
TSStringId strId
throw (EStream)
 

Definition at line 199 of file string_mapper.cpp.

References NLMISC::TSStringId.

00200 {
00201         std::string tmp;
00202         if (f.isReading())
00203         {               
00204                 f.serial(tmp);
00205                 strId = add(tmp);
00206         }
00207         else
00208         {
00209                 tmp = get(strId);
00210                 f.serial(tmp);
00211         }
00212 }


Field Documentation

char* NLMISC::CStaticStringMapper::_AllStrings [private]
 

Definition at line 119 of file string_mapper.h.

uint32 NLMISC::CStaticStringMapper::_IdCounter [private]
 

Definition at line 118 of file string_mapper.h.

Referenced by add(), clear(), CStaticStringMapper(), and getCount().

std::vector<char*> NLMISC::CStaticStringMapper::_IdToStr [private]
 

Definition at line 120 of file string_mapper.h.

Referenced by clear(), get(), memoryCompress(), and memoryUncompress().

bool NLMISC::CStaticStringMapper::_MemoryCompressed [private]
 

Definition at line 121 of file string_mapper.h.

std::map<TSStringId, std::string> NLMISC::CStaticStringMapper::_TempIdTable [private]
 

Definition at line 116 of file string_mapper.h.

Referenced by add(), clear(), get(), memoryCompress(), and memoryUncompress().

std::map<std::string, TSStringId> NLMISC::CStaticStringMapper::_TempStringTable [private]
 

Definition at line 115 of file string_mapper.h.

Referenced by add(), clear(), memoryCompress(), and memoryUncompress().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:34:43 2004 for NeL by doxygen 1.3.6