PDA

View Full Version : MStatus Confusion


MatthiasBuehlmann
07-15-2008, 12:20 PM
Hey There!

I'm reading the book "Complete Maya Programming" written by David Gould

something that confuses me a lot is that he uses throughout his whole book the following syntax to do error checking:


MStatus stat;
stat = someMayaFunction();
//errorchecking:
if(!stat)
MGlobal::displayWarning( "Some Error Message" );



however.. if i look at the enumerations, then i see that
MS::kSuccess = 0
MS::kFailure = 1

and all the other states are >1

so... by checking if(!stat) he is actually checking for success instead of checking for failure.


However - his plugins work if i compile them. so it seams i missed something.


Another thing is, that 'MStatus' is a class. this class contains the enumerations MStatusCode (MS::kSuccess for example is such an MStatusCode enumeration).
now - if a function has the returntype MStatus, it is legal to write
return MS::kSuccess;
inside this function.
But why? the return type is MStatus - so why can i return an enumeration which is actually nothing else then an integer?


you see... a lot of confusion. can you help me on that?

thank you

ticket01
07-15-2008, 01:20 PM
Hi Matthias,

let me try to help you out here.


so... by checking if(!stat) he is actually checking for success instead of checking for failure.

Actually he's not checking for failure but for NOT success which is a difference since we don't have only 2 but a lot more possible states here. That's why implementing the success case as zero is the logical choice. The result is that the if clause will always be true in case your status code != 0.

Have a look at the implementation details in MStatus.h where the != and == operators are overridden to handle more than just true and false.


Another thing is, that 'MStatus' is a class. this class contains the enumerations MStatusCode (MS::kSuccess for example is such an MStatusCode enumeration).
now - if a function has the returntype MStatus, it is legal to write
return MS::kSuccess;
inside this function.
But why? the return type is MStatus - so why can i return an enumeration which is actually nothing else then an integer?


It can be used like an integer but in this case - since the MStatusCode enum is a member of the MStatus class - its type is MStatus and not integer and can therefore be returned without any problems when it's prepended with its namespace MStatus:: oder MS::. There's a typedef for that somewhere in MStatus.h, too.

Hope I could help.

MatthiasBuehlmann
07-15-2008, 01:51 PM
hi ticket01!

thank you for your help!


Actually he's not checking for failure but for NOT success which is a difference since we don't have only 2 but a lot more possible states here. That's why implementing the success case as zero is the logical choice. The result is that the if clause will always be true in case your status code != 0.


but 0==false
and every other integer == true

so:

kSuccess==0==false
kFailure==1==true
kWhatever==whatever==true

then:
!kSurccess== !0 ==true
!kFailure== !1 == false
!kWhatever == !whatever == false

that means that

if(!stat) in the case of failure will be if(!kFailure) which is if(false)
and in the case of kSuccess it will be if(true)

so it should evaluate to true in the case of success and evaluate to false in the case of Failure.

no?

ticket01
07-15-2008, 02:54 PM
Matthias, what you're writing would be absolutely correct if there would be no operator overloading which is the key concept to understand here.

if(!stat) is being expanded to if(stat==0), then the overloaded operator == kicks in. Since the == operator does a comparison with a private fStatusCode (have a look at the inline code in MStatus.h), which is not documented because its not of our concern, I would guess that this is where you trouble understanding the problem comes from.

MatthiasBuehlmann
07-15-2008, 05:51 PM
Hey Ticket01

now i got it! thank you very much for your help! :)

CGTalk Moderation
07-15-2008, 05:51 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.