00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
00070
00072 static uint8 ServerId;
00074 static const uint64 MaxEntityId;
00076 static const CEntityId Unknown;
00077
00078
00079
00080
00082
00083
00084 CEntityId ()
00085 {
00086 FullId = 0;
00087 DetailedId.Type = 127;
00088
00089
00090
00091
00092
00093
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
00118
00119
00120
00121
00122
00123
00124
00125 }
00126
00127 CEntityId (const CEntityId &a)
00128 {
00129 FullId = a.FullId;
00130
00131
00132
00133
00134
00135
00136 }
00137
00139 CEntityId (NLMISC::IStream &is)
00140 {
00141 is.serial(FullId);
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
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
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
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
00194
00195 uint64 getRawId() const
00196 {
00197 return FullId;
00198
00199
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
00259
00261
00262 bool operator == (const CEntityId &a) const
00263
00264 {
00265
00266 CEntityId testId ( FullId ^ a.FullId );
00267 testId.DetailedId.DynamicId = 0;
00268 return testId.FullId == 0;
00269
00270
00271
00272
00273 }
00274
00275 bool operator < (const CEntityId &a) const
00276
00277 {
00278 return getUniqueId() < a.getUniqueId();
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 }
00299
00300 bool operator > (const CEntityId &a) const
00301
00302 {
00303 return getUniqueId() > a.getUniqueId();
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
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
00345
00346
00347
00348
00349 return *this;
00350 }
00351
00352 const CEntityId &operator = (uint64 p)
00353 {
00354 FullId = p;
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 return *this;
00365 }
00366
00367
00368
00369
00370
00371 operator uint64 () const
00372 {
00373 return FullId;
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 }
00386
00387 void setServiceId (uint8 sid)
00388 {
00389
00390
00391
00392
00393
00394
00395
00396
00397 DetailedId.DynamicId = sid;
00398 DetailedId.CreatorId = sid;
00399 ServerId = sid;
00400 }
00401
00402
00403
00404
00405
00407 void save(NLMISC::IStream &os)
00408
00409 {
00410 os.serial(FullId);
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 }
00422
00424 void load(NLMISC::IStream &is)
00425
00426 {
00427 is.serial(FullId);
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 }
00441
00442 void serial (NLMISC::IStream &f) throw (NLMISC::EStream)
00443
00444 {
00445 if (f.isReading ())
00446 {
00447 load (f);
00448 }
00449 else
00450 {
00451 save (f);
00452 }
00453 }
00454
00455
00456
00457
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
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
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
00526 str += "0x" + std::string(b);
00527 }
00528
00530
00531
00532
00533
00534
00535
00536
00538
00539
00540
00541 };
00542
00543 inline std::stringstream &operator << (std::stringstream &__os, const CEntityId &__t)
00544 {
00545 __os << __t.toString ();
00546 return __os;
00547 }
00548
00549 }
00550
00551 #endif // NL_ENTITY_ID_H
00552
00553