Commit 700c62bf authored by Christian Hergert's avatar Christian Hergert
Browse files

shortcuts: add static inline to check struct type

This adds a simple magic value we can use to assert the struct type. A
little bit useful while tracking down some missuse bugs.
parent c61c06e9
......@@ -40,6 +40,7 @@ dzl_shortcut_closure_chain_new (DzlShortcutClosureType type)
g_assert (type < DZL_SHORTCUT_CLOSURE_LAST);
ret = g_slice_new0 (DzlShortcutClosureChain);
ret->magic = DZL_SHORTCUT_CLOSURE_CHAIN_MAGIC;
ret->node.data = ret;
ret->type = type;
......@@ -52,14 +53,19 @@ dzl_shortcut_closure_chain_append (DzlShortcutClosureChain *chain,
{
DzlShortcutClosureChain *ret;
g_return_val_if_fail (!chain || DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), NULL);
g_return_val_if_fail (!element || DZL_IS_SHORTCUT_CLOSURE_CHAIN (element), NULL);
g_return_val_if_fail (chain || element, NULL);
if (chain == NULL)
return element;
if (element == NULL)
return chain;
ret = g_slist_append (&chain->node, &element->node)->data;
g_return_val_if_fail (ret != NULL, NULL);
g_return_val_if_fail (DZL_IS_SHORTCUT_CLOSURE_CHAIN (ret), NULL);
return ret;
}
......@@ -70,15 +76,23 @@ dzl_shortcut_closure_chain_free (DzlShortcutClosureChain *chain)
if (chain == NULL)
return;
g_assert (DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain));
g_assert (chain->node.data == (gpointer)chain);
if (chain->executing)
{
g_warning ("Attempt to dispose a closure chain while executing, leaking");
return;
}
chain->magic = 0xAAAAAAAA;
if (chain->node.next)
dzl_shortcut_closure_chain_free (chain->node.next->data);
chain->node.next = NULL;
chain->node.data = NULL;
if (chain->type == DZL_SHORTCUT_CLOSURE_ACTION)
dzl_clear_pointer (&chain->action.params, g_variant_unref);
else if (chain->type == DZL_SHORTCUT_CLOSURE_CALLBACK)
......@@ -100,6 +114,7 @@ dzl_shortcut_closure_chain_append_callback (DzlShortcutClosureChain *chain,
{
DzlShortcutClosureChain *tail;
g_return_val_if_fail (!chain || DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), NULL);
g_return_val_if_fail (callback != NULL, NULL);
tail = dzl_shortcut_closure_chain_new (DZL_SHORTCUT_CLOSURE_CALLBACK);
......@@ -116,6 +131,7 @@ dzl_shortcut_closure_chain_append_command (DzlShortcutClosureChain *chain,
{
DzlShortcutClosureChain *tail;
g_return_val_if_fail (!chain || DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), NULL);
g_return_val_if_fail (command != NULL, NULL);
tail = dzl_shortcut_closure_chain_new (DZL_SHORTCUT_CLOSURE_COMMAND);
......@@ -132,6 +148,7 @@ dzl_shortcut_closure_chain_append_action (DzlShortcutClosureChain *chain,
{
DzlShortcutClosureChain *tail;
g_return_val_if_fail (!chain || DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), NULL);
g_return_val_if_fail (group_name != NULL, NULL);
g_return_val_if_fail (action_name != NULL, NULL);
......@@ -152,6 +169,7 @@ dzl_shortcut_closure_chain_append_action_string (DzlShortcutClosureChain *chain,
g_autofree gchar *prefix = NULL;
g_autofree gchar *name = NULL;
g_return_val_if_fail (!chain || DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), NULL);
g_return_val_if_fail (detailed_action_name != NULL, NULL);
if (!dzl_g_action_name_parse_full (detailed_action_name, &prefix, &name, &target_value))
......@@ -179,6 +197,7 @@ dzl_shortcut_closure_chain_append_signalv (DzlShortcutClosureChain *chain,
const gchar *detail_str;
GQuark detail = 0;
g_return_val_if_fail (!chain || DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), NULL);
g_return_val_if_fail (signal_name != NULL, NULL);
if (params != NULL)
......@@ -221,6 +240,7 @@ dzl_shortcut_closure_chain_append_signal (DzlShortcutClosureChain *chain,
{
g_autoptr(GArray) params = NULL;
g_return_val_if_fail (!chain || DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), NULL);
g_return_val_if_fail (signal_name != NULL, NULL);
params = g_array_new (FALSE, FALSE, sizeof (GValue));
......@@ -249,10 +269,10 @@ dzl_shortcut_closure_chain_append_signal (DzlShortcutClosureChain *chain,
}
static gboolean
find_instance_and_signal (GtkWidget *widget,
const gchar *signal_name,
gpointer *instance,
GSignalQuery *query)
find_instance_and_signal (GtkWidget *widget,
const gchar *signal_name,
gpointer *instance,
GSignalQuery *query)
{
DzlShortcutController *controller;
......@@ -319,7 +339,7 @@ signal_activate (DzlShortcutClosureChain *chain,
GSignalQuery query;
gpointer instance = NULL;
g_assert (chain != NULL);
g_assert (DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain));
g_assert (chain->type == DZL_SHORTCUT_CLOSURE_SIGNAL);
g_assert (GTK_IS_WIDGET (widget));
......@@ -379,7 +399,7 @@ static gboolean
command_activate (DzlShortcutClosureChain *chain,
GtkWidget *widget)
{
g_assert (chain != NULL);
g_assert (DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain));
g_assert (GTK_IS_WIDGET (widget));
for (; widget != NULL; widget = gtk_widget_get_parent (widget))
......@@ -407,7 +427,7 @@ dzl_shortcut_closure_chain_execute (DzlShortcutClosureChain *chain,
DZL_ENTRY;
g_return_val_if_fail (chain != NULL, FALSE);
g_return_val_if_fail (DZL_IS_SHORTCUT_CLOSURE_CHAIN (chain), FALSE);
g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
if (chain->executing)
......
......@@ -16,8 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DZL_SHORTCUT_THEME_PRIVATE_H
#define DZL_SHORTCUT_THEME_PRIVATE_H
#pragma once
#include "shortcuts/dzl-shortcut-chord.h"
#include "shortcuts/dzl-shortcut-closure-chain.h"
......@@ -28,6 +27,8 @@
G_BEGIN_DECLS
#define DZL_SHORTCUT_CLOSURE_CHAIN_MAGIC 0x81236261
typedef struct
{
DzlShortcutChordTable *table;
......@@ -74,6 +75,8 @@ struct _DzlShortcutClosureChain
{
GSList node;
guint magic;
DzlShortcutClosureType type : 3;
DzlShortcutPhase phase : 3;
guint executing : 1;
......@@ -146,6 +149,10 @@ gboolean _dzl_shortcut_chord_table_iter_next (DzlShortcut
gpointer *value);
void _dzl_shortcut_chord_table_iter_steal (DzlShortcutChordTableIter *iter);
G_END_DECLS
static inline gboolean
DZL_IS_SHORTCUT_CLOSURE_CHAIN (DzlShortcutClosureChain *self)
{
return self != NULL && self->magic == DZL_SHORTCUT_CLOSURE_CHAIN_MAGIC;
}
#endif /* DZL_SHORTCUT_THEME_PRIVATE_H */
G_END_DECLS
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment