libgimp: use G_DECLARE_DERIVABLE_TYPE and G_DECLARE_FINAL_TYPE for…

… the public API.

This was initially proposed by Niels De Graef in !101, and though I
still think this is much less practical for day-to-day development, it
is actually much nicer for the public part of the API. So let's use
these only in public libgimp* API only, not in core.

I actually already started to use these for some of the libgimpwidgets
classes and am now moving libgimp main classes to these macros.

* It doesn't expose the priv member (which is completely useless for
  plug-in developers, only to be used for core development).
* It forces us to never have any variable members in the public API
  (which we were doing fine so far in newest API, but it's nice to be
  enforced with these macros).
* When using G_DECLARE_FINAL_TYPE in particular, it adds flexibility as
  we can change the structure size and the members order as these are
  not exposed. And if some day, we make the class derivable with some
  signals to handle, only then will we expose the class with some
  _gimp_reserved* padding (instead of from the start when none is
  needed). Therefore we will allow for further extension far in the
  future.

Moreover most of these libgimp classes were so far not using any private
values, so we were declaring a `priv` member with a bogus contents just
"in case we needed it in future" (as we couldn't change the struct
size). So even the easiness of having a priv member was very relative
for this public API so far (unlike in core code where we actually have
much more complex interactions and using priv data all the time).
This commit is contained in:
Jehan 2021-04-06 12:39:52 +02:00
parent acf104a0ab
commit 32310f5e4b
16 changed files with 60 additions and 277 deletions

View file

