I'm working on some code that generates c level programs dynamically, and was wondering if there's any way to include some data in the program definition. Right now, I'm adding a string constant that contains some text, and when an object is created from the class, I fill in the c level structures. That works, but it seems somewhat less efficient than if I were able to specify some data for the program. I looked at the definition for struct program, and didn't see anything obvious, but figured I'd ask the experts :)
For those interested in the rationale, I'm working on adding inheritability to my objective c module. I've got a method that will generate a pike program for an objective c class, inspecting the class and creating stubs for each method in that class. Right now, I don't see any way to include a pointer to the class in the program definition, so I'm currently adding a constant that contains the class name. Then, when the object is instantiated, I look up the constant and then look up the class from the value of the constant. I'd like to cut down on the overhead of this operation, if possible.
Thoughts?
Bill
Without knowing the details, it sounds to me like you could use a parent object to store this data in. You can use LOW_PARENT_INFO(Pike_fp->current_object, program) to find the parent object, and then get_storage() the usual way to get that objects storage, which if course could contain any c level data you'd like.
ok, well I can create a class whose purpose is to hold the class definition pointer. then I can create the object and populate the storage.
the question then, is what is the relationship between this object (presumably there'd be an object for each class I wanted to represent, and this for each pike program i generate.
can the parent object be attached to the program when I define it?
Bill
On Mon, 25 Sep 2006, Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
Without knowing the details, it sounds to me like you could use a parent object to store this data in. You can use LOW_PARENT_INFO(Pike_fp->current_object, program) to find the parent object, and then get_storage() the usual way to get that objects storage, which if course could contain any c level data you'd like.
!DSPAM:45184491210043198212392!
What you do is that you define your dynamic class as a member of an outer class. Then you create an instance of the outer class, and index out the inner class. This will give you an svalue which acts like a regular "program" type value, but which includes a reference to the outer object which will carry on to any objects created from this value.
So basically you'll need to create two classes for each dynamic class, one outer class which contains the common storage (the actual storage definition can be inherited from a base class), and inside that class your "real" class.
On the other hand, maybe this is somewhat overcomplicated for your needs. Another solution would be to just have a single class with storage for the class defintion pointer. Then when you create a new class, you instantiate an object of that class, populate the storage, and simply add_object_constant() it to the new class. With this solution you'll need an identifier for the constant, but you can hide it with ID_STATIC|ID_PRIVATE. If you add the constant immediately after start_new_program(), and don't set any fancy flags on the program itself, the object should appear as p->constants[0].sval.u.object.
I like the sound of that... it's simple, and should work, as I plan on caching the programs once they're generated.
Bill
On Tue, 26 Sep 2006, Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
On the other hand, maybe this is somewhat overcomplicated for your needs. Another solution would be to just have a single class with storage for the class defintion pointer. Then when you create a new class, you instantiate an object of that class, populate the storage, and simply add_object_constant() it to the new class. With this solution you'll need an identifier for the constant, but you can hide it with ID_STATIC|ID_PRIVATE. If you add the constant immediately after start_new_program(), and don't set any fancy flags on the program itself, the object should appear as p->constants[0].sval.u.object.
!DSPAM:45196bce295102456319790!
I'm not sure what happens if the user happens to inherit one of your generated classes though, if the constant would be copied to the subclass or if you need to traverse the inheritance graph in that case.
pike-devel@lists.lysator.liu.se