<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title></title>
	<atom:link href="http://blogs.gnome.org/robsta/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.gnome.org/robsta</link>
	<description>The days are long at night</description>
	<lastBuildDate>Tue, 30 Nov 2010 17:53:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>GProperty &#8212; GObject properties made easy</title>
		<link>http://blogs.gnome.org/robsta/2010/11/30/gproperty-gobject-properties-made-easy/</link>
		<comments>http://blogs.gnome.org/robsta/2010/11/30/gproperty-gobject-properties-made-easy/#comments</comments>
		<pubDate>Tue, 30 Nov 2010 11:57:03 +0000</pubDate>
		<dc:creator>robsta</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.gnome.org/robsta/?p=16</guid>
		<description><![CDATA[Implementing GObject properties may be considered cumbersome to the uninitiated, but once you&#8217;ve got a few implementations under your belt it&#8217;s actually quite repetitive boilerplate code. On top of that, if you want to provide explicit property accessors in addition to the g_object_get/set API, they need to be implemented as well, which means more of [...]]]></description>
			<content:encoded><![CDATA[<p>Implementing GObject properties may be considered cumbersome to the uninitiated, but once you&#8217;ve got a few implementations under your belt it&#8217;s actually quite repetitive boilerplate code. On top of that, if you want to provide explicit property accessors in addition to the g_object_get/set API, they need to be implemented as well, which means more of the same. It would be nice to implement the boilerplate code once for all, so here comes &#8230;</p>
<p>GProperty is a subclass of GParamSpec¹ that provides common ways of implementing properties. And by being based on GParamSpec it retains compatibility and works exactly like expected when looking at a GObject from the outside. The proof of concept implementation also provides <em>egg_object_get_property()</em> and <em>egg_object_set_property()</em> GObject methods, that dispatch property access to the respective GProperty.</p>
<p><strong>Field-based properties</strong></p>
<p>Probably the most common way is to store the value of a property in a field of the object&#8217;s private data blob. GProperty provides a ready-made implementation that manages access to that field. If you need to react on property changes you&#8217;d just hook up to the property-changed notification.</p>
<p>The following snippet implements an <em>int</em> and a <em>string</em> property backed by the respective field in the private struct. No need for a property-id enum or implementation of g_object_get/set_property().</p>
<pre>typedef struct
{
  gint   number;
  gchar *text;
} EggTestOffsetPrivate;

static void
egg_test_offset_class_init (EggTestOffsetClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec  *pspec;
  guint        id = 0;

  g_type_class_add_private (klass, sizeof (EggTestOffsetPrivate));

  object_class-&gt;get_property = egg_object_get_property;
  object_class-&gt;set_property = egg_object_set_property;

  pspec = egg_int_property_for_field ("number",
                                      G_STRUCT_OFFSET (EggTestOffsetPrivate, number),
                                      G_MININT32, G_MAXINT32, 1,
                                      G_PARAM_READWRITE);
  g_object_class_install_property (object_class, ++id, pspec);

  pspec = egg_string_property_for_field ("text",
                                         G_STRUCT_OFFSET (EggTestOffsetPrivate, text),
                                         NULL,
                                         G_PARAM_READWRITE);
  g_object_class_install_property (object_class, ++id, pspec);
}
</pre>
<p><strong>Accessor-based properties</strong></p>
<p>Another way of using GProperty is with accessor functions that are explicitly managing a property. The following snippet creates GProperties that are dispatching access from g_object_get/set() to the custom accessors, again saving custom g_object_get/set_property() overrides.</p>
<pre>
typedef struct
{
  gint   number;
  gchar *text;
} EggTestAccessorsPrivate;

static void
egg_test_accessors_class_init (EggTestAccessorsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec  *pspec;
  guint        id = 0;

  g_type_class_add_private (klass, sizeof (EggTestAccessorsPrivate));

  object_class->get_property = egg_object_get_property;
  object_class->set_property = egg_object_set_property;

  pspec = egg_int_property_for_accessors (
            "number",
            (EggIntPropertyGetterFunc) egg_test_accessors_get_number,
            (EggIntPropertySetterFunc) egg_test_accessors_set_number,
            G_MININT32, G_MAXINT32, 1,
            G_PARAM_READWRITE);
  g_object_class_install_property (object_class, ++id, pspec);

  pspec = egg_string_property_for_accessors (
            "text",
            (EggStringPropertyGetterFunc) egg_test_accessors_get_text,
            (EggStringPropertySetterFunc) egg_test_accessors_set_text,
            NULL,
            G_PARAM_READWRITE);
  g_object_class_install_property (object_class, ++id, pspec);
}

/* standard getter and setter functions omitted */
</pre>
<p><strong>Property proxies</strong></p>
<p>Maybe slightly more obscure than the above, but hopefully still useful are proxied properties. The goal is to transparently export properties of an aggregate object to the outside.</p>
<p>The following snippet implements a custom gtk widget using a button and spinner inside a hbox, and exports the button&#8217;s <em>label</em> and the spinner&#8217;s <em>value</em> as properties of the toplevel hbox subclass.</p>
<pre>
typedef struct
{
  GtkWidget *button;
  GtkWidget *spinner;
} EggTestProxyPrivate;

static void
egg_test_proxy_class_init (EggTestProxyClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec  *pspec;
  guint        id = 0;

  g_type_class_add_private (klass, sizeof (EggTestProxyPrivate));

  object_class->get_property = egg_object_get_property;
  object_class->set_property = egg_object_set_property;

  pspec = egg_string_property_proxy_for_object_offset (
                      "label",
                      G_STRUCT_OFFSET (EggTestProxyPrivate, button),
                      "label",
                      NULL,
                      G_PARAM_READWRITE);
  g_object_class_install_property (object_class, ++id, pspec);

  pspec = egg_double_property_proxy_for_object_offset (
                      "value",
                      G_STRUCT_OFFSET (EggTestProxyPrivate, spinner),
                      "value",
                      0.0, 10.0, 1.0,
                      G_PARAM_READWRITE);
  g_object_class_install_property (object_class, ++id, pspec);
}
</pre>
<p>The code with full examples can be found at <a href="http://gitorious.org/egg-gobject">http://gitorious.org/egg-gobject</a> At the moment only properties of type <em>double</em>, <em>int</em> and <em>string</em> are implemented, but chances for broader support are not all that bad. If feedback is positive I&#8217;d like to consider migration to libegg in gnome&#8217;s git.</p>
<p>¹ actually G&lt;Foo&gt;Property is a subclass of GParamSpec&lt;Foo&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gnome.org/robsta/2010/11/30/gproperty-gobject-properties-made-easy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>If I had five wishes &#8230; to (GC)C maintainers</title>
		<link>http://blogs.gnome.org/robsta/2010/11/17/if-i-had-five-wishes-to-gcc-maintainers/</link>
		<comments>http://blogs.gnome.org/robsta/2010/11/17/if-i-had-five-wishes-to-gcc-maintainers/#comments</comments>
		<pubDate>Wed, 17 Nov 2010 16:20:26 +0000</pubDate>
		<dc:creator>robsta</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.gnome.org/robsta/?p=12</guid>
		<description><![CDATA[C puts the bread on the table for many of us, so every once in a while, when not just bashing C++, endulging in the awesomeness of Ruby, or wondering why PHP is still powering a good deal of the modern interwebs, the topic comes to C, its greatness, and how C99 is still not [...]]]></description>
			<content:encoded><![CDATA[<p>C puts the bread on the table for many of us, so every once in a while, when not just bashing C++, endulging in the awesomeness of Ruby, or wondering why PHP is still powering a good deal of the modern interwebs, the topic comes to C, its greatness, and how C99 is still not ubiquitous.</p>
<p>Personally I think, with the year being 2010 and all, just five humble features would make a whole lot of difference and put the language firmly in the 21st century, while not sacrificing any of the spirit of being a high-level assembly language. So here goes (in no particular order) &#8230;</p>
<p><strong>Extended support for anonymous aggregates</strong></p>
<p>C99&#8242;s anonymous aggregates are useful, for example when passing a one-off compound datatype to a function. What would be even more helpful would be the possibility to return an anonymous aggregate from a function, this would essentially allow returning multiple values:</p>
<pre>struct { float width; float height; }
clutter_actor_get_size (ClutterActor *actor) {
  struct { float width; float height; } size;
  /* Fill size.width and size.height */
  return size;
}
[...]
struct { float width; float height; } size;
size = clutter_actor_get_size (actor);</pre>
<p>This feature would be even more helpful with</p>
<p><strong>Variable declarations using &#8220;auto&#8221;</strong></p>
<p>When a variable is initialised in place it should be possible for today&#8217;s compilers to figure out the data type, there&#8217;s even things like the &#8220;L&#8221; suffix to numbers for specifying the desired range. Leveraging &#8220;auto&#8221; the above example would become a bit less verbose:</p>
<pre>struct { float width; float height; }
clutter_actor_get_size (ClutterActor *actor) {
  struct { float width; float height; } size;
  /* Fill size.width and size.height */
  return size;
}
[...]
auto size = clutter_actor_get_size (actor);</pre>
<p><strong>Lambda functions</strong></p>
<p>Along the lines of anonymous aggregates it would seem very natural to do lambda functions by basically &#8220;casting&#8221; a block of statements. The formal parameters would be derived from the &#8220;cast&#8221; operator. This approach, in my opinion, would be more natural to C than llvm&#8217;s block syntax using ^.</p>
<pre>clutter_container_foreach (container,
                           (void (*)(ClutterActor *actor, gpointer data))
                           {
                             printf ("%s\n", clutter_actor_get_name (actor));
                           },
                           NULL);
</pre>
<p><strong>Type extension</strong></p>
<p>GObject based C code is typically interspersed with type casts, but this does not seem strictly necessary from a semantic point of view. A pointer to a compound instance in C is by definition also a pointer to the first attribute. It should be fairly straight forward to account for that in the compiler, and thus allow for implicit &#8220;upcasting&#8221;, i.e. assigning a pointer of a &#8220;derived&#8221; type to a pointer of type of an (first-member) embedded struct. There would be no need for the C compiler to warn about pointer types not matching, because the example is actually semantically correct.</p>
<pre>/* Lots of GObject boilerplate code omitted. */
typedef struct {
  ClutterActor parent;
} FooActor;
[...]
FooActor *actor = foo_actor_new ();
clutter_actor_set_x (actor, 100.0);
</pre>
<p><strong>An #import preprocessor directive</strong></p>
<p>In this day and age it seems redundant having to type function signatures twice, once in the header and once again the the C file. It would be very handy if preprocessors could import symbols from other C files, without doing the verbatim insertion that is #include. For libraries which want to install headers I would imagine a compiler option that extracts definements and non-static symbols from a C file, possibly supporting filtering on prefixes, so the headers could be generated on the fly by the build system.</p>
<p>That might do for the next 30 years or so, I suppose.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gnome.org/robsta/2010/11/17/if-i-had-five-wishes-to-gcc-maintainers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blogs.gnome.org/robsta/feed/ ) in 0.25569 seconds, on Feb 23rd, 2012 at 12:17 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 23rd, 2012 at 1:17 am UTC -->