@ -23,13 +23,7 @@
#include "gimp.h"
struct _GimpChannelPrivate
{
gpointer unused;
};
G_DEFINE_TYPE_WITH_PRIVATE (GimpChannel, gimp_channel, GIMP_TYPE_DRAWABLE)
G_DEFINE_TYPE (GimpChannel, gimp_channel, GIMP_TYPE_DRAWABLE)
#define parent_class gimp_drawable_parent_class
@ -42,7 +36,6 @@ gimp_channel_class_init (GimpChannelClass *klass)
static void
gimp_channel_init (GimpChannel *channel)
{
channel->priv = gimp_channel_get_instance_private (channel);
}
/**

View file

@ -33,23 +33,9 @@ G_BEGIN_DECLS
#define GIMP_TYPE_CHANNEL (gimp_channel_get_type ())
#define GIMP_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_CHANNEL, GimpChannel))
#define GIMP_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_CHANNEL, GimpChannelClass))
#define GIMP_IS_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_CHANNEL))
#define GIMP_IS_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_CHANNEL))
#define GIMP_CHANNEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_CHANNEL, GimpChannelClass))
G_DECLARE_DERIVABLE_TYPE (GimpChannel, gimp_channel, GIMP, CHANNEL, GimpDrawable)
typedef struct _GimpChannelClass GimpChannelClass;
typedef struct _GimpChannelPrivate GimpChannelPrivate;
struct _GimpChannel
{
GimpDrawable parent_instance;
GimpChannelPrivate *priv;
};
struct _GimpChannelClass
{
GimpDrawableClass parent_class;
@ -66,8 +52,6 @@ struct _GimpChannelClass
};
GType gimp_channel_get_type (void) G_GNUC_CONST;
GimpChannel * gimp_channel_get_by_id (gint32 channel_id);
GimpChannel * gimp_channel_new (GimpImage *image,

View file

@ -26,13 +26,7 @@
#include "gimptilebackendplugin.h"
struct _GimpDrawablePrivate
{
gpointer unused;
};
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GimpDrawable, gimp_drawable, GIMP_TYPE_ITEM)
G_DEFINE_ABSTRACT_TYPE (GimpDrawable, gimp_drawable, GIMP_TYPE_ITEM)
#define parent_class gimp_drawable_parent_class
@ -45,7 +39,6 @@ gimp_drawable_class_init (GimpDrawableClass *klass)
static void
gimp_drawable_init (GimpDrawable *drawable)
{
drawable->priv = gimp_drawable_get_instance_private (drawable);
}

View file

@ -32,24 +32,11 @@ G_BEGIN_DECLS
#include <libgimp/gimpitem.h>
#define GIMP_TYPE_DRAWABLE (gimp_drawable_get_type ())
#define GIMP_DRAWABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_DRAWABLE, GimpDrawable))
#define GIMP_DRAWABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_DRAWABLE, GimpDrawableClass))
#define GIMP_IS_DRAWABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_DRAWABLE))
#define GIMP_IS_DRAWABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_DRAWABLE))
#define GIMP_DRAWABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_DRAWABLE, GimpDrawableClass))
G_DECLARE_DERIVABLE_TYPE (GimpDrawable, gimp_drawable, GIMP, DRAWABLE, GimpItem)
typedef struct _GimpDrawableClass GimpDrawableClass;
typedef struct _GimpDrawablePrivate GimpDrawablePrivate;
struct _GimpDrawable
{
GimpItem parent_instance;
GimpDrawablePrivate *priv;
};
struct _GimpDrawableClass
{
GimpItemClass parent_class;
@ -67,8 +54,6 @@ struct _GimpDrawableClass
};
GType gimp_drawable_get_type (void) G_GNUC_CONST;
GimpDrawable * gimp_drawable_get_by_id (gint32 drawable_id);
GeglBuffer * gimp_drawable_get_buffer (GimpDrawable *drawable);

View file

@ -36,9 +36,9 @@ enum
N_PROPS
};
struct _GimpImagePrivate
struct _GimpImage
{
GObject parent_instance;
gint id;
};
@ -53,7 +53,7 @@ static void gimp_image_get_property (GObject *object,
GParamSpec *pspec);
G_DEFINE_TYPE_WITH_PRIVATE (GimpImage, gimp_image, G_TYPE_OBJECT)
G_DEFINE_TYPE (GimpImage, gimp_image, G_TYPE_OBJECT)
#define parent_class gimp_image_parent_class
@ -82,7 +82,6 @@ gimp_image_class_init (GimpImageClass *klass)
static void
gimp_image_init (GimpImage *image)
{
image->priv = gimp_image_get_instance_private (image);
}
static void
@ -96,7 +95,7 @@ gimp_image_set_property (GObject *object,
switch (property_id)
{
case PROP_ID:
image->priv->id = g_value_get_int (value);
image->id = g_value_get_int (value);
break;
default:
@ -116,7 +115,7 @@ gimp_image_get_property (GObject *object,
switch (property_id)
{
case PROP_ID:
g_value_set_int (value, image->priv->id);
g_value_set_int (value, image->id);
break;
default:
@ -139,7 +138,7 @@ gimp_image_get_property (GObject *object,
gint32
gimp_image_get_id (GimpImage *image)
{
return image ? image->priv->id : -1;
return image ? image->id : -1;
}
/**
@ -486,12 +485,12 @@ gimp_image_get_thumbnail_data (GimpImage *image,
/**
* gimp_image_get_thumbnail:
* @image: the image ID
* @image: the #GimpImage
* @width: the requested thumbnail width (<= 1024 pixels)
* @height: the requested thumbnail height (<= 1024 pixels)
* @alpha: how to handle an alpha channel
*
* Retrieves a thumbnail pixbuf for the image identified by @image->priv->id.
* Retrieves a thumbnail pixbuf for @image.
* The thumbnail will be not larger than the requested size.
*
* Returns: (transfer full): a new #GdkPixbuf

View file

@ -30,42 +30,11 @@ G_BEGIN_DECLS
/* For information look into the C source or the html documentation */
#define GIMP_TYPE_IMAGE (gimp_image_get_type ())
#define GIMP_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_IMAGE, GimpImage))
#define GIMP_IMAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_IMAGE, GimpImageClass))
#define GIMP_IS_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_IMAGE))
#define GIMP_IS_IMAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_IMAGE))
#define GIMP_IMAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_IMAGE, GimpImageClass))
G_DECLARE_FINAL_TYPE (GimpImage, gimp_image, GIMP, IMAGE, GObject)
typedef struct _GimpImageClass GimpImageClass;
typedef struct _GimpImagePrivate GimpImagePrivate;
struct _GimpImage
{
GObject parent_instance;
GimpImagePrivate *priv;
};
struct _GimpImageClass
{
GObjectClass parent_class;
/* Padding for future expansion */
void (*_gimp_reserved1) (void);
void (*_gimp_reserved2) (void);
void (*_gimp_reserved3) (void);
void (*_gimp_reserved4) (void);
void (*_gimp_reserved5) (void);
void (*_gimp_reserved6) (void);
void (*_gimp_reserved7) (void);
void (*_gimp_reserved8) (void);
};
GType gimp_image_get_type (void) G_GNUC_CONST;
gint32 gimp_image_get_id (GimpImage *image);
GimpImage * gimp_image_get_by_id (gint32 image_id);

