digitalmars.D - ClassInfo
- Kyle Furlong (13/13) Apr 28 2005 So I am working on persistence, and being a newbie, its tough. I have
- Jarrett Billingsley (3/5) Apr 29 2005 Yep.
- Kyle Furlong (3/13) Apr 29 2005 So the classinfo.init.length property is the current size of the object
- Tom S (13/28) Apr 29 2005 But... the object doesn't change it's size in memory... it's data can be...
- Kyle Furlong (17/48) Apr 29 2005 I have looked at mango, but it seems that Kris only provides the IPickle...
- Unknown W. Brackets (13/30) Apr 29 2005 Kyle, I'm afraid this is what I meant when I talked about "reflection"
- Kyle Furlong (7/43) Apr 30 2005 For the scheme I described above to work, each class would have to
- Ben Hinkle (13/40) Apr 29 2005 One of the tools I want to eventually use the "DMD front end starter kit...
- Kyle Furlong (6/53) Apr 30 2005 I saw dmdfe, but was unsure how to incorporate it, could you give
- Ben Hinkle (9/25) Apr 30 2005 The metadata is stored in the parse tree created in memory while reading...
- Kyle Furlong (4/34) Apr 30 2005 Gonna need more than that. I dont even know what to do with these stubs....
- Tom S (63/67) Apr 30 2005 I guess it's not what you are looking for, since the user of my lib has
- Kyle Furlong (3/88) Apr 30 2005 I think that for the meantime I would settle for the functionality you
- Tom S (4/6) Apr 30 2005 This code is public domain, you're welcome to use it.
So I am working on persistence, and being a newbie, its tough. I have this method: void Set(Object o) { file.Stream.writeBlock(cast(ubyte*)o, o.classinfo.init.length); } One specific question I have is, is classinfo populated with the info for the class that the object really is? For example: MyObject m = new MyObject; ObjectDatabase o = Dope.Open("filename"); o.Set(m); Of course I would like to have it work that way. Any tips, pointers, smacks on the head for being an idiot are welcome! Kyle
Apr 28 2005
"Kyle Furlong" <ky220 umail.ucsb.edu> wrote in message news:d4scbl$1ik8$1 digitaldaemon.com...One specific question I have is, is classinfo populated with the info for the class that the object really is? For example:Yep.
Apr 29 2005
Jarrett Billingsley wrote:"Kyle Furlong" <ky220 umail.ucsb.edu> wrote in message news:d4scbl$1ik8$1 digitaldaemon.com...So the classinfo.init.length property is the current size of the object in memory?One specific question I have is, is classinfo populated with the info for the class that the object really is? For example:Yep.
Apr 29 2005
Kyle Furlong wrote:Jarrett Billingsley wrote:But... the object doesn't change it's size in memory... it's data can be allocated dynamically, yet init.length doesn't change because only pointers/references are assigned some values. Note that you wouldn't like to serialize the object just plain writing its bytes to a file. Sure, you'd store some data, but whilst loading it in another program run, you'd load pointers/refernces that would no longer be valid. You might take a look at the Mango library, which has some serialization support. I've written my own lib, yet it needs some readme/tutorial... -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/"Kyle Furlong" <ky220 umail.ucsb.edu> wrote in message news:d4scbl$1ik8$1 digitaldaemon.com...So the classinfo.init.length property is the current size of the object in memory?One specific question I have is, is classinfo populated with the info for the class that the object really is? For example:Yep.
Apr 29 2005
Tom S wrote:Kyle Furlong wrote:I have looked at mango, but it seems that Kris only provides the IPickle interface, and leaves it to the user to implement the actually serializing code. I want to make a lib which can take any object passed to it without knowing what class it is, push it to a file, and be able to reconstitute it. I understand that the pointers wont be valid accross sessions, but there should be a way to substitute virtual pointers when writing to file, so that when trying to deserialize, you would take the root object, follow any of your "virtual pointers" to other objects stored, construct those objects in the same way, and pass back the "real" pointer to the newly created object to the root object. Does that scheme sound plausible? What I am looking for is something other than a class specific implementation, where you for example write a serialize method for each class. Maybe this isnt possible in D currently, but any other pointers anyone has, feel free to speak out. BTW Tom, what does your library actually do? Could you post some sample code? KyleJarrett Billingsley wrote:But... the object doesn't change it's size in memory... it's data can be allocated dynamically, yet init.length doesn't change because only pointers/references are assigned some values. Note that you wouldn't like to serialize the object just plain writing its bytes to a file. Sure, you'd store some data, but whilst loading it in another program run, you'd load pointers/refernces that would no longer be valid. You might take a look at the Mango library, which has some serialization support. I've written my own lib, yet it needs some readme/tutorial..."Kyle Furlong" <ky220 umail.ucsb.edu> wrote in message news:d4scbl$1ik8$1 digitaldaemon.com...So the classinfo.init.length property is the current size of the object in memory?One specific question I have is, is classinfo populated with the info for the class that the object really is? For example:Yep.
Apr 29 2005
Kyle, I'm afraid this is what I meant when I talked about "reflection" are going to have a real fun time making it completely transparent, as I don't know that you can. Now, if classes are self-aware and can serialize/unserialize itself with methods written for each class, that's more than possible. But if you want to automate that part transparently... well, that's specifically why some languages have reflection. Your method would work fine for any class that had no arrays, pointers, or other classes as members. Failing that, you'd have to "find" the pointers in a way much the same as garbage collecting, I suppose, and serialize those too. -[Unknown]I have looked at mango, but it seems that Kris only provides the IPickle interface, and leaves it to the user to implement the actually serializing code. I want to make a lib which can take any object passed to it without knowing what class it is, push it to a file, and be able to reconstitute it. I understand that the pointers wont be valid accross sessions, but there should be a way to substitute virtual pointers when writing to file, so that when trying to deserialize, you would take the root object, follow any of your "virtual pointers" to other objects stored, construct those objects in the same way, and pass back the "real" pointer to the newly created object to the root object. Does that scheme sound plausible? What I am looking for is something other than a class specific implementation, where you for example write a serialize method for each class. Maybe this isnt possible in D currently, but any other pointers anyone has, feel free to speak out. BTW Tom, what does your library actually do? Could you post some sample code? Kyle
Apr 29 2005
Unknown W. Brackets wrote:Kyle, I'm afraid this is what I meant when I talked about "reflection" are going to have a real fun time making it completely transparent, as I don't know that you can. Now, if classes are self-aware and can serialize/unserialize itself with methods written for each class, that's more than possible. But if you want to automate that part transparently... well, that's specifically why some languages have reflection. Your method would work fine for any class that had no arrays, pointers, or other classes as members. Failing that, you'd have to "find" the pointers in a way much the same as garbage collecting, I suppose, and serialize those too. -[Unknown]For the scheme I described above to work, each class would have to provide a method to tell the library which fields are which types. I can get at the fields in memory, but I cant tell what type they are. So you are right, it cannot be completely transparent without some outside metadata. KyleI have looked at mango, but it seems that Kris only provides the IPickle interface, and leaves it to the user to implement the actually serializing code. I want to make a lib which can take any object passed to it without knowing what class it is, push it to a file, and be able to reconstitute it. I understand that the pointers wont be valid accross sessions, but there should be a way to substitute virtual pointers when writing to file, so that when trying to deserialize, you would take the root object, follow any of your "virtual pointers" to other objects stored, construct those objects in the same way, and pass back the "real" pointer to the newly created object to the root object. Does that scheme sound plausible? What I am looking for is something other than a class specific implementation, where you for example write a serialize method for each class. Maybe this isnt possible in D currently, but any other pointers anyone has, feel free to speak out. BTW Tom, what does your library actually do? Could you post some sample code? Kyle
Apr 30 2005
One of the tools I want to eventually use the "DMD front end starter kit" for was a program (I've been calling it "deflect") that takes a class source file (say A) and generates a "metadata" class output source (for say MetaA) that can be compiled and linked with A so that code that wants to serialize and deserialize A can ask MetaA to do it. A meta class is a bit like the ClassInfo class but it has more hooks - eg the entire reflection API in Java would be possible. You can ask MetaA for the fields in A, their offsets, their types etc etc. If you are feeling ambitious you can grab the DMD Front End starter kit from http://home.comcast.net/~benhinkle/dmdfe/ and take a crack at it. Note that such a program would do more than support serialization so it might be too much for your particular needs. -BenBut... the object doesn't change it's size in memory... it's data can be allocated dynamically, yet init.length doesn't change because only pointers/references are assigned some values. Note that you wouldn't like to serialize the object just plain writing its bytes to a file. Sure, you'd store some data, but whilst loading it in another program run, you'd load pointers/refernces that would no longer be valid. You might take a look at the Mango library, which has some serialization support. I've written my own lib, yet it needs some readme/tutorial...I have looked at mango, but it seems that Kris only provides the IPickle interface, and leaves it to the user to implement the actually serializing code. I want to make a lib which can take any object passed to it without knowing what class it is, push it to a file, and be able to reconstitute it. I understand that the pointers wont be valid accross sessions, but there should be a way to substitute virtual pointers when writing to file, so that when trying to deserialize, you would take the root object, follow any of your "virtual pointers" to other objects stored, construct those objects in the same way, and pass back the "real" pointer to the newly created object to the root object. Does that scheme sound plausible? What I am looking for is something other than a class specific implementation, where you for example write a serialize method for each class. Maybe this isnt possible in D currently, but any other pointers anyone has, feel free to speak out. BTW Tom, what does your library actually do? Could you post some sample code? Kyle
Apr 29 2005
Ben Hinkle wrote:I saw dmdfe, but was unsure how to incorporate it, could you give examples, or instructions on how to go about getting at that meta class you talk about? Like I said, I am very new to the C/D paradigm, growing KyleOne of the tools I want to eventually use the "DMD front end starter kit" for was a program (I've been calling it "deflect") that takes a class source file (say A) and generates a "metadata" class output source (for say MetaA) that can be compiled and linked with A so that code that wants to serialize and deserialize A can ask MetaA to do it. A meta class is a bit like the ClassInfo class but it has more hooks - eg the entire reflection API in Java would be possible. You can ask MetaA for the fields in A, their offsets, their types etc etc. If you are feeling ambitious you can grab the DMD Front End starter kit from http://home.comcast.net/~benhinkle/dmdfe/ and take a crack at it. Note that such a program would do more than support serialization so it might be too much for your particular needs. -BenBut... the object doesn't change it's size in memory... it's data can be allocated dynamically, yet init.length doesn't change because only pointers/references are assigned some values. Note that you wouldn't like to serialize the object just plain writing its bytes to a file. Sure, you'd store some data, but whilst loading it in another program run, you'd load pointers/refernces that would no longer be valid. You might take a look at the Mango library, which has some serialization support. I've written my own lib, yet it needs some readme/tutorial...I have looked at mango, but it seems that Kris only provides the IPickle interface, and leaves it to the user to implement the actually serializing code. I want to make a lib which can take any object passed to it without knowing what class it is, push it to a file, and be able to reconstitute it. I understand that the pointers wont be valid accross sessions, but there should be a way to substitute virtual pointers when writing to file, so that when trying to deserialize, you would take the root object, follow any of your "virtual pointers" to other objects stored, construct those objects in the same way, and pass back the "real" pointer to the newly created object to the root object. Does that scheme sound plausible? What I am looking for is something other than a class specific implementation, where you for example write a serialize method for each class. Maybe this isnt possible in D currently, but any other pointers anyone has, feel free to speak out. BTW Tom, what does your library actually do? Could you post some sample code? Kyle
Apr 30 2005
The metadata is stored in the parse tree created in memory while reading in the source file. That is what the dmd front end is for. The dmd back end (which Walter has but not dmdfe) takes that tree and generates code and object files. What I'm suggesting is taking that tree and generating source code that would allow D programmers to query and manipulate classes and/or class instances. So there would be alot of work to do just to get to the point of writing the class info to a file much less a D source file that obeys some API. My guess is that given your D experience it would be too much unless you have experience with compiler internals (which I don't).One of the tools I want to eventually use the "DMD front end starter kit" for was a program (I've been calling it "deflect") that takes a class source file (say A) and generates a "metadata" class output source (for say MetaA) that can be compiled and linked with A so that code that wants to serialize and deserialize A can ask MetaA to do it. A meta class is a bit like the ClassInfo class but it has more hooks - eg the entire reflection API in Java would be possible. You can ask MetaA for the fields in A, their offsets, their types etc etc. If you are feeling ambitious you can grab the DMD Front End starter kit from http://home.comcast.net/~benhinkle/dmdfe/ and take a crack at it. Note that such a program would do more than support serialization so it might be too much for your particular needs.I saw dmdfe, but was unsure how to incorporate it, could you give examples, or instructions on how to go about getting at that meta class you talk about? Like I said, I am very new to the C/D paradigm, growing up
Apr 30 2005
Ben Hinkle wrote:Gonna need more than that. I dont even know what to do with these stubs. When to they get called, what data is available when they do, etc. etc. I really am a newbie.The metadata is stored in the parse tree created in memory while reading in the source file. That is what the dmd front end is for. The dmd back end (which Walter has but not dmdfe) takes that tree and generates code and object files. What I'm suggesting is taking that tree and generating source code that would allow D programmers to query and manipulate classes and/or class instances. So there would be alot of work to do just to get to the point of writing the class info to a file much less a D source file that obeys some API. My guess is that given your D experience it would be too much unless you have experience with compiler internals (which I don't).One of the tools I want to eventually use the "DMD front end starter kit" for was a program (I've been calling it "deflect") that takes a class source file (say A) and generates a "metadata" class output source (for say MetaA) that can be compiled and linked with A so that code that wants to serialize and deserialize A can ask MetaA to do it. A meta class is a bit like the ClassInfo class but it has more hooks - eg the entire reflection API in Java would be possible. You can ask MetaA for the fields in A, their offsets, their types etc etc. If you are feeling ambitious you can grab the DMD Front End starter kit from http://home.comcast.net/~benhinkle/dmdfe/ and take a crack at it. Note that such a program would do more than support serialization so it might be too much for your particular needs.I saw dmdfe, but was unsure how to incorporate it, could you give examples, or instructions on how to go about getting at that meta class you talk about? Like I said, I am very new to the C/D paradigm, growing up
Apr 30 2005
Kyle Furlong wrote:BTW Tom, what does your library actually do? Could you post some sample code? KyleI guess it's not what you are looking for, since the user of my lib has to implement a 'describe' function to enable serialization, e.g.: or or And classes have to be registered in a factory: Yet for simple structs it's enough to use a mixin: ... and it's capable of serializing whole complicated object hierarchies just like this: Note: Circular referencing classes will serialize just fine, as class instances are serialized just once. But this is only implemented for classes. If you have structs that recursively reference each other, you'd get a nice infinite loop. I didn't need that functionality yet, but it's gonna get there. The API is probably also going to change. Anyways, if you want to take a look ( to get some ideas or find out how not to do it ;) ), the sources are here: http://codeinsane.info/listDir.spy?viewType=normal&path=code/heresy The file is called serializer.d but I've given you a link to a wider code base because serializer.d is dependent on some other modules. -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Apr 30 2005
Tom S wrote:Kyle Furlong wrote:I think that for the meantime I would settle for the functionality you provide? Will you allow others to use your code? Let me know.BTW Tom, what does your library actually do? Could you post some sample code? KyleI guess it's not what you are looking for, since the user of my lib has to implement a 'describe' function to enable serialization, e.g.: or or And classes have to be registered in a factory: Yet for simple structs it's enough to use a mixin: ... and it's capable of serializing whole complicated object hierarchies just like this: Note: Circular referencing classes will serialize just fine, as class instances are serialized just once. But this is only implemented for classes. If you have structs that recursively reference each other, you'd get a nice infinite loop. I didn't need that functionality yet, but it's gonna get there. The API is probably also going to change. Anyways, if you want to take a look ( to get some ideas or find out how not to do it ;) ), the sources are here: http://codeinsane.info/listDir.spy?viewType=normal&path=code/heresy The file is called serializer.d but I've given you a link to a wider code base because serializer.d is dependent on some other modules.
Apr 30 2005
Kyle Furlong wrote:I think that for the meantime I would settle for the functionality you provide? Will you allow others to use your code? Let me know.This code is public domain, you're welcome to use it. -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Apr 30 2005