test_object.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "test_object.h"
  2. #include <glib.h>
  3. #include <glib-object.h>
  4. // helper enum
  5. GType
  6. gi_cpp_enum_get_type (void)
  7. {
  8. static GType enum_type = 0;
  9. static const GEnumValue enum_types[] = {
  10. {ENUM_VALUE_0, "EnumValue0", "v0"},
  11. {ENUM_VALUE_1, "EnumValue1", "v1"},
  12. {0, NULL, NULL}
  13. };
  14. if (!enum_type) {
  15. enum_type = g_enum_register_static ("GICppEnum", enum_types);
  16. }
  17. return enum_type;
  18. }
  19. // helper flags
  20. GType
  21. gi_cpp_flags_get_type (void)
  22. {
  23. static GType flags_type = 0;
  24. static const GFlagsValue flags_types[] = {
  25. {FLAG_VALUE_0, "FlagValue0", "f0"},
  26. {FLAG_VALUE_1, "FlagValue1", "f1"},
  27. {0, NULL, NULL}
  28. };
  29. if (!flags_type) {
  30. flags_type = g_flags_register_static ("GICppFlags", flags_types);
  31. }
  32. return flags_type;
  33. }
  34. // interface
  35. typedef GICppExampleInterface GICppIExampleInterface;
  36. G_DEFINE_INTERFACE (GICppIExample, gi_cpp_example_interface, 0)
  37. static void
  38. gi_cpp_example_interface_default_init (GICppExampleInterface *iface)
  39. {
  40. (void) iface;
  41. }
  42. // property interface
  43. typedef GICppPropertyInterface GICppIPropertyInterface;
  44. G_DEFINE_INTERFACE (GICppIProperty, gi_cpp_property_interface, 0)
  45. static void
  46. gi_cpp_property_interface_default_init (GICppPropertyInterface *iface)
  47. {
  48. (void) iface;
  49. g_object_interface_install_property (iface,
  50. g_param_spec_int (NAME_INUMBER, "ItfNumber",
  51. "ItfNumber", 0, 50, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  52. }
  53. enum {
  54. EXAMPLE_TO_INT,
  55. EXAMPLE_TO_STRING,
  56. EXAMPLE_TO_VOID,
  57. EXAMPLE_TO_OUTPUT_INT,
  58. /* FILL ME */
  59. LAST_SIGNAL
  60. };
  61. static guint example_signals[LAST_SIGNAL] = { 0 };
  62. struct _GICppExample
  63. {
  64. GObject object;
  65. /* properties */
  66. gchar *data;
  67. gint number;
  68. gdouble fnumber;
  69. GObject *obj;
  70. gboolean present;
  71. CEnum enumvalue;
  72. CFlags flagsvalue;
  73. GError *error;
  74. gint itf_number;
  75. };
  76. static int
  77. vmethod_interface_default (GICppExampleItf * itf, int a)
  78. {
  79. // sanity check
  80. g_assert (GI_IS_CPP_EXAMPLE (itf));
  81. return 2 + a;
  82. }
  83. static void
  84. gi_cpp_example_interface_init (GICppExampleInterface *iface)
  85. {
  86. iface->vmethod = vmethod_interface_default;
  87. }
  88. #define gi_cpp_example_parent_class parent_class
  89. G_DEFINE_TYPE_WITH_CODE (GICppExample, gi_cpp_example, G_TYPE_OBJECT,
  90. G_IMPLEMENT_INTERFACE (gi_cpp_example_interface_get_type(), gi_cpp_example_interface_init))
  91. static void
  92. gi_cpp_example_get_property (GObject * object, guint prop_id, GValue * value,
  93. GParamSpec * pspec)
  94. {
  95. GICppExample *ex = GI_CPP_EXAMPLE (object);
  96. switch (prop_id) {
  97. case PROP_DATA:
  98. g_value_set_string (value, ex->data);
  99. break;
  100. case PROP_NUMBER:
  101. g_value_set_int (value, ex->number);
  102. break;
  103. case PROP_FNUMBER:
  104. g_value_set_double (value, ex->fnumber);
  105. break;
  106. case PROP_PRESENT:
  107. g_value_set_boolean (value, ex->present);
  108. break;
  109. case PROP_OBJECT:
  110. g_value_set_object (value, ex->obj);
  111. break;
  112. case PROP_ENUM:
  113. g_value_set_enum (value, ex->enumvalue);
  114. break;
  115. case PROP_FLAGS:
  116. g_value_set_flags (value, ex->flagsvalue);
  117. break;
  118. case PROP_ERROR:
  119. g_value_set_boxed (value, ex->error);
  120. break;
  121. default:
  122. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  123. break;
  124. }
  125. }
  126. static void
  127. gi_cpp_example_set_property (GObject * object, guint prop_id, const GValue * value,
  128. GParamSpec * pspec)
  129. {
  130. GICppExample *ex = GI_CPP_EXAMPLE (object);
  131. switch (prop_id) {
  132. case PROP_DATA:
  133. g_free (ex->data);
  134. ex->data = g_value_dup_string (value);
  135. break;
  136. case PROP_NUMBER:
  137. ex->number = g_value_get_int (value);
  138. break;
  139. case PROP_FNUMBER:
  140. ex->fnumber = g_value_get_double (value);
  141. break;
  142. case PROP_PRESENT:
  143. ex->present = g_value_get_boolean (value);
  144. break;
  145. case PROP_OBJECT:
  146. if (ex->obj)
  147. g_object_unref (ex->obj);
  148. ex->obj = g_value_dup_object (value);
  149. break;
  150. case PROP_ENUM:
  151. ex->enumvalue = g_value_get_enum (value);
  152. break;
  153. case PROP_FLAGS:
  154. ex->flagsvalue = g_value_get_flags (value);
  155. break;
  156. case PROP_ERROR:
  157. if (ex->error)
  158. g_error_free(ex->error);
  159. ex->error = g_value_dup_boxed(value);
  160. break;
  161. default:
  162. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  163. break;
  164. }
  165. }
  166. static void
  167. gi_cpp_example_finalize (GObject * object)
  168. {
  169. GICppExample *ex = GI_CPP_EXAMPLE (object);
  170. g_free (ex->data);
  171. if (ex->obj)
  172. g_object_unref (ex->obj);
  173. if (ex->error)
  174. g_error_free(ex->error);
  175. G_OBJECT_CLASS (parent_class)->finalize (object);
  176. }
  177. static int
  178. vmethod_default (GICppExample * ex, int a, int b)
  179. {
  180. // sanity check
  181. g_assert (GI_IS_CPP_EXAMPLE (ex));
  182. (void)ex;
  183. return a + b;
  184. }
  185. static void
  186. gi_cpp_example_class_init (GICppExampleClass * klass)
  187. {
  188. GObjectClass *gobject_class;
  189. gobject_class = (GObjectClass *) klass;
  190. gobject_class->finalize = gi_cpp_example_finalize;
  191. gobject_class->set_property = gi_cpp_example_set_property;
  192. gobject_class->get_property = gi_cpp_example_get_property;
  193. g_object_class_install_property (gobject_class, PROP_NUMBER,
  194. g_param_spec_int (NAME_NUMBER, "Number",
  195. "Number", 0, 50, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  196. g_object_class_install_property (gobject_class, PROP_FNUMBER,
  197. g_param_spec_double (NAME_FNUMBER, "FNumber",
  198. "FNumber", 0, 50, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  199. g_object_class_install_property (gobject_class, PROP_DATA,
  200. g_param_spec_string (NAME_DATA, "Data", "Data", NULL,
  201. G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  202. g_object_class_install_property (gobject_class, PROP_PRESENT,
  203. g_param_spec_boolean (NAME_PRESENT, "Present", "Present",
  204. FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  205. g_object_class_install_property (gobject_class, PROP_OBJECT,
  206. g_param_spec_object (NAME_OBJECT, "Object", "Object",
  207. G_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  208. g_object_class_install_property (gobject_class, PROP_ENUM,
  209. g_param_spec_enum (NAME_ENUM, "Enum", "Enum",
  210. GI_CPP_TYPE_ENUM, ENUM_VALUE_0,
  211. G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  212. g_object_class_install_property (gobject_class, PROP_FLAGS,
  213. g_param_spec_flags (NAME_FLAGS, "Flags", "Flags",
  214. GI_CPP_TYPE_FLAGS, FLAG_VALUE_0,
  215. G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  216. g_object_class_install_property (gobject_class, PROP_ERROR,
  217. g_param_spec_boxed (NAME_ERROR, "Error", "Error",
  218. G_TYPE_ERROR, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  219. example_signals[EXAMPLE_TO_INT] =
  220. g_signal_new ("to-int", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
  221. 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_INT, 4,
  222. G_TYPE_OBJECT, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_STRING);
  223. example_signals[EXAMPLE_TO_STRING] =
  224. g_signal_new ("to-string", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
  225. 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_STRING, 2,
  226. G_TYPE_INT, G_TYPE_INT64);
  227. example_signals[EXAMPLE_TO_VOID] =
  228. g_signal_new ("to-void", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
  229. 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 3,
  230. G_TYPE_DOUBLE, GI_CPP_TYPE_ENUM, GI_CPP_TYPE_FLAGS);
  231. example_signals[EXAMPLE_TO_VOID] =
  232. g_signal_new ("to-output-int", G_TYPE_FROM_CLASS (klass),
  233. G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
  234. G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_PTR_ARRAY, G_TYPE_POINTER);
  235. klass->vmethod = vmethod_default;
  236. }
  237. static void
  238. gi_cpp_example_init (GICppExample * ex)
  239. {
  240. (void)ex;
  241. }
  242. GICppExample *
  243. gi_cpp_example_new ()
  244. {
  245. return g_object_new (GI_CPP_TYPE_EXAMPLE, NULL);
  246. }