## Closures

Closures — Functions as first-class objects

## Synopsis

    #include <gobject.h>

    #define     G_CLOSURE_NEEDS_MARSHAL         (closure)
    #define     G_CLOSURE_N_NOTIFIERS           (cl)
    #define     G_CCLOSURE_SWAP_DATA            (cclosure)
    #define     G_CALLBACK                      (f)
    void        (*GCallback)                    (void);
    struct      GClosure;
    #define     G_TYPE_CLOSURE
    struct      GCClosure;
    void        (*GClosureMarshal)              (GClosure *closure,
                                                 GValue *return_value,
                                                 guint n_param_values,
                                                 const GValue *param_values,
                                                 gpointer invocation_hint,
                                                 gpointer marshal_data);
    void        (*GClosureNotify)               (gpointer data,
                                                 GClosure *closure);
    struct      GClosureNotifyData;
    GClosure*   g_cclosure_new                  (GCallback callback_func,
                                                 gpointer user_data,
                                                 GClosureNotify destroy_data);
    GClosure*   g_cclosure_new_swap             (GCallback callback_func,
                                                 gpointer user_data,
                                                 GClosureNotify destroy_data);
    GClosure*   g_cclosure_new_object           (GCallback callback_func,
                                                 GObject *object);
    GClosure*   g_cclosure_new_object_swap      (GCallback callback_func,
                                                 GObject *object);
    GClosure*   g_closure_new_object            (guint sizeof_closure,
                                                 GObject *object);
    GClosure*   g_closure_ref                   (GClosure *closure);
    void        g_closure_sink                  (GClosure *closure);
    void        g_closure_unref                 (GClosure *closure);
    void        g_closure_invoke                (GClosure *closure,
                                                 GValue *return_value,
                                                 guint n_param_values,
                                                 const GValue *param_values,
                                                 gpointer invocation_hint);
    void        g_closure_invalidate            (GClosure *closure);
    void        g_closure_add_finalize_notifier (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);
    void        g_closure_add_invalidate_notifier
                                                (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);
    void        g_closure_remove_finalize_notifier
                                                (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);
    void        g_closure_remove_invalidate_notifier
                                                (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);
    GClosure*   g_closure_new_simple            (guint sizeof_closure,
                                                 gpointer data);
    void        g_closure_set_marshal           (GClosure *closure,
                                                 GClosureMarshal marshal);
    void        g_closure_add_marshal_guards    (GClosure *closure,
                                                 gpointer pre_marshal_data,
                                                 GClosureNotify pre_marshal_notify,
                                                 gpointer post_marshal_data,
                                                 GClosureNotify post_marshal_notify);
    void        g_closure_set_meta_marshal      (GClosure *closure,
                                                 gpointer marshal_data,
                                                 GClosureMarshal meta_marshal);
    void        g_source_set_closure            (GSource *source,
                                                 GClosure *closure);
    #define     G_TYPE_IO_CHANNEL
    #define     G_TYPE_IO_CONDITION

## Description

## Details

### G_CLOSURE_NEEDS_MARSHAL()

    #define	G_CLOSURE_NEEDS_MARSHAL(closure) (((GClosure*) (closure))->marshal == NULL)

`closure` : |

### G_CLOSURE_N_NOTIFIERS()

    #define     G_CLOSURE_N_NOTIFIERS(cl)

`cl` : |

### G_CCLOSURE_SWAP_DATA()

    #define	G_CCLOSURE_SWAP_DATA(cclosure)	 (((GClosure*) (closure))->derivative_flag)

`cclosure` : |

### G_CALLBACK()

    #define	G_CALLBACK(f)			 ((GCallback) (f))

`f` : |

### GCallback ()

    void        (*GCallback)                    (void);

### struct GClosure

    struct GClosure {

    };

guint `in_marshal` : 1 |
guint `is_invalid` : 1 | Indicates whether the closure has been invalidated by `g_closure_invalidate()`

### G_TYPE_CLOSURE

    #define	G_TYPE_CLOSURE		(g_closure_get_type ())

The GType for GClosure.

### struct GCClosure

    struct GCClosure {

      GClosure	closure;
      gpointer	callback;
    };

### GClosureMarshal ()

    void        (*GClosureMarshal)              (GClosure *closure,
                                                 GValue *return_value,
                                                 guint n_param_values,
                                                 const GValue *param_values,
                                                 gpointer invocation_hint,
                                                 gpointer marshal_data);

`closure` : |
`return_value` : |
`n_param_values` : |
`param_values` : |
`invocation_hint` : |
`marshal_data` : |

### GClosureNotify ()

    void        (*GClosureNotify)               (gpointer data,
                                                 GClosure *closure);

`data` : |
`closure` : |

### struct GClosureNotifyData

    struct GClosureNotifyData {

      gpointer       data;
      GClosureNotify notify;
    };

### g_cclosure_new ()

    GClosure*   g_cclosure_new                  (GCallback callback_func,
                                                 gpointer user_data,
                                                 GClosureNotify destroy_data);

`callback_func` : |
`user_data` : |
`destroy_data` : |
Returns : |

### g_cclosure_new_swap ()

    GClosure*   g_cclosure_new_swap             (GCallback callback_func,
                                                 gpointer user_data,
                                                 GClosureNotify destroy_data);

`callback_func` : |
`user_data` : |
`destroy_data` : |
Returns : |

