PDA

View Full Version : [API] Why (MStatus::kSuccess != 0) ??


Windows90
01-10-2011, 11:34 AM
Hi,
We often use the following code to check and handle exception in maya api,
MS stat = doSomething();
if(!stat){
outputErr();
}


I checked the definition of MStatus::kSuccess:
enum MStatusCode
{
//! The operation was successful
kSuccess = 0,
//! The operation failed
kFailure,
//! The operation failed due to insufficient memory
kInsufficientMemory,
//! An invalid parameter was provided
kInvalidParameter,
//! Application is not licensed for the attempted operation
kLicenseFailure,
//! Returned by MPxNode::compute for unrecognised plugs
kUnknownParameter,
//! Not currently used
kNotImplemented,
//! Not currently used
kNotFound,
//! Not currently used
kEndOfFile
};

So I think kSuccess should be equal to 0 as defined, however if so then we should use "if (stat)" to check if encounter a err instead? Why not this way? :surprised

Robert Bateman
01-10-2011, 12:54 PM
MStatus::operator bool() const
{
return statusCode == MS::kSuccess;
}


They've overloaded the cast to boolean operator to change it's meaning. It's not *exactly* done as written above (because the internals are hidden from you), but effectively it's doing the same thing

Keilun
01-10-2011, 12:56 PM
This is because you are looking at two different structures.

Case 1:

MS stat = doSomething();
if(!stat){
outputErr();
}

This is looking at the MStatus class. The operator bool() is overloaded on MStatus to return true if there is no error, false otherwise, so the above code is correct.


Case 2:

enum MStatusCode
{
//! The operation was successful
kSuccess = 0,
//! The operation failed
kFailure,
//! The operation failed due to insufficient memory
kInsufficientMemory,
//! An invalid parameter was provided
kInvalidParameter,
//! Application is not licensed for the attempted operation
kLicenseFailure,
//! Returned by MPxNode::compute for unrecognised plugs
kUnknownParameter,
//! Not currently used
kNotImplemented,
//! Not currently used
kNotFound,
//! Not currently used
kEndOfFile
};

This is the MStatusCode enumeration. It is not the same as MStatus, it just holds the values. If when you dereferenced MStatus it returned an MStatusCode instead, then yes you would invert the check in case 1. However that's not the case.

Windows90
01-11-2011, 12:33 AM
MStatus::operator bool() const
{
return statusCode == MS::kSuccess;
}


They've overloaded the cast to boolean operator to change it's meaning. It's not *exactly* done as written above (because the internals are hidden from you), but effectively it's doing the same thing

Hi Robert Bateman, I run "int(MS::kSuccess)" and get a result of "1", and then I am more confused. But now after reading your reply I could understand that because int(statusCode == MS::kSuccess) = 1.
Thanks!

Windows90
01-11-2011, 12:36 AM
This is because you are looking at two different structures.
This is the MStatusCode enumeration. It is not the same as MStatus, it just holds the values. If when you dereferenced MStatus it returned an MStatusCode instead, then yes you would invert the check in case 1. However that's not the case.

Hi Keilun, yes I really had messed them up, Thanks for your clear explanation! :beer:

CGTalk Moderation
01-11-2011, 12:36 AM
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.