tb_id.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_ID_H
  6. #define TB_ID_H
  7. #include "tb_types.h"
  8. #include "tb_hash.h"
  9. #include "tb_str.h"
  10. namespace tb {
  11. typedef void (*TBIDRegisterStringCallback) (uint32 id, const char* string);
  12. /** TBID is a wrapper for a uint32 to be used as ID.
  13. The uint32 can be set directly to any uint32, or it can be
  14. set from a string which will be hashed into the uint32. */
  15. class TBID
  16. {
  17. public:
  18. TBID(uint32 id = 0) { Set(id); }
  19. TBID(const char *string) { Set(string); }
  20. TBID(const TBID &id) { Set(id); }
  21. #ifdef TB_RUNTIME_DEBUG_INFO
  22. void Set(uint32 newid);
  23. void Set(const TBID &newid);
  24. void Set(const char *string);
  25. #else
  26. void Set(uint32 newid) { id = newid; }
  27. void Set(const TBID &newid) { id = newid; }
  28. void Set(const char *string) { id = TBGetHash(string); if (tbidRegisterCallback) tbidRegisterCallback(id, string); }
  29. #endif
  30. operator uint32 () const { return id; }
  31. const TBID& operator = (const TBID &id) { Set(id); return *this; }
  32. private:
  33. uint32 id;
  34. public:
  35. /** This string is here to aid debugging (Only in debug builds!)
  36. It should not to be used in your code! */
  37. #ifdef TB_RUNTIME_DEBUG_INFO
  38. friend class TBLanguage;
  39. TBStr debug_string;
  40. #endif
  41. static TBIDRegisterStringCallback tbidRegisterCallback;
  42. };
  43. }; // namespace tb
  44. #endif // TB_ID_H