Re: [Vala] Vala-list Digest, Vol 26, Issue 8

10 views
Skip to first unread message

golnaz nilieh

unread,
Jan 6, 2010, 4:20:14 AM1/6/10
to ali....@gmail.com, vala...@gnome.org
>
>
> On Wed, Jan 6, 2010 at 7:28 AM, Jan Hudec <bu...@ucw.cz> wrote:
> >
> >
> > On Tue, January 5, 2010 13:12, Frederik wrote:
> >> Non-value-type static class variables are only initialized after the
> >> class was instantiated at least once. This behaviour is a little bit
> >> counter-intuitive, and I hope it will change in the future.
> >
> > I have to disappoint you -- that behaviour can't change.
> >
> > The problem is, that the while the mechanism for static constructors
> > exists for C++, there does not seem to be a portable way to use it from
> > C (in gcc you can use the __attribute__((constructor)), but that's an
> > extension).
> >
> >> You have several options:
> >>
> >> - create a throw-away instance:
> >>
> >> ? static int main (string[] args) {
> >> ? ? ? new Global ();
> >> ? ? ? stdout.printf ("all data is in: " + Global.dataDir);
> >> ? ? ? return 0;
> >> ? }
> >
> > Actually, there is no need to do that -- calling typeof(Global); is
> enough.
> >
> >> - call a static initialization method:
> >>
> >> ? static int main (string[] args) {
> >> ? ? ? Global.init ();
> >> ? ? ? stdout.printf ("all data is in: " + Global.dataDir);
> >> ? ? ? return 0;
> >> ? }
> >
> > This should not work. *static* methods do not cause a class to be
> > initialized (*class* methods do, though).
> >
> >> - make 'dataDir' const, if it is not intended to change
> >
> > - make the variable a class one instead of static one.
> >
> > public class Global {
> > ? ?class string dataDir = "whatever";
> > }
> >
>
> To me to correct way is to use:
> public static const string dataDir = "/usr/local/share/";
> instead of:
> public static string dataDir = "/usr/local/share/";
>
> Also please follow the Vala coding conventions (data_dir instead of
> dataDir)
>
> --
> Ali
>


Thanks to all. I defined an empty constructor for Global, and called it in
main function. also I changed dataDir to a constant variable.
But a new question: Is there a way to pass the installation path to dataDir?
I use this macro in configure.ac:

AC_PREFIX_DEFAULT(/usr/local)

can i use it as a variable in a vala source file?

Frederik

unread,
Jan 6, 2010, 4:56:32 AM1/6/10
to vala-list
golnaz nilieh wrote:
> Thanks to all. I defined an empty constructor for Global, and called it in
> main function. also I changed dataDir to a constant variable.
> But a new question: Is there a way to pass the installation path to dataDir?
> I use this macro in configure.ac:
>
> AC_PREFIX_DEFAULT(/usr/local)
>
> can i use it as a variable in a vala source file?

Yes, all you need is a small vapi file for your 'config.h', e.g.:
http://code.google.com/p/xnoise/source/browse/vapi/config.vapi

Use it with

--pkg config --vapidir path/to/my/vapi

and access it via

Config.DATADIR


Best regards,

Frederik
_______________________________________________
Vala-list mailing list
Vala...@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list

Ali Sabil

unread,
Jan 6, 2010, 4:56:39 AM1/6/10
to golnaz nilieh, vala...@gnome.org

If you define dataDir as constant you don't need the constructor and
you don't need to call it.

> But a new question: Is there a way to pass the installation path to dataDir?
> I use this macro in configure.ac:
>
> AC_PREFIX_DEFAULT(/usr/local)
>
> can i use it as a variable in a vala source file?
>

I already posted a message about that in this mailing list, but it
seems like you are subscribing to a mail digest, it will take a while
before you get it.

Andrea Bolognani

unread,
Jan 6, 2010, 9:17:59 AM1/6/10
to vala...@gnome.org
On Wed, Jan 06, 2010 at 10:56:32AM +0100, Frederik wrote:

> golnaz nilieh wrote:
> > Thanks to all. I defined an empty constructor for Global, and called it in
> > main function. also I changed dataDir to a constant variable.
> > But a new question: Is there a way to pass the installation path to dataDir?
> > I use this macro in configure.ac:
> >
> > AC_PREFIX_DEFAULT(/usr/local)
> >
> > can i use it as a variable in a vala source file?
>
> Yes, all you need is a small vapi file for your 'config.h', e.g.:
> http://code.google.com/p/xnoise/source/browse/vapi/config.vapi
>
> Use it with
>
> --pkg config --vapidir path/to/my/vapi
>
> and access it via
>
> Config.DATADIR

I like this solution. Only, I can't seem to find a way to get DATADIR
defined in config.h.

Looking at xnoise, I was unable to find any obvious spot where the magic
happens. Is that done by gnome-autogen? I have a custom autogen.sh. Or am
I missing something hidden in plain sight?

--
Andrea Bolognani <e...@kiyuko.org>
Resistance is futile, you will be garbage collected.

signature.asc

Ali Sabil

unread,
Jan 6, 2010, 10:22:28 AM1/6/10
to Andrea Bolognani, vala...@gnome.org
Reply all
Reply to author
Forward
0 new messages