PageRenderTime 43ms CodeModel.GetById 23ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/src/wrappers/gobject/library/g_value.e

http://github.com/tybor/Liberty
Specman e | 569 lines | 409 code | 80 blank | 80 comment | 10 complexity | 8ff23072d5f500ab79d47faaf7256bdd MD5 | raw file
  1indexing
  2	description: "Generic values, a polymorphic type that can hold values of any other type"
  3	copyright: "Copyright (c) 2005, Paolo Redaelli"
  4	license: "LGPL"
  5	date: "$Date:  $"
  6	revision: "$ $"
  7
  8class G_VALUE
  9
 10inherit
 11	C_STRUCT
 12	FREEZABLE
 13
 14insert
 15	G_TYPE
 16	G_TYPES
 17	GLIB_MEMORY_ALLOCATION export {} all end
 18	G_VALUE_EXTERNALS
 19	
 20creation
 21	make, from_external_pointer, with_gtype,
 22	make_boolean, make_integer, make_natural, make_real, make_real_64, make_real_32,
 23	make_string, make_object, make_pointer,
 24	from_boolean, from_integer, from_natural, from_real, from_string,
 25	from_object, from_pointer
 26
 27feature {} -- Creation
 28
 29	make is
 30			-- Create a undefined GValue.
 31		do
 32			handle := g_try_malloc0 (struct_size)
 33			if handle.is_null then raise_exception (No_more_memory) end
 34			-- handle := g_value_init(malloc_g_value, g_type_invalid)
 35		ensure
 36			not_valid: not is_valid
 37		end
 38
 39	with_gtype (a_gtype: INTEGER) is
 40			-- Create a GValue that holds values of a_gtype type index. A
 41			-- GType is a numerical value which represents the unique
 42			-- identifier of a type registered in the GObject type
 43			-- system. Fundamental types are provided by G_TYPE_EXTERNALS
 44			-- (use insert when you need it).
 45			
 46			-- TODO: forcing end-user to inherit from an external class
 47			-- is a temporary solution; provide higher level/more
 48			-- Eiffellish API.
 49			
 50		require valid_type: is_g_type (a_gtype)
 51		do
 52			handle := g_value_init (malloc_g_value, a_gtype)
 53		end
 54
 55	make_boolean is
 56			-- create a new boolean G_VALUE
 57		local ptr: POINTER
 58		do
 59			ptr := malloc_g_value
 60			handle := g_value_init (ptr, g_type_boolean)
 61			check handle=ptr end
 62			-- Note: from "The Glib Object system v0.10.0" by Mathieu
 63			-- Lacagetin
 64			-- (http://www.le-hacker.org/papers/gobject/ch02.html) it
 65			-- seems that ptr is meaningless: it is thrown away all the
 66			-- times. We will discard it accordingly. Paolo 2005-06-02
 67		ensure is_boolean: is_boolean
 68		end
 69
 70	make_integer is
 71			-- create a new integer G_VALUE
 72		do
 73			handle := g_value_init (malloc_g_value, g_type_int)
 74		ensure is_integer: is_integer
 75		end
 76
 77	make_natural is
 78			-- create a new natural G_VALUE
 79		do
 80			handle := g_value_init (malloc_g_value, g_type_uint)
 81		ensure is_natural: is_natural
 82		end
 83
 84	make_real, make_real_64 is
 85			-- create a new real G_VALUE (Note: using C type `double'
 86		do
 87			handle := g_value_init (malloc_g_value, g_type_double)
 88		ensure is_real: is_real
 89		end
 90
 91	make_real_32 is
 92			-- create a new REAL_32 G_VALUE (Note: using C type `float')
 93		do
 94			handle := g_value_init (malloc_g_value, g_type_double)
 95		ensure is_real: is_real
 96		end
 97
 98	make_string is
 99			-- create a new string G_VALUE
100		do
101			handle := g_value_init (malloc_g_value, g_type_string)
102		ensure is_string: is_string
103		end
104
105	make_object is
106			-- create a new object G_VALUE
107		do
108			handle := g_value_init (malloc_g_value, g_type_object)
109		ensure is_object: is_object
110		end
111
112	make_pointer is
113			-- create a new pointer G_VALUE
114		do
115			handle := g_value_init (malloc_g_value, g_type_pointer)
116		ensure is_pointer: is_pointer
117		end
118
119	from_boolean (a_boolean: BOOLEAN) is
120			-- create a new boolean G_VALUE
121		local ptr: POINTER
122		do
123			ptr := malloc_g_value
124			handle := g_value_init (ptr, g_type_boolean)
125			check handle=ptr end
126			-- Note: from "The Glib Object system v0.10.0" by Mathieu
127			-- Lacagetin
128			-- (http://www.le-hacker.org/papers/gobject/ch02.html) it
129			-- seems that ptr is meaningless: it is thrown away all the
130			-- times. We will discard it accordingly. Paolo 2005-06-02
131			set_boolean (a_boolean)
132		ensure
133			is_boolean: is_boolean
134			value_set: boolean = a_boolean
135		end
136
137	from_integer (a_integer: INTEGER) is
138			-- create a new integer G_VALUE
139		do
140			handle := g_value_init (malloc_g_value, g_type_int)
141			set_integer (a_integer)
142		ensure
143			is_integer: is_integer
144			value_set: integer = a_integer
145		end
146
147	from_natural (a_natural: INTEGER) is
148			-- create a new natural G_VALUE
149		do
150			handle := g_value_init (malloc_g_value, g_type_uint)
151			set_natural (a_natural)
152		ensure
153			is_natural: is_natural
154			value_set: natural = a_natural
155		end
156
157	from_real, from_real_64 (a_real: REAL_64) is
158			-- create a new real G_VALUE (Note: using C type `double'
159		do
160			handle := g_value_init (malloc_g_value, g_type_double)
161			set_real (a_real)
162		ensure
163			is_real: is_real
164			value_set: real = a_real
165		end
166
167	from_real_32 (a_real_32: REAL_32) is
168			-- create a new REAL_32 G_VALUE (Note: using C type `float')
169		do
170			handle := g_value_init (malloc_g_value, g_type_float)
171			set_real_32 (a_real_32)
172		ensure
173			is_real_32: is_real_32
174			value_set: real_32 = a_real_32
175		end
176
177	from_string (a_string: STRING) is
178			-- create a new string G_VALUE
179		require string_not_void: a_string/=Void
180		do
181			handle := g_value_init (malloc_g_value, g_type_string)
182			set_string (a_string)
183		ensure
184			is_string: is_string
185			value_set: string.is_equal(a_string)
186		end
187
188	from_object (an_object: G_OBJECT) is
189			-- create a new object G_VALUE
190		require object_not_void: an_object/=Void
191		do
192			handle := g_value_init (malloc_g_value, g_type_object)
193			set_object (an_object)
194		ensure
195			is_object: is_object
196			value_set: object.is_equal(an_object.handle)
197		end
198
199	from_pointer (a_pointer: POINTER) is
200			-- create a new pointer G_VALUE
201		do
202			handle := g_value_init (malloc_g_value, g_type_pointer)
203			set_pointer (a_pointer)
204		ensure
205			is_pointer: is_pointer
206			value_set: pointer.is_equal (a_pointer)
207		end
208
209feature {ANY}
210
211	is_initialized: BOOLEAN is
212			-- Returns True if value is a valid and initialized G_VALUE
213		do
214			Result := g_is_value (handle).to_boolean
215		end
216
217	type_name: STRING is
218			-- type name of value
219		do
220			create Result.from_external (g_value_type_name (handle))
221		end
222
223	type: INTEGER is
224			-- GType numerical value
225		do
226			Result := g_value_type (handle)
227		end
228
229	is_valid: BOOLEAN is
230		do
231			Result := (type /= g_type_invalid)
232		end
233
234	is_a (a_type: INTEGER): BOOLEAN is
235			-- Is Current gtype conforming to `a_type'?
236		do
237			Result := g_type_is_a (type, a_type).to_boolean
238		end
239
240	holds (a_type: INTEGER): BOOLEAN is
241		require
242			is_g_type (a_type)
243		do
244			Result := g_value_holds (handle, a_type).to_boolean
245		end
246
247feature {ANY} -- Boolean
248	is_boolean: BOOLEAN is
249			-- Is current value a boolean?
250		do
251			Result := g_value_holds_boolean (handle).to_boolean
252		end
253
254	boolean: BOOLEAN is
255			-- If the current value is a boolean, return it
256		require
257			is_boolean: is_boolean
258		do
259			Result := (g_value_get_boolean (handle) = 1)
260		end
261
262
263	set_boolean (a_value: BOOLEAN) is
264			-- If the current value is a boolean, set it.
265		require
266			is_boolean: is_boolean
267		do
268			g_value_set_boolean (handle, a_value.to_integer)
269		end
270
271feature {ANY} -- Integer
272	is_integer: BOOLEAN is
273			-- Is current value an integer?
274		do
275			Result := g_value_holds_int (handle).to_boolean
276		end
277
278	integer: INTEGER is
279			-- If current value is an integer, returns it
280		require
281			is_integer: is_integer
282		do
283			Result := g_value_get_int (handle)
284		end
285
286	set_integer (a_value: INTEGER) is
287			-- If the current value is a integer, set it.
288		require
289			is_integer: is_integer
290		do
291			g_value_set_int (handle, a_value)
292		end
293
294feature {ANY} -- Natural
295	is_natural: BOOLEAN is
296			-- Is current value an natural?
297		do
298			Result := g_value_holds_uint (handle).to_boolean
299		end
300
301	natural: INTEGER is
302			-- If current value is an natural, returns it
303		require
304			is_natural: is_natural
305		do
306			Result := g_value_get_uint (handle)
307		end
308
309	set_natural (a_value: INTEGER) is
310			-- If the current value is a natural, set it.
311		require
312			is_natural: is_natural
313		do
314			g_value_set_uint (handle, a_value)
315		end
316
317feature {ANY} -- Real
318
319	is_real, is_real_64: BOOLEAN is
320			-- Is current value a real? Note: REAL is mapped to C double
321		do
322			Result := g_value_holds_double (handle).to_boolean
323		end
324
325	real, real_64: REAL is
326			-- If current value is an real, returns it. Note: REAL is mapped to C double
327		require
328			is_real: is_real
329		do
330			Result := g_value_get_double (handle)
331		end
332
333	set_real, set_real_64 (a_value: REAL_64) is
334			-- If the current value is a real, set it. Note: REAL is mapped to C double
335		require
336			is_real: is_real
337		do
338			g_value_set_double (handle, a_value)
339		end
340
341feature {ANY} -- Real_32
342
343	is_real_32: BOOLEAN is
344			-- Is current value a REAL_32? Note: REAL is mapped to C float
345		do
346			Result := g_value_holds_float (handle).to_boolean
347		end
348
349	real_32: REAL_32 is
350			-- If current value is an real_32, returns it. Note: REAL is mapped to C float
351		require
352			is_real_32: is_real_32
353		do
354			Result := g_value_get_float (handle)
355		end
356
357	set_real_32 (a_value: REAL_32) is
358			-- If the current value is a REAL_32, set it. Note: REAL_32 is mapped to C float
359		require
360			is_real_32: is_real_32
361		do
362			g_value_set_float (handle, a_value)
363		end
364
365feature {ANY} -- Character
366
367	is_character: BOOLEAN is
368			-- Is current value a character?
369		do
370			Result := g_value_holds_char (handle).to_boolean
371		end
372
373	character: CHARACTER is
374			-- If current value is an character, returns it.
375		require
376			is_character: is_character
377		do
378			Result := g_value_get_char (handle)
379		end
380
381	set_character (a_value: CHARACTER) is
382			-- If the current value is a character, set it.
383		require
384			is_character: is_character
385		do
386			g_value_set_char (handle, a_value)
387		end
388
389feature {ANY} -- String
390
391	is_string: BOOLEAN is
392			-- Is current value a string?
393		do
394			Result := g_value_holds_string (handle).to_boolean
395		end
396
397	string: STRING is
398			-- If current value is an string, returns it.
399			-- Note that a gvalue might be holding a NULL string
400		require
401			is_string: is_string
402		local
403			p: POINTER
404		do
405			p := g_value_get_string (handle)
406			if p.is_not_null then
407				-- ATTENTION: because this returns a const, we need to copy the memory
408				-- const gchar* g_value_get_string (const GValue *value);
409				create Result.from_external_copy (p)
410			end
411		end
412
413	set_string (a_value: STRING) is
414			-- If the current value is a string, set it.
415		require
416			is_string: is_string
417			value_not_void: a_value/=Void
418		do
419			g_value_set_string (handle, a_value.to_external)
420		end
421
422feature {ANY} -- Object
423
424	is_object: BOOLEAN is
425			-- Is current value an object?
426		do
427			Result := g_value_holds_object(handle).to_boolean
428		end
429
430	object: POINTER is
431			-- If current value is an string, returns it.
432		require
433			is_object: is_object
434		do
435			Result := g_value_get_object (handle)
436		end
437
438	set_object (a_value: G_OBJECT) is
439			-- If the current value is an object, set it.
440		require
441			a_value /= Void
442			is_object: is_object
443		do
444			g_value_set_object (handle, a_value.handle)
445		end
446
447feature {ANY} -- Pointer
448
449	is_pointer: BOOLEAN is
450			-- Is current value a pointer?
451		do
452			Result := g_value_holds_pointer (handle).to_boolean
453		end
454
455	pointer: POINTER is
456			-- If current value is a pointer, returns it.
457		require
458			is_pointer: is_pointer
459		do
460			Result := g_value_get_pointer (handle)
461		end
462
463	set_pointer (a_value: POINTER) is
464			-- If the current value is a pointer, set it.
465		require
466			is_pointer: is_pointer
467		do
468			g_value_set_pointer (handle, a_value)
469		end
470
471	-- TODO: wrap all Parameter specification functions, such as
472	-- g_param_spec_* ()
473
474feature {G_OBJECT} -- Type changing features
475
476	turn_to_boolean is
477			-- Reset Current and make it a boolean value
478		do
479			if is_initialized then
480				g_value_unset (handle)
481			end
482			handle := g_value_init (handle, g_type_boolean)
483		ensure
484			is_boolean: is_boolean
485		end
486
487	turn_to_integer is
488			-- Reset Current and make it a integer value
489		do
490			if is_initialized then
491				g_value_unset (handle)
492			end
493			handle := g_value_init (handle, g_type_int)
494		ensure
495			is_integer: is_integer
496		end
497
498	turn_to_natural is
499			-- Reset Current and make it a natural value
500		do
501			if is_initialized then
502				g_value_unset (handle)
503			end
504			handle := g_value_init (handle, g_type_uint)
505		ensure
506			is_natural: is_natural
507		end
508
509	turn_to_real, turn_to_real_64 is
510			-- Reset Current and make it a real value
511		do
512			if is_initialized then
513				g_value_unset (handle)
514			end
515			handle := g_value_init (handle, g_type_double)
516		ensure
517			is_real: is_real
518		end
519
520	turn_to_real_32 is
521			-- Reset Current and make it a REAL_32 value
522		do
523			if is_initialized then
524				g_value_unset (handle)
525			end
526			handle := g_value_init (handle, g_type_float)
527		ensure
528			is_real_32: is_real_32
529		end
530
531	turn_to_string is
532			-- Reset Current and make it a string value
533		do
534			if is_initialized then
535				g_value_unset (handle)
536			end
537			handle := g_value_init (handle, g_type_string)
538		ensure
539			is_string: is_string
540		end
541
542feature
543	struct_size: INTEGER is
544		external "C inline use <glib-object.h>"
545		alias "sizeof(GValue)"
546		end
547
548feature {} -- Disposing
549
550	dispose is
551		-- If not petriefied unset and free the value.
552		do
553			if not is_petrified then
554				if is_initialized then
555					g_value_unset (handle)
556				else
557					print ("G_VALUE::dispose: disposing an uninitialised G_VALUE%N")
558				end
559				g_free (handle)
560			end
561			handle := default_pointer
562		end
563
564invariant
565
566	handle_not_null: is_not_null
567--	is_initialized
568
569end