tb_id.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "tb_id.h"
  6. #include "tb_hashtable.h"
  7. namespace tb {
  8. TBIDRegisterStringCallback TBID::tbidRegisterCallback = nullptr;
  9. // == TBID ==============================================================================
  10. #ifdef TB_RUNTIME_DEBUG_INFO
  11. // Hash table for checking if we get any collisions (same hash value for TBID's created
  12. // from different strings)
  13. static TBHashTableAutoDeleteOf<TBID> all_id_hash;
  14. static bool is_adding = false;
  15. void TBID::Set(uint32 newid)
  16. {
  17. id = newid;
  18. debug_string.Clear();
  19. if (!is_adding && tb_core_is_initialized())
  20. {
  21. if (!all_id_hash.Get(id))
  22. {
  23. is_adding = true;
  24. all_id_hash.Add(id, new TBID(*this));
  25. is_adding = false;
  26. }
  27. }
  28. }
  29. void TBID::Set(const TBID &newid)
  30. {
  31. id = newid;
  32. TB_IF_DEBUG(debug_string.Set(newid.debug_string));
  33. if (!is_adding && tb_core_is_initialized())
  34. {
  35. if (TBID *other_id = all_id_hash.Get(id))
  36. {
  37. // If this happens, 2 different strings result in the same hash.
  38. // It might be a good idea to change one of them, but it might not matter.
  39. assert(other_id->debug_string.Equals(debug_string));
  40. }
  41. else
  42. {
  43. is_adding = true;
  44. all_id_hash.Add(id, new TBID(*this));
  45. is_adding = false;
  46. }
  47. }
  48. }
  49. void TBID::Set(const char *string)
  50. {
  51. id = TBGetHash(string);
  52. TB_IF_DEBUG(debug_string.Set(string));
  53. if (!is_adding && tb_core_is_initialized())
  54. {
  55. if (TBID *other_id = all_id_hash.Get(id))
  56. {
  57. assert(other_id->debug_string.Equals(debug_string));
  58. }
  59. else
  60. {
  61. is_adding = true;
  62. all_id_hash.Add(id, new TBID(*this));
  63. if (tbidRegisterCallback)
  64. tbidRegisterCallback(id, string);
  65. is_adding = false;
  66. }
  67. }
  68. }
  69. #endif // TB_RUNTIME_DEBUG_INFO
  70. }; // namespace tb