1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* libMirage: object
* Copyright (C) 2006-2014 Rok Mandeljc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* SECTION: mirage-object
* @title: MirageObject
* @short_description: Base object class.
* @see_also: #MirageContext, #MirageContextual
* @include: mirage-object.h
*
* #MirageObject is used as a base object class throughout libMirage. It
* implements #MirageContextual interface, which allows attachment of
* #MirageContext. It also implements support for constructing parent-child
* hierarchy, which allows propagation of the #MirageContext and its changes
* from parent to the child objects.
*/
#include "mirage/config.h"
#include "mirage/mirage.h"
#include <glib/gi18n-lib.h>
/**********************************************************************\
* Private structure *
\**********************************************************************/
struct _MirageObjectPrivate
{
gpointer parent; /* Soft-reference (= no ref) to parent */
MirageContext *context;
};
/**********************************************************************\
* Debug context changes *
\**********************************************************************/
static void mirage_object_parent_context_changed_handler (MirageObject *self, MirageObject *parent)
{
/* Get the new context and set it */
MirageContext *context = mirage_contextual_get_context(MIRAGE_CONTEXTUAL(parent));
mirage_contextual_set_context(MIRAGE_CONTEXTUAL(self), context);
if (context) {
g_object_unref(context);
}
}
/**********************************************************************\
* Public API *
\**********************************************************************/
/**
* mirage_object_set_parent:
* @self: a #MirageObject
* @parent: (in) (allow-none) (type MirageObject): parent
*
* Sets object's parent. If @parent is %NULL, the object's parent is
* reset.
*/
void mirage_object_set_parent (MirageObject *self, gpointer parent)
{
if (self->priv->parent) {
/* Remove "debug-context-change" signal handler */
g_signal_handlers_disconnect_by_func(self->priv->parent, mirage_object_parent_context_changed_handler, self);
/* Remove previous weak reference pointer */
g_object_remove_weak_pointer(G_OBJECT(self->priv->parent), &self->priv->parent);
}
self->priv->parent = parent;
if (parent) {
/* Add weak pointer to parent */
g_object_add_weak_pointer(parent, &self->priv->parent);
/* Connect "*/
g_signal_connect_swapped(parent, "context-changed", (GCallback)mirage_object_parent_context_changed_handler, self);
/* Set parent's context by simulating the signal */
mirage_object_parent_context_changed_handler(self, parent);
}
}
/**
* mirage_object_get_parent:
* @self: a #MirageObject
*
* Returns pointer to object's parent object.
*
* Returns: (transfer full) (type MirageObject): parent object, or %NULL.
*/
gpointer mirage_object_get_parent (MirageObject *self)
{
if (self->priv->parent) {
g_object_ref(self->priv->parent);
}
return self->priv->parent;
}
/**********************************************************************\
* MirageContextual methods implementation *
\**********************************************************************/
static void mirage_object_set_context (MirageContextual *_self, MirageContext *context)
{
MirageObject *self = MIRAGE_OBJECT(_self);
if (context == self->priv->context) {
/* Don't do anything if we're trying to set the same context */
return;
}
/* If context is already set, free it */
if (self->priv->context) {
g_object_unref(self->priv->context);
self->priv->context = NULL;
}
/* Set context and ref it */
if (context) {
self->priv->context = g_object_ref(context);
}
/* Signal change, so that children object can pick it up */
g_signal_emit_by_name(self, "context-changed", NULL);
}
static MirageContext *mirage_object_get_context (MirageContextual *_self)
{
MirageObject *self = MIRAGE_OBJECT(_self);
if (self->priv->context) {
g_object_ref(self->priv->context);
}
return self->priv->context;
}
/**********************************************************************\
* Object init *
\**********************************************************************/
static void mirage_object_contextual_init (MirageContextualInterface *iface);
G_DEFINE_TYPE_WITH_CODE(MirageObject,
mirage_object,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(MIRAGE_TYPE_CONTEXTUAL, mirage_object_contextual_init)
G_ADD_PRIVATE(MirageObject))
static void mirage_object_init (MirageObject *self)
{
self->priv = mirage_object_get_instance_private(self);
self->priv->parent = NULL;
self->priv->context = NULL;
}
static void mirage_object_dispose (GObject *gobject)
{
MirageObject *self = MIRAGE_OBJECT(gobject);
/* Remove weak reference pointer to parent */
if (self->priv->parent) {
g_signal_handlers_disconnect_by_func(self->priv->parent, mirage_object_parent_context_changed_handler, self);
g_object_remove_weak_pointer(G_OBJECT(self->priv->parent), &self->priv->parent);
self->priv->parent = NULL;
}
/* Unref context */
if (self->priv->context) {
g_object_unref(self->priv->context);
self->priv->context = NULL;
}
/* Chain up to the parent class */
return G_OBJECT_CLASS(mirage_object_parent_class)->dispose(gobject);
}
static void mirage_object_class_init (MirageObjectClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->dispose = mirage_object_dispose;
/* Signals */
/**
* MirageObject::context-changed:
* @object: a #MirageObject
*
* Emitted when a new #MirageContext is set to a #MirageObject.
*/
g_signal_new("context-changed", G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
}
static void mirage_object_contextual_init (MirageContextualInterface *iface)
{
iface->set_context = mirage_object_set_context;
iface->get_context = mirage_object_get_context;
}
|