Files
glib/gobject/tests/internal-type.c
Emmanuele Bassi 9b4bd73d42 Add a G_DECLARE macro for "protected" types
Certain libraries want to provide types that are derivable for internal
users, but final for any external consumer of the API. Other languages
use the term "protected" to refer to this kind of type visibility
attribute.

Since we're providing macros to declare derivable and final types, we
should also provide a macro for declaring protected types.

The mechanism is the same as the other G_DECLARE macros; protected
types:

 - define an opaque type for the instance, like G_DEFINE_FINAL_TYPE
 - define an opaque type for the class
 - define cast and type check functions for instance pointers, for
 public consumers, and cast and type check functions for class pointers,
 for internal consumers
 - omit an accessor for retrieving the class structure from the instance
 structure, as it would be pointless to do so
2024-12-13 17:09:45 +01:00

54 lines
949 B
C

/* internal-type.c: Test internal type
*
* SPDX-FileCopyrightText: 2024 Bilal Elmoussaoui
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <glib-object.h>
struct _RandomClass {
GObjectClass parent_class;
gint some_value;
};
struct _Random {
GObject parent;
};
G_DECLARE_INTERNAL_TYPE (Random, random, G, RANDOM, GObject)
G_DEFINE_FINAL_TYPE (Random, random, G_TYPE_OBJECT)
static void
random_class_init (RandomClass *klass)
{
klass->some_value = 3;
}
static void
random_init (Random *self)
{
}
static void
test_internal_type (void)
{
GObject *object;
RandomClass *klass;
object = g_object_new (random_get_type (), NULL);
klass = G_RANDOM_GET_CLASS (object);
g_assert_cmpint (klass->some_value, ==, 3);
g_assert_true (G_IS_RANDOM (object));
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/type/internal-type", test_internal_type);
return g_test_run ();
}