/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

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