Blame docs/reference/gobject/html/howto-interface-override.html

Packit ae235b
Packit ae235b
<html>
Packit ae235b
<head>
Packit ae235b
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Packit ae235b
<title>Overriding interface methods: GObject Reference Manual</title>
Packit ae235b
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
Packit ae235b
<link rel="home" href="index.html" title="GObject Reference Manual">
Packit ae235b
<link rel="up" href="howto-interface.html" title="How to define and implement interfaces">
Packit ae235b
<link rel="prev" href="howto-interface-properties.html" title="Interface properties">
Packit ae235b
<link rel="next" href="howto-signals.html" title="How to create and use signals">
Packit ae235b
<meta name="generator" content="GTK-Doc V1.27 (XML mode)">
Packit ae235b
<link rel="stylesheet" href="style.css" type="text/css">
Packit ae235b
</head>
Packit ae235b
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
Packit ae235b
Packit ae235b
Packit ae235b
Home
Packit ae235b
Up
Packit ae235b
Prev
Packit ae235b
Next
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Overriding interface methods
Packit ae235b

Packit ae235b
      If a base class already implements an interface and a derived
Packit ae235b
      class needs to implement the same interface but needs to override certain
Packit ae235b
      methods, you must reimplement the interface and set only the interface
Packit ae235b
      methods which need overriding.
Packit ae235b
    

Packit ae235b

Packit ae235b
      In this example, ViewerAudioFile is derived from
Packit ae235b
      ViewerFile. Both implement the ViewerEditable
Packit ae235b
      interface. ViewerAudioFile only implements one method of the
Packit ae235b
      ViewerEditable interface and uses the base class implementation of
Packit ae235b
      the other.
Packit ae235b

Packit ae235b
Packit ae235b
  
Packit ae235b
    
Packit ae235b
      
Packit ae235b
        
1
Packit ae235b
2
Packit ae235b
3
Packit ae235b
4
Packit ae235b
5
Packit ae235b
6
Packit ae235b
7
Packit ae235b
8
Packit ae235b
9
Packit ae235b
10
Packit ae235b
11
Packit ae235b
12
Packit ae235b
13
Packit ae235b
14
Packit ae235b
15
Packit ae235b
16
Packit ae235b
17
Packit ae235b
18
Packit ae235b
19
Packit ae235b
20
Packit ae235b
21
Packit ae235b
22
Packit ae235b
23
Packit ae235b
24
Packit ae235b
25
Packit ae235b
26
Packit ae235b
27
Packit ae235b
28
Packit ae235b
29
Packit ae235b
30
Packit ae235b
31
Packit ae235b
32
Packit ae235b
33
Packit ae235b
34
Packit ae235b
35
Packit ae235b
36
Packit ae235b
        
static void
Packit ae235b
viewer_audio_file_editable_save (ViewerEditable  *editable,
Packit ae235b
                                 GError         **error)
Packit ae235b
{
Packit ae235b
  ViewerAudioFile *self = VIEWER_AUDIO_FILE (editable);
Packit ae235b
Packit ae235b
  g_print ("Audio file implementation of editable interface save method.\n");
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
viewer_audio_file_editable_interface_init (ViewerEditableInterface *iface)
Packit ae235b
{
Packit ae235b
  /* Override the implementation of save(). */
Packit ae235b
  iface->save = viewer_audio_file_editable_save;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Leave iface->undo and ->redo alone, they are already set to the
Packit ae235b
   * base class implementation.
Packit ae235b
   */
Packit ae235b
}
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (ViewerAudioFile, viewer_audio_file, VIEWER_TYPE_FILE,
Packit ae235b
                         G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
Packit ae235b
                                                viewer_audio_file_editable_interface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
viewer_audio_file_class_init (ViewerAudioFileClass *klass)
Packit ae235b
{
Packit ae235b
  /* Nothing here. */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
viewer_audio_file_init (ViewerAudioFile *self)
Packit ae235b
{
Packit ae235b
  /* Nothing here. */
Packit ae235b
}
Packit ae235b
      
Packit ae235b
    
Packit ae235b
  
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
    

Packit ae235b

Packit ae235b
      To access the base class interface implementation use
Packit ae235b
      g_type_interface_peek_parent
Packit ae235b
      from within an interface's default_init function.
Packit ae235b
    

Packit ae235b

Packit ae235b
      To call the base class implementation of an interface
Packit ae235b
      method from an derived class where than interface method has been
Packit ae235b
      overridden, stash away the pointer returned from
Packit ae235b
      g_type_interface_peek_parent
Packit ae235b
      in a global variable.
Packit ae235b
    

Packit ae235b

Packit ae235b
      In this example ViewerAudioFile overrides the
Packit ae235b
      save interface method. In its overridden method
Packit ae235b
      it calls the base class implementation of the same interface method.
Packit ae235b

Packit ae235b
Packit ae235b
  
Packit ae235b
    
Packit ae235b
      
Packit ae235b
        
1
Packit ae235b
2
Packit ae235b
3
Packit ae235b
4
Packit ae235b
5
Packit ae235b
6
Packit ae235b
7
Packit ae235b
8
Packit ae235b
9
Packit ae235b
10
Packit ae235b
11
Packit ae235b
12
Packit ae235b
13
Packit ae235b
14
Packit ae235b
15
Packit ae235b
16
Packit ae235b
17
Packit ae235b
18
Packit ae235b
19
Packit ae235b
20
Packit ae235b
21
Packit ae235b
22
Packit ae235b
23
Packit ae235b
24
Packit ae235b
25
Packit ae235b
26
Packit ae235b
27
Packit ae235b
28
Packit ae235b
29
Packit ae235b
30
Packit ae235b
31
Packit ae235b
32
Packit ae235b
33
Packit ae235b
34
Packit ae235b
35
Packit ae235b
36
Packit ae235b
37
Packit ae235b
        
static ViewerEditableInterface *viewer_editable_parent_interface = NULL;
Packit ae235b
Packit ae235b
static void
Packit ae235b
viewer_audio_file_editable_save (ViewerEditable  *editable,
Packit ae235b
                                 GError         **error)
Packit ae235b
{
Packit ae235b
  ViewerAudioFile *self = VIEWER_AUDIO_FILE (editable);
Packit ae235b
Packit ae235b
  g_print ("Audio file implementation of editable interface save method.\n");
Packit ae235b
Packit ae235b
  /* Now call the base implementation */
Packit ae235b
  viewer_editable_parent_interface->save (editable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
viewer_audio_file_editable_interface_init (ViewerEditableInterface *iface)
Packit ae235b
{
Packit ae235b
  viewer_editable_parent_interface = g_type_interface_peek_parent (iface);
Packit ae235b
Packit ae235b
  iface->save = viewer_audio_file_editable_save;
Packit ae235b
}
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (ViewerAudioFile, viewer_audio_file, VIEWER_TYPE_FILE,
Packit ae235b
                         G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
Packit ae235b
                                                viewer_audio_file_editable_interface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
viewer_audio_file_class_init (ViewerAudioFileClass *klass)
Packit ae235b
{
Packit ae235b
  /* Nothing here. */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
viewer_audio_file_init (ViewerAudioFile *self)
Packit ae235b
{
Packit ae235b
  /* Nothing here. */
Packit ae235b
}
Packit ae235b
      
Packit ae235b
    
Packit ae235b
  
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
    

Packit ae235b
Packit ae235b
Packit ae235b

Generated by GTK-Doc V1.27
Packit ae235b
</body>
Packit ae235b
</html>