ModelReferenceHelper

ModelReferenceHelper — a way to control object life-cycle when using ModelReference objects

Description

When using ModelSimpleReference to create ModelReference objects, a problem often arises. The part of the model ("the source object") that is responsible for monitoring the data source and updating the ModelReference holds a strong reference on the ModelSimpleReference. This means that the ModelReference can not hold a strong reference on the source object and that, once remaining outside references to the source object are dropped, it will be finalized. This finalization occurs even if there are users of the model still monitoring the ModelReference for changes -- changes that will now no longer be delivered.

ModelReferenceHelper introduces a 3rd object to resolve this situation. The reference helper (blue in the diagram) is held (by strong reference) by the source object (orange). The reference helper has only a weak reference to the source object. Just like a ModelSimpleReference, the reference helper keeps a strong reference to the value of the reference (green). When model_reference_helper_get_reference() is called, a ModelReference (ping) is created that holds a strong reference to the source object. The source object is kept alive to update the reference for as long as the reference exists.

The core API of ModelReferenceHelper consists of model_reference_helper_new(), model_reference_helper_set() and model_reference_helper_get_reference(). Additionally, there are helper functions to set and construct from one of the basic value types without requiring the manual construction of a corresponding ModelObject instance.

Details

ModelReferenceHelper

typedef struct _ModelReferenceHelper ModelReferenceHelper;

This is an opaque structure; it may not be accessed directly.


model_reference_helper_get_reference ()

ModelReference *    model_reference_helper_get_reference
                                                        (ModelReferenceHelper *helper);

Creates and returns a ModelReference object for the value stored in helper. Any ModelReference created with this function will have its "changed" signal emitted when model_reference_helper_set() is called on helper.

This function is not thread-safe with respect to g_object_unref() being called in other threads. See bug 548954 for why.

helper :

a ModelReferenceHelper Returns: a ModelReference

model_reference_helper_new ()

ModelReferenceHelper * model_reference_helper_new       (GObject *owner,
                                                         ModelObject *value);

Creates a new model reference helper for use with owner. The initial value of the helper is value.

owner :

the "owner" GObject

value :

the ModelObject with the initial value

returns :

a new ModelReferenceHelper

model_reference_helper_set ()

void                model_reference_helper_set          (ModelReferenceHelper *helper,
                                                         ModelObject *value);

Changes the value held by helper to value.

If the helper has outstanding references (as returned by model_reference_helper_get_reference()) then the "changed" signal will be emitted on them.

helper :

a ModelReferenceHelper

value :

the ModelObject to use as the new value, or NULL

model_reference_helper_new_string ()

ModelReferenceHelper * model_reference_helper_new_string
                                                        (GObject *owner,
                                                         const gchar *value);

Creates a new ModelReferenceHelper holding a string.

This is a convenience wrapper around model_reference_helper_new() and model_string_new().

owner :

the "owner" GObject

value :

a string

returns :

a new ModelReferenceHelper

model_reference_helper_new_integer ()

ModelReferenceHelper * model_reference_helper_new_integer
                                                        (GObject *owner,
                                                         gint value);

Creates a new ModelReferenceHelper holding an integer.

This is a convenience wrapper around model_reference_helper_new() and model_integer_new().

owner :

the "owner" GObject

value :

an integer

returns :

a new ModelReferenceHelper

model_reference_helper_new_float ()

ModelReferenceHelper * model_reference_helper_new_float (GObject *owner,
                                                         gdouble value);

Creates a new ModelReferenceHelper holding a floating point value.

This is a convenience wrapper around model_reference_helper_new() and model_float_new().

owner :

the "owner" GObject

value :

a double precision floating point value

returns :

a new ModelReferenceHelper

model_reference_helper_new_boolean ()

ModelReferenceHelper * model_reference_helper_new_boolean
                                                        (GObject *owner,
                                                         gboolean value);

Creates a new ModelReferenceHelper holding a boolean value.

This is a convenience wrapper around model_reference_helper_new() and model_boolean_new().

owner :

the "owner" GObject

value :

a boolean value

returns :

a new ModelReferenceHelper

model_reference_helper_set_string ()

void                model_reference_helper_set_string   (ModelReferenceHelper *helper,
                                                         const gchar *value);

Updates the value of helper to the given string.

This is a convenience wrapper around model_reference_helper_set() and model_string_new().

helper :

a ModelReferenceHelper

value :

a string

model_reference_helper_set_integer ()

void                model_reference_helper_set_integer  (ModelReferenceHelper *helper,
                                                         gint value);

Updates the value of helper to the given integer.

This is a convenience wrapper around model_reference_helper_set() and model_integer_new().

helper :

a ModelReferenceHelper

value :

an integer

model_reference_helper_set_float ()

void                model_reference_helper_set_float    (ModelReferenceHelper *helper,
                                                         gdouble value);

Updates the value of helper to the given floating point value.

This is a convenience wrapper around model_reference_helper_set() and model_float_new().

helper :

a ModelReferenceHelper

value :

a double precision floating point value

model_reference_helper_set_boolean ()

void                model_reference_helper_set_boolean  (ModelReferenceHelper *helper,
                                                         gboolean value);

Updates the value of helper to the given boolean value.

This is a convenience wrapper around model_reference_helper_set() and model_boolean_new().

helper :

a ModelReferenceHelper

value :

a boolean value