| 1 |
> |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "amtk-action-info-central-store.h" |
| 6 |
#include "amtk-action-info.h" |
| 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 |
struct _AmtkActionInfoCentralStorePrivate |
| 38 |
{ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
GHashTable *hash_table; |
| 43 |
}; |
| 44 |
|
| 45 |
static AmtkActionInfoCentralStore *singleton = NULL; |
| 46 |
|
| 47 |
G_DEFINE_TYPE_WITH_PRIVATE (AmtkActionInfoCentralStore, amtk_action_info_central_store, G_TYPE_OBJECT) |
| 48 |
|
| 49 |
static void |
| 50 |
amtk_action_info_central_store_finalize (GObject *object) |
| 51 |
{ |
| 52 |
AmtkActionInfoCentralStore *central_store = AMTK_ACTION_INFO_CENTRAL_STORE (object); |
| 53 |
|
| 54 |
g_hash_table_unref (central_store->priv->hash_table); |
| 55 |
|
| 56 |
if (singleton == central_store) |
| 57 |
{ |
| 58 |
singleton = NULL; |
| 59 |
} |
| 60 |
|
| 61 |
G_OBJECT_CLASS (amtk_action_info_central_store_parent_class)->finalize (object); |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
amtk_action_info_central_store_class_init (AmtkActionInfoCentralStoreClass *klass) |
| 66 |
{ |
| 67 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 68 |
|
| 69 |
object_class->finalize = amtk_action_info_central_store_finalize; |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
amtk_action_info_central_store_init (AmtkActionInfoCentralStore *central_store) |
| 74 |
{ |
| 75 |
central_store->priv = amtk_action_info_central_store_get_instance_private (central_store); |
| 76 |
|
| 77 |
central_store->priv->hash_table = g_hash_table_new_full (g_str_hash, |
| 78 |
g_str_equal, |
| 79 |
g_free, |
| 80 |
(GDestroyNotify) amtk_action_info_unref); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
AmtkActionInfoCentralStore * |
| 90 |
amtk_action_info_central_store_get_singleton (void) |
| 91 |
{ |
| 92 |
if (G_UNLIKELY (singleton == NULL)) |
| 93 |
{ |
| 94 |
singleton = g_object_new (AMTK_TYPE_ACTION_INFO_CENTRAL_STORE, NULL); |
| 95 |
} |
| 96 |
|
| 97 |
return singleton; |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
_amtk_action_info_central_store_unref_singleton (void) |
| 102 |
{ |
| 103 |
if (singleton != NULL) |
| 104 |
{ |
| 105 |
g_object_unref (singleton); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
void |
| 115 |
_amtk_action_info_central_store_add (AmtkActionInfoCentralStore *central_store, |
| 116 |
AmtkActionInfo *info) |
| 117 |
{ |
| 118 |
const gchar *action_name; |
| 119 |
|
| 120 |
g_return_if_fail (AMTK_IS_ACTION_INFO_CENTRAL_STORE (central_store)); |
| 121 |
g_return_if_fail (info != NULL); |
| 122 |
|
| 123 |
action_name = amtk_action_info_get_action_name (info); |
| 124 |
g_return_if_fail (action_name != NULL); |
| 125 |
|
| 126 |
if (g_hash_table_lookup (central_store->priv->hash_table, action_name) != NULL) |
| 127 |
{ |
| 128 |
g_warning ("The AmtkActionInfoCentralStore already contains an AmtkActionInfo " |
| 129 |
"with the action name “%s”. Libraries must namespace their action names.", |
| 130 |
action_name); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
g_hash_table_insert (central_store->priv->hash_table, |
| 135 |
g_strdup (action_name), |
| 136 |
amtk_action_info_ref (info)); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
AmtkActionInfo * |
| 148 |
amtk_action_info_central_store_lookup (AmtkActionInfoCentralStore *central_store, |
| 149 |
const gchar *action_name) |
| 150 |
{ |
| 151 |
g_return_val_if_fail (AMTK_IS_ACTION_INFO_CENTRAL_STORE (central_store), NULL); |
| 152 |
g_return_val_if_fail (action_name != NULL, NULL); |
| 153 |
|
| 154 |
return g_hash_table_lookup (central_store->priv->hash_table, action_name); |
| 155 |
} |