[Vala] Chain up base constructor troubles with C code...
Hi there, to avoid some nasty duplicated code I try to use inheritance with a base abstract class and derived views. I need to create a terminal view and a folder view, while it works fine with my terminal view which is written in Vala, I've some troubles with the folder view which is C code. :( I have the following : namespace Manager { public class FolderView : Fm.FolderView, BaseView { construct { base.new (Fm.FolderViewMode.LIST_VIEW); //Object (); //base (Fm.FolderViewMode.LIST_VIEW); } public FolderView () { //Object (mode: Fm.FolderViewMode.LIST_VIEW); //base (Fm.FolderViewMode.LIST_VIEW); base.new (Fm.FolderViewMode.LIST_VIEW); } } } I tried a few things without much success, the problem is that the base object doesn't seem to be created. BaseView is an abstract class in Vala and Fm.FolderView is my base object in C, the one that gives my some troubles. :-P In my Vapi file for the base object, I have : [CCode (cheader_filename = gtk/fm-folder-view.h)] public class FolderView : Gtk.ScrolledWindow, Atk.Implementor, Gtk.Buildable { public Fm.FolderViewMode mode; [CCode (has_construct_function = false, cname = fm_folder_view_new, type = GtkWidget*)] public FolderView (int mode); } The problem is that the new function (fm_folder_view_new) is not called from Vala code. I tried to use Object (mode: Fm.FolderViewMode.LIST_VIEW); but mode is not a property and it fails. Is there a way so that when doing var = new Manager.FolderView (); the base function fm_folder_view_new is called ? Thanks. :) -- Axel FILMORE ## https://github.com/afilmore ## Vala - Compiler For The GObject Type System https://live.gnome.org/Vala ## ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Chain up base constructor troubles with C code...
Axel, I believe that when you're inheriting from a C class in Vala code, calling the base class's constructor is not supported [1]. Instead, just replicate the original constructor's functionality at the beginning of your derived class's constructor: public class FolderView : Fm.FolderView, BaseView { public FolderView() { // initialize Fm.FolderView's properties fmFoldViewProp1 = foo; ... // do FolderView-specific initialization foldViewProp1 = bar; } } - Kerrick [1] As far as I understand it, this is because the C constructors (e.g. fm_folder_view_new) do two logically separate things: 1) allocate memory for the new object 2) initialize the fields comprising that memory Since (in general) a derived object occupies more memory than the corresponding base object, the base constructor generally doesn't allocate enough memory at step (1), and so the object returned by the base constructor is pretty much useless (unless the derived constructor wants to call realloc, but that would incur a performance penalty). Instead, each constructor has to duplicate the functionality of its parent constructor. In Vala, they handled this problem a little more nicely, and so constructor chaining works as expected, but again this only works if you're inheriting from a Vala class. This discussion has more information: http://osdir.com/ml/vala-list/2009-09/msg00376.html On Sun, May 27, 2012 at 6:51 AM, Axel FILMORE axel.film...@gmail.comwrote: Hi there, to avoid some nasty duplicated code I try to use inheritance with a base abstract class and derived views. I need to create a terminal view and a folder view, while it works fine with my terminal view which is written in Vala, I've some troubles with the folder view which is C code. :( I have the following : namespace Manager { public class FolderView : Fm.FolderView, BaseView { construct { base.new (Fm.FolderViewMode.LIST_VIEW); //Object (); //base (Fm.FolderViewMode.LIST_VIEW); } public FolderView () { //Object (mode: Fm.FolderViewMode.LIST_VIEW); //base (Fm.FolderViewMode.LIST_VIEW); base.new (Fm.FolderViewMode.LIST_VIEW); } } } I tried a few things without much success, the problem is that the base object doesn't seem to be created. BaseView is an abstract class in Vala and Fm.FolderView is my base object in C, the one that gives my some troubles. :-P In my Vapi file for the base object, I have : [CCode (cheader_filename = gtk/fm-folder-view.h)] public class FolderView : Gtk.ScrolledWindow, Atk.Implementor, Gtk.Buildable { public Fm.FolderViewMode mode; [CCode (has_construct_function = false, cname = fm_folder_view_new, type = GtkWidget*)] public FolderView (int mode); } The problem is that the new function (fm_folder_view_new) is not called from Vala code. I tried to use Object (mode: Fm.FolderViewMode.LIST_VIEW); but mode is not a property and it fails. Is there a way so that when doing var = new Manager.FolderView (); the base function fm_folder_view_new is called ? Thanks. :) -- Axel FILMORE #-**---# https://github.com/afilmore #-**---# Vala - Compiler For The GObject Type System https://live.gnome.org/Vala #-**---# __**_ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/**mailman/listinfo/vala-listhttps://mail.gnome.org/mailman/listinfo/vala-list ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Chain up base constructor troubles with C code...
On 27/05/2012 17:54, Kerrick Staley wrote: Axel, I believe that when you're inheriting from a C class in Vala code, calling the base class's constructor is not supported [1]. Instead, just replicate the original constructor's functionality at the beginning of your derived class's constructor: public class FolderView : Fm.FolderView, BaseView { public FolderView() { // initialize Fm.FolderView's properties fmFoldViewProp1 = foo; ... // do FolderView-specific initialization foldViewProp1 = bar; } } It works just great, this is what I have : public class FolderView : Fm.FolderView, BaseView { public FolderView (Gtk.Notebook parent, string directory) { // Object (); // not needed :) base.set_mode (Fm.FolderViewMode.LIST_VIEW); base.small_icon_size = 16; base.big_icon_size =36; base.single_click = false; } } I first tried to call Object () but you are absolutely correct, just setting the properties works as expected. Marvelous. :-P So, here's the result. The Folder View : http://pix.toile-libre.org/upload/original/1338136960.png The Terminal : http://pix.toile-libre.org/upload/original/1338137023.png Great information, thanks a lot, I'm not very familiar with GObject yet and I often have that kind of troubles with derived classes. Thank you, best regards. :) -- Axel FILMORE ## https://github.com/afilmore ## Vala - Compiler For The GObject Type System https://live.gnome.org/Vala ## ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list