Reference to an object
Ricje
Posts: 1
Hi,
Comming from a vast C++ background, I'm pretty much certain that I'm not looking at this problem the right way.
I'm looking for a way to pass a reference to a SPIN object that I created in one object to another object that is running on the same COG. Basically, I'd like to have a global object that I can use from any object running in a given COG. I don't want to access it from different COG.
I've tried the obvious @MyObject to get the adress of my object and pass it to other object but the compiler does't let me use the adress operator on an object.
Surely there is a way to do what I'm trying to do, but how?
Thanks
Comming from a vast C++ background, I'm pretty much certain that I'm not looking at this problem the right way.
I'm looking for a way to pass a reference to a SPIN object that I created in one object to another object that is running on the same COG. Basically, I'd like to have a global object that I can use from any object running in a given COG. I don't want to access it from different COG.
I've tried the obvious @MyObject to get the adress of my object and pass it to other object but the compiler does't let me use the adress operator on an object.
Surely there is a way to do what I'm trying to do, but how?
Thanks
Comments
One way to functionally get what you want is for your "global" object to have all its variables in a DAT section instead of a VAR section. This way the variables are shared by all instances of the object. The method code is already shared and is read-only. You can incorporate this object in any other objects in your program and there will be only one instance of its variables and code. There will be a separate internal table for each declared instance, but this table is small.
Here's one example of this technique: obex.parallax.com/objects/189/
For example, the code I am working on right now in C is for the Pic24. It has two I2C ports on it, and to access them, I had to create two *.c objects, but all functions had to be named different between them. In spin it is as easy as creating two sources of the object, and calling the functions remains very similar.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
You are right when you say it creates cleaner looking code, but your example is more a lack of knowledge than a lack of C. If your functions are the same, but have to operate on different data/hardware-adresses you'd simply put all the differences into a structure. So, you'd have one structure for I2C port one and one structure for I2C port two. When calling the functions you simply pass the adress of the structure it should currently use.
Using such an indirection is basically what all the object oriented languages (C++ / SPIN) do.
The code would have to be set up quite a bit different than the excerpt shown below then. All the parameters would have to be passed as a pointer to the structure, and the function would have to do a bit of decoding each time, not?
You are correct about my lack of knowledge of C, however C (and spin) is pretty powerful, even in the hands of dummies.