Debian Codegraph @

lib/wp/si-factory.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "si-factory.h"
10 #include "log.h"
11
12 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-si-factory")
13
14 /*! \defgroup wpsifactory WpSiFactory */
15 /*!
16 * \struct WpSiFactory
17 *
18 * A factory for session items.
19 *
20 * The most simple way to register a new item implementation would be:
21 * \code
22 * wp_si_factory_register (core,
23 * wp_si_factory_new_simple ("foobar", FOO_TYPE_BAR));
24 * \endcode
25 *
26 * And the most simple way to construct an item from a registered factory:
27 * \code
28 * item = wp_session_item_make (core, "foobar");
29 * \endcode
30 *
31 * \gproperties
32 *
33 * \gproperty{name, gchar *, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY,
34 * The factory's name}
35 */
36 enum {
37 PROP_0,
38 PROP_NAME,
39 };
40
41 typedef struct _WpSiFactoryPrivate WpSiFactoryPrivate;
42 struct _WpSiFactoryPrivate
43 {
44 GQuark name_quark;
45 };
46
47 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (WpSiFactory, wp_si_factory, G_TYPE_OBJECT)
48
49 static void
50 wp_si_factory_init (WpSiFactory * self)
51 {
52 }
53
54 static void
55 wp_si_factory_set_property (GObject * object, guint property_id,
56 const GValue * value, GParamSpec * pspec)
57 {
58 WpSiFactory *self = WP_SI_FACTORY (object);
59 WpSiFactoryPrivate *priv = wp_si_factory_get_instance_private (self);
60
61 switch (property_id) {
62 case PROP_NAME:
63 priv->name_quark = g_quark_from_string (g_value_get_string (value));
64 break;
65 default:
66 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
67 break;
68 }
69 }
70
71 static void
72 wp_si_factory_get_property (GObject * object, guint property_id, GValue * value,
73 GParamSpec * pspec)
74 {
75 WpSiFactory *self = WP_SI_FACTORY (object);
76 WpSiFactoryPrivate *priv = wp_si_factory_get_instance_private (self);
77
78 switch (property_id) {
79 case PROP_NAME:
80 g_value_set_string (value, g_quark_to_string (priv->name_quark));
81 break;
82 default:
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
84 break;
85 }
86 }
87
88 static void
89 wp_si_factory_class_init (WpSiFactoryClass * klass)
90 {
91 GObjectClass * object_class = (GObjectClass *) klass;
92
93 object_class->get_property = wp_si_factory_get_property;
94 object_class->set_property = wp_si_factory_set_property;
95
96 g_object_class_install_property (object_class, PROP_NAME,
97 g_param_spec_string ("name", "name", "The factory's name", "",
98 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
99 }
100
101 /*!
102 * \brief Gets the name of the factory
103 *
104 * \ingroup wpsifactory
105 * \param self the factory
106 * \returns the factory name
107 */
108 const gchar *
109 wp_si_factory_get_name (WpSiFactory * self)
110 {
111 g_return_val_if_fail (WP_IS_SI_FACTORY (self), NULL);
112
113 WpSiFactoryPrivate *priv = wp_si_factory_get_instance_private (self);
114 return g_quark_to_string (priv->name_quark);
115 }
116
117 /*!
118 * \brief Creates a new instance of the session item that is constructed
119 * by this factory
120 *
121 * \ingroup wpsifactory
122 * \param self the factory
123 * \param core the core
124 * \returns (transfer full): a new session item instance
125 */
126 WpSessionItem *
127 wp_si_factory_construct (WpSiFactory * self, WpCore * core)
128 {
129 g_return_val_if_fail (WP_IS_SI_FACTORY (self), NULL);
130 g_return_val_if_fail (WP_SI_FACTORY_GET_CLASS (self)->construct, NULL);
131
132 return WP_SI_FACTORY_GET_CLASS (self)->construct (self, core);
133 }
134
135 static gboolean
136 find_factory_func (gpointer factory, gpointer name_quark)
137 {
138 if (!WP_IS_SI_FACTORY (factory))
139 return FALSE;
140
141 WpSiFactoryPrivate *priv =
142 wp_si_factory_get_instance_private ((WpSiFactory *) factory);
143 return priv->name_quark == GPOINTER_TO_UINT (name_quark);
144 }
145
146 /*!
147 * \brief Looks up a factory matching a name
148 *
149 * \ingroup wpsifactory
150 * \param core the core
151 * \param factory_name the lookup name
152 * \returns (transfer full) (nullable): the factory matching the lookup name
153 */
154 WpSiFactory *
155 wp_si_factory_find (WpCore * core, const gchar * factory_name)
156 {
157 g_return_val_if_fail (WP_IS_CORE (core), NULL);
158
159 GQuark q = g_quark_try_string (factory_name);
160 if (q == 0)
161 return NULL;
162 GObject *f = wp_core_find_object (core,
163 (GEqualFunc) find_factory_func, GUINT_TO_POINTER (q));
164 return f ? WP_SI_FACTORY (f) : NULL;
165 }
166
167 /*!
168 * \brief Finds the factory associated with the given \a name from the \a core
169 * and uses it to construct a new WpSessionItem.
170 *
171 * \ingroup wpsifactory
172 * \param core the WpCore
173 * \param factory_name the name of the factory to be used for constructing
174 * the object
175 * \returns (transfer full) (nullable): the new session item
176 */
177 WpSessionItem *
178 wp_session_item_make (WpCore * core, const gchar * factory_name)
179 {
180 g_autoptr (WpSiFactory) f = wp_si_factory_find (core, factory_name);
181 return f ? wp_si_factory_construct (f, core) : NULL;
182 }
183
184 struct _WpSimpleSiFactory
185 {
186 WpSiFactory parent;
187 GType si_type;
188 };
189
190 G_DECLARE_FINAL_TYPE (WpSimpleSiFactory, wp_simple_si_factory,
191 WP, SIMPLE_SI_FACTORY, WpSiFactory)
192 G_DEFINE_TYPE (WpSimpleSiFactory, wp_simple_si_factory, WP_TYPE_SI_FACTORY)
193
194 static void
195 wp_simple_si_factory_init (WpSimpleSiFactory * self)
196 {
197 }
198
199 static WpSessionItem *
200 wp_simple_si_factory_construct (WpSiFactory * self, WpCore *core)
201 {
202 return g_object_new (WP_SIMPLE_SI_FACTORY (self)->si_type,
203 "core", core,
204 NULL);
205 }
206
207 static void
208 wp_simple_si_factory_class_init (WpSimpleSiFactoryClass * klass)
209 {
210 WpSiFactoryClass * factory_class = (WpSiFactoryClass *) klass;
211
212 factory_class->construct = wp_simple_si_factory_construct;
213 }
214
215 /*!
216 * \brief Creates a simple factory that constructs objects of a given GType.
217 *
218 * \ingroup wpsifactory
219 * \param factory_name the factory name; must be a static string!
220 * \param si_type the WpSessionItem subclass type to instantiate for
221 * constructing items
222 * \returns (transfer full): the new factory
223 */
224 WpSiFactory *
225 wp_si_factory_new_simple (const gchar * factory_name, GType si_type)
226 {
227 g_return_val_if_fail (factory_name != NULL, NULL);
228 g_return_val_if_fail (g_type_is_a (si_type, WP_TYPE_SESSION_ITEM), NULL);
229
230 WpSimpleSiFactory *self = g_object_new (
231 wp_simple_si_factory_get_type (), NULL);
232
233 /* assign the quark directly to use g_quark_from_static_string */
234 WpSiFactoryPrivate *priv =
235 wp_si_factory_get_instance_private (WP_SI_FACTORY (self));
236 priv->name_quark = g_quark_from_static_string (factory_name);
237
238 self->si_type = si_type;
239
240 return WP_SI_FACTORY (self);
241 }