Is there any way to typecast a memory reference to the proper type if the proper type is known but in string form?
Buffer b = castFunction("Buffer", someMemoryReference);
delete( (Buffer someMemoryReference) );
if (typeOfMemRef == "Buffer") {
delete ( (Buffer) someMemoryReference );
} else if (typeOfMemRef == "Array") {
delete ( (Array) someMemoryReference );
} // and so on
SystemAdmin - Fri Apr 02 14:25:39 EDT 2010 |
Re: Any way to do run-time type casting? |
Re: Any way to do run-time type casting?
Hmm ... thats a hard one ... DOORS DXL is highly polymorphic, meaning there can be lots of functions that have the same name but different parameters. The DXL parser will determine the right function to call by looking at the parameters and it will do it before the code even starts running. So there is no other chance than putting a call to each function in your code and surround them by ifs. Once your code starts running the DXL interpreter already decided which function he will call. BUT: There is a way to get around this. Since DOORS does not protect its memory and every DXL program might run in the same DXL context, but does share the same DOORS process memory one DXL code can poke around in each others code. So we can use eval_ to create our DXL code at runtime and decide at runtime what type we will use:
// A buffer and an array
Buffer s = create()
Array a = create(1,3)
void polymorphicDelete (int ad, string type) {
string code = "int mem=" ad "\n" type "& var = addr_ mem; delete var; var = null"
print "Running code: " code "\n"
eval_ code
}
s = "Test"
print stringOf s
// this is the hard part ...
polymorphicDelete ((addr_ (&s)) int, "Buffer")
polymorphicDelete ((addr_ (&a)) int, "Array")
print "Buffer was set to null: " (null s) "\n"
print "Array was set to null: "(null a) "\n"
Buffer& var = addr_ mem;
delete var
var = null
|