uid.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_UTIL_UID_HPP
  17. #define HEADER_SUPERTUX_UTIL_UID_HPP
  18. #include <assert.h>
  19. #include <stdint.h>
  20. #include <functional>
  21. #include <iosfwd>
  22. class UID;
  23. namespace std {
  24. template<>
  25. struct hash<UID>
  26. {
  27. size_t operator()(const UID& uid) const;
  28. };
  29. } // namespace std {
  30. class UID
  31. {
  32. friend class UIDGenerator;
  33. friend std::ostream& operator<<(std::ostream& os, const UID& uid);
  34. friend size_t std::hash<UID>::operator()(const UID&) const;
  35. public:
  36. using Magic = uint8_t;
  37. private:
  38. explicit UID(uint32_t value) :
  39. m_value(value)
  40. {
  41. assert(m_value != 0);
  42. }
  43. public:
  44. UID() : m_value(0) {}
  45. UID(const UID& other) = default;
  46. UID& operator=(const UID& other) = default;
  47. inline operator bool() const {
  48. return m_value != 0;
  49. }
  50. inline bool operator<(const UID& other) const {
  51. return m_value < other.m_value;
  52. }
  53. inline bool operator==(const UID& other) const {
  54. return m_value == other.m_value;
  55. }
  56. inline bool operator!=(const UID& other) const {
  57. return m_value != other.m_value;
  58. }
  59. inline Magic get_magic() const { return static_cast<Magic>((m_value & 0xffff0000u) >> 16); }
  60. protected:
  61. uint32_t m_value;
  62. };
  63. std::ostream& operator<<(std::ostream& os, const UID& uid);
  64. #endif
  65. /* EOF */