Skip to content

Instantly share code, notes, and snippets.

@ebassi
Created July 26, 2020 17:45
Show Gist options
  • Save ebassi/be083324abc7848be331e1f2e7db91df to your computer and use it in GitHub Desktop.
Save ebassi/be083324abc7848be331e1f2e7db91df to your computer and use it in GitHub Desktop.
Quick test for using C99's _Bool with GLib's gboolean
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <glib-object.h>
typedef unsigned char my_bool_t;
G_DECLARE_FINAL_TYPE (TestObject, test_object, TEST, OBJECT, GObject)
struct _TestObject
{
GObject parent_instance;
gboolean v_gbool;
bool v_bool;
char *v_str;
};
G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
enum {
PROP_GBOOL = 1,
PROP_BOOL,
PROP_STR,
N_PROPS
};
static GParamSpec *obj_props[N_PROPS];
static void
test_object_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TestObject *self = TEST_OBJECT (gobject);
switch (prop_id)
{
case PROP_GBOOL:
self->v_gbool = g_value_get_boolean (value);
break;
case PROP_BOOL:
self->v_bool = g_value_get_boolean (value);
break;
case PROP_STR:
g_free (self->v_str);
self->v_str = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
}
static void
test_object_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TestObject *self = TEST_OBJECT (gobject);
switch (prop_id)
{
case PROP_GBOOL:
g_value_set_boolean (value, self->v_gbool);
break;
case PROP_BOOL:
g_value_set_boolean (value, self->v_bool);
break;
case PROP_STR:
g_value_set_string (value, self->v_str);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
}
static void
test_object_class_init (TestObjectClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = test_object_set_property;
gobject_class->get_property = test_object_get_property;
obj_props[PROP_GBOOL] =
g_param_spec_boolean ("gbool", NULL, NULL, FALSE, G_PARAM_READWRITE);
obj_props[PROP_BOOL] =
g_param_spec_boolean ("bool", NULL, NULL, FALSE, G_PARAM_READWRITE);
obj_props[PROP_STR] =
g_param_spec_string ("str", NULL, NULL, NULL, G_PARAM_READWRITE);
g_object_class_install_properties (gobject_class, N_PROPS, obj_props);
}
static void
test_object_init (TestObject *self)
{
}
int
main (int argc __attribute__((unused)),
char *argv[] __attribute__((unused)))
{
fprintf (stdout,
"sizeof bool := %lu\n"
"sizeof int := %lu\n"
"sizeof uchar := %lu\n"
"sizeof my_bool_t := %lu\n"
"sizeof gboolean := %lu\n",
sizeof (bool),
sizeof (int),
sizeof (unsigned char),
sizeof (my_bool_t),
sizeof (gboolean));
TestObject *obj = g_object_new (test_object_get_type (), NULL);
gboolean res_gbool = TRUE;
bool res_bool = false;
g_object_set (obj, "gbool", FALSE, "bool", true, "str", "Hello", NULL);
g_print ("*** g_object_set():\n"
"\tgbool - FALSE: %s (%d)\n"
"\tbool - true: %s (%d)\n",
obj->v_gbool ? "true" : "false", obj->v_gbool,
obj->v_bool ? "true" : "false", obj->v_bool);
g_assert_false (obj->v_gbool);
g_assert_true (obj->v_bool);
g_object_get (obj, "bool", &res_bool, "gbool", &res_gbool, NULL);
g_print ("*** g_object_get():\n"
"\tgbool -> res_gbool: %s (%d)\n"
"\tbool -> res_bool: %s (%d)\n",
res_gbool ? "true" : "false", res_gbool,
res_bool ? "true" : "false", res_bool);
g_object_unref (obj);
g_assert_false (res_gbool);
g_assert_true (res_bool);
obj = g_object_new (test_object_get_type (), NULL);
g_object_set (obj, "gbool", false, "bool", TRUE, "str", "World", NULL);
g_print ("*** g_object_set():\n"
"\tgbool - false: %s (%d)\n"
"\tbool - TRUE: %s (%d)\n",
obj->v_gbool ? "true" : "false", obj->v_gbool,
obj->v_bool ? "true" : "false", obj->v_bool);
g_assert_false (obj->v_gbool);
g_assert_true (obj->v_bool);
g_object_get (obj, "gbool", &res_bool, "bool", &res_gbool, NULL);
g_print ("*** g_object_get():\n"
"\tgbool -> res_bool: %s (%d)\n"
"\tbool -> res_gbool: %s (%d)\n",
res_bool ? "true" : "false", res_bool,
res_gbool ? "true" : "false", res_gbool);
g_object_unref (obj);
g_assert_false (res_bool);
g_assert_true (res_gbool);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment