summaryrefslogtreecommitdiff
path: root/src/mm-utils.h
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2015-01-15 11:33:51 +0100
committerAleksander Morgado <aleksander@aleksander.es>2015-02-07 12:57:48 +0100
commitc6ba09403a404d191e388db3b54d0145f057075c (patch)
tree754dee8dc63cb91861a341567e2356878276f273 /src/mm-utils.h
parent6fb83b5e029134ab9844f05517a444f2df501773 (diff)
utils: imported singleton setup from NetworkManager
Diffstat (limited to 'src/mm-utils.h')
-rw-r--r--src/mm-utils.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/mm-utils.h b/src/mm-utils.h
new file mode 100644
index 00000000..2b0a9340
--- /dev/null
+++ b/src/mm-utils.h
@@ -0,0 +1,83 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * 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:
+ *
+ * Singleton support imported from NetworkManager.
+ * (C) Copyright 2014 Red Hat, Inc.
+ */
+
+#ifndef MM_UTILS_H
+#define MM_UTILS_H
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include "mm-log.h"
+
+/*****************************************************************************/
+
+#define MM_DEFINE_SINGLETON_INSTANCE(TYPE) \
+ static TYPE *singleton_instance
+
+#define MM_DEFINE_SINGLETON_WEAK_REF(TYPE) \
+ MM_DEFINE_SINGLETON_INSTANCE (TYPE); \
+ static void \
+ _singleton_instance_weak_ref_cb (gpointer data, \
+ GObject *where_the_object_was) \
+ { \
+ mm_dbg ("disposing %s singleton (%p)", G_STRINGIFY (TYPE), singleton_instance); \
+ singleton_instance = NULL; \
+ } \
+ static inline void \
+ mm_singleton_instance_weak_ref_register (void) \
+ { \
+ g_object_weak_ref (G_OBJECT (singleton_instance), _singleton_instance_weak_ref_cb, NULL); \
+ }
+
+#define MM_DEFINE_SINGLETON_DESTRUCTOR(TYPE) \
+ MM_DEFINE_SINGLETON_INSTANCE (TYPE); \
+ static void __attribute__((destructor)) \
+ _singleton_destructor (void) \
+ { \
+ if (singleton_instance) { \
+ if (G_OBJECT (singleton_instance)->ref_count > 1) \
+ mm_dbg ("disown %s singleton (%p)", G_STRINGIFY (TYPE), singleton_instance); \
+ g_object_unref (singleton_instance); \
+ } \
+ }
+
+/* By default, the getter will assert that the singleton will be created only once. You can
+ * change this by redefining MM_DEFINE_SINGLETON_ALLOW_MULTIPLE. */
+#ifndef MM_DEFINE_SINGLETON_ALLOW_MULTIPLE
+#define MM_DEFINE_SINGLETON_ALLOW_MULTIPLE FALSE
+#endif
+
+#define MM_DEFINE_SINGLETON_GETTER(TYPE, GETTER, GTYPE, ...) \
+ MM_DEFINE_SINGLETON_INSTANCE (TYPE); \
+ MM_DEFINE_SINGLETON_WEAK_REF (TYPE); \
+ TYPE * \
+ GETTER (void) \
+ { \
+ if (G_UNLIKELY (!singleton_instance)) { \
+ static char _already_created = FALSE; \
+ \
+ g_assert (!_already_created || (MM_DEFINE_SINGLETON_ALLOW_MULTIPLE)); \
+ _already_created = TRUE; \
+ singleton_instance = (g_object_new (GTYPE, ##__VA_ARGS__, NULL)); \
+ g_assert (singleton_instance); \
+ mm_singleton_instance_weak_ref_register (); \
+ mm_dbg ("create %s singleton (%p)", G_STRINGIFY (TYPE), singleton_instance); \
+ } \
+ return singleton_instance; \
+ } \
+ MM_DEFINE_SINGLETON_DESTRUCTOR(TYPE)
+
+#endif /* MM_UTILS_H */