summaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2019-01-22 18:28:36 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2019-01-22 18:34:46 (GMT)
commitc1bcf3e7bd0a256005bd15832117b78cee5fdfab (patch)
tree54188ab3933526ca79ae3e8f86dd99adad49e681 /src/plugins
parent3a8bc79d69acae3735cc0203b54d93b4137caa09 (diff)
Allowed to initialize instances of created dynamic types if needed.
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/dt.c98
-rw-r--r--src/plugins/dt.h8
-rw-r--r--src/plugins/plugin.c4
3 files changed, 95 insertions, 15 deletions
diff --git a/src/plugins/dt.c b/src/plugins/dt.c
index afdba21..14b03f0 100644
--- a/src/plugins/dt.c
+++ b/src/plugins/dt.c
@@ -26,6 +26,7 @@
#include <assert.h>
#include <malloc.h>
+#include <string.h>
@@ -44,9 +45,14 @@
typedef struct _type_dyn_info_t
{
GType type; /* Identifiant unique obtenu */
- GClassInitFunc init; /* Définition des méthodes */
+
+ GClassInitFunc cinit; /* Phase d'initialisation #1 */
gconstpointer data; /* Eventuelles données utiles */
+ GInstanceInitFunc init; /* Phase d'initialisation #2 */
+
+ void *pattern; /* Modèle de données d'instance*/
+
} type_dyn_info_t;
/* Description de fichier binaire (instance) */
@@ -98,10 +104,10 @@ static void g_dynamic_types_unuse(GDynamicTypes *);
static void g_dynamic_types_complete_type(GDynamicTypes *, GType, GTypeInfo *, GTypeValueTable *);
/* Retrouve les informations concernant un type dynamique. */
-static const type_dyn_info_t *g_dynamic_types_find(const GDynamicTypes *, GType);
+static type_dyn_info_t *g_dynamic_types_find(const GDynamicTypes *, GType);
/* Fournit un identifiant GLib pour un nouveau type. */
-static GType g_dynamic_types_register_type(GDynamicTypes *, GType, const char *, GClassInitFunc, gconstpointer);
+static GType g_dynamic_types_register_type(GDynamicTypes *, GType, const char *, GClassInitFunc, gconstpointer, GInstanceInitFunc);
@@ -297,7 +303,7 @@ static void g_dynamic_types_unuse(GDynamicTypes *types)
static void g_dynamic_types_complete_type(GDynamicTypes *types, GType type, GTypeInfo *info, GTypeValueTable *table)
{
- const type_dyn_info_t *nfo; /* Source d'inspiration */
+ type_dyn_info_t *nfo; /* Source d'inspiration */
GType parent; /* Type parent du type */
GTypeQuery query; /* Informations complémentaires*/
@@ -312,10 +318,11 @@ static void g_dynamic_types_complete_type(GDynamicTypes *types, GType type, GTyp
/* Définition */
info->class_size = query.class_size;
- info->class_init = nfo->init;
+ info->class_init = nfo->cinit;
info->class_data = nfo->data;
info->instance_size = query.instance_size;
+ info->instance_init = nfo->init;
}
@@ -333,7 +340,7 @@ static void g_dynamic_types_complete_type(GDynamicTypes *types, GType type, GTyp
* *
******************************************************************************/
-static const type_dyn_info_t *g_dynamic_types_find(const GDynamicTypes *types, GType target)
+static type_dyn_info_t *g_dynamic_types_find(const GDynamicTypes *types, GType target)
{
type_dyn_info_t *result; /* Informations à retourner */
size_t i; /* Boucle de parcours */
@@ -353,8 +360,9 @@ static const type_dyn_info_t *g_dynamic_types_find(const GDynamicTypes *types, G
* *
* Paramètres : parent = type GLib parent. *
* name = désignation du nouveau type. *
-* init = procédure d'initialisation de la classe associée. *
+* cinit = procédure d'initialisation de la classe associée. *
* data = éventuelles données à associer à la future classe. *
+* init = procédure d'initialisation pour chaque instance. *
* *
* Description : Fournit un identifiant GLib pour un nouveau type. *
* *
@@ -364,7 +372,7 @@ static const type_dyn_info_t *g_dynamic_types_find(const GDynamicTypes *types, G
* *
******************************************************************************/
-static GType g_dynamic_types_register_type(GDynamicTypes *types, GType parent, const char *name, GClassInitFunc init, gconstpointer data)
+static GType g_dynamic_types_register_type(GDynamicTypes *types, GType parent, const char *name, GClassInitFunc cinit, gconstpointer data, GInstanceInitFunc init)
{
GType result; /* Identifiant à retourner */
type_dyn_info_t *new; /* Mémorisation de paramètres */
@@ -379,9 +387,14 @@ static GType g_dynamic_types_register_type(GDynamicTypes *types, GType parent, c
new = malloc(sizeof(type_dyn_info_t));
new->type = result;
- new->init = init;
+
+ new->cinit = cinit;
new->data = data;
+ new->init = init;
+
+ new->pattern = NULL;
+
/* Inscription définitive */
types->info = realloc(types->info, ++types->count * sizeof(type_dyn_info_t *));
@@ -449,8 +462,9 @@ void exit_chrysalide_dynamic_types(void)
* *
* Paramètres : parent = type GLib parent. *
* name = désignation du nouveau type. *
-* init = procédure d'initialisation de la classe associée. *
+* cinit = procédure d'initialisation de la classe associée. *
* data = éventuelles données à associer à la future classe. *
+* init = procédure d'initialisation pour chaque instance. *
* *
* Description : Fournit un identifiant GLib pour un nouveau type. *
* *
@@ -460,14 +474,74 @@ void exit_chrysalide_dynamic_types(void)
* *
******************************************************************************/
-GType built_dynamic_type(GType parent, const char *name, GClassInitFunc init, gconstpointer data)
+GType build_dynamic_type(GType parent, const char *name, GClassInitFunc cinit, gconstpointer data, GInstanceInitFunc init)
{
GType result; /* Identifiant à retourner */
result = g_type_from_name(name);
if (result == 0)
- result = g_dynamic_types_register_type(_chrysalide_dtypes, parent, name, init, data);
+ result = g_dynamic_types_register_type(_chrysalide_dtypes, parent, name, cinit, data, init);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : instance = instance portant les données à conserver. *
+* *
+* Description : Enregistre les données correspondant à une instance. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void register_dynamic_type_pattern(GObject *instance)
+{
+ GType type; /* Type d'instances concerné */
+ type_dyn_info_t *nfo; /* Source d'inspiration */
+ GTypeQuery query; /* Informations complémentaires*/
+
+ type = G_TYPE_FROM_INSTANCE(instance);
+
+ nfo = g_dynamic_types_find(_chrysalide_dtypes, type);
+ assert(nfo != NULL);
+
+ g_type_query(type, &query);
+
+ if (nfo->pattern == NULL)
+ nfo->pattern = malloc(query.instance_size);
+
+ memcpy(nfo->pattern, instance, query.instance_size);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : type = type d'une instance créée sans initialisation. *
+* *
+* Description : Fournit les données correspondant à une instance initiale. *
+* *
+* Retour : Données issues de la première instance d'un type ou NULL. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void *get_dynamic_type_pattern(GType type)
+{
+ void *result; /* Modèle éventuel à retourner */
+ type_dyn_info_t *nfo; /* Source d'inspiration */
+
+ nfo = g_dynamic_types_find(_chrysalide_dtypes, type);
+ assert(nfo != NULL);
+
+ result = nfo->pattern;
return result;
diff --git a/src/plugins/dt.h b/src/plugins/dt.h
index b9d4656..c25f4fd 100644
--- a/src/plugins/dt.h
+++ b/src/plugins/dt.h
@@ -40,7 +40,13 @@ bool init_chrysalide_dynamic_types(void);
void exit_chrysalide_dynamic_types(void);
/* Fournit un identifiant GLib pour un nouveau type. */
-GType built_dynamic_type(GType, const char *, GClassInitFunc, gconstpointer);
+GType build_dynamic_type(GType, const char *, GClassInitFunc, gconstpointer, GInstanceInitFunc);
+
+/* Enregistre les données correspondant à une instance. */
+void register_dynamic_type_pattern(GObject *);
+
+/* Fournit les données correspondant à une instance initiale. */
+void *get_dynamic_type_pattern(GType);
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index ff3db31..1bdcf04 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -450,8 +450,8 @@ GPluginModule *g_plugin_module_new(const gchar *filename)
if (!valid)
goto bad_plugin;
- gtype = built_dynamic_type(G_TYPE_PLUGIN_MODULE, interface->gtp_name,
- (GClassInitFunc)g_plugin_module_init_gclass, module);
+ gtype = build_dynamic_type(G_TYPE_PLUGIN_MODULE, interface->gtp_name,
+ (GClassInitFunc)g_plugin_module_init_gclass, module, NULL);
if (gtype == G_TYPE_INVALID)
goto bad_plugin;