# 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  

entity_id.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_ENTITY_ID_H
00027 #define NL_ENTITY_ID_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 #include "nel/misc/debug.h"
00031 #include "nel/misc/common.h"
00032 #include "nel/misc/stream.h"
00033 
00034 namespace NLMISC {
00035 
00042 struct CEntityId
00043 {
00044 
00045 private :
00046         // ---------------------------------------------------------------------------------
00047         // instantiated data
00048 
00049         union 
00050         {
00051                 struct
00052                 {
00054                 uint64  DynamicId   :  8;
00056                 uint64  CreatorId   :  8;
00058                 uint64  Type :  8;
00060                 uint64  Id : 40;
00061                 } DetailedId;
00062 
00063                 uint64 FullId;
00064         };
00065 
00066 public :
00067 
00068         // ---------------------------------------------------------------------------------
00069         // static data
00070 
00072         static uint8 ServerId;
00074         static const uint64 MaxEntityId;
00076         static const CEntityId Unknown;
00077 
00078         // ---------------------------------------------------------------------------------
00079         // constructors
00080 
00082 
00083 
00084         CEntityId ()
00085         {
00086                 FullId = 0;
00087                 DetailedId.Type = 127;
00088 
00089                 /*
00090                 DynamicId = 0;
00091                 CreatorId = 0;
00092                 Type = 127;
00093                 Id = 0;
00094                 */
00095         }
00096 
00097         CEntityId (uint8 type, uint64 id, uint8 creator, uint8 dynamic)
00098         {
00099                 DetailedId.DynamicId = dynamic;
00100                 DetailedId.CreatorId = creator;
00101                 DetailedId.Type = type;
00102                 DetailedId.Id = id;
00103         }
00104 
00105         CEntityId (uint8 type, uint64 id)
00106         {
00107                 DetailedId.Type = type;
00108                 DetailedId.Id = id;
00109                 DetailedId.CreatorId = ServerId;
00110                 DetailedId.DynamicId = ServerId;
00111         }
00112 
00113         explicit CEntityId (uint64 p)
00114         {       
00115                 FullId = p;
00116                 /*
00117                 DynamicId = (p & 0xff);
00118                 p >>= 8;
00119                 CreatorId = (p & 0xff);
00120                 p >>= 8;
00121                 Type = (p & 0xff);
00122                 p >>= 8;
00123                 Id = (p);                       
00124                 */
00125         }
00126 
00127         CEntityId (const CEntityId &a)
00128         {
00129                 FullId = a.FullId;
00130                 /*
00131                 DynamicId = a.DynamicId;
00132                 CreatorId = a.CreatorId;                        
00133                 Type = a.Type;
00134                 Id = a.Id;
00135                 */
00136         }
00137 
00139         CEntityId (NLMISC::IStream &is)
00140         {
00141                 is.serial(FullId);
00142                 /*
00143                 uint64 p;
00144                 is.serial(p);
00145 
00146                 DynamicId = (p & 0xff);
00147                 p >>= 8;
00148                 CreatorId = (p & 0xff);
00149                 p >>= 8;
00150                 Type = (p & 0xff);
00151                 p >>= 8;
00152                 Id = p;
00153                 */
00154         }
00155 
00156         explicit CEntityId (const char *str)
00157         {
00158                 char *ident = (char*)str;
00159                 char *id;
00160                 char *type;
00161                 char *creator;
00162                 char *dyn;
00163                 id = ident;
00164                 uint base = 10;
00165 
00166 //Sameh si le nombre est en hexa alors mettre la base à 16.
00167                 if(str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
00168                 {
00169                         base = 16;
00170                         str+=2;
00171                 }
00172 
00173                 ident = (char*)str;
00174                 id = ident;
00175 
00176                 while(*ident != ':') if (*ident!=0) ++ident; else {*this=Unknown; return;}              
00177                 type = ident;
00178                 while(*ident != ':') if (*ident!=0) ++ident; else {*this=Unknown; return;}              
00179                 creator = ident;
00180                 while(*ident != ':') if (*ident!=0) ++ident; else {*this=Unknown; return;}              
00181                 dyn = ident;    
00182 
00183 //Sameh conversion en fonction de la base.
00184                 DetailedId.DynamicId = atoiInt64(dyn, base);
00185                 DetailedId.CreatorId = atoiInt64(creator, base);
00186                 DetailedId.Type = atoiInt64(type, base);
00187                 DetailedId.Id = atoiInt64(id, base);
00188         }
00190         
00191 
00192         // ---------------------------------------------------------------------------------
00193         // accessors
00194 
00195         uint64 getRawId() const
00196         {
00197                 return FullId;
00198                 /*
00199                 return (uint64)*this;
00200                 */
00201         }
00202 
00203         uint64 getShortId() const
00204         {
00205                 return DetailedId.Id;
00206         }
00207 
00208         void setShortId( uint64 shortId )
00209         {
00210                 DetailedId.Id = shortId;
00211         }
00212 
00213         uint8 getDynamicId() const
00214         {
00215                 return DetailedId.DynamicId;
00216         }
00217 
00218         void setDynamicId( uint8 dynId )
00219         {
00220                 DetailedId.DynamicId = dynId;
00221         }
00222 
00223         uint8 getCreatorId() const
00224         {
00225                 return DetailedId.CreatorId;
00226         }
00227 
00228         void setCreatorId( uint8 creatorId )
00229         {
00230                 DetailedId.CreatorId = creatorId;
00231         }
00232 
00233         uint8 getType() const
00234         {
00235                 return (uint8)DetailedId.Type;
00236         }
00237 
00238         void setType( uint8 type )
00239         {
00240                 DetailedId.Type = type;
00241         }
00242 
00243         uint64 getUniqueId() const
00244         {
00245                 CEntityId id;
00246                 id.FullId = FullId;
00247                 id.DetailedId.DynamicId = 0;
00248                 return id.FullId;
00249         }
00250 
00251         bool isUnknownId() const
00252         {
00253                 return DetailedId.Type == 127;
00254         }
00255 
00256 
00257         // ---------------------------------------------------------------------------------
00258         // operators
00259 
00261 
00262         bool operator == (const CEntityId &a) const
00263 //      virtual bool operator == (const CEntityId &a) const
00264         {
00265 
00266                 CEntityId testId ( FullId ^ a.FullId );
00267                 testId.DetailedId.DynamicId = 0;
00268                 return testId.FullId == 0;
00269 
00270                 /*
00271                 return (Id == a.DetailedId.Id && DetailedId.CreatorId == a.DetailedId.CreatorId && DetailedId.Type == a.DetailedId.Type);
00272                 */
00273         }
00274 
00275         bool operator < (const CEntityId &a) const
00276 //      virtual bool operator < (const CEntityId &a) const
00277         {
00278                 return getUniqueId() < a.getUniqueId();
00279 
00280                 /*
00281                 if (Type < a.Type)
00282                 {
00283                         return true;
00284                 }
00285                 else if (Type == a.Type)
00286                 {
00287                         if (Id < a.Id)
00288                         {
00289                                 return true;
00290                         }
00291                         else if (Id == a.Id)
00292                         {
00293                                 return (CreatorId < a.CreatorId);
00294                         }
00295                 }               
00296                 return false;
00297                 */
00298         }
00299 
00300         bool operator > (const CEntityId &a) const
00301 //      virtual bool operator > (const CEntityId &a) const
00302         {
00303                 return getUniqueId() > a.getUniqueId();
00304 
00305                 /*
00306                 if (Type > a.Type)
00307                 {
00308                         return true;
00309                 }
00310                 else if (Type == a.Type)
00311                 {
00312                         if (Id > a.Id)
00313                         {
00314                                 return true;
00315                         }
00316                         else if (Id == a.Id)
00317                         {
00318                                 return (CreatorId > a.CreatorId);
00319                         }
00320                 }
00321                 // lesser
00322                 return false;
00323                 */
00324         }
00326 
00327         const CEntityId &operator ++(int)
00328         {
00329                 if(DetailedId.Id < MaxEntityId)
00330                 {
00331                         DetailedId.Id ++;
00332                 }
00333                 else
00334                 {
00335                         nlerror ("CEntityId looped (max was %"NL_I64"d", MaxEntityId);
00336                 }
00337                 return *this;
00338         }
00339 
00340         const CEntityId &operator = (const CEntityId &a)
00341         {
00342                 FullId = a.FullId;
00343                 /*
00344                 DynamicId = a.DynamicId;
00345                 CreatorId = a.CreatorId;
00346                 Type = a.Type;
00347                 Id = a.Id;
00348                 */
00349                 return *this;
00350         }
00351 
00352         const CEntityId &operator = (uint64 p)
00353         {                       
00354                 FullId = p;
00355                 /*
00356                 DynamicId = (uint64)(p & 0xff);
00357                 p >>= 8;
00358                 CreatorId = (uint64)(p & 0xff);
00359                 p >>= 8;
00360                 Type = (uint64)(p & 0xff);
00361                 p >>= 8;
00362                 Id = (uint64)(p);
00363                 */
00364                 return *this;
00365         }
00366 
00367 
00368         // ---------------------------------------------------------------------------------
00369         // other methods...
00370 
00371         operator uint64 () const
00372         {
00373                 return FullId;
00374                 /*
00375                 uint64 p = Id;
00376                 p <<= 8;
00377                 p |= (uint64)Type;
00378                 p <<= 8;
00379                 p |= (uint64)CreatorId;
00380                 p <<= 8;
00381                 p |= (uint64)DynamicId;
00382 
00383                 return p;
00384                 */
00385         }
00386 
00387         void setServiceId (uint8 sid)
00388         {
00389                 /*
00390                 
00391                   Daniel says: Who wrote this?! It's horrible!!!
00392                   you're mixing statics and non-statics indisciminately
00393                   This needs to be fixed!!!
00394                 
00395                 */
00396 
00397                 DetailedId.DynamicId = sid;
00398                 DetailedId.CreatorId = sid;
00399                 ServerId = sid;
00400         }
00401 
00402 
00403         // ---------------------------------------------------------------------------------
00404         // loading, saving, serialising...
00405 
00407         void save(NLMISC::IStream &os)
00408 //      virtual void save(NLMISC::IStream &os)
00409         {
00410                 os.serial(FullId);
00411                 /*
00412                 uint64 p = Id;
00413                 p <<= 8;
00414                 p |= (uint64)Type;
00415                 p <<= 8;
00416                 p |= (uint64)CreatorId;
00417                 p <<= 8;
00418                 p |= (uint64)DynamicId;
00419                 os.serial(p);
00420                 */
00421         }
00422 
00424         void load(NLMISC::IStream &is)
00425 //      virtual void load(NLMISC::IStream &is)
00426         {
00427                 is.serial(FullId);
00428                 /*
00429                 uint64 p;
00430                 is.serial(p);
00431 
00432                 DynamicId = (uint64)(p & 0xff);
00433                 p >>= 8;
00434                 CreatorId = (uint64)(p & 0xff);
00435                 p >>= 8;
00436                 Type = (uint64)(p & 0xff);
00437                 p >>= 8;
00438                 Id = (uint64)(p);
00439                 */
00440         }
00441 
00442         void serial (NLMISC::IStream &f) throw (NLMISC::EStream)
00443 //      virtual void serial (NLMISC::IStream &f) throw (NLMISC::EStream)
00444         {
00445                 if (f.isReading ())
00446                 {
00447                         load (f);
00448                 }
00449                 else
00450                 {                               
00451                         save (f);
00452                 }
00453         }
00454 
00455 
00456         // ---------------------------------------------------------------------------------
00457         // string convertions
00458 
00460         std::string toString() const
00461         {
00462                 std::string id;
00463                 getDebugString (id);
00464                 return "(" + id + ")";
00465         }
00466 
00468         void    fromString(const char *str)
00469 //      virtual void    fromString(const char *str)
00470         {
00471                 uint64          id;
00472                 uint            type;
00473                 uint            creatorId;
00474                 uint            dynamicId;
00475 
00476                 if (sscanf(str, "(%"NL_I64"x:%x:%x:%x)", &id, &type, &creatorId, &dynamicId) != 4)
00477                         return;
00478 
00479                 DetailedId.Id = id;
00480                 DetailedId.Type = type;
00481                 DetailedId.CreatorId = creatorId;
00482                 DetailedId.DynamicId = dynamicId;
00483         }
00484         
00486         void getDebugString(std::string &str) const
00487 //      virtual void getDebugString(std::string &str) const
00488         {                                                                                       
00489                 char b[256];
00490                 memset(b,0,255);
00491                 memset(b,'0',19);
00492                 sint n;
00493 
00494                 uint64 x = DetailedId.Id;
00495                 char baseTable[] = "0123456789abcdef";
00496                 for(n = 10; n < 19; n ++)
00497                 {
00498                         b[19 - n] = baseTable[(x & 15)];
00499                         x >>= 4;
00500                 }
00501                 b[19 - 9] = ':';
00502 
00503                 x = DetailedId.Type;
00504                 for(n = 7; n < 9; n ++)
00505                 {                               
00506                         b[19 - n] = baseTable[(x & 15)];
00507                         x >>= 4;
00508                 }
00509                 b[19 - 6] = ':';
00510 
00511                 x = DetailedId.CreatorId;
00512                 for(n = 4; n < 6; n ++)
00513                 {                               
00514                         b[19 - n] = baseTable[(x & 15)];
00515                         x >>= 4;
00516                 }
00517                 b[19 - 3] = ':';
00518 
00519                 x = DetailedId.DynamicId;
00520                 for(n = 1; n < 3; n ++)
00521                 {                                                       
00522                         b[19 - n] = baseTable[(x & 15)];
00523                         x >>= 4;
00524                 }
00525 //Sameh To be sure that the number is in hexa.
00526                 str += "0x" + std::string(b);
00527         }
00528 /*
00530 
00531         std::string     getClassName ()
00532 //      virtual std::string     getClassName ()
00533         {
00534                 return std::string ("<CEntityId>");
00535         }
00536 
00538 */
00539 
00540 //      friend std::stringstream &operator << (std::stringstream &__os, const CEntityId &__t);
00541 };      
00542 
00543 inline std::stringstream &operator << (std::stringstream &__os, const CEntityId &__t)
00544 {
00545         __os << __t.toString ();
00546         return __os;
00547 }
00548 
00549 } // NLMISC
00550 
00551 #endif // NL_ENTITY_ID_H
00552 
00553 /* End of entity_id.h */