2 * @file value.c Value wrapper API
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 #define OUTGOING_FLAG 0x01
33 purple_value_new(PurpleType type
, ...)
38 g_return_val_if_fail(type
!= PURPLE_TYPE_UNKNOWN
, NULL
);
40 value
= g_new0(PurpleValue
, 1);
46 if (type
== PURPLE_TYPE_SUBTYPE
)
47 value
->u
.subtype
= va_arg(args
, int);
48 else if (type
== PURPLE_TYPE_BOXED
)
49 value
->u
.specific_type
= g_strdup(va_arg(args
, char *));
57 purple_value_new_outgoing(PurpleType type
, ...)
62 g_return_val_if_fail(type
!= PURPLE_TYPE_UNKNOWN
, NULL
);
64 value
= g_new0(PurpleValue
, 1);
70 if (type
== PURPLE_TYPE_SUBTYPE
)
71 value
->u
.subtype
= va_arg(args
, int);
72 else if (type
== PURPLE_TYPE_BOXED
)
73 value
->u
.specific_type
= g_strdup(va_arg(args
, char *));
77 value
->flags
|= OUTGOING_FLAG
;
83 purple_value_destroy(PurpleValue
*value
)
85 g_return_if_fail(value
!= NULL
);
87 if (purple_value_get_type(value
) == PURPLE_TYPE_BOXED
)
89 g_free(value
->u
.specific_type
);
91 else if (purple_value_get_type(value
) == PURPLE_TYPE_STRING
)
93 g_free(value
->data
.string_data
);
100 purple_value_dup(const PurpleValue
*value
)
102 PurpleValue
*new_value
;
105 g_return_val_if_fail(value
!= NULL
, NULL
);
107 type
= purple_value_get_type(value
);
109 if (type
== PURPLE_TYPE_SUBTYPE
)
111 new_value
= purple_value_new(PURPLE_TYPE_SUBTYPE
,
112 purple_value_get_subtype(value
));
114 else if (type
== PURPLE_TYPE_BOXED
)
116 new_value
= purple_value_new(PURPLE_TYPE_BOXED
,
117 purple_value_get_specific_type(value
));
120 new_value
= purple_value_new(type
);
122 new_value
->flags
= value
->flags
;
126 case PURPLE_TYPE_CHAR
:
127 purple_value_set_char(new_value
, purple_value_get_char(value
));
130 case PURPLE_TYPE_UCHAR
:
131 purple_value_set_uchar(new_value
, purple_value_get_uchar(value
));
134 case PURPLE_TYPE_BOOLEAN
:
135 purple_value_set_boolean(new_value
, purple_value_get_boolean(value
));
138 case PURPLE_TYPE_SHORT
:
139 purple_value_set_short(new_value
, purple_value_get_short(value
));
142 case PURPLE_TYPE_USHORT
:
143 purple_value_set_ushort(new_value
, purple_value_get_ushort(value
));
146 case PURPLE_TYPE_INT
:
147 purple_value_set_int(new_value
, purple_value_get_int(value
));
150 case PURPLE_TYPE_UINT
:
151 purple_value_set_uint(new_value
, purple_value_get_uint(value
));
154 case PURPLE_TYPE_LONG
:
155 purple_value_set_long(new_value
, purple_value_get_long(value
));
158 case PURPLE_TYPE_ULONG
:
159 purple_value_set_ulong(new_value
, purple_value_get_ulong(value
));
162 case PURPLE_TYPE_INT64
:
163 purple_value_set_int64(new_value
, purple_value_get_int64(value
));
166 case PURPLE_TYPE_UINT64
:
167 purple_value_set_uint64(new_value
, purple_value_get_uint64(value
));
170 case PURPLE_TYPE_STRING
:
171 purple_value_set_string(new_value
, purple_value_get_string(value
));
174 case PURPLE_TYPE_OBJECT
:
175 purple_value_set_object(new_value
, purple_value_get_object(value
));
178 case PURPLE_TYPE_POINTER
:
179 purple_value_set_pointer(new_value
, purple_value_get_pointer(value
));
182 case PURPLE_TYPE_ENUM
:
183 purple_value_set_enum(new_value
, purple_value_get_enum(value
));
186 case PURPLE_TYPE_BOXED
:
187 purple_value_set_boxed(new_value
, purple_value_get_boxed(value
));
198 purple_value_get_type(const PurpleValue
*value
)
200 g_return_val_if_fail(value
!= NULL
, PURPLE_TYPE_UNKNOWN
);
206 purple_value_get_subtype(const PurpleValue
*value
)
208 g_return_val_if_fail(value
!= NULL
, 0);
209 g_return_val_if_fail(purple_value_get_type(value
) == PURPLE_TYPE_SUBTYPE
, 0);
211 return value
->u
.subtype
;
215 purple_value_get_specific_type(const PurpleValue
*value
)
217 g_return_val_if_fail(value
!= NULL
, NULL
);
218 g_return_val_if_fail(purple_value_get_type(value
) == PURPLE_TYPE_BOXED
, NULL
);
220 return value
->u
.specific_type
;
224 purple_value_is_outgoing(const PurpleValue
*value
)
226 g_return_val_if_fail(value
!= NULL
, FALSE
);
228 return (value
->flags
& OUTGOING_FLAG
);
232 purple_value_set_char(PurpleValue
*value
, char data
)
234 g_return_if_fail(value
!= NULL
);
236 value
->data
.char_data
= data
;
240 purple_value_set_uchar(PurpleValue
*value
, unsigned char data
)
242 g_return_if_fail(value
!= NULL
);
244 value
->data
.uchar_data
= data
;
248 purple_value_set_boolean(PurpleValue
*value
, gboolean data
)
250 g_return_if_fail(value
!= NULL
);
252 value
->data
.boolean_data
= data
;
256 purple_value_set_short(PurpleValue
*value
, short data
)
258 g_return_if_fail(value
!= NULL
);
260 value
->data
.short_data
= data
;
264 purple_value_set_ushort(PurpleValue
*value
, unsigned short data
)
266 g_return_if_fail(value
!= NULL
);
268 value
->data
.ushort_data
= data
;
272 purple_value_set_int(PurpleValue
*value
, int data
)
274 g_return_if_fail(value
!= NULL
);
276 value
->data
.int_data
= data
;
280 purple_value_set_uint(PurpleValue
*value
, unsigned int data
)
282 g_return_if_fail(value
!= NULL
);
284 value
->data
.int_data
= data
;
288 purple_value_set_long(PurpleValue
*value
, long data
)
290 g_return_if_fail(value
!= NULL
);
292 value
->data
.long_data
= data
;
296 purple_value_set_ulong(PurpleValue
*value
, unsigned long data
)
298 g_return_if_fail(value
!= NULL
);
300 value
->data
.long_data
= data
;
304 purple_value_set_int64(PurpleValue
*value
, gint64 data
)
306 g_return_if_fail(value
!= NULL
);
308 value
->data
.int64_data
= data
;
312 purple_value_set_uint64(PurpleValue
*value
, guint64 data
)
314 g_return_if_fail(value
!= NULL
);
316 value
->data
.uint64_data
= data
;
320 purple_value_set_string(PurpleValue
*value
, const char *data
)
322 g_return_if_fail(value
!= NULL
);
323 g_return_if_fail(data
== NULL
|| g_utf8_validate(data
, -1, NULL
));
325 g_free(value
->data
.string_data
);
326 value
->data
.string_data
= g_strdup(data
);
330 purple_value_set_object(PurpleValue
*value
, void *data
)
332 g_return_if_fail(value
!= NULL
);
334 value
->data
.object_data
= data
;
338 purple_value_set_pointer(PurpleValue
*value
, void *data
)
340 g_return_if_fail(value
!= NULL
);
342 value
->data
.pointer_data
= data
;
346 purple_value_set_enum(PurpleValue
*value
, int data
)
348 g_return_if_fail(value
!= NULL
);
350 value
->data
.enum_data
= data
;
354 purple_value_set_boxed(PurpleValue
*value
, void *data
)
356 g_return_if_fail(value
!= NULL
);
358 value
->data
.boxed_data
= data
;
362 purple_value_get_char(const PurpleValue
*value
)
364 g_return_val_if_fail(value
!= NULL
, 0);
366 return value
->data
.char_data
;
370 purple_value_get_uchar(const PurpleValue
*value
)
372 g_return_val_if_fail(value
!= NULL
, 0);
374 return value
->data
.uchar_data
;
378 purple_value_get_boolean(const PurpleValue
*value
)
380 g_return_val_if_fail(value
!= NULL
, FALSE
);
382 return value
->data
.boolean_data
;
386 purple_value_get_short(const PurpleValue
*value
)
388 g_return_val_if_fail(value
!= NULL
, 0);
390 return value
->data
.short_data
;
394 purple_value_get_ushort(const PurpleValue
*value
)
396 g_return_val_if_fail(value
!= NULL
, 0);
398 return value
->data
.ushort_data
;
402 purple_value_get_int(const PurpleValue
*value
)
404 g_return_val_if_fail(value
!= NULL
, 0);
406 return value
->data
.int_data
;
410 purple_value_get_uint(const PurpleValue
*value
)
412 g_return_val_if_fail(value
!= NULL
, 0);
414 return value
->data
.int_data
;
418 purple_value_get_long(const PurpleValue
*value
)
420 g_return_val_if_fail(value
!= NULL
, 0);
422 return value
->data
.long_data
;
426 purple_value_get_ulong(const PurpleValue
*value
)
428 g_return_val_if_fail(value
!= NULL
, 0);
430 return value
->data
.long_data
;
434 purple_value_get_int64(const PurpleValue
*value
)
436 g_return_val_if_fail(value
!= NULL
, 0);
438 return value
->data
.int64_data
;
442 purple_value_get_uint64(const PurpleValue
*value
)
444 g_return_val_if_fail(value
!= NULL
, 0);
446 return value
->data
.uint64_data
;
450 purple_value_get_string(const PurpleValue
*value
)
452 g_return_val_if_fail(value
!= NULL
, NULL
);
454 return value
->data
.string_data
;
458 purple_value_get_object(const PurpleValue
*value
)
460 g_return_val_if_fail(value
!= NULL
, NULL
);
462 return value
->data
.object_data
;
466 purple_value_get_pointer(const PurpleValue
*value
)
468 g_return_val_if_fail(value
!= NULL
, NULL
);
470 return value
->data
.pointer_data
;
474 purple_value_get_enum(const PurpleValue
*value
)
476 g_return_val_if_fail(value
!= NULL
, -1);
478 return value
->data
.enum_data
;
482 purple_value_get_boxed(const PurpleValue
*value
)
484 g_return_val_if_fail(value
!= NULL
, NULL
);
486 return value
->data
.boxed_data
;