id_string.h - エンジンの C API リファレンス

id_string.h
  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include "platform.h"
  5. namespace stingray_plugin_foundation {
  6. #define ID32_FORMAT "#ID[%08x]"
  7. #define ID64_FORMAT "#ID[%016llx]"
  8. // An IdString64 is a 64-bit hash of a string value. It can be used
  9. // instead of strings to save memory and processing time.
  10. //
  11. // If you know that the keyspace is small and want to save memory
  12. // you can use an IdString32 instead of an IdString64. IdString32
  13. // uses the same hash function as IdString64, but just keeps the
  14. // first 32 bits of the hash so you can do a reverse lookup using
  15. // the same tables.
  16. class IdString64
  17. {
  18. public:
  19. IdString64();
  20. explicit IdString64(uint64_t id);
  21. explicit IdString64(const char *s);
  22. IdString64(const char *s, uint64_t id);
  23. IdString64(unsigned len, const char *s);
  24. // Returns the stored hash value.
  25. uint64_t id() const {return _id;}
  26. // True if this is the empty string.
  27. bool empty() const {return _id == 0;}
  28. // True if this is not the empty string.
  29. bool nonempty() const {return _id != 0;}
  30. bool operator==(const IdString64 &o) const {return _id == o._id;}
  31. bool operator!=(const IdString64 &o) const {return _id != o._id;}
  32. bool operator<(const IdString64 &o) const {return _id < o._id;}
  33. // Converts the id to a string on the format #ID[xxxxxxxxxxxxxxxx].
  34. // Returns a pointer to a static copy of the result. This will be
  35. // overwritten by next call.
  36. const char *to_string() const {return to_id_hex();}
  37. // Converts the id to a string on the format #ID[xxxxxxxxxxxxxxxx].
  38. // Returns a pointer to a static copy of the result. This will be
  39. // overwritten by next call.
  40. const char *to_id_hex() const;
  41. static const int HEX_BUFFER_SIZE = 17;
  42. // Writes a hexadecimal null terminated string to the memory pointed
  43. // to by `s`. There must be 17 bytes of allocated memory where `s`
  44. // is pointing.
  45. void to_hex(char *s) const {sprintf(s, "%016llx", (unsigned long long)_id);}
  46. private:
  47. uint64_t _id;
  48. };
  49. // A 32 bit hashed string value. If your keyspace is too big and you
  50. // get collisions with this hash, you should switch to an IdString64
  51. // instead.
  52. class IdString32
  53. {
  54. public:
  55. IdString32();
  56. explicit IdString32(unsigned id);
  57. explicit IdString32(uint64_t id64);
  58. explicit IdString32(const char *s);
  59. IdString32(const char *s, unsigned id);
  60. IdString32(unsigned len, const char *s);
  61. template <class STREAM> void serialize(STREAM &s) {
  62. s & _id;
  63. }
  64. // Returns the stored hash value.
  65. unsigned id() const {return _id;}
  66. // True if this is the empty string.
  67. bool empty() const {return _id == 0;}
  68. // True if this is not the empty string.
  69. bool nonempty() const {return _id != 0;}
  70. bool operator==(const IdString32 &o) const {return _id == o._id;}
  71. bool operator!=(const IdString32 &o) const {return _id != o._id;}
  72. bool operator<(const IdString32 &o) const {return _id < o._id;}
  73. const char *to_string() const {return to_id_hex();}
  74. const char *to_id_hex() const;
  75. static const int HEX_BUFFER_SIZE = 9;
  76. char *to_hex(char *s) const {sprintf(s, "%08x", _id); return s;}
  77. private:
  78. unsigned _id;
  79. };
  80. struct idstring_hash {
  81. unsigned operator()(const IdString64 &t) const {return (t.id() >> 32);}
  82. unsigned operator()(const IdString32 &t) const {return t.id();}
  83. };
  84. }
  85. #include "id_string.inl"