View file

@ -37,10 +37,10 @@ enum
};
struct _GimpItemPrivate
typedef struct _GimpItemPrivate
{
gint id;
};
} GimpItemPrivate;
static void gimp_item_set_property (GObject *object,
@ -82,7 +82,6 @@ gimp_item_class_init (GimpItemClass *klass)
static void
gimp_item_init (GimpItem *item)
{
item->priv = gimp_item_get_instance_private (item);
}
static void
@ -92,11 +91,12 @@ gimp_item_set_property (GObject *object,
GParamSpec *pspec)
{
GimpItem *item = GIMP_ITEM (object);
GimpItemPrivate *priv = gimp_item_get_instance_private (item);
switch (property_id)
{
case PROP_ID:
item->priv->id = g_value_get_int (value);
priv->id = g_value_get_int (value);
break;
default:
@ -112,11 +112,12 @@ gimp_item_get_property (GObject *object,
GParamSpec *pspec)
{
GimpItem *item = GIMP_ITEM (object);
GimpItemPrivate *priv = gimp_item_get_instance_private (item);
switch (property_id)
{
case PROP_ID:
g_value_set_int (value, item->priv->id);
g_value_set_int (value, priv->id);
break;
default:
@ -140,7 +141,16 @@ gimp_item_get_property (GObject *object,
gint32
gimp_item_get_id (GimpItem *item)
{
return item ? item->priv->id : -1;
if (item)
{
GimpItemPrivate *priv = gimp_item_get_instance_private (item);
return priv->id;
}
else
{
return -1;
}
}
/**

View file

@ -32,23 +32,9 @@ G_BEGIN_DECLS
#define GIMP_TYPE_ITEM (gimp_item_get_type ())
#define GIMP_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_ITEM, GimpItem))
#define GIMP_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_ITEM, GimpItemClass))
#define GIMP_IS_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_ITEM))
#define GIMP_IS_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_ITEM))
#define GIMP_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_ITEM, GimpItemClass))
G_DECLARE_DERIVABLE_TYPE (GimpItem, gimp_item, GIMP, ITEM, GObject)
typedef struct _GimpItemClass GimpItemClass;
typedef struct _GimpItemPrivate GimpItemPrivate;
struct _GimpItem
{
GObject parent_instance;
GimpItemPrivate *priv;
};
struct _GimpItemClass
{
GObjectClass parent_class;
@ -65,8 +51,6 @@ struct _GimpItemClass
};
GType gimp_item_get_type (void) G_GNUC_CONST;
gint32 gimp_item_get_id (GimpItem *item);
GimpItem * gimp_item_get_by_id (gint32 item_id);

View file

@ -25,13 +25,12 @@
#include "gimp.h"
struct _GimpLayerPrivate
struct _GimpLayer
{
gpointer unused;
GimpDrawable parent_instance;
};
G_DEFINE_TYPE_WITH_PRIVATE (GimpLayer, gimp_layer, GIMP_TYPE_DRAWABLE)
G_DEFINE_TYPE (GimpLayer, gimp_layer, GIMP_TYPE_DRAWABLE)
#define parent_class gimp_layer_parent_class
@ -44,7 +43,6 @@ gimp_layer_class_init (GimpLayerClass *klass)
static void
gimp_layer_init (GimpLayer *layer)
{
layer->priv = gimp_layer_get_instance_private (layer);
}

View file

@ -31,41 +31,9 @@ G_BEGIN_DECLS
#define GIMP_TYPE_LAYER (gimp_layer_get_type ())
#define GIMP_LAYER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_LAYER, GimpLayer))
#define GIMP_LAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_LAYER, GimpLayerClass))
#define GIMP_IS_LAYER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_LAYER))
#define GIMP_IS_LAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_LAYER))
#define GIMP_LAYER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_LAYER, GimpLayerClass))
G_DECLARE_FINAL_TYPE (GimpLayer, gimp_layer, GIMP, LAYER, GimpDrawable)
typedef struct _GimpLayerClass GimpLayerClass;
typedef struct _GimpLayerPrivate GimpLayerPrivate;
struct _GimpLayer
{
GimpDrawable parent_instance;
GimpLayerPrivate *priv;
};
struct _GimpLayerClass
{
GimpDrawableClass parent_class;
/* Padding for future expansion */
void (*_gimp_reserved1) (void);
void (*_gimp_reserved2) (void);
void (*_gimp_reserved3) (void);
void (*_gimp_reserved4) (void);
void (*_gimp_reserved5) (void);
void (*_gimp_reserved6) (void);
void (*_gimp_reserved7) (void);
void (*_gimp_reserved8) (void);
};
GType gimp_layer_get_type (void) G_GNUC_CONST;
GimpLayer * gimp_layer_get_by_id (gint32 layer_id);
GimpLayer * gimp_layer_new (GimpImage *image,

View file

@ -24,13 +24,13 @@
#include "gimp.h"
struct _GimpLayerMaskPrivate
struct _GimpLayerMask
{
gpointer unused;
GimpChannel parent_instance;
};
G_DEFINE_TYPE_WITH_PRIVATE (GimpLayerMask, gimp_layer_mask, GIMP_TYPE_CHANNEL)
G_DEFINE_TYPE (GimpLayerMask, gimp_layer_mask, GIMP_TYPE_CHANNEL)
#define parent_class gimp_layer_mask_parent_class
@ -43,7 +43,6 @@ gimp_layer_mask_class_init (GimpLayerMaskClass *klass)
static void
gimp_layer_mask_init (GimpLayerMask *layer_mask)
{
layer_mask->priv = gimp_layer_mask_get_instance_private (layer_mask);
}
/**

View file

@ -32,40 +32,9 @@ G_BEGIN_DECLS
#define GIMP_TYPE_LAYER_MASK (gimp_layer_mask_get_type ())
#define GIMP_LAYER_MASK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_LAYER_MASK, GimpLayerMask))
#define GIMP_LAYER_MASK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_LAYER_MASK, GimpLayerMaskClass))
#define GIMP_IS_LAYER_MASK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_LAYER_MASK))
#define GIMP_IS_LAYER_MASK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_LAYER_MASK))
#define GIMP_LAYER_MASK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_LAYER_MASK, GimpLayerMaskClass))
G_DECLARE_FINAL_TYPE (GimpLayerMask, gimp_layer_mask, GIMP, LAYER_MASK, GimpChannel)
typedef struct _GimpLayerMaskClass GimpLayerMaskClass;
typedef struct _GimpLayerMaskPrivate GimpLayerMaskPrivate;
struct _GimpLayerMask
{
GimpChannel parent_instance;
GimpLayerMaskPrivate *priv;
};
struct _GimpLayerMaskClass
{
GimpChannelClass parent_class;
/* Padding for future expansion */
void (*_gimp_reserved1) (void);
void (*_gimp_reserved2) (void);
void (*_gimp_reserved3) (void);
void (*_gimp_reserved4) (void);
void (*_gimp_reserved5) (void);
void (*_gimp_reserved6) (void);
void (*_gimp_reserved7) (void);
void (*_gimp_reserved8) (void);
};
GType gimp_layer_mask_get_type (void) G_GNUC_CONST;
GimpLayerMask * gimp_layer_mask_get_by_id (gint32 layer_mask_id);

View file

@ -23,13 +23,13 @@
#include "gimp.h"
struct _GimpSelectionPrivate
struct _GimpSelection
{
gpointer unused;
GimpChannel parent_instance;
};
G_DEFINE_TYPE_WITH_PRIVATE (GimpSelection, gimp_selection, GIMP_TYPE_CHANNEL)
G_DEFINE_TYPE (GimpSelection, gimp_selection, GIMP_TYPE_CHANNEL)
#define parent_class gimp_selection_parent_class
@ -42,7 +42,6 @@ gimp_selection_class_init (GimpSelectionClass *klass)
static void
gimp_selection_init (GimpSelection *selection)
{
selection->priv = gimp_selection_get_instance_private (selection);
}
/**

View file

@ -31,41 +31,9 @@ G_BEGIN_DECLS
#define GIMP_TYPE_SELECTION (gimp_selection_get_type ())
#define GIMP_SELECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_SELECTION, GimpSelection))
#define GIMP_SELECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_SELECTION, GimpSelectionClass))
#define GIMP_IS_SELECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_SELECTION))
#define GIMP_IS_SELECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_SELECTION))
#define GIMP_SELECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_SELECTION, GimpSelectionClass))
G_DECLARE_FINAL_TYPE (GimpSelection, gimp_selection, GIMP, SELECTION, GimpChannel)
typedef struct _GimpSelectionClass GimpSelectionClass;
typedef struct _GimpSelectionPrivate GimpSelectionPrivate;
struct _GimpSelection
{
GimpChannel parent_instance;
GimpSelectionPrivate *priv;
};
struct _GimpSelectionClass
{
GimpChannelClass parent_class;
/* Padding for future expansion */
void (*_gimp_reserved1) (void);
void (*_gimp_reserved2) (void);
void (*_gimp_reserved3) (void);
void (*_gimp_reserved4) (void);
void (*_gimp_reserved5) (void);
void (*_gimp_reserved6) (void);
void (*_gimp_reserved7) (void);
void (*_gimp_reserved8) (void);
};
GType gimp_selection_get_type (void) G_GNUC_CONST;
GimpSelection * gimp_selection_get_by_id (gint32 selection_id);
GimpLayer * gimp_selection_float (GimpImage *image,
@ -78,4 +46,3 @@ GimpLayer * gimp_selection_float (GimpImage *image,
G_END_DECLS
#endif /* __GIMP_SELECTION_H__ */

View file

@ -24,13 +24,12 @@
#include "gimp.h"
struct _GimpVectorsPrivate
struct _GimpVectors
{
gpointer unused;
GimpItem parent_instance;
};
G_DEFINE_TYPE_WITH_PRIVATE (GimpVectors, gimp_vectors, GIMP_TYPE_ITEM)
G_DEFINE_TYPE (GimpVectors, gimp_vectors, GIMP_TYPE_ITEM)
#define parent_class gimp_vectors_parent_class
@ -43,7 +42,6 @@ gimp_vectors_class_init (GimpVectorsClass *klass)
static void
gimp_vectors_init (GimpVectors *vectors)
{
vectors->priv = gimp_vectors_get_instance_private (vectors);
}
/**

View file

@ -32,41 +32,9 @@ G_BEGIN_DECLS
#define GIMP_TYPE_VECTORS (gimp_vectors_get_type ())
#define GIMP_VECTORS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_VECTORS, GimpVectors))
#define GIMP_VECTORS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_VECTORS, GimpVectorsClass))
#define GIMP_IS_VECTORS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_VECTORS))
#define GIMP_IS_VECTORS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_VECTORS))
#define GIMP_VECTORS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_VECTORS, GimpVectorsClass))
G_DECLARE_FINAL_TYPE (GimpVectors, gimp_vectors, GIMP, VECTORS, GimpItem)
typedef struct _GimpVectorsClass GimpVectorsClass;
typedef struct _GimpVectorsPrivate GimpVectorsPrivate;
struct _GimpVectors
{
GimpItem parent_instance;
GimpVectorsPrivate *priv;
};
struct _GimpVectorsClass
{
GimpItemClass parent_class;
/* Padding for future expansion */
void (*_gimp_reserved1) (void);
void (*_gimp_reserved2) (void);
void (*_gimp_reserved3) (void);
void (*_gimp_reserved4) (void);
void (*_gimp_reserved5) (void);
void (*_gimp_reserved6) (void);
void (*_gimp_reserved7) (void);
void (*_gimp_reserved8) (void);
};
GType gimp_vectors_get_type (void) G_GNUC_CONST;
GimpVectors * gimp_vectors_get_by_id (gint32 vectors_id);