|
Packit |
ae235b |
/* GObject - GLib Type, Object, Parameter and Signal Library
|
|
Packit |
ae235b |
* Copyright (C) 2006 Imendio AB
|
|
Packit |
ae235b |
*
|
|
Packit |
ae235b |
* This library is free software; you can redistribute it and/or
|
|
Packit |
ae235b |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
ae235b |
* License as published by the Free Software Foundation; either
|
|
Packit |
ae235b |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
ae235b |
*
|
|
Packit |
ae235b |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
ae235b |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
ae235b |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
ae235b |
* Lesser General Public License for more details.
|
|
Packit |
ae235b |
*
|
|
Packit |
ae235b |
* You should have received a copy of the GNU Lesser General
|
|
Packit |
ae235b |
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
ae235b |
*/
|
|
Packit |
ae235b |
#undef G_LOG_DOMAIN
|
|
Packit |
ae235b |
#define G_LOG_DOMAIN "TestSingleton"
|
|
Packit |
ae235b |
#include <glib-object.h>
|
|
Packit |
ae235b |
#include <string.h>
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* --- MySingleton class --- */
|
|
Packit |
ae235b |
typedef struct {
|
|
Packit |
ae235b |
GObject parent_instance;
|
|
Packit |
ae235b |
} MySingleton;
|
|
Packit |
ae235b |
typedef struct {
|
|
Packit |
ae235b |
GObjectClass parent_class;
|
|
Packit |
ae235b |
} MySingletonClass;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static GType my_singleton_get_type (void);
|
|
Packit |
ae235b |
#define MY_TYPE_SINGLETON (my_singleton_get_type ())
|
|
Packit |
ae235b |
#define MY_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
|
|
Packit |
ae235b |
#define MY_IS_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
|
|
Packit |
ae235b |
#define MY_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
|
|
Packit |
ae235b |
#define MY_IS_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
|
|
Packit |
ae235b |
#define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT)
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static MySingleton *the_one_and_only = NULL;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* --- methods --- */
|
|
Packit |
ae235b |
static GObject*
|
|
Packit |
ae235b |
my_singleton_constructor (GType type,
|
|
Packit |
ae235b |
guint n_construct_properties,
|
|
Packit |
ae235b |
GObjectConstructParam *construct_properties)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
if (the_one_and_only)
|
|
Packit |
ae235b |
return g_object_ref (G_OBJECT (the_one_and_only));
|
|
Packit |
ae235b |
else
|
|
Packit |
ae235b |
return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
my_singleton_init (MySingleton *self)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
g_assert (the_one_and_only == NULL);
|
|
Packit |
ae235b |
the_one_and_only = self;
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
my_singleton_class_init (MySingletonClass *klass)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor;
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* --- test program --- */
|
|
Packit |
ae235b |
int
|
|
Packit |
ae235b |
main (int argc,
|
|
Packit |
ae235b |
char *argv[])
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
MySingleton *singleton, *obj;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* create the singleton */
|
|
Packit |
ae235b |
singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
|
|
Packit |
ae235b |
g_assert (singleton != NULL);
|
|
Packit |
ae235b |
/* assert _singleton_ creation */
|
|
Packit |
ae235b |
obj = g_object_new (MY_TYPE_SINGLETON, NULL);
|
|
Packit |
ae235b |
g_assert (singleton == obj);
|
|
Packit |
ae235b |
g_object_unref (obj);
|
|
Packit |
ae235b |
/* shutdown */
|
|
Packit |
ae235b |
g_object_unref (singleton);
|
|
Packit |
ae235b |
return 0;
|
|
Packit |
ae235b |
}
|