Searching an array of objects
TL
Posts: 25
I have an object called "Message." Each instance of Message contains multiple variables - an ID#, some data, a string.
In the calling routine, I declare an array of "message" as follows:
So what I want to do is have a method that does this:
So how would I do that? I have the array size stored in a DAT field, and I can see how to access the array elements (ID, Data, etc) using low-level byte offsets, given a pointer to the array. But is there a way to do this using higher level routines? I guess I'm asking is there is any equivalent to the "this" object in other object-oriented languages.
-TL
(Darn, I keep finding more typos to fix...)
Post Edited (TL) : 1/22/2008 10:46:40 PM GMT
Messenger.spin VAR long _ID 'ID associated with the message long _data 'Data block associated with the message byte _str[noparse][[/noparse]32] 'String of text message associated with this ID DAT arraysize long 1 PUB setsize(myarraysize) if myarraysize > 255 return -1 longmove(@arraysize,@myarraysize,1) PUB set(myID,mydata,mystrptr) 'EXAMPLE CALL: mymsg.set(1,2,@somestring) OR mymsg.set(1,2,string("Some text")) _ID := myID _data := mydata strcopy(@_str,mystrptr) etc
In the calling routine, I declare an array of "message" as follows:
Another_program.spin CON MSG_NUM = 4 'Allow up to 4 messages in array OBJ message[noparse][[/noparse]MSG_NUM]: "Messenger" PUB Main | i message.setsize(MSG_NUM) 'set size of array for object repeat i from 0 to 3 message[noparse][[/noparse] i ].set (i,i*2, string("This is a test"))
So what I want to do is have a method that does this:
myindex := message.find(myID)
So how would I do that? I have the array size stored in a DAT field, and I can see how to access the array elements (ID, Data, etc) using low-level byte offsets, given a pointer to the array. But is there a way to do this using higher level routines? I guess I'm asking is there is any equivalent to the "this" object in other object-oriented languages.
-TL
(Darn, I keep finding more typos to fix...)
Post Edited (TL) : 1/22/2008 10:46:40 PM GMT
Comments
You can generally use the DAT section as class attribute space (as you already do), and allocate an array associating IDs to addresses... This is not so good as you have to know in advance how large this must be, and there is no way to send compile time constants "downstream" from the main object....
You can however create a linked chain using one cell in object space... This chain has always to be searched when doing something like "find".
PUB find_str(id)
... similiar...
It's doable but inflates my array by one LONG per cell. No biggie.
I'm having trouble following your code though. It's very dense. (I'm sure you're aware that's a 'C' compliment. As opposed to saying the programmer is dense...)
I tried doing it with byte offsets and discovered (ha!) that I couldn't get the base address of the object array. This actually makes sense, now that I think of it, because although the VAR elements have a base address, there are also a common DAT and code elements in the object. Hmmm...
All kind of "yooglie."
-TL
in the array? Using the pseudo-id you can grab
the ID# and its message directly.
regards peter
If I understand what you're suggesting, it wouldn't let you do the search with an object method.
Unless someone more experienced can show me otherwise, it appears that you can't access an object array from within that object.
-TL
It does not only appear so
Despite the fancy words you use ("object array") this is something quite different as you have in true OO languages.
SPIN has no datastructures, thus there is nothing you can access. SPIN also has only local access to object attributes in the first place
So you have to think of workarounds, based on the fact that only pointers to memory cells can be freely used throughout all "objects".