How can I map a C variable in a C module between two objects? I.e.
object1 (C): storage->somevar;
object2 (C or Pike) anothervar -> object1.storage->somevar;
map_variable doesn't seem to be the right solution, or is it?
Sorry for a followup to my own post, but would this scenario work as expected:
map_variable(...) in object1
object_index_no_free2(..) on the variable in object2 and assign_svalue to the "shadow" variable in object2?
/ Marek Habersack (Grendel)
Previous text:
2002-09-06 18:28: Subject: Mapping a variable in an object from C
How can I map a C variable in a C module between two objects? I.e.
object1 (C): storage->somevar;
object2 (C or Pike) anothervar -> object1.storage->somevar;
map_variable doesn't seem to be the right solution, or is it?
/ Marek Habersack (Grendel)
If you just want to access the storage of another object directly, you can use get_storage(). Then the object has to be of that particular class (or inherit it) of course, otherwise get_storage() will return NULL.
If you need it to work with any object which has a variable "somevar" then you need to do something different.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2002-09-06 18:28: Subject: Mapping a variable in an object from C
How can I map a C variable in a C module between two objects? I.e.
object1 (C): storage->somevar;
object2 (C or Pike) anothervar -> object1.storage->somevar;
map_variable doesn't seem to be the right solution, or is it?
/ Marek Habersack (Grendel)
I need the original variable be affected by the changes to the other variable, in the other object. I.e. all the other objects should contain only references to the original variable, kinda shared storage. The thing is that the "inferior" object, the one which contains the aliased variable, doesn't know anything about the higher level object - thus it cannot access it directly (from Pike).
/ Marek Habersack (Grendel)
Previous text:
2002-09-06 20:22: Subject: Mapping a variable in an object from C
If you just want to access the storage of another object directly, you can use get_storage(). Then the object has to be of that particular class (or inherit it) of course, otherwise get_storage() will return NULL.
If you need it to work with any object which has a variable "somevar" then you need to do something different.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Um, now it seems you're talking about something else. Do you want to catch writes from pike code to a particular field so that you can update some other object with this value? That sounds like a rather strange design, but you should be able to do it by overloading the `[]= operator.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2002-09-06 20:32: Subject: Mapping a variable in an object from C
I need the original variable be affected by the changes to the other variable, in the other object. I.e. all the other objects should contain only references to the original variable, kinda shared storage. The thing is that the "inferior" object, the one which contains the aliased variable, doesn't know anything about the higher level object - thus it cannot access it directly (from Pike).
/ Marek Habersack (Grendel)
nope, I want something like this:
object1: mapping mymapping = ([]);
objectX: mapping myothermapping = object1->mymapping;
objectY: mapping myothermapping = object1->mymapping;
(objects X and Y are of the same class)
But the mappings in objects X and Y are supposed to be initialized to point (reference) to the object1->mymapping in the C code which lives in the create() function of object1 which, in turn, gets an object passed as one of the parameters. The thing is that objects X and Y need to use and/or modify the mapping stored in object1 but they have no idea that the object exists. The C code in object1's create() first checks whether the passed object is of a correct type and then it should retrieve the index of the 'myothermapping' variable and set the svalue so that it references the mymapping mapping. i.e. do the C equivalent of the initialization above.
/ Marek Habersack (Grendel)
Previous text:
2002-09-06 20:47: Subject: Mapping a variable in an object from C
Um, now it seems you're talking about something else. Do you want to catch writes from pike code to a particular field so that you can update some other object with this value? That sounds like a rather strange design, but you should be able to do it by overloading the `[]= operator.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
The thing is that objects X and Y need to use and/or modify the mapping stored in object1 but they have no idea that the object exists.
So why not just store a reference to object1 in objectX (and objectY)? Then they know that the object exists. Problem solved.
Or alternatively, you could just store a reference to the mapping, but then they would no longer share it with object1 if you overwrite the field "mymapping" in object1 with a completely new mapping. Depends on what you want to do.
The C code in object1's create() first checks whether the passed object is of a correct type and then it should retrieve the index of the 'myothermapping' variable and set the svalue so that it references the mymapping mapping. i.e. do the C equivalent of the initialization
above.
If you know where "myothermapping" is located in the storage, all you need to do is use get_storage, like I said. That will check that the class of the object is correct as well, so you can even remove that test.
Anyway, this whole thing seems a little backwards. Why do you send an objectX to the create method of object1 that is has to modify? Wouldn't it be more natural to pass an object1 to the create method of objectX, so that it can store a reference to it (or its mapping)? That would be more like your pike code.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2002-09-06 20:53: Subject: Mapping a variable in an object from C
nope, I want something like this:
object1: mapping mymapping = ([]);
objectX: mapping myothermapping = object1->mymapping;
objectY: mapping myothermapping = object1->mymapping;
(objects X and Y are of the same class)
But the mappings in objects X and Y are supposed to be initialized to point (reference) to the object1->mymapping in the C code which lives in the create() function of object1 which, in turn, gets an object passed as one of the parameters. The thing is that objects X and Y need to use and/or modify the mapping stored in object1 but they have no idea that the object exists. The C code in object1's create() first checks whether the passed object is of a correct type and then it should retrieve the index of the 'myothermapping' variable and set the svalue so that it references the mymapping mapping. i.e. do the C equivalent of the initialization above.
/ Marek Habersack (Grendel)
The thing is that objects X and Y need to use and/or modify the mapping stored in object1 but they have no idea that the object exists.
So why not just store a reference to object1 in objectX (and objectY)? Then they know that the object exists. Problem solved.
Not really, since I pass objectX to object1 as the create() parameter. That would be a chicken-and-egg problem then.
Or alternatively, you could just store a reference to the mapping, but then they would no longer share it with object1 if you overwrite the field "mymapping" in object1 with a completely new mapping. Depends on what you want to do.
object1 is the one which primarily fills the mapping, objects X and Y can modify the data, but they primarily use it. They won't ever use the data simultaneously, though - just that they have to reach back to the object1 instance. So, as you can see, the setup is pretty complex, but that makes an elegant solution for the user. The question is - will the assigning the svalue in objects X/Y work to make them refer back to object1?
The C code in object1's create() first checks whether the passed object is of a correct type and then it should retrieve the index of the 'myothermapping' variable and set the svalue so that it references the mymapping mapping. i.e. do the C equivalent of the initialization
above.
If you know where "myothermapping" is located in the storage, all you
it is not. It's just a pike-level variable.
Anyway, this whole thing seems a little backwards. Why do you send an objectX to the create method of object1 that is has to modify?
objectX contains callbacks which are called from C methods defined in object1; object1, in turn, collects some data that it puts in its own storage (and makes use of it at times) before calling up to the callbacks defined in the passed object.
Wouldn't it be more natural to pass an object1 to the create method of objectX, so that it can store a reference to it (or its mapping)? That would be more like your pike code.
As I said, that would be chicken-and-egg since then both object would have to know each other. The alternative method is to collect the data in the callback object (objectX) but that would be, for some reason, less elegant...
/ Marek Habersack (Grendel)
Previous text:
2002-09-06 21:00: Subject: Mapping a variable in an object from C
The thing is that objects X and Y need to use and/or modify the mapping stored in object1 but they have no idea that the object exists.
So why not just store a reference to object1 in objectX (and objectY)? Then they know that the object exists. Problem solved.
Or alternatively, you could just store a reference to the mapping, but then they would no longer share it with object1 if you overwrite the field "mymapping" in object1 with a completely new mapping. Depends on what you want to do.
The C code in object1's create() first checks whether the passed object is of a correct type and then it should retrieve the index of the 'myothermapping' variable and set the svalue so that it references the mymapping mapping. i.e. do the C equivalent of the initialization
above.
If you know where "myothermapping" is located in the storage, all you need to do is use get_storage, like I said. That will check that the class of the object is correct as well, so you can even remove that test.
Anyway, this whole thing seems a little backwards. Why do you send an objectX to the create method of object1 that is has to modify? Wouldn't it be more natural to pass an object1 to the create method of objectX, so that it can store a reference to it (or its mapping)? That would be more like your pike code.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
object1 is the one which primarily fills the mapping, objects X and Y can modify the data, but they primarily use it. They won't ever use the data simultaneously, though - just that they have to reach back to the object1 instance. So, as you can see, the setup is pretty complex, but that makes an elegant solution for the user. The question is - will the assigning the svalue in objects X/Y work to make them refer back to object1?
No, but you don't have to assign the svalue in order to put things into the mapping. mappings are reference types.
it is not. It's just a pike-level variable.
Then you need to index it out, using object_index_no_free.
objectX contains callbacks which are called from C methods defined in object1; object1, in turn, collects some data that it puts in its own storage (and makes use of it at times) before calling up to the callbacks defined in the passed object.
Can't you just pass a reference to the mapping as a parameter to the callback then?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2002-09-06 21:45: Subject: Mapping a variable in an object from C
The thing is that objects X and Y need to use and/or modify the mapping stored in object1 but they have no idea that the object exists.
So why not just store a reference to object1 in objectX (and objectY)? Then they know that the object exists. Problem solved.
Not really, since I pass objectX to object1 as the create() parameter. That would be a chicken-and-egg problem then.
Or alternatively, you could just store a reference to the mapping, but then they would no longer share it with object1 if you overwrite the field "mymapping" in object1 with a completely new mapping. Depends on what you want to do.
object1 is the one which primarily fills the mapping, objects X and Y can modify the data, but they primarily use it. They won't ever use the data simultaneously, though - just that they have to reach back to the object1 instance. So, as you can see, the setup is pretty complex, but that makes an elegant solution for the user. The question is - will the assigning the svalue in objects X/Y work to make them refer back to object1?
The C code in object1's create() first checks whether the passed object is of a correct type and then it should retrieve the index of the 'myothermapping' variable and set the svalue so that it references the mymapping mapping. i.e. do the C equivalent of the initialization
above.
If you know where "myothermapping" is located in the storage, all you
it is not. It's just a pike-level variable.
Anyway, this whole thing seems a little backwards. Why do you send an objectX to the create method of object1 that is has to modify?
objectX contains callbacks which are called from C methods defined in object1; object1, in turn, collects some data that it puts in its own storage (and makes use of it at times) before calling up to the callbacks defined in the passed object.
Wouldn't it be more natural to pass an object1 to the create method of objectX, so that it can store a reference to it (or its mapping)? That would be more like your pike code.
As I said, that would be chicken-and-egg since then both object would have to know each other. The alternative method is to collect the data in the callback object (objectX) but that would be, for some reason, less elegant...
/ Marek Habersack (Grendel)
and I should mention that the objects are of two completely different classes...
/ Marek Habersack (Grendel)
Previous text:
2002-09-06 20:22: Subject: Mapping a variable in an object from C
If you just want to access the storage of another object directly, you can use get_storage(). Then the object has to be of that particular class (or inherit it) of course, otherwise get_storage() will return NULL.
If you need it to work with any object which has a variable "somevar" then you need to do something different.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Yes, but I meant objects of _any_ class. It doesn't matter from where you call get_storage, just as long as you know what class the object you're getting storage on will have. If you don't know what class it will have (or inherit), but only the name of the field, _then_ you'll have to do something more complicated.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2002-09-06 20:33: Subject: Mapping a variable in an object from C
and I should mention that the objects are of two completely different classes...
/ Marek Habersack (Grendel)
pike-devel@lists.lysator.liu.se