Top |
A generic value manager.
Dynamically allocated CtplValue are created with ctpl_value_new()
and freed
with ctpl_value_free()
.
Statically allocated ones are initialized with ctpl_value_init()
and
uninitialized with ctpl_value_free_value()
.
You can set the data they holds with ctpl_value_set_int()
,
ctpl_value_set_float()
, ctpl_value_set_string()
and ctpl_value_set_array()
;
you can add elements to an array value with ctpl_value_array_append()
,
ctpl_value_array_prepend()
, ctpl_value_array_append_int()
,
ctpl_value_array_prepend_int()
, ctpl_value_array_append_float()
,
ctpl_value_array_prepend_float()
, ctpl_value_array_append_string()
and
ctpl_value_array_prepend_string()
.
To get the value held by a CtplValue, use ctpl_value_get_int()
,
ctpl_value_get_float()
, ctpl_value_get_string()
, ctpl_value_get_array_int()
,
ctpl_value_get_array_float()
or ctpl_value_get_array_string()
depending on
the type of the value.
For array value, yo can also use ctpl_value_get_array()
to get the list of
the different values in that array.
You can get the type held by a value with ctpl_value_get_held_type()
.
Value may be converted to other types with ctpl_value_convert()
, and to a
string representation using ctpl_value_to_string()
.
Example 8. Simple usage of dynamically allocated generic values
1 2 3 4 5 6 7 |
CtplValue *val; val = ctpl_value_new (); ctpl_value_set_int (val, 42); /* Free all data allocated for the value and the held data */ ctpl_value_free (val); |
Example 9. Simple usage of statically allocated generic values
1 2 3 4 5 6 7 8 |
CtplValue val; ctpl_value_init (&val); ctpl_value_set_int (&val, 42); /* Free all memory that might have been allocated for the held * data */ ctpl_value_free_value (&val); |
#define CTPL_VALUE_HOLDS(value, vtype)
Checks whether a CtplValue holds a value of the given type.
#define CTPL_VALUE_HOLDS_INT(value)
Check whether a CtplValue holds an integer value.
#define CTPL_VALUE_HOLDS_FLOAT(value)
Check whether a CtplValue holds a floating point value.
#define CTPL_VALUE_HOLDS_STRING(value)
Check whether a CtplValue holds a string.
#define CTPL_VALUE_HOLDS_ARRAY(value)
Check whether a CtplValue holds an array of values.
void
ctpl_value_init (CtplValue *value
);
Initializes a CtplValue.
This function is useful for statically allocated values, and is not required
for dynamically allocated values created by ctpl_value_new()
.
void ctpl_value_copy (const CtplValue *src_value
,CtplValue *dst_value
);
Copies the value of a CtplValue into another.
See ctpl_value_dup()
if you want to duplicate the value and not only its
content.
CtplValue *
ctpl_value_dup (const CtplValue *value
);
Duplicates a CtplValue.
This function simply creates a new CtplValue with ctpl_value_new()
then
copies value
into it using ctpl_value_copy()
.
void
ctpl_value_free_value (CtplValue *value
);
Frees the data held by a CtplValue.
This function is only useful to the end user for statically allocated values
since ctpl_value_free()
does all the job needed to completely release an
allocated CtplValue.
void
ctpl_value_free (CtplValue *value
);
Frees all resources used by a CtplValue.
This function can't be used with statically allocated values since it also
frees the value itself and not only its content. If you want to free a
statically allocated value, use ctpl_value_free_value()
.
CtplValue *
ctpl_value_new_int (glong val
);
Creates a new CtplValue and sets its value to val
.
See ctpl_value_new()
and ctpl_value_set_int()
.
CtplValue *
ctpl_value_new_float (gdouble val
);
Creates a new CtplValue and sets its value to val
.
See ctpl_value_new()
and ctpl_value_set_float()
.
CtplValue *
ctpl_value_new_string (const gchar *val
);
Creates a new CtplValue and sets its value to val
.
See ctpl_value_new()
and ctpl_value_set_string()
.
CtplValue * ctpl_value_new_arrayv (CtplValueType type
,gsize count
,va_list ap
);
Creates a new CtplValue and sets its values to the given ones.
See ctpl_value_new()
and ctpl_value_set_arrayv()
.
As this function takes a variadic argument, there is no control on the values neither on their type nor on any other of their properties. Then, you have to take care to pass strictly right data to it if you won't see your program crash -- in the better case.
type |
The type of the array's elements |
|
count |
The number of elements |
|
ap |
A va_list containing the values of the type specified by |
CtplValue * ctpl_value_new_array (CtplValueType type
,gsize count
,...
);
Creates a new CtplValue and sets its values to the given ones.
See ctpl_value_new_arrayv()
.
As this function takes a variadic argument, there is no control on the values neither on their type nor on any other of their properties. Then, you have to take care to pass strictly right data to it if you won't see your program crash -- in the better case.
type |
The type of the array's elements |
|
count |
The number of elements |
|
... |
A |
void ctpl_value_set_int (CtplValue *value
,glong val
);
Sets the value of a CtplValue to the given integer.
void ctpl_value_set_float (CtplValue *value
,gdouble val
);
Sets the value of a CtplValue to the given float.
void ctpl_value_set_string (CtplValue *value
,const gchar *val
);
Sets the value of a CtplValue to the given string. The string is copied.
void ctpl_value_set_arrayv (CtplValue *value
,CtplValueType type
,gsize count
,va_list ap
);
Sets the value of a CtplValue from the given list of elements.
See ctpl_value_array_append()
, ctpl_value_array_append_int()
,
ctpl_value_array_append_float()
and ctpl_value_array_append_string()
.
As this function takes a variadic argument, there is no control on the values neither on their type nor on any other of their properties. Then, you have to take care to pass strictly right data to it if you won't see your program crash -- in the better case.
void ctpl_value_set_array (CtplValue *value
,CtplValueType type
,gsize count
,...
);
Sets the value of a CtplValue from the given elements.
See ctpl_value_set_arrayv()
.
As this function takes a variadic argument, there is no control on the values neither on their type nor on any other of their properties. Then, you have to take care to pass strictly right data to it if you won't see your program crash -- in the better case.
void ctpl_value_set_array_intv (CtplValue *value
,gsize count
,va_list ap
);
Sets the value of a CtplValue from the given integers.
This is a convenience wrapper around ctpl_value_set_arrayv()
, and the same
care have to been taken about.
void ctpl_value_set_array_int (CtplValue *value
,gsize count
,...
);
Sets the value of a CtplValue from the given integers.
This is a convenience wrapper around ctpl_value_set_array()
, and the same
care have to been taken about.
void ctpl_value_set_array_floatv (CtplValue *value
,gsize count
,va_list ap
);
Sets the value of a CtplValue from the given floats.
This is a convenience wrapper around ctpl_value_set_arrayv()
, and the same
care have to been taken about.
void ctpl_value_set_array_float (CtplValue *value
,gsize count
,...
);
Sets the value of a CtplValue from the given floats.
This is a convenience wrapper around ctpl_value_set_array()
, and the same
care have to been taken about.
void ctpl_value_set_array_stringv (CtplValue *value
,gsize count
,va_list ap
);
Sets the value of a CtplValue from the given strings.
This is a convenience wrapper around ctpl_value_set_arrayv()
, and the same
care have to been taken about.
void ctpl_value_set_array_string (CtplValue *value
,gsize count
,...
);
Sets the value of a CtplValue from the given strings.
This is a convenience wrapper around ctpl_value_set_array()
, and the same
care have to been taken about.
void ctpl_value_array_append (CtplValue *value
,const CtplValue *val
);
Appends a CtplValue to another CtplValue holding an array. The appended value is copied.
void ctpl_value_array_prepend (CtplValue *value
,const CtplValue *val
);
Prepends a CtplValue to another CtplValue holding an array. The prepended value is copied.
void ctpl_value_array_append_int (CtplValue *value
,glong val
);
Appends an integer to a CtplValue holding an array.
void ctpl_value_array_prepend_int (CtplValue *value
,glong val
);
Prepends an integer to a CtplValue holding an array.
void ctpl_value_array_append_float (CtplValue *value
,gdouble val
);
Appends a float to a CtplValue holding an array.
void ctpl_value_array_prepend_float (CtplValue *value
,gdouble val
);
Prepends a float to a CtplValue holding an array.
void ctpl_value_array_append_string (CtplValue *value
,const gchar *val
);
Appends a string to a CtplValue holding an array. The string is copied.
void ctpl_value_array_prepend_string (CtplValue *value
,const gchar *val
);
Prepends a string to a CtplValue holding an array. The string is copied.
gsize
ctpl_value_array_length (const CtplValue *value
);
Gets the number of elements in a CtplValue that holds an array.
CtplValue * ctpl_value_array_index (const CtplValue *value
,gsize idx
);
Index an array, getting its idx
-th element.
CtplValueType
ctpl_value_get_held_type (const CtplValue *value
);
Gets the type held by the a CtplValue.
glong
ctpl_value_get_int (const CtplValue *value
);
Gets the value of a CtplValue holding a integer.
gdouble
ctpl_value_get_float (const CtplValue *value
);
Gets the value of a CtplValue holding a float.
const gchar *
ctpl_value_get_string (const CtplValue *value
);
Gets the value of a CtplValue holding a string.
A string owned by the value that should not be modified or freed, or
NULL
if an error occurs.
const GSList *
ctpl_value_get_array (const CtplValue *value
);
Gets the values of a CtplValue holding an array as a GSList in which each element holds a CtplValue holding the element value.
glong * ctpl_value_get_array_int (const CtplValue *value
,gsize *length
);
Gets the values of a CtplValue as an array of int. The value must hold an array and all array's elements must be integers.
gdouble * ctpl_value_get_array_float (const CtplValue *value
,gsize *length
);
Gets the values of a CtplValue as an array of floats.
value
must hold an array and all array's elements must be floats.
gchar ** ctpl_value_get_array_string (const CtplValue *value
,gsize *length
);
Gets the values held by a CtplValue as an array of strings.
value
must hold an array containing only strings.
A newly allocated
NULL
-terminated array of strings, or NULL
on error. Free with
g_strfreev()
when no longer needed.
[array length=length][transfer full]
gchar *
ctpl_value_to_string (const CtplValue *value
);
Converts a CtplValue to a string.
Arrays are flattened to the form [val1, val2, val3]. It may not be what you want, but flattening an array is not the primary goal of this function and you should consider doing it yourself if it is what you want - flattening an array.
A newly allocated string representing the value. You should free
this value with g_free()
when no longer needed.
gboolean ctpl_value_convert (CtplValue *value
,CtplValueType vtype
);
Tries to convert a CtplValue to another type.
The performed conversion might be called "non-destructive": the value will not loose precision, but the conversion will rather fail if it would lead to a loss. An good example is converting a floating-point value to an integer one: the conversion will only happen if it would not truncate the floating part.
The current implementation of floating-point value comparison might be lossy, and then the above example might be somewhat wrong in practice.
Converting to a string uses ctpl_value_to_string()
.
Even if it will never fail, the result might not be the one you expect
when converting an array.
const gchar *
ctpl_value_type_get_name (CtplValueType type
);
Gets a human-readable name for a value type.
#define ctpl_value_get_held_type_name(v)
Gets a human-readable name for the type held by a value.
See also ctpl_value_type_get_name()
.