1 /* Fo
2  * fo-boolean.c: Boolean datatype
3  *
4  * Copyright (C) 2001 Sun Microsystems
5  * Copyright (C) 2007 Menteith Consulting Ltd
6  *
7  * See COPYING for the status of this software.
8  */
9 
10 #include "fo-utils.h"
11 #include "fo-datatype.h"
12 #include "fo-datatype-private.h"
13 #include "fo-boolean.h"
14 
15 
16 enum {
17   PROP_0,
18   PROP_VALUE
19 };
20 
21 struct _FoBoolean
22 {
23   FoDatatype parent_instance;
24 
25   gboolean value;
26 };
27 
28 struct _FoBooleanClass
29 {
30   FoDatatypeClass parent_class;
31 };
32 
33 static void fo_boolean_init        (FoBoolean      *boolean);
34 static void fo_boolean_class_init  (FoBooleanClass *klass);
35 static void fo_boolean_set_property (GObject         *object,
36                                   guint            prop_id,
37                                   const GValue    *value,
38                                   GParamSpec      *pspec);
39 static void fo_boolean_get_property   (GObject         *object,
40                                        guint            prop_id,
41                                        GValue          *value,
42                                        GParamSpec      *pspec);
43 static void fo_boolean_finalize    (GObject           *object);
44 
45 static FoDatatype * fo_boolean_copy          (FoDatatype *datatype);
46 static gchar*       fo_boolean_sprintf       (FoObject   *object);
47 static void         fo_boolean_set_value     (FoDatatype *boolean,
48 					      gboolean    new_value);
49 
50 static gpointer parent_class;
51 
52 /**
53  * fo_boolean_get_type:
54  *
55  * Register the #FoBoolean object type.
56  *
57  * Return value: #GType value of the #FoBoolean object type.
58  **/
59 GType
fo_boolean_get_type(void)60 fo_boolean_get_type (void)
61 {
62   static GType object_type = 0;
63 
64   if (!object_type)
65     {
66       static const GTypeInfo object_info =
67       {
68         sizeof (FoBooleanClass),
69         (GBaseInitFunc) NULL,
70         (GBaseFinalizeFunc) NULL,
71         (GClassInitFunc) fo_boolean_class_init,
72         NULL,           /* class_finalize */
73         NULL,           /* class_data */
74         sizeof (FoBoolean),
75         0,              /* n_preallocs */
76         (GInstanceInitFunc) fo_boolean_init,
77 	NULL		/* value_table */
78       };
79 
80       object_type = g_type_register_static (FO_TYPE_DATATYPE,
81                                             "FoBoolean",
82                                             &object_info, 0);
83     }
84 
85   return object_type;
86 }
87 
88 /**
89  * fo_boolean_init:
90  * @boolean: #FoBoolean object to initialise.
91  *
92  * Implements #GInstanceInitFunc for #FoBoolean.
93  **/
94 static void
fo_boolean_init(FoBoolean * boolean)95 fo_boolean_init (FoBoolean *boolean)
96 {
97   boolean->value = FALSE;
98 }
99 
100 /**
101  * fo_boolean_class_init:
102  * @klass: #FoBooleanClass object to initialise.
103  *
104  * Implements #GClassInitFunc for #FoBooleanClass.
105  **/
106 void
fo_boolean_class_init(FoBooleanClass * klass)107 fo_boolean_class_init (FoBooleanClass *klass)
108 {
109   GObjectClass *object_class = G_OBJECT_CLASS (klass);
110 
111   parent_class = g_type_class_peek_parent (klass);
112 
113   object_class->finalize = fo_boolean_finalize;
114 
115   object_class->set_property = fo_boolean_set_property;
116   object_class->get_property = fo_boolean_get_property;
117 
118   FO_OBJECT_CLASS (klass)->print_sprintf = fo_boolean_sprintf;
119 
120   g_object_class_install_property (object_class,
121                                    PROP_VALUE,
122                                    g_param_spec_boolean ("value",
123 							 _("Value"),
124 							 _("Boolean value"),
125 							 FALSE,
126 							 G_PARAM_READWRITE |
127 							 G_PARAM_CONSTRUCT_ONLY));
128 
129   FO_DATATYPE_CLASS (klass)->copy = fo_boolean_copy;
130 }
131 
132 /**
133  * fo_boolean_finalize:
134  * @object: #FoBoolean object to finalize.
135  *
136  * Implements #GObjectFinalizeFunc for #FoBoolean.
137  **/
138 void
fo_boolean_finalize(GObject * object)139 fo_boolean_finalize (GObject *object)
140 {
141   FoBoolean *boolean;
142 
143   boolean = FO_BOOLEAN (object);
144 
145   G_OBJECT_CLASS (parent_class)->finalize (object);
146 }
147 
148 
149 /**
150  * fo_boolean_get_property:
151  * @object:  #GObject whose property will be retrieved.
152  * @prop_id: Property ID assigned when property registered.
153  * @value:   #GValue to set with property value.
154  * @pspec:   Parameter specification for this property type.
155  *
156  * Implements #GObjectGetPropertyFunc for #FoBoolean.
157  **/
158 void
fo_boolean_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)159 fo_boolean_get_property (GObject         *object,
160                          guint            prop_id,
161                          GValue          *value,
162                          GParamSpec      *pspec)
163 {
164   FoDatatype *boolean;
165 
166   boolean = FO_DATATYPE (object);
167 
168   switch (prop_id)
169     {
170     case PROP_VALUE:
171       g_value_set_boolean (value, fo_boolean_get_value (boolean));
172       break;
173     default:
174       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175       break;
176     }
177 }
178 
179 /**
180  * fo_boolean_set_property:
181  * @object:  #GObject whose property will be set.
182  * @prop_id: Property ID assigned when property registered.
183  * @value:   New value for property.
184  * @pspec:   Parameter specification for this property type.
185  *
186  * Implements #GObjectSetPropertyFunc for #FoBoolean.
187  **/
188 void
fo_boolean_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)189 fo_boolean_set_property (GObject         *object,
190                          guint            prop_id,
191                          const GValue    *value,
192                          GParamSpec      *pspec)
193 {
194   FoDatatype *boolean;
195 
196   boolean = FO_DATATYPE (object);
197 
198   switch (prop_id)
199     {
200     case PROP_VALUE:
201       fo_boolean_set_value (boolean, g_value_get_boolean (value));
202       break;
203     default:
204       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205       break;
206     }
207 }
208 
209 /**
210  * fo_boolean_new:
211  *
212  * Creates a new #FoBoolean initialized to default value.
213  *
214  * Return value: the new #FoBoolean.
215  **/
216 FoDatatype *
fo_boolean_new(void)217 fo_boolean_new (void)
218 {
219   FoDatatype *boolean;
220 
221   boolean = FO_DATATYPE (g_object_new (fo_boolean_get_type (), NULL));
222 
223   return boolean;
224 }
225 
226 /**
227  * fo_boolean_new_with_value:
228  * @value: gboolean of the new #FoBoolean.
229  *
230  * Creates a new #FoBoolean set to @value.
231  *
232  * Return value: The new #FoBoolean.
233  **/
234 FoDatatype*
fo_boolean_new_with_value(gboolean value)235 fo_boolean_new_with_value (gboolean value)
236 {
237   FoDatatype *boolean = fo_boolean_new ();
238 
239   fo_boolean_set_value (boolean, value);
240 
241   return boolean;
242 }
243 
244 /**
245  * fo_boolean_get_value:
246  * @boolean: #FoBoolean.
247  *
248  * Get the value of @boolean.
249  *
250  * Return value: #gboolean value of @boolean.
251  **/
252 gboolean
fo_boolean_get_value(FoDatatype * boolean)253 fo_boolean_get_value (FoDatatype *boolean)
254 {
255   g_return_val_if_fail (boolean != NULL, 0);
256   g_return_val_if_fail (FO_IS_BOOLEAN (boolean), 0);
257 
258   return FO_BOOLEAN (boolean)->value;
259 }
260 
261 /**
262  * fo_boolean_set_value:
263  * @boolean:    #FoBoolean.
264  * @new_value: New value for @boolean.
265  *
266  * Set the value of @boolean.
267  **/
268 void
fo_boolean_set_value(FoDatatype * boolean,gboolean new_value)269 fo_boolean_set_value (FoDatatype *boolean,
270 		      gboolean    new_value)
271 {
272   g_return_if_fail (boolean != NULL);
273   g_return_if_fail (FO_IS_BOOLEAN (boolean));
274 
275   FO_BOOLEAN (boolean)->value = new_value;
276   /*g_object_notify(G_OBJECT (boolean), "value");*/
277 }
278 
279 /**
280  * fo_boolean_sprintf:
281  * @object: #FoBoolean whose value is to be printed.
282  *
283  * Create and return a string representation of the value of @object,
284  * which must be an #FoBoolean.
285  *
286  * String must be freed by caller.
287  *
288  * Return value: String representation of value of @object.
289  **/
290 gchar*
fo_boolean_sprintf(FoObject * object)291 fo_boolean_sprintf (FoObject *object)
292 {
293   g_return_val_if_fail (object != NULL, NULL);
294   g_return_val_if_fail (FO_IS_BOOLEAN (object), NULL);
295 
296   return g_strdup_printf (FO_BOOLEAN (object)->value ? "true" : "false");
297 }
298 
299 /**
300  * fo_boolean_copy:
301  * @datatype: #FoBoolean to be copied.
302  *
303  * Create a copy of @datatype.
304  *
305  * Return value: New #FoBoolean.
306  **/
307 FoDatatype*
fo_boolean_copy(FoDatatype * datatype)308 fo_boolean_copy (FoDatatype *datatype)
309 {
310   FoDatatype *boolean;
311 
312   g_return_val_if_fail (datatype != NULL, NULL);
313   g_return_val_if_fail (FO_IS_BOOLEAN (datatype), NULL);
314 
315   boolean = fo_boolean_new ();
316   FO_BOOLEAN (boolean)->value = FO_BOOLEAN (datatype)->value;
317 
318   return boolean;
319 }
320 
321 /**
322  * fo_boolean_get_true:
323  *
324  * Create and return an #FoBoolean with the well-known value %true.
325  *
326  * This saves creation of multiple #FoBoolean objects that all have the
327  * value %true.
328  *
329  * Return value: #FoBoolean with value %true.
330  **/
331 FoDatatype*
fo_boolean_get_true(void)332 fo_boolean_get_true (void)
333 {
334   static FoDatatype *boolean = NULL;
335 
336   if (!boolean)
337     {
338       boolean = fo_boolean_new ();
339       FO_BOOLEAN (boolean)->value = TRUE;
340     }
341 
342   return boolean;
343 }
344 
345 /**
346  * fo_boolean_get_false:
347  *
348  * Create and return an #FoBoolean with the well-known value %false.
349  *
350  * This saves creation of multiple #FoBoolean objects that all have the
351  * value %false.
352  *
353  * Return value: #FoBoolean with value %false.
354  **/
355 FoDatatype*
fo_boolean_get_false(void)356 fo_boolean_get_false (void)
357 {
358   static FoDatatype *boolean = NULL;
359 
360   if (!boolean)
361     {
362       boolean = fo_boolean_new ();
363       FO_BOOLEAN (boolean)->value = FALSE;
364     }
365 
366   return boolean;
367 }
368 
369