StringHash.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Urho {
  4. [StructLayout(LayoutKind.Sequential)]
  5. public struct StringHash
  6. {
  7. public int Code;
  8. [Preserve]
  9. public StringHash (int code)
  10. {
  11. this.Code = code;
  12. }
  13. [Preserve]
  14. public StringHash (string str)
  15. {
  16. this.Code = urho_stringhash_from_string (str);
  17. }
  18. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  19. static extern int urho_stringhash_from_string (string str);
  20. public override string ToString ()
  21. {
  22. return $"StringHash({Code:x})";
  23. }
  24. public static explicit operator StringHash(string s)
  25. {
  26. return new StringHash(s);
  27. }
  28. public static bool operator ==(StringHash hash1, StringHash hash2)
  29. {
  30. return hash1.Code.Equals(hash2.Code);
  31. }
  32. public static bool operator !=(StringHash hash1, StringHash hash2)
  33. {
  34. return !(hash1 == hash2);
  35. }
  36. }
  37. // Points to a StringHash
  38. public struct StringHashRef {
  39. IntPtr ptr;
  40. [Preserve]
  41. public StringHashRef (IntPtr ptr)
  42. {
  43. this.ptr = ptr;
  44. }
  45. public static implicit operator StringHash (StringHashRef r)
  46. {
  47. return new StringHash (Marshal.ReadInt32 (r.ptr));
  48. }
  49. }
  50. }