tb_id.cpp 1.8 KB

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