LCOV - code coverage report
Current view: top level - glib/gobject/tests - singleton.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 47 52 90.4 %
Date: 2024-04-30 05:17:35 Functions: 11 12 91.7 %
Branches: 7 8 87.5 %

           Branch data     Line data    Source code
       1                 :            : /* GObject - GLib Type, Object, Parameter and Signal Library
       2                 :            :  * Copyright (C) 2006 Imendio AB
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This library is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General
      17                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18                 :            :  */
      19                 :            : 
      20                 :            : #include <glib-object.h>
      21                 :            : 
      22                 :            : /* --- MySingleton class --- */
      23                 :            : 
      24                 :            : struct _MySingleton {
      25                 :            :   GObject parent_instance;
      26                 :            :   int myint;
      27                 :            : };
      28                 :            : 
      29                 :            : #define MY_TYPE_SINGLETON my_singleton_get_type ()
      30                 :            : G_DECLARE_FINAL_TYPE (MySingleton, my_singleton, MY, SINGLETON, GObject)
      31   [ +  +  +  -  :          5 : G_DEFINE_FINAL_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT)
                   +  + ]
      32                 :            : 
      33                 :            : static MySingleton *the_one_and_only = NULL;
      34                 :            : 
      35                 :            : /* --- methods --- */
      36                 :            : static GObject*
      37                 :          3 : my_singleton_constructor (GType                  type,
      38                 :            :                           guint                  n_construct_properties,
      39                 :            :                           GObjectConstructParam *construct_properties)
      40                 :            : {
      41         [ +  + ]:          3 :   if (the_one_and_only)
      42                 :          1 :     return g_object_ref (G_OBJECT (the_one_and_only));
      43                 :            :   else
      44                 :          2 :     return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
      45                 :            : }
      46                 :            : 
      47                 :            : static void
      48                 :          2 : my_singleton_finalize (GObject *object)
      49                 :            : {
      50                 :          2 :   g_assert ((GObject *) the_one_and_only == object);
      51                 :          2 :   the_one_and_only = NULL;
      52                 :            : 
      53                 :          2 :   G_OBJECT_CLASS (my_singleton_parent_class)->finalize (object);
      54                 :          2 : }
      55                 :            : 
      56                 :            : static void
      57                 :          2 : my_singleton_init (MySingleton *self)
      58                 :            : {
      59                 :          2 :   g_assert_null (the_one_and_only);
      60                 :          2 :   the_one_and_only = self;
      61                 :          2 : }
      62                 :            : 
      63                 :            : static void
      64                 :          2 : my_singleton_set_property (GObject      *gobject,
      65                 :            :                            guint         prop_id,
      66                 :            :                            const GValue *value,
      67                 :            :                            GParamSpec   *pspec)
      68                 :            : {
      69                 :          2 :   MySingleton *self = (MySingleton *) gobject;
      70                 :            : 
      71                 :          2 :   g_assert (prop_id == 1);
      72                 :            : 
      73                 :          2 :   self->myint = g_value_get_int (value);
      74                 :          2 : }
      75                 :            : 
      76                 :            : static void
      77                 :          0 : my_singleton_get_property (GObject    *gobject,
      78                 :            :                            guint       prop_id,
      79                 :            :                            GValue     *value,
      80                 :            :                            GParamSpec *pspec)
      81                 :            : {
      82                 :          0 :   MySingleton *self = (MySingleton *) gobject;
      83                 :            : 
      84                 :          0 :   g_assert (prop_id == 1);
      85                 :            : 
      86                 :          0 :   g_value_set_int (value, self->myint);
      87                 :          0 : }
      88                 :            : 
      89                 :            : static void
      90                 :          1 : my_singleton_class_init (MySingletonClass *klass)
      91                 :            : {
      92                 :          1 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
      93                 :            : 
      94                 :          1 :   object_class->constructor = my_singleton_constructor;
      95                 :          1 :   object_class->finalize = my_singleton_finalize;
      96                 :          1 :   object_class->set_property = my_singleton_set_property;
      97                 :          1 :   object_class->get_property = my_singleton_get_property;
      98                 :            : 
      99                 :          1 :   g_object_class_install_property (G_OBJECT_CLASS (klass), 1,
     100                 :            :                                    g_param_spec_int ("foo", NULL, NULL,
     101                 :            :                                                      0, G_MAXINT, 0,
     102                 :            :                                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
     103                 :          1 : }
     104                 :            : 
     105                 :            : static void
     106                 :          1 : test_singleton_construction (void)
     107                 :            : {
     108                 :            :   MySingleton *singleton, *obj;
     109                 :            : 
     110                 :            :   /* create the singleton */
     111                 :          1 :   singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
     112                 :          1 :   g_assert_nonnull (singleton);
     113                 :            : 
     114                 :            :   /* assert _singleton_ creation */
     115                 :          1 :   obj = g_object_new (MY_TYPE_SINGLETON, NULL);
     116                 :          1 :   g_assert_true (singleton == obj);
     117                 :          1 :   g_object_unref (obj);
     118                 :            : 
     119                 :            :   /* shutdown */
     120                 :          1 :   g_object_unref (singleton);
     121                 :          1 : }
     122                 :            : 
     123                 :            : static void
     124                 :          1 : test_singleton_construct_property (void)
     125                 :            : {
     126                 :            :   MySingleton *singleton;
     127                 :            : 
     128                 :          1 :   g_test_summary ("Test that creating a singleton with a construct-time property works");
     129                 :          1 :   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2666");
     130                 :            : 
     131                 :            :   /* create the singleton and set a property at construction time */
     132                 :          1 :   singleton = g_object_new (MY_TYPE_SINGLETON, "foo", 1, NULL);
     133                 :          1 :   g_assert_nonnull (singleton);
     134                 :            : 
     135                 :            :   /* shutdown */
     136                 :          1 :   g_object_unref (singleton);
     137                 :          1 : }
     138                 :            : 
     139                 :            : int
     140                 :          1 : main (int   argc,
     141                 :            :       char *argv[])
     142                 :            : {
     143                 :          1 :   g_test_init (&argc, &argv, NULL);
     144                 :            : 
     145                 :          1 :   g_test_add_func ("/gobject/singleton/construction", test_singleton_construction);
     146                 :          1 :   g_test_add_func ("/gobject/singleton/construct-property", test_singleton_construct_property);
     147                 :            : 
     148                 :          1 :   return g_test_run ();
     149                 :            : }

Generated by: LCOV version 1.14