Re: [Vala] Display values of an enum.



I had never heard of EnumClass or EnumValue until I saw your code but
after a quick play around with it the following seems to actually
work:


enum Something
{
        TEST1,
        TEST2,
        TEST3;
        
        public string toString ()
        {
                return ((EnumClass)typeof (Something).class_ref ()).get_value
(this).value_name;
        }
}

void main()
{
        Something e  = Something.TEST3;
        
        print (e.toString () + "\n");
}


bob tbird:~/Projects/obj$ valac enumtest.vala
bob tbird:~/Projects/obj$ ./enumtest
SOMETHING_TEST3


Its a bit of a perl one-liner. Maybe someone more experienced knows a shortcut?

You can also use "value_nick" to get a less fully qualified name but
it seems to convert to lowercase with hyphens.  "maximum" and
"minimum" work as well instead of "get_value(this)" if you want to
enumerate;

Hope this helps

On 10 July 2010 20:20, Peterpaul Klein Haneveld <pp_kl_h hotmail com> wrote:






Hi All,

I've been interested in vala from the beginning, but only
 recently started doing some simple tests in it. In my daily work I'm a
Software Engineer developing in Java/JEE. In my private projects I use C
 mostly (using my own object system, so I can develop object oriented.)

I'm
 porting a small java framework (I wrote myself) for writing Command
Line Interfaces (CLI) to vala, in order to get more acquainted with
vala. Now I'm having a problem with Enumerations. I'm trying to iterate
over the values of an Enumeration. My testcase looks as follows:

---
 start of EnumTest.vala:
enum EnumTest {TEST1, TEST2, TEST3}

void
 main() {
   EnumClass enumClass = (EnumClass) typeof
(EnumTest).class_ref();
   unowned EnumValue[] falues =
enumClass.values;
   for (int i = 0; i < falues.length; i++) {

 unowned EnumValue falue = falues[i];
       stdout.printf("%s",
falue.value_name);
   }
}
--- end of EnumTest.vala.

First
 of all, I don't know whether this is the way it should be done, but at
least valac -C doesn't complain. Please enlighten me if I'm doing
anything wrong in the vala code :-).
I'm trying to compile this in
Ubuntu Lucid, 64-bits, using the vala ppa, but I get an error on the
generated C code.

$ uname -a


Linux peterpaul-vaio 2.6.32-23-generic #37-Ubuntu SMP Fri Jun 11
08:03:28 UTC 2010 x86_64 GNU/Linux


$ valac --version


Vala 0.9.1
$ valac EnumTest.vala
/home/peterpaul/projects/vala/EnumTest.vala.c:
 In function ‘_vala_main’:
/home/peterpaul/projects/vala/EnumTest.vala.c:45:
 warning: assignment from incompatible pointer type
/home/peterpaul/projects/vala/EnumTest.vala.c:45:
 error: ‘GEnumClass’ has no member named ‘values_length1’
error: cc
exited with status 256
Compilation failed: 1 error(s), 0 warning(s)

I've
 also generated the C code using 'valac -C', and studied the generated
code. Indeed it contains a reference to values_length1. By googling
around, I found that this might need to be: 'n_values'. However when
changing this in the C file, compilation still gives me another error:

$
 gcc -g3 -o enumtest EnumTest.c `pkg-config --libs --cflags glib-2.0`
`pkg-config --libs --cflags gobject-2.0`
EnumTest.c: In function
‘_vala_main’:
EnumTest.c:45: warning: assignment from incompatible
pointer type

Here is the relevant section from the C file:
---
 start part of EnumTest.c
39:    GEnumClass* enumClass;
40:
GEnumValue** _tmp0_;
41:    gint _falues_size_;
42:    gint
falues_length1;
43:    GEnumValue** falues;
44:    enumClass =
(GEnumClass*) g_type_class_ref (TYPE_ENUM_TEST);
45:    falues =
(_tmp0_ = enumClass->values, falues_length1 = enumClass->n_values,
 _falues_size_ = falues_length1, _tmp0_);
--- end part of EnumTest.c

When
 I explicitly cast in line 45: falues = (_tmp0_ = (GEnumValue**)
enumClass->values, falues_length1 = enumClass->n_values,
_falues_size_ = falues_length1, _tmp0_);
I get a segmentation fault
in the generated binary (enumtest).

Coming to a conclusion, I
think I've found a bug in the vala compiler, in that it doesn't generate
 the correct code for the EnumClass length of values. But also when I
try to fix the generated C code, I cannot create something working. Note
 that I'm not familiar with the internals of the GObject system. So in
the end it could still be my lack of knowledge. I hope that someone can
have a look at this, and tell me how it should be done, or verify
valac's behavior.

Thanks in advance,
Peterpaul Klein Haneveld.
_________________________________________________________________
Al je email accounts in 1 inbox. Het kan in Hotmail.
http://www.microsoft.com/netherlands/windowslive/Views/productdetail.aspx?product=Hotmail
_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]