StringHash.cs 938 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }
  29. // Points to a StringHash
  30. public struct StringHashRef {
  31. IntPtr ptr;
  32. [Preserve]
  33. public StringHashRef (IntPtr ptr)
  34. {
  35. this.ptr = ptr;
  36. }
  37. public static implicit operator StringHash (StringHashRef r)
  38. {
  39. return new StringHash (Marshal.ReadInt32 (r.ptr));
  40. }
  41. }
  42. }