1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-11-21 09:57:30 +01:00
Files
.gitlab-ci
build
docs
gio
glib
gmodule
gobject
gthread
m4macros
po
subprojects
tests
collate
gobject
.gitignore
Makefile.am
accumulator.c
defaultiface.c
deftype.c
dynamictype.c
gvalue-test.c
meson.build
override.c
paramspec-test.c
performance-threaded.c
performance.c
references.c
signals.c
singleton.c
testcommon.h
testgobject.c
testmarshal.list
testmodule.c
testmodule.h
timeloop-closure.c
refcount
.gitignore
Makefile.am
assert-msg-test.c
assert-msg-test.gdb
asyncqueue-test.c
atomic-test.c
bit-test.c
casefold.txt
casemap.txt
child-test.c
completion-test.c
cxx-test.C
datetime.c
dirname-test.c
env-test.c
file-test.c
gen-casefold-txt.py
gen-casemap-txt.py
gio-test.c
iochannel-test-infile
iochannel-test.c
libmoduletestplugin_a.c
libmoduletestplugin_b.c
mainloop-test.c
mapping-test.c
memchunks.c
meson.build
module-test.c
onceinit.c
qsort-test.c
relation-test.c
run-assert-msg-test.sh
run-collate-tests.sh
slice-color.c
slice-concurrent.c
slice-test.c
slice-threadinit.c
sources.c
spawn-test-win32-gui.c
spawn-test.c
testgdate.c
testgdateparser.c
testglib.c
thread-test.c
threadpool-test.c
timeloop-basic.c
timeloop.c
type-test.c
unicode-caseconv.c
unicode-collate.c
unicode-encoding.c
unicode-normalize.c
utf8.txt
.dir-locals.el
.gitattributes
.gitignore
.gitlab-ci.yml
AUTHORS
CONTRIBUTING.md
COPYING
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-16
ChangeLog.pre-2-18
ChangeLog.pre-2-2
ChangeLog.pre-2-20
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL.in
Makefile.am
NEWS
NEWS.pre-1-3
README
README.md
README.rationale
README.win32
acglib.m4
acinclude.m4
autogen.sh
check-abis.sh
configure.ac
gio-2.0.pc.in
gio-unix-2.0.pc.in
gio-windows-2.0.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-tap.mk
glib.doap
glib.mk
glib.supp
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0.pc.in
gthread-2.0.pc.in
meson.build
meson_options.txt
msvc_recommended_pragmas.h
sanity_check
tap-driver.sh
tap-test
glib/tests/gobject/singleton.c

85 lines
2.8 KiB
C
Raw Normal View History

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