Debian Codegraph @

amtk/actions-menus-toolbars/amtk-action-info-central-store.c

raw symbols SCIP
1 /* SPDX-FileCopyrightText: 2017 - Sébastien Wilmet <[email protected]>
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 */
4
5 #include "amtk-action-info-central-store.h"
6 #include "amtk-action-info.h"
7
8 /**
9 * SECTION:amtk-action-info-central-store
10 * @Short_description: Aggregation of all #AmtkActionInfoStore's
11 * @Title: AmtkActionInfoCentralStore
12 * @See_also: #AmtkActionInfoStore
13 *
14 * #AmtkActionInfoCentralStore is a singleton class containing the aggregation
15 * of all #AmtkActionInfoStore's. Each time an #AmtkActionInfo is added to an
16 * #AmtkActionInfoStore, it is also added to the #AmtkActionInfoCentralStore.
17 */
18
19 /* API design:
20 *
21 * Why both AmtkActionInfoStore and AmtkActionInfoCentralStore are needed?
22 *
23 * Advantage of AmtkActionInfoStore:
24 * - amtk_action_info_store_check_all_used()
25 *
26 * Advantages of AmtkActionInfoCentralStore:
27 * - The central store checks if there are no duplicated action names
28 * (globally).
29 * - Permits to write AmtkFactory functions that don't require an
30 * AmtkActionInfoStore parameter.
31 *
32 * By having the two classes, we have the best of both worlds. We should not be
33 * afraid to create a lot of classes, and see things in big, even if it's a bit
34 * Java-ish.
35 */
36
37 struct _AmtkActionInfoCentralStorePrivate
38 {
39 /* Key: owned gchar*: action name.
40 * Value: owned AmtkActionInfo.
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 * amtk_action_info_central_store_get_singleton:
85 *
86 * Returns: (transfer none): the #AmtkActionInfoCentralStore singleton instance.
87 * Since: 3.0
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 /* singleton is not set to NULL here, it is set to NULL in
109 * amtk_action_info_central_store_finalize() (i.e. when we are sure that
110 * the ref count reaches 0).
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 * amtk_action_info_central_store_lookup:
141 * @central_store: the #AmtkActionInfoCentralStore.
142 * @action_name: an action name.
143 *
144 * Returns: (transfer none): the found #AmtkActionInfo, or %NULL.
145 * Since: 2.0
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 }