Method

GObjectObjectreplace_data

since: 2.34

Declaration

gboolean
g_object_replace_data (
  GObject* object,
  const gchar* key,
  gpointer oldval,
  gpointer newval,
  GDestroyNotify destroy,
  GDestroyNotify* old_destroy
)

Description

Compares the user data for the key key on object with oldval, and if they are the same, replaces oldval with newval.

This is like a typical atomic compare-and-exchange operation, for user data on an object.

If the previous value was replaced then ownership of the old value (oldval) is passed to the caller, including the registered destroy notify for it (passed out in old_destroy). It’s up to the caller to free this as needed, which may or may not include using old_destroy as sometimes replacement should not destroy the object in the normal way.

See g_object_set_data() for guidance on using a small, bounded set of values for key.

Available since: 2.34

This method is not directly available to language bindings.

Parameters

key

Type: const gchar*

A string, naming the user data pointer.

The data is owned by the caller of the function.
The value is a NUL terminated UTF-8 string.
oldval

Type: gpointer

The old value to compare against.

The argument can be NULL.
The data is owned by the caller of the function.
newval

Type: gpointer

The new value.

The argument can be NULL.
The data is owned by the caller of the function.
destroy

Type: GDestroyNotify

A destroy notify for the new value.

The argument can be NULL.
old_destroy

Type: GDestroyNotify

Destroy notify for the existing value.

The argument will be set by the function.
The argument can be NULL.
The data is owned by the caller of the function.

Return value

Type: gboolean

TRUE if the existing value for key was replaced by newval, FALSE otherwise.