Difference between revisions of "Hacking:Code Snippets/GObject"

From GIMP Developer Wiki
Jump to: navigation, search
(Implementation)
(Corrected origin 'Java class' to 'C++ class')
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
The example described here is a GObject implementation of the following Java class:
+
The example described here is a GObject implementation of the following C++ class:
  
 
<pre>class GimpCageConfig : public GimpImageMapConfig
 
<pre>class GimpCageConfig : public GimpImageMapConfig
Line 24: Line 24:
 
struct _GimpCageConfig
 
struct _GimpCageConfig
 
{
 
{
   GimpImageMapConfig parent_instance;
+
   GimpImageMapConfig parent_instance;
 
};
 
};
  
 
struct _GimpCageConfigClass
 
struct _GimpCageConfigClass
 
{
 
{
   GimpImageMapConfigClass parent_class;
+
   GimpImageMapConfigClass parent_class;
 
};
 
};
  
GType         gimp_cage_config_get_type               (void) G_GNUC_CONST;</pre>
+
GType gimp_cage_config_get_type (void) G_GNUC_CONST;</pre>
 
+
  
 
== Implementation ==
 
== Implementation ==
Line 42: Line 41:
  
  
G_DEFINE_TYPE_WITH_CODE (GimpCageConfig, gimp_cage_config,
+
G_DEFINE_TYPE (GimpCageConfig,
                        GIMP_TYPE_IMAGE_MAP_CONFIG,
+
              gimp_cage_config,
                        G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG,
+
              GIMP_TYPE_IMAGE_MAP_CONFIG)
                                                NULL))
+
  
 
static void
 
static void
Line 61: Line 59:
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 
}</pre>
 
}</pre>
 +
 +
== Code Generator ==
 +
 +
A good way to learn or hack GObject is to use a boilerplate generator. There is a good one in the Anjuta IDE.

Latest revision as of 16:29, 23 August 2012

The example described here is a GObject implementation of the following C++ class:

class GimpCageConfig : public GimpImageMapConfig
{
  public:
    GimpCageConfig() {};
    virtual ~GimpCageConfig() {};
};


Header

#define GIMP_TYPE_CAGE_CONFIG            (gimp_cage_config_get_type ())
#define GIMP_CAGE_CONFIG(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_CAGE_CONFIG, GimpCageConfig))
#define GIMP_CAGE_CONFIG_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_CAGE_CONFIG, GimpCageConfigClass))
#define GIMP_IS_CAGE_CONFIG(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_CAGE_CONFIG))
#define GIMP_IS_CAGE_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_CAGE_CONFIG))
#define GIMP_CAGE_CONFIG_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_CAGE_CONFIG, GimpCageConfigClass))


typedef struct _GimpCageConfigClass GimpCageConfigClass;

struct _GimpCageConfig
{
  GimpImageMapConfig parent_instance;
};

struct _GimpCageConfigClass
{
  GimpImageMapConfigClass parent_class;
};

GType gimp_cage_config_get_type (void) G_GNUC_CONST;

Implementation

#include "gimpcageconfig.h"

static void gimp_cage_config_finalize (GObject *object);


G_DEFINE_TYPE (GimpCageConfig,
               gimp_cage_config,
               GIMP_TYPE_IMAGE_MAP_CONFIG)

static void
gimp_cage_config_class_init (GimpCageConfigClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  object_class->finalize     = gimp_cage_config_finalize;
}

static void
gimp_cage_config_finalize (GObject *object)
{
  GimpCageConfig  *gcc = GIMP_CAGE_CONFIG (object);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

Code Generator

A good way to learn or hack GObject is to use a boilerplate generator. There is a good one in the Anjuta IDE.