Re: [Vala] Read object's properties



Hi!

Thanks for the advice, it works.
But isn't there a more general way to achieve my goal? A way to access
all the data from an instance? And why does the `sizeof` function
doesn't work correctly for classes?
I looked into the generated C sources and understand that a Vala class
is split into several structs but my knowledge of C (and the GObject
system in special) isn't that good, so I cannot implement a save
function in C myself (at least not yet and I would still prefer to do it
in Vala).
But from a user's point of view I have to say that I expected the normal
`sizeof` to work with all data types and still think so. Where is the
sense of a `sizeof` function for data types which size is already known?
Why should I test everytime at runtime for the size of e.g. an int?
Don't get me wrong, I still like Vala ;-) But I'm a little bit shocked
that there seems to be no way to simply read a class directly from
memory.

Hope somebody proofs me wrong,
Philipp.


Am Montag, den 23.03.2015, 13:10 +0100 schrieb Andrea Del Signore:
Hi,


because you defined the "attributes" as fields (eg. private class
instance variable) and not a properties.


See https://wiki.gnome.org/Projects/Vala/Tutorial#Properties for more
details.


Try to change the Test class as:

public class Test : GLib.Object, Saveable {
        public string name { get; private set; }
        public int zahl { get; private set; }

        private List<string> liste;

        public bool boolean { get; set; }
        protected uint16 kleine;

        internal int32 integ;

        public Test (string name, int zahl, List<string> liste) {
                this.name = name;
                this.zahl = zahl;
                this.liste = liste.copy ();
        }
}


you should at least see the name, zahl and boolean properties listed
when calling the save_object function.


Ciao,

Andrea





On Mon, Mar 23, 2015 at 12:15 PM, Philipp Trommler
<keinerschreibtmirwas msn com> wrote:
        Hi!
        
        This is my first post on this list so I just want to say hello
        to
        everybody. My name is Philipp Trommler, I'm from Dresden,
        Germany and
        I'm programming Vala as a hobby. I really like Vala as I like
        the
        syntax, the fact that it's performant and the good integration
        into the
        gnome ecosystem. In the last days I wanted to dig a bit deeper
        into
        serious Vala programming what leads me to my question:
        
        What are common ways to access an object's attributes? I
        checked and
        tried `GLib.ParamSpec` but receiving an empty array all the
        time. Is
        there any other method?
        
        The background is that I want to save an object's attributes
        into a
        file. Alternatively I would save the whole object (that's what
        I tried
        before, without luck, because I can't figure out how to access
        all of
        the object's parts in memory), but accessing the attributes
        would be
        enough for now.
        
        Maybe I'm just doing something wrong, so here's my code:
        
        ```
        public interface Saveable : GLib.Object {
                public virtual void save_object (string filename) {
                        unowned GLib.ObjectClass object_class =
        this.get_class ();
                        GLib.File file = GLib.File.new_for_path
        (filename);
                        GLib.FileOutputStream file_output_stream =
        file.create
        (GLib.FileCreateFlags.PRIVATE |
        GLib.FileCreateFlags.REPLACE_DESTINATION);
                        foreach (GLib.ParamSpec param_spec in
        object_class.list_properties ())
        {
                                stdout.printf ("%s\n",
        param_spec.get_name ());
                        }
                }
        }
        
        public class Test : GLib.Object, Saveable {
                private string name;
                private int zahl;
                private List<string> liste;
        
                public bool boolean;
                protected uint16 kleine;
        
                internal int32 integ;
        
                public Test (string name, int zahl, List<string>
        liste) {
                        this.name = name;
                        this.zahl = zahl;
                        this.liste = liste.copy ();
                }
        }
        ```
        
        So, as you can see, the class `Test` has some attributes. Why
        can't I
        access any of them?
        
        Greetings,
        Philipp.
        
        _______________________________________________
        vala-list mailing list
        vala-list gnome org
        https://mail.gnome.org/mailman/listinfo/vala-list
        



Attachment: signature.asc
Description: This is a digitally signed message part



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