| Top |
| #define | GWY_DICT_PATHSEP |
| #define | GWY_DICT_PATHSEP_STR |
| struct | GwyDict |
| struct | GwyDictClass |
GwyDict is a general-purpose dictionary-like data container, it can hold atomic types, strings and objects. However, objects must implement the GwySerializable interface, because the dictionary itself is serializable.
A new dictionary can be created with gwy_dict_new(), items can be stored with function like
gwy_dict_set_double(), read with gwy_dict_get_double(), and removed with gwy_dict_remove() or
gwy_dict_remove_by_prefix(). A presence of a value can be tested with gwy_dict_contains(), convenience
functions for reading (updating) a value only if it is present like gwy_dict_gis_double(), are available too.
GwyDict takes ownership of stored non-atomic items. For strings, this means you cannot store static strings
(use g_strdup() to duplicate them), and must not free stored dynamic strings, as the dictionary will free them
itself when they are removed or when the dictionary is finalized. For objects, this means it takes a reference on
the object (released when the object is removed or the dictionary is finalized), so you usually want to
g_object_unref() objects after storing them to a dictionary.
Items in a GwyDict can be identified by a GQuark or the corresponding string. While GQuark's are atomic
values and allow faster access, they are less convenient for casual usage -- each GQuark key function like
gwy_dict_set_double() thus has a string-key counterpart gwy_dict_set_double_by_name().
void (*GwyDictForeachFunc) (GQuark key,GValue *value,gpointer user_data);
Type of callback function for container for-each operation.
GwyDict *
gwy_dict_new (void);
Creates a new dictionary-like data container.
[constructor]
GwyDict *
gwy_dict_new_in_construction (void);
Creates a new dictionary-like data container and marks it as being in construction.
This is a convenience function calling gwy_dict_start_construction() on the newly created dictionary.
[constructor]
guint
gwy_dict_get_n_items (GwyDict *dict);
Gets the number of items in a dictionary.
GType gwy_dict_value_type (GwyDict *dict,GQuark key);
Returns the type of value in dict
identified by key
.
gboolean gwy_dict_contains (GwyDict *dict,GQuark key);
Returns TRUE if dict
contains a value identified by key
.
GValue gwy_dict_get_value (GwyDict *dict,GQuark key);
Returns the value in dict
identified by key
.
The returned GValue should not be modified since such modification is not detectable and would not emit “item-changed”.
gboolean gwy_dict_gis_value (GwyDict *dict,GQuark key,GValue *value);
Get-if-set a generic value from a dictionary.
On success, value
will contain a copy of the data in dict
.
void gwy_dict_set_value (GwyDict *dict,GQuark key,GValue *value);
Inserts or updates one value in a dictionary.
The value is copied and the caller remains responsible for unsetting value
.
gboolean gwy_dict_remove (GwyDict *dict,GQuark key);
Removes a value identified by key
from a dictionary.
guint gwy_dict_remove_by_prefix (GwyDict *dict,const gchar *prefix);
Removes a values whose key start with prefix
from container dict
.
prefix
can be NULL, all values are then removed.
GwyDict * gwy_dict_duplicate_by_prefix (GwyDict *dict,...);
Duplicates a dictionary keeping only values under given prefixes.
Like gwy_dict_copy(), this method creates a deep copy, that is contained object are physically duplicated
too, not just referenced again.
Signal ::item-changed is not emitted during the construction of the copy (in case you manage to get hold on the returned object before the copying is finished).
GwyDict * gwy_dict_duplicate_by_prefixv (GwyDict *dict,guint n,const gchar **prefixes);
Duplicates a dictionary keeping only values under given prefixes.
Like gwy_dict_copy(), this method creates a deep copy, that is contained object are physically duplicated
too, not just referenced again.
Signal ::item-changed is not emitted during the construction of the copy (in case you manage to get hold on the returned object before the copying is finished).
void gwy_dict_copy_value (GwyDict *source,GwyDict *dest,GQuark srckey,GQuark destkey);
Copies one value from a dictionary to another one.
The source value must exist. If it is an object or boxed, it is copied by value, i.e. the functions always does a deep copy.
gint gwy_dict_transfer (GwyDict *source,GwyDict *dest,const gchar *source_prefix,const gchar *dest_prefix,gboolean deep,gboolean force);
Copies a items from one place in container to another place.
The copies are shallow, objects are not physically duplicated, only referenced in dest
, unless deep
is TRUE.
The order in which items are transferred is not defined. Signals for all changed items are emitted after the transfer is finished (not during transfer).
Source items within source_prefix
are not allowed to change during the transfer. This is mostly relevant when
force
is TRUE and existing destination objects are finalised or deep
is TRUE and new objects are created,
either of which could have cascading effects.
source |
Source container. |
|
dest |
Destination container. It may be the same container as |
|
source_prefix |
Prefix in |
|
dest_prefix |
Prefix in |
|
deep |
|
|
force |
|
gboolean gwy_dict_rename (GwyDict *dict,GQuark key,GQuark newkey,gboolean force);
Makes a value in dict
identified by key
to be identified by newkey
.
When force
is TRUE existing value at newkey
is removed from dict
. When it's FALSE, an existing value
newkey
inhibits the rename and FALSE is returned.
guint gwy_dict_foreach (GwyDict *dict,const gchar *prefix,GwyDictForeachFunc function,gpointer user_data);
Calls a function on each item in a dictionary, possibly limited to a prefix.
The function must not modify the container contents (see g_hash_table_foreach()). Modifying individual items by
changing the contents of a GValue is strongly discouraged because it is not detectable and no
GwyDict::item-changed signal is emitted.
GQuark *
gwy_dict_keys (GwyDict *dict);
Gets all quark keys of a dictionary.
A newly allocated 0-terminated array with quark keys of all dict
items, in no particular order. The
number of items can be obtained with gwy_dict_get_n_items().
[array zero-terminated=1]
const gchar **
gwy_dict_keys_by_name (GwyDict *dict);
Gets all string keys of a dictionary.
A newly allocated array with string keys of all dict
items, in no particular order. The number of
items can be obtained with gwy_dict_get_n_items(). Unlike the array the strings are owned by GLib
and must not be freed.
[transfer container][array zero-terminated=1]
GQuark * gwy_dict_keys_with_prefix (GwyDict *dict,const gchar *prefix,guint *n);
Gets quark keys of a dictionary that start with given prefix.
const gchar ** gwy_dict_keys_with_prefix_by_name (GwyDict *dict,const gchar *prefix,guint *n);
Gets string keys of a dictionary that start with given prefix.
void gwy_dict_set_boolean (GwyDict *dict,GQuark key,gboolean value);
Stores a boolean into dict
, identified by key
.
gboolean gwy_dict_get_boolean (GwyDict *dict,GQuark key);
Returns the boolean in dict
identified by key
.
gboolean gwy_dict_gis_boolean (GwyDict *dict,GQuark key,gboolean *value);
Get-if-set a boolean from a dictionary.
void gwy_dict_set_uchar (GwyDict *dict,GQuark key,guchar value);
Stores an unsigned character into dict
, identified by key
.
guchar gwy_dict_get_uchar (GwyDict *dict,GQuark key);
Returns the unsigned character in dict
identified by key
.
gboolean gwy_dict_gis_uchar (GwyDict *dict,GQuark key,guchar *value);
Get-if-set an unsigned char from a dictionary.
void gwy_dict_set_int32 (GwyDict *dict,GQuark key,gint32 value);
Stores a 32bit integer into dict
, identified by key
.
gint32 gwy_dict_get_int32 (GwyDict *dict,GQuark key);
Returns the 32bit integer in dict
identified by key
.
gboolean gwy_dict_gis_int32 (GwyDict *dict,GQuark key,gint32 *value);
Get-if-set a 32bit integer from a dictionary.
void gwy_dict_set_enum (GwyDict *dict,GQuark key,guint value);
Stores an enum into dict
, identified by key
.
Note enums are treated as 32bit integers.
guint gwy_dict_get_enum (GwyDict *dict,GQuark key);
Returns the enum in dict
identified by key
.
Note enums are treated as 32bit integers.
gboolean gwy_dict_gis_enum (GwyDict *dict,GQuark key,guint *value);
Get-if-set an enum from a dictionary.
Note enums are treated as 32bit integers.
void gwy_dict_set_int64 (GwyDict *dict,GQuark key,gint64 value);
Stores a 64bit integer into dict
, identified by key
.
gint64 gwy_dict_get_int64 (GwyDict *dict,GQuark key);
Returns the 64bit integer in dict
identified by key
.
gboolean gwy_dict_gis_int64 (GwyDict *dict,GQuark key,gint64 *value);
Get-if-set a 64bit integer from a dictionary.
void gwy_dict_set_double (GwyDict *dict,GQuark key,gdouble value);
Stores a double into dict
, identified by key
.
gdouble gwy_dict_get_double (GwyDict *dict,GQuark key);
Returns the double in dict
identified by key
.
gboolean gwy_dict_gis_double (GwyDict *dict,GQuark key,gdouble *value);
Get-if-set a double from a dictionary.
void gwy_dict_set_string (GwyDict *dict,GQuark key,gchar *value);
Stores a string into dict
, identified by key
.
Since the ownership is passed to the container, you must not access the string data afterwards. The container is
allowed to immediately free value
, for example, when it already holds an identical string at key
.
void gwy_dict_set_const_string (GwyDict *dict,GQuark key,const gchar *value);
Stores a string into dict
, identified by key
.
The container makes a copy of the string, so it can be used on static strings.
const gchar * gwy_dict_get_string (GwyDict *dict,GQuark key);
Returns the string in dict
identified by key
.
The returned string must be treated as constant and never freed or modified.
gboolean gwy_dict_gis_string (GwyDict *dict,GQuark key,const gchar **value);
Get-if-set a string from a dictionary.
The string possibly stored in value
must be treated as constant and never freed or modified.
void gwy_dict_set_object (GwyDict *dict,GQuark key,gpointer value);
Stores an object into dict
, identified by key
.
The container claims ownership on the object, i.e. its reference count is incremented. Use
gwy_dict_pass_object() to pass your reference to dict
.
The object must implement GwySerializable interface to allow serialization of the container.
void gwy_dict_pass_object (GwyDict *dict,GQuark key,gpointer value);
Stores an object into dict
, identified by key
, passing reference to the container.
The reference on the object you hold is passed to dict
. This is often useful when importing data as you do
not have to (in fact must not) release your reference afterwards. Use gwy_dict_set_object() if you want to
keep your reference.
The object must implement GwySerializable interface to allow serialization of the container.
gpointer gwy_dict_get_object (GwyDict *dict,GQuark key);
Returns the object in dict
identified by key
.
The returned object does not have its reference count increased, use g_object_ref() if you want to access it even
when dict
may cease to exist.
gboolean gwy_dict_gis_object (GwyDict *dict,GQuark key,gpointer value);
Get-if-set an object from a dictionary.
The object possibly stored in value
doesn't have its reference count increased, use g_object_ref() if you want
to access it even when dict
may cease to exist.
void gwy_dict_set_boxed (GwyDict *dict,GType type,GQuark key,gpointer value);
Stores a boxed pointer into dict
, identified by key
.
The container makes a copy of the boxed pointer using g_boxed_copy(). The caller remains responsible for freeing
it with g_boxed_free().
The boxed type must be serializable to allow serialization of the container.
void gwy_dict_pass_boxed (GwyDict *dict,GType type,GQuark key,gpointer value);
Stores a boxed pointer into dict
, identified by key
, passing ownership to the container.
Since the ownership is passed to the container, you must not access the boxed data afterwards. The container is
allowed to immediately free value
using g_boxed_free(), for example, when it already holds an identical value at
key
. In general, since boxed pointers are not reference counted, passing the ownership is prone to subtle errors.
Unless you created the boxed pointer in order to just pass it to dict
and forget about it, it is safer to use
gwy_dict_set_boxed().
The boxed type must be serializable to allow serialization of the container.
gpointer gwy_dict_get_boxed (GwyDict *dict,GType type,GQuark key);
Returns the boxed pointer in dict
identified by key
.
It is an error to request a boxed pointer of different type than is actually stored.
gboolean gwy_dict_gis_boxed (GwyDict *dict,GType type,GQuark key,gpointer value);
Get-if-set a boxed pointer from a dictionary.
It is an error to request a boxed pointer of different type than is actually stored.
The boxed pointer is copied to value
by value, which is always possbile as only serialisable boxed types can be
stored in a GwyDict. The value
pointer thus does not have the extra level of indirection objects and strings
have:
1 2 |
GwyXYZ xyz; gwy_dict_gis_boxed(dict, GWY_TYPE_XYZ, quark, &xyz); |
void
gwy_dict_start_construction (GwyDict *dict);
Marks a dictionary as being initially constructed.
It is an error to call the function on dict
which is not empty. It is an error to call it when dict
is
already marked as being constructed. It is undefined behaviour to start and finish the construction multiple times,
even if dict
remains empty.
When a dictionary is constructed by internal means, for instance during deserialisation or
gwy_dict_copy(), it does not emit any signals, in particular ::item-changed. However, data containers are also
frequntly constructed for instance to represent files in file import modules, where emitting signals is pointless
and slows down the import (more substantially for subclasses like GwyFile which respond to changed items). You can
use this function to prevent signal emission in such case.
Remember to call gwy_dict_finish_construction() when you are done. A GwyDict marked as still being in
construction can be considered an invalid object.
void
gwy_dict_finish_construction (GwyDict *dict);
Marks the construction of a dictionary as finished.
The data container must be marked as being constructed with gwy_dict_start_construction(). The call may result
in a subclass or other user scanning the contents. See gwy_dict_start_construction() for more discussion.
gboolean
gwy_dict_is_being_constructed (GwyDict *dict);
Queries if a dictionary is being initially constructed.
GwyDict *
gwy_dict_copy (GwyDict *dict);
Create a new container as a copy of an existing one.
This function is a convenience gwy_serializable_copy() wrapper.
Signal ::item-changed is not emitted during the construction of the copy (in case you manage to get hold on the
returned object before the copying is finished). The new object is not marked as being in construction, regardless
of the state of dict
.
void gwy_dict_assign (GwyDict *destination,GwyDict *source);
Makes one container equal to another.
This function is a convenience gwy_serializable_assign() wrapper.
Signal ::item-changed is emitted as usual, making the operation possibly relatively expensive. The in-construction state of either object is unchanged.
GPtrArray *
gwy_dict_serialize_to_text (GwyDict *dict);
Creates a text representation of dict
contents.
Note only simple data types are supported as serialization of compound objects is not controllable.
GwyDict *
gwy_dict_deserialize_from_text (const gchar *text);
Restores a dictionary from is text representation.
Signal ::item-changed is not emitted during the construction (in case you manage to get hold on the returned object before the deserialisation is finished).
text |
Text containing serialized container contents as dumped by |
#define gwy_dict_value_type_by_name(dict,name) gwy_dict_value_type(dict,g_quark_try_string(name))
Gets the type of value in container dict
identified by name name
.
#define gwy_dict_contains_by_name(dict,name) gwy_dict_contains(dict,g_quark_try_string(name))
Expands to TRUE if container dict
contains a value identified by name name
.
#define gwy_dict_set_value_by_name(dict,name,value) gwy_dict_set_value(dict,g_quark_from_string(name),value)
Inserts or updates one value in a dictionary.
The value is copied and the caller remains responsible for unsetting value
.
#define gwy_dict_get_value_by_name(dict,name) gwy_dict_get_value(dict,g_quark_from_string(name))
Gets the value in container dict
identified by name name
.
The returned GValue should not be modified since such modification is not detectable and would not emit “item-changed”.
#define gwy_dict_gis_value_by_name(dict,name,value) gwy_dict_gis_value(dict,g_quark_try_string(name),value)
Get-if-set a generic value from a dictionary.
On success, value
will contain a copy of the data in dict
.
Expands to TRUE if value
was actually updated, FALSE when there is no such value in the container.
#define gwy_dict_copy_value_by_name(dict1,dict2,name1,name2) gwy_dict_copy_value(dict1,dict2,g_quark_try_string(name1),g_quark_try_string(name2))
Copies one value identified by a string key from one container to another one.
The source value must exist. If it is an object or boxed, it is copied by value, i.e. the functions always does a deep copy.
#define gwy_dict_remove_by_name(dict,name) gwy_dict_remove(dict,g_quark_try_string(name))
Removes a value identified by name name
from container dict
.
Expands to TRUE if there was such a value and was removed.
#define gwy_dict_rename_by_name(dict,name,nn,f) gwy_dict_rename(dict,g_quark_try_string(name),g_quark_from_string(nn),f)
Makes a value in container dict
identified by name name
to be identified by new name nn
.
See gwy_dict_rename() for details.
#define gwy_dict_set_boolean_by_name(dict,name,value) gwy_dict_set_boolean(dict,g_quark_from_string(name),value)
Stores a boolean into container dict
, identified by name name
.
#define gwy_dict_get_boolean_by_name(dict,name) gwy_dict_get_boolean(dict,g_quark_from_string(name))
Gets the boolean in container dict
identified by name name
.
#define gwy_dict_gis_boolean_by_name(dict,name,value) gwy_dict_gis_boolean(dict,g_quark_try_string(name),value)
Get-if-set a boolean from a dictionary.
Expands to TRUE if value
was actually updated, FALSE when there is no such boolean in the container.
#define gwy_dict_set_uchar_by_name(dict,name,value) gwy_dict_set_uchar(dict,g_quark_from_string(name),value)
Stores an unsigned character into container dict
, identified by name name
.
#define gwy_dict_get_uchar_by_name(dict,name) gwy_dict_get_uchar(dict,g_quark_from_string(name))
Gets the unsigned character in container dict
identified by name name
.
#define gwy_dict_gis_uchar_by_name(dict,name,value) gwy_dict_gis_uchar(dict,g_quark_try_string(name),value)
Get-if-set an unsigned char from a dictionary.
Expands to TRUE if value
was actually updated, FALSE when there is no such unsigned char in the container.
#define gwy_dict_set_int32_by_name(dict,name,value) gwy_dict_set_int32(dict,g_quark_from_string(name),value)
Stores a 32bit integer into container dict
, identified by name name
.
#define gwy_dict_get_int32_by_name(dict,name) gwy_dict_get_int32(dict,g_quark_from_string(name))
Gets the 32bit integer in container dict
identified by name name
.
#define gwy_dict_gis_int32_by_name(dict,name,value) gwy_dict_gis_int32(dict,g_quark_try_string(name),value)
Get-if-set a 32bit integer from a dictionary.
Expands to TRUE if value
was actually updated, FALSE when there is no such 32bit integer in the container.
#define gwy_dict_set_enum_by_name(dict,name,value) gwy_dict_set_enum(dict,g_quark_from_string(name),value)
Stores an enum into container dict
, identified by name name
.
Note enums are treated as 32bit integers.
#define gwy_dict_get_enum_by_name(dict,name) gwy_dict_get_enum(dict,g_quark_from_string(name))
Gets the enum in container dict
identified by name name
.
Note enums are treated as 32bit integers.
#define gwy_dict_gis_enum_by_name(dict,name,value) gwy_dict_gis_enum(dict,g_quark_try_string(name),value)
Get-if-set an enum from a dictionary.
Note enums are treated as 32bit integers.
Expands to TRUE if value
was actually updated, FALSE when there is no such enum in the container.
#define gwy_dict_set_int64_by_name(dict,name,value) gwy_dict_set_int64(dict,g_quark_from_string(name),value)
Stores a 64bit integer into container dict
, identified by name name
.
#define gwy_dict_get_int64_by_name(dict,name) gwy_dict_get_int64(dict,g_quark_from_string(name))
Gets the 64bit integer in container dict
identified by name name
.
#define gwy_dict_gis_int64_by_name(dict,name,value) gwy_dict_gis_int64(dict,g_quark_try_string(name),value)
Get-if-set a 64bit integer from a dictionary.
Expands to TRUE if value
was actually updated, FALSE when there is no such 64bit integer in the container.
#define gwy_dict_set_double_by_name(dict,name,value) gwy_dict_set_double(dict,g_quark_from_string(name),value)
Stores a double into container dict
, identified by name name
.
#define gwy_dict_get_double_by_name(dict,name) gwy_dict_get_double(dict,g_quark_from_string(name))
Gets the double in container dict
identified by name name
.
#define gwy_dict_gis_double_by_name(dict,name,value) gwy_dict_gis_double(dict,g_quark_try_string(name),value)
Get-if-set a double from a dictionary.
Expands to TRUE if value
was actually updated, FALSE when there is no such double in the container.
#define gwy_dict_set_string_by_name(dict,name,value) gwy_dict_set_string(dict,g_quark_from_string(name),value)
Stores a string into container dict
, identified by name name
.
See gwy_dict_set_string() for details.
#define gwy_dict_set_const_string_by_name(dict,name,value) gwy_dict_set_const_string(dict,g_quark_from_string(name),value)
Stores a string into container dict
, identified by name name
.
The container makes a copy of the string, so it can be used on static strings.
#define gwy_dict_get_string_by_name(dict,name) gwy_dict_get_string(dict,g_quark_from_string(name))
Gets the string in container dict
identified by name name
.
The returned string must be treated as constant and never freed or modified.
#define gwy_dict_gis_string_by_name(dict,name,value) gwy_dict_gis_string(dict,g_quark_try_string(name),value)
Get-if-set a string from a dictionary.
The string possibly stored in value
must be treated as constant and never freed or modified.
Expands to TRUE if value
was actually updated, FALSE when there is no such string in the container.
#define gwy_dict_set_object_by_name(dict,name,value) gwy_dict_set_object(dict,g_quark_from_string(name),value)
Stores an object into container dict
, identified by name name
.
See gwy_dict_set_object() for details.
#define gwy_dict_pass_object_by_name(dict,name,value) gwy_dict_pass_object(dict,g_quark_from_string(name),value)
Stores an object into container dict
, identified by name name
, passing reference to the container.
See gwy_dict_pass_object() for details.
#define gwy_dict_get_object_by_name(dict,name) gwy_dict_get_object(dict,g_quark_from_string(name))
Gets the object in container dict
identified by name name
.
The returned object does not have its reference count increased, use g_object_ref() if you want to access it even
when dict
may cease to exist.
#define gwy_dict_gis_object_by_name(dict,name,value) gwy_dict_gis_object(dict,g_quark_try_string(name),value)
Get-if-set an object from a dictionary.
The object possibly stored in value
doesn't have its reference count increased, use g_object_ref() if you want
to access it even when dict
may cease to exist.
Expands to TRUE if value
was actually updated, FALSE when there is no such object in the container.
#define gwy_dict_set_boxed_by_name(dict,type,name,value) gwy_dict_set_boxed(dict,type,g_quark_from_string(name),value)
Stores a boxed pointer into container dict
, identified by name name
.
See gwy_dict_set_boxed() for details.
#define gwy_dict_pass_boxed_by_name(dict,type,name,value) gwy_dict_pass_boxed(dict,type,g_quark_from_string(name),value)
Stores an boxed pointer into container dict
, identified by name name
, passing ownership to the container.
See gwy_dict_pass_boxed() for details.
#define gwy_dict_get_boxed_by_name(dict,type,name) gwy_dict_get_boxed(dict,type,g_quark_from_string(name))
Gets the boxed pointer in container dict
identified by name name
.
See gwy_dict_get_boxed().
#define gwy_dict_gis_boxed_by_name(dict,type,name,value) gwy_dict_gis_boxed(dict,type,g_quark_try_string(name),value)
Get-if-set a boxed pointer from a dictionary.
It is an error to request a boxed pointer of different type than is actually stored.
The boxed pointer is copied to value
by value, which is always possbile as only serialisable boxed types can be
stored in a GwyDict. The value
pointer thus does not have the extra level of indirection objects and strings
have:
1 2 |
GwyXYZ xyz; gwy_dict_gis_boxed_by_name(dict, GWY_TYPE_XYZ, "/some/name", &xyz); |
Expands to TRUE if value
was actually updated, FALSE when there is no such boxed in the container.
#define GWY_DICT_PATHSEP '/'
Path separator to be used for hierarchical structures in the container.
#define GWY_DICT_PATHSEP_STR "/"
Path separator to be used for hierarchical structures in the container, as a string.
struct GwyDict;
The GwyDict struct contains private data only and should be accessed using the functions below.
“item-changed” signalvoid user_function (GwyDict *gwydict, guint arg1, gpointer user_data)
The ::item-changed signal is emitted whenever a dictionary item is changed. The detail is the string key identifier.
gwydict |
The GwyDict which received the signal. |
|
arg1 |
The quark key identifying the changed item. |
|
user_data |
user data set when the signal handler was connected. |
Flags: Has Details