/src/wrappers/glib/library/fundamentals/glib_type_conversion_macros.e

http://github.com/tybor/Liberty · Specman e · 127 lines · 18 code · 31 blank · 78 comment · 0 complexity · ab2641e27c3cfb1732e81d6bb1354807 MD5 · raw file

  1. indexing
  2. description: "Type Conversion Macros: portably storing integers in pointer variables."
  3. copyright: "(C) 2005 Paolo Redaelli "
  4. license: "LGPL v2 or later"
  5. date: "$Date:$"
  6. revision: "$REvision:$"
  7. deferred class GLIB_TYPE_CONVERSION_MACROS
  8. -- Many times GLib, GTK+, and other libraries allow you to pass
  9. -- "user data" to a callback, in the form of a void pointer. From
  10. -- time to time you want to pass an integer instead of a
  11. -- pointer. You could allocate an integer, with something like:
  12. -- int *ip = g_new (int, 1);
  13. -- *ip = 42;
  14. -- But this is inconvenient, and it's annoying to have to free the
  15. -- memory at some later time.
  16. -- Pointers are always at least 32 bits in size (on all platforms
  17. -- GLib intends to support). Thus you can store at least 32-bit
  18. -- integer values in a pointer value. Naively, you might try this,
  19. -- but it's incorrect:
  20. -- gpointer p;
  21. -- int i;
  22. -- p = (void*) 42;
  23. -- i = (int) p;
  24. -- Again, that example was not correct, don't copy it. The problem
  25. -- is that on some systems you need to do this:
  26. -- gpointer p;
  27. -- int i;
  28. -- p = (void*) (long) 42;
  29. -- i = (int) (long) p;
  30. -- So GPOINTER_TO_INT(), GINT_TO_POINTER(), etc. do the right thing
  31. -- on the current platform.
  32. -- Warning: YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT
  33. -- PORTABLE IN ANY WAY SHAPE OR FORM. These macros ONLY allow
  34. -- storing integers in pointers, and only preserve 32 bits of the
  35. -- integer; values outside the range of a 32-bit integer will be
  36. -- mangled.
  37. inherit ANY undefine is_equal, copy end
  38. feature {} -- External calls
  39. gint_to_pointer (an_int: INTEGER): POINTER is
  40. -- #define GINT_TO_POINTER(i) ((gpointer) (i))
  41. -- Stuffs an integer into a pointer type.
  42. -- Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS
  43. -- NOT PORTABLE IN ANY WAY SHAPE OR FORM. These macros ONLY
  44. -- allow storing integers in pointers, and only preserve 32
  45. -- bits of the integer; values outside the range of a 32-bit
  46. -- integer will be mangled. i : integer to stuff into a
  47. -- pointer.
  48. external "C macro use <glib.h>"
  49. alias "GINT_TO_POINTER"
  50. end
  51. gpointer_to_int (a_p: POINTER): INTEGER is
  52. -- #define GPOINTER_TO_INT(p) ((gint) (p))
  53. -- Extracts an integer from a pointer. The integer must have
  54. -- been stored in the pointer with GINT_TO_POINTER().
  55. -- Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS
  56. -- NOT PORTABLE IN ANY WAY SHAPE OR FORM. These macros ONLY
  57. -- allow storing integers in pointers, and only preserve 32
  58. -- bits of the integer; values outside the range of a 32-bit
  59. -- integer will be mangled. p : pointer containing an
  60. -- integer.
  61. external "C macro use <glib.h>"
  62. alias "GPOINTER_TO_INT"
  63. end
  64. -- GUINT_TO_POINTER()
  65. -- #define GUINT_TO_POINTER(u) ((gpointer) (u))
  66. -- Stuffs an unsigned integer into a pointer type.
  67. -- u : unsigned integer to stuff into the pointer.
  68. -- GPOINTER_TO_UINT()
  69. -- #define GPOINTER_TO_UINT(p) ((guint) (p))
  70. -- Extracts an unsigned integer from a pointer. The integer must have been stored in the pointer with GUINT_TO_POINTER().
  71. -- p : pointer to extract an unsigned integer from.
  72. -- GSIZE_TO_POINTER()
  73. -- #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
  74. -- Stuffs a gsize into a pointer type.
  75. -- s : gsize to stuff into the pointer.
  76. -- GPOINTER_TO_SIZE()
  77. -- #define GPOINTER_TO_SIZE(p) ((gsize) (p))
  78. -- Extracts a gsize from a pointer. The gsize must have been stored in the pointer with GSIZE_TO_POINTER().
  79. -- p : pointer to extract a gsize from.
  80. -- -- guint_to_pointer (a_uint: ): is
  81. -- external "C macro use <glib.h>"
  82. -- alias "GUINT_TO_POINTER"
  83. -- end
  84. -- GPOINTER_TO_UINT (a_p: ): is
  85. -- external "C macro use <glib.h>"
  86. -- alias "GPOINTER_TO_UINT"
  87. -- end
  88. -- GSIZE_TO_POINTER (a_s: ): is
  89. -- external "C macro use <glib.h>"
  90. -- alias "GSIZE_TO_POINTER"
  91. -- end
  92. -- GPOINTER_TO_SIZE (a_p: ): is
  93. -- external "C macro use <glib.h>"
  94. -- alias "GPOINTER_TO_SIZE"
  95. -- end
  96. end