StringHash.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/Str.h"
  24. namespace Urho3D
  25. {
  26. class StringHashRegister;
  27. /// 32-bit hash value for a string.
  28. class URHO3D_API StringHash
  29. {
  30. public:
  31. /// Construct with zero value.
  32. StringHash() noexcept :
  33. value_(0)
  34. {
  35. }
  36. /// Copy-construct from another hash.
  37. StringHash(const StringHash& rhs) noexcept = default;
  38. /// Construct with an initial value.
  39. explicit StringHash(unsigned value) noexcept :
  40. value_(value)
  41. {
  42. }
  43. /// Construct from a C string.
  44. StringHash(const char* str) noexcept; // NOLINT(google-explicit-constructor)
  45. /// Construct from a string.
  46. StringHash(const String& str) noexcept; // NOLINT(google-explicit-constructor)
  47. /// Assign from another hash.
  48. StringHash& operator =(const StringHash& rhs) noexcept = default;
  49. /// Add a hash.
  50. StringHash operator +(const StringHash& rhs) const
  51. {
  52. StringHash ret;
  53. ret.value_ = value_ + rhs.value_;
  54. return ret;
  55. }
  56. /// Add-assign a hash.
  57. StringHash& operator +=(const StringHash& rhs)
  58. {
  59. value_ += rhs.value_;
  60. return *this;
  61. }
  62. /// Test for equality with another hash.
  63. bool operator ==(const StringHash& rhs) const { return value_ == rhs.value_; }
  64. /// Test for inequality with another hash.
  65. bool operator !=(const StringHash& rhs) const { return value_ != rhs.value_; }
  66. /// Test if less than another hash.
  67. bool operator <(const StringHash& rhs) const { return value_ < rhs.value_; }
  68. /// Test if greater than another hash.
  69. bool operator >(const StringHash& rhs) const { return value_ > rhs.value_; }
  70. /// Return true if nonzero hash value.
  71. explicit operator bool() const { return value_ != 0; }
  72. /// Return hash value.
  73. /// @property
  74. unsigned Value() const { return value_; }
  75. /// Return as string.
  76. String ToString() const;
  77. /// Return string which has specific hash value. Return first string if many (in order of calculation). Use for debug purposes only. Return empty string if URHO3D_HASH_DEBUG is off.
  78. String Reverse() const;
  79. /// Return hash value for HashSet & HashMap.
  80. unsigned ToHash() const { return value_; }
  81. /// Calculate hash value from a C string.
  82. static unsigned Calculate(const char* str, unsigned hash = 0);
  83. /// Get global StringHashRegister. Use for debug purposes only. Return nullptr if URHO3D_HASH_DEBUG is off.
  84. static StringHashRegister* GetGlobalStringHashRegister();
  85. /// Zero hash.
  86. static const StringHash ZERO;
  87. private:
  88. /// Hash value.
  89. unsigned value_;
  90. };
  91. }