### g_cclosure_new_object ()

    GClosure*   g_cclosure_new_object           (GCallback callback_func,
                                                 GObject *object);

`callback_func` : |
`object` : |
Returns : |

### g_cclosure_new_object_swap ()

    GClosure*   g_cclosure_new_object_swap      (GCallback callback_func,
                                                 GObject *object);

`callback_func` : |
`object` : |
Returns : |

### g_closure_new_object ()

    GClosure*   g_closure_new_object            (guint sizeof_closure,
                                                 GObject *object);

`sizeof_closure` : |
`object` : |
Returns : |

### g_closure_ref ()

    GClosure*   g_closure_ref                   (GClosure *closure);

Increment the reference count on a closure to force it staying alive while the caller holds a pointer to it.

`closure` : | GClosure to increment the reference count on
Returns : | The `closure` passed in, for convenience

### g_closure_sink ()

    void        g_closure_sink                  (GClosure *closure);

Take over the initial ownership of a closure. When closures are newly created, they get an initial reference count of 1, eventhough no caller has yet invoked `g_closure_ref()` on the `closure`. Code entities that store closures for notification purposes are supposed to call this function, for example like this:

    static GClosure *notify_closure = NULL;
    void
    foo_notify_set_closure (GClosure *closure)
    {
      if (notify_closure)
        g_closure_unref (notify_closure);
      notify_closure = closure;
      if (notify_closure)
        {
          g_closure_ref (notify_closure);
          g_closure_sink (notify_closure);
        }
    }

Because `g_closure_sink()` may decrement the reference count of a closure (if it hasn't been called on `closure` yet) just like `g_closure_unref()`, `g_closure_ref()` should be called prior to this function.

`closure` : | GClosure to decrement the initial reference count on, if it's still being held

### g_closure_unref ()

    void        g_closure_unref                 (GClosure *closure);

Decrement the reference count of a closure after it was previously incremented by the same caller. The closure will most likely be destroyed and freed after this function returns.

`closure` : | GClosure to decrement the reference count on

### g_closure_invoke ()

    void        g_closure_invoke                (GClosure *closure,
                                                 GValue *return_value,
                                                 guint n_param_values,
                                                 const GValue *param_values,
                                                 gpointer invocation_hint);

`closure` : |
`return_value` : |
`n_param_values` : |
`param_values` : |
`invocation_hint` : |

### g_closure_invalidate ()

    void        g_closure_invalidate            (GClosure *closure);

This function sets a flag on the closure to indicate that it's calling environment has become invalid, and thus causes any future invocations of `g_closure_invoke()` on this `closure` to be ignored. Also, invalidation notifiers installed on the closure will be called at this point, and since invalidation notifiers may unreference the closure, `closure` should be considered an invalidated pointer atfer this function, unles `g_closure_ref()` was called beforehand.

`closure` : | GClosure to invalidate

### g_closure_add_finalize_notifier ()

    void        g_closure_add_finalize_notifier (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);

`closure` : |
`notify_data` : |
`notify_func` : |

### g_closure_add_invalidate_notifier ()

    void        g_closure_add_invalidate_notifier
                                                (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);

`closure` : |
`notify_data` : |
`notify_func` : |

### g_closure_remove_finalize_notifier ()

    void        g_closure_remove_finalize_notifier
                                                (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);

`closure` : |
`notify_data` : |
`notify_func` : |

### g_closure_remove_invalidate_notifier ()

    void        g_closure_remove_invalidate_notifier
                                                (GClosure *closure,
                                                 gpointer notify_data,
                                                 GClosureNotify notify_func);

`closure` : |
`notify_data` : |
`notify_func` : |

### g_closure_new_simple ()

    GClosure*   g_closure_new_simple            (guint sizeof_closure,
                                                 gpointer data);

`sizeof_closure` : |
`data` : |
Returns : |

### g_closure_set_marshal ()

    void        g_closure_set_marshal           (GClosure *closure,
                                                 GClosureMarshal marshal);

`closure` : |
`marshal` : |

### g_closure_add_marshal_guards ()

    void        g_closure_add_marshal_guards    (GClosure *closure,
                                                 gpointer pre_marshal_data,
                                                 GClosureNotify pre_marshal_notify,
                                                 gpointer post_marshal_data,
                                                 GClosureNotify post_marshal_notify);

`closure` : |
`pre_marshal_data` : |
`pre_marshal_notify` : |
`post_marshal_data` : |
`post_marshal_notify` : |

### g_closure_set_meta_marshal ()

    void        g_closure_set_meta_marshal      (GClosure *closure,
                                                 gpointer marshal_data,
                                                 GClosureMarshal meta_marshal);

`closure` : |
`marshal_data` : |
`meta_marshal` : |

### g_source_set_closure ()

    void        g_source_set_closure            (GSource *source,
                                                 GClosure *closure);

Set the callback for a source as a GClosure.

If the source is not one of the standard GLib types, the `closure_callback` and `closure_marshal` fields of the GSourceFuncs structure must have been filled in with pointers to appropriate functions.

`source` : | the source
`closure` : | a GClosure

### G_TYPE_IO_CHANNEL

    #define G_TYPE_IO_CHANNEL (g_io_channel_get_type ())

The GType for GIOChannel.

### G_TYPE_IO_CONDITION

    #define G_TYPE_IO_CONDITION (g_io_condition_get_type ())

The GType for GIOCondition.