Testing an object's class in C++

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing an object's class in C++

jamshark70-2
QUESTION 1

In the lang/ sources, I see:

inline bool isKindOf(PyrObjectHdr *obj, struct PyrClass *testclass)

and

inline bool isKindOfSlot(PyrSlot *slot, struct PyrClass *testclass)

and

if (isKindOfSlot(parentSlot, identDictClass))

where

PyrClass *identDictClass = s_identitydictionary->u.classobj;

and

s_identitydictionary = getsym("IdentityDictionary");

So I did:

// in PyrObject.cpp
PyrSymbol *s_rest;
...
s_rest = getsym("Rest");

and

PyrClass *restClass = s_rest->u.classobj;

and

isKindOf(&dur, restClass)

and I get:

/home/dlm/share/sc-hjh.git/lang/LangPrimSource/PyrListPrim.cpp:510:35: error: cannot convert ‘PyrSlot* {aka pyrslot*}’ to ‘PyrObjectHdr*’ for argument ‘1’ to ‘bool isKindOf(PyrObjectHdr*, PyrClass*)’
       if (isKindOf(&dur, restClass)) {
                                   ^

So... why the [expletive] is "s_identitydictionary->u.classobj" compatible with PyrObjectHdr* but "s_rest->u.classobj" is not, when the types of, and assignments into, s_identitydictionary and s_rest are identical except for the strings provided to getsym()?

(PS, I did try slotRawObject(restClass) and slotRawObject(&restClass), neither of them work. PPS, neither IdentityDictionary nor Rest are intrinsic classes. So it looks like everything is the same except the class name, except the one compiles successfully and the other goes kaboom... at which point I would like to travel back in time, find Dennis Ritchie, and convince him to take up fishing.)

QUESTION 2

Assuming I can ever get 'dur' to point to a Rest object, and test successfully for this, how do I access the Rest object's 'value' slot in C++?

hjh


_______________________________________________
sc-dev mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-dev/
search: http://www.listarc.bham.ac.uk/lists/sc-dev/search/
Reply | Threaded
Open this post in threaded view
|

Re: Testing an object's class in C++

jamshark70-2
 ---- On Thu, 16 Mar 2017 21:40:01 +0800 James Harkins <[hidden email]> wrote ----
 > /home/dlm/share/sc-hjh.git/lang/LangPrimSource/PyrListPrim.cpp:510:35: error: cannot convert ‘PyrSlot* {aka pyrslot*}’ to ‘PyrObjectHdr*’ for argument ‘1’ to ‘bool isKindOf(PyrObjectHdr*, PyrClass*)’
 >        if (isKindOf(&dur, restClass)) {
 >                                    ^

Oh... sheepish grin... I must revise my question to, "why the [expletive] are c++ compiler errors indexed starting from 1 when everything else in the bloody language is indexed from 0?"

It's almost like they want it to be confusing.

hjh


_______________________________________________
sc-dev mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-dev/
search: http://www.listarc.bham.ac.uk/lists/sc-dev/search/
Reply | Threaded
Open this post in threaded view
|

Re: Testing an object's class in C++

brianlheim
Hahaha, I was just typing my answer, which began with "Take another look at the error message"... You could also double-check with the types in the method signature.

It looks like you might want to use isKindOfSlot instead, which is what's in the original code you're trying to imitate, and which wraps the cast for you. Note that `isKindOf` will throw a runtime error if the slot isn't an Object because it asserts IsObj(slot); `isKindOfSlot` will just return false.

```
// PyrKernel.h:70-73inline bool isKindOfSlot(PyrSlot *slot, struct PyrClass *testclass)
{
    return IsObj(slot) && isKindOf(slotRawObject(slot), testclass);
}
```

As to your second question, no idea yet...

-Brian

On Thu, Mar 16, 2017 at 9:50 AM, James Harkins <[hidden email]> wrote:
 ---- On Thu, 16 Mar 2017 21:40:01 +0800 James Harkins <[hidden email]> wrote ----
 > /home/dlm/share/sc-hjh.git/lang/LangPrimSource/PyrListPrim.cpp:510:35: error: cannot convert ‘PyrSlot* {aka pyrslot*}’ to ‘PyrObjectHdr*’ for argument ‘1’ to ‘bool isKindOf(PyrObjectHdr*, PyrClass*)’
 >        if (isKindOf(&dur, restClass)) {
 >                                    ^

Oh... sheepish grin... I must revise my question to, "why the [expletive] are c++ compiler errors indexed starting from 1 when everything else in the bloody language is indexed from 0?"

It's almost like they want it to be confusing.



--
_______________________________
Brian Heim
507-429-6468

B.M. '14 University of Texas at Austin
M.M. '16 Yale School of Music
Reply | Threaded
Open this post in threaded view
|

Re: Testing an object's class in C++

brianlheim
Err, sorry, I mixed that last part up. Instead of this:

> Note that `isKindOf` will throw a runtime error if the slot isn't an Object because it asserts IsObj(slot); `isKindOfSlot` will just return false.

I should have said this:

> Note that `isKindOf(slotRawObject(x), ...)` will throw a runtime error if the slot isn't an Object because it asserts IsObj(slot); `isKindOfSlot` will just return false.

On Thu, Mar 16, 2017 at 9:56 AM, brian heim <[hidden email]> wrote:
Hahaha, I was just typing my answer, which began with "Take another look at the error message"... You could also double-check with the types in the method signature.

It looks like you might want to use isKindOfSlot instead, which is what's in the original code you're trying to imitate, and which wraps the cast for you. Note that `isKindOf` will throw a runtime error if the slot isn't an Object because it asserts IsObj(slot); `isKindOfSlot` will just return false.

```
// PyrKernel.h:70-73inline bool isKindOfSlot(PyrSlot *slot, struct PyrClass *testclass)
{
    return IsObj(slot) && isKindOf(slotRawObject(slot), testclass);
}
```

As to your second question, no idea yet...

-Brian

On Thu, Mar 16, 2017 at 9:50 AM, James Harkins <[hidden email]> wrote:
 ---- On Thu, 16 Mar 2017 21:40:01 +0800 James Harkins <[hidden email]> wrote ----
 > /home/dlm/share/sc-hjh.git/lang/LangPrimSource/PyrListPrim.cpp:510:35: error: cannot convert ‘PyrSlot* {aka pyrslot*}’ to ‘PyrObjectHdr*’ for argument ‘1’ to ‘bool isKindOf(PyrObjectHdr*, PyrClass*)’
 >        if (isKindOf(&dur, restClass)) {
 >                                    ^

Oh... sheepish grin... I must revise my question to, "why the [expletive] are c++ compiler errors indexed starting from 1 when everything else in the bloody language is indexed from 0?"

It's almost like they want it to be confusing.



--
_______________________________
Brian Heim
<a href="tel:(507)%20429-6468" value="+15074296468" target="_blank">507-429-6468

B.M. '14 University of Texas at Austin
M.M. '16 Yale School of Music



--
_______________________________
Brian Heim
507-429-6468

B.M. '14 University of Texas at Austin
M.M. '16 Yale School of Music
Reply | Threaded
Open this post in threaded view
|

Re: Testing an object's class in C++

jamshark70-2
In reply to this post by brianlheim
 ---- On Thu, 16 Mar 2017 21:56:50 +0800 brian heim <[hidden email]> wrote ----
 > It  looks like you might want to use isKindOfSlot instead, which is what's in the  original code you're trying to imitate...

Got it, and that works.

Still can't get to the Rest slot though -- I get *something* with "dur = slotRawObject(&dur)->slots[0];" but slotDoubleVal then returns an error code.

SetSymbol(&key, s_dur);
identDict_lookup(slotRawObject(a), &key, calcHash(&key), &dur);
err = slotDoubleVal(&dur, &fdur);
if (err) {
  if (NotNil(&dur)) {
    printf("is rest = %d\n", isKindOfSlot(&dur, restClass));
    if (isKindOfSlot(&dur, restClass)) {
      dur = slotRawObject(&dur)->slots[0];
      err = slotDoubleVal(g->sp, &fdur);  // ERROR HERE
      if (err) return err;
    } else {
      return err;  // should already be "wrong type"
    }
  } else {
        SetNil(g->sp);
        return errNone;
  }
}

sc3> (dur: Rest(1.0)).delta
is rest = 1  <<-- meaning, isKindOfSlot worked!
ERROR: Primitive '_Event_Delta' failed.
Wrong type.

hjh


_______________________________________________
sc-dev mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-dev/
search: http://www.listarc.bham.ac.uk/lists/sc-dev/search/