How to check for a null value in a variable passed as a reference

Tags: dxl doors
By : Ajedi32
Source: Stackoverflow.com
Question!

In DXL, how do I check whether a variable holds a null value after passing it to a function as a reference? The usual method using (null variableName) doesn't seem to be working properly:

void valueBasedNullTest(Buffer b) {
  print "Value based: "
  print "null b => "
  if (null b) {
    print "true"
  } else {
    print "false"
  }
  print "\n"
}

void referenceBasedNullTest(Buffer &b) {
  print "Reference based: "
  print "null b => "
  if (null b) {
    print "true"
  } else {
    print "false"
  }
  print "\n"
}

Buffer someBuffer = null
valueBasedNullTest(someBuffer)
referenceBasedNullTest(someBuffer)

Result:

Value based: null b => true
Reference based: null b => false

I'm currently running Rational DOORS 9.2.

Why is this happening, and how can I work around it?

By : Ajedi32


Answers

I see the problem you are talking about, it does seem very strange. The way I would handle it is:

void referenceBasedNullTest(Buffer &b) {
  print "Reference based: "
  print "null b => "
  if (length(b) <=0) {
    print "true"
  } else {
    print "false"
  }
  print "\n"
}

Buffer someBuffer = create
//valueBasedNullTest(someBuffer)
referenceBasedNullTest(someBuffer)
delete(someBuffer)

This makes sure the buffer exists, but you can still test to see if it has anything in it. Don't forget to delete the Buffer at the end of use.



Okay, here's what I ultimately went with. This answer is based on a discussion I found on the Rational DOORS DXL Forum about how to check for unassigned variables.

I still can't quite wrap my head around exactly how it works, but my understanding is that it's checking the memory address of whatever variable you pass it, and basing its answer on the fact that null objects seem to always have an address of 0. (Feel free to prove me wrong.)

/*
Regular null check returns incorrect results in DOORS 9.2 under the following condition:

    void referenceBasedNullTest(Buffer &b) {
      print "Reference based: "
      print "null b => "
      if (null b) {
        print "true"
      } else {
        print "false"
      }
      print "\n"
    }

    Buffer someBuffer = null
    referenceBasedNullTest(someBuffer)

isNull works correctly in this case.
*/
bool isNull(_ &value) {
  int *intRef = (addr_ ((addr_ (value)) int))
  return (0 == *intRef)
}

In any case, it seems to work fine for my purposes.

Quick test:

int nullInt = null
int blankInt = 0
int unassignedInt
int goodInt = 42

print "isNull(nullInt)\t\t=> "     isNull(nullInt) "\n"
print "isNull(blankInt)\t\t=> "    isNull(blankInt) "\n"
print "isNull(goodInt)\t\t=> "     isNull(goodInt) "\n"
print "isNull(unassignedInt)\t=> " isNull(unassignedInt) "\n"

print "\n"

Skip nullSkip = null
Skip blankSkip = create
Skip unassignedSkip

print "isNull(nullSkip)\t\t=> "     isNull(nullSkip) "\n"
print "isNull(blankSkip)\t\t=> "    isNull(blankSkip) "\n"
print "isNull(unassignedSkip)\t=> " isNull(unassignedSkip) "\n"

Result:

isNull(nullInt)        => true
isNull(blankInt)       => true  // Note: 0 is null for int values
isNull(goodInt)        => false
isNull(unassignedInt)  => false

isNull(nullSkip)       => true
isNull(blankSkip)      => false
isNull(unassignedSkip) => false
By : Ajedi32


This video can help you solving your question :)
By: admin