Shop OBEX P1 Docs P2 Docs Learn Events
Pointers to methods in Spin? — Parallax Forums

Pointers to methods in Spin?

Duane DegnDuane Degn Posts: 10,588
edited 2010-08-31 18:15 in Propeller 1
I think I remember from my C programming days that C (or C++) had pointers to methods (or where they called subroutines in C?). Is there a way of doing the same thing in Spin?

I think I understand both the sybol address '@' and object address plus symbol '@@' but I don't see any way of using a pointer to a method.

As I've mentioned in several other threads, I'm working on a menu program for Rayman's PTP. I've found it easier for me to keep track of the virtual buttons as "structures" in the DAT section.

I'm hoping I can add a pointer to the method the button envokes to this data structure.

I doubt there is an easy way to do this but I thought ask just in case it's not as hard as I think.

Duane

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-31 16:18
    Unfortunately, you can't. There is no such thing as a pointer to a method and there's no way to produce one other than by "cheating". Usually this involves putting a non-standard Spin byte code into your program (possible with BST or Homespun ... not with the Prop Tool) and requires some knowledge of the internal structure of your binary program. I wouldn't recommend this technique for anything for public consumption.

    Usually this sort of thing is done by storing the index into a CASE statement with each case being a call to the appropriate method.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2010-08-31 18:15
    Thanks for the answer Mike. I'm not above a little cheating but in this case the cheating would be harder than the conventional solution.

    On the topic of cheating. The Object Address Plus can only be used with a variable not a pointer (at least that's the way I understand it). So I use this piece of code to come up number to use as my own version of object address plus. (diy stands for do it yourself)
      menuId := @menuStart
      diyObjectAddressPlus := @ledArrayButton0 - word[menuId]
    
    Here are the variables it refers to.
    DAT
      ledArrayButton0   byte 0, 7, 11, _White, _Red, _menuLedArray,      "  LED  ", 0
     
      menuStart   word @ledArrayButton0, @ledArrayButton1, @timerAlarmButton0, @timerAlarmButton1, {
                        }    @otherMenuButton0, @otherMenuButton1, 0
    
    Now that I have a value for diyObjectAddressPlus, I can use it in this method to find the adjusted location of items listed in each group (here a group is a list of buttons for a particular menu).
    PUB MenuButtons | buttonLocation, localIndex
      localIndex := menuId 
                ' menuId can be the location of any of the lists of menu buttons
      buttonLocation := 1
      repeat while buttonLocation <> diyObjectAddressPlus
        buttonLocation := word[localIndex] + diyObjectAddressPlus
        if buttonLocation == diyObjectAddressPlus  ' end my list with zero to identify the end.
                                         ' since I'm adding diyObjectAddressPlus to each pointer 
                                        'I look for its value instead of zero
          return
        PrintButton(buttonLocation) ' I can get away without using the @@ operator.
        localIndex += 2
    
    Which lets me access the many lists of data locations (the first being menuStart (as long as the lists are all in the same object)). This doesn't really have much to do with pointers to methods but I think it's pretty cool. I'm starting to understand the way to access parts of Propeller memory but I don't think I'm ready to try hacking method addresses into my code yet.

    I just discovered this tweak about getting around using the @@ operator and wanted to share it.

    Thanks again Mike.

    Duane
Sign In or Register to comment.