PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

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