Function Name in String -> call function possible?

Hello,

I look for a generic way to print object data.

The point is, that I not only use object attributes but I also want to call functions which return additional data for an object (E.g. getURL())

The code below is working, but I've hard coded each supported functions.

Is there a way to do it generic?

 Thanks a lot, Jörg

 

Skip attr_list = create

put(attr_list, "ID", "Absolute Number")
put(attr_list, "Text", "Object Text")
put(attr_list, "URL", "getResourceURL()")
put(attr_list, "Identifier", "identifier()")


void demo(Object obj)
{
  string value
  
  for a in attr_list do
  {
        string key = (string key attr_list)
        string result = "???"
        find(attr_list,key, value)

        if (matches( "(.*)\\(", value))
        {
                string function = value[ match 1]
                if ( function == "getResourceURL" ) result = getResourceURL obj               
                if ( function == "identifier"     ) result = identifier obj
        }
        else
        {
                result = probeAttr_(obj,value) 
        }
        print key " -> " value ": " result"\n"
  }
}

demo(current Object)

 


JörgWerner - Tue Jan 12 05:59:30 EST 2016

Re: Function Name in String -> call function possible?
Mathias Mamsch - Wed Jan 13 03:09:00 EST 2016

Not a nice way. You could use eval_ though which would allow you to generate DXL code on the fly. For this you would need to pass the Object reference to the eval code. Something like this:

string callFuncOnObject(string funcName, Object o) {
    string sCode = "
    Object oEval = addr_ " ((addr_ o) int) "
    return_ " funcName " oEval
    "
    
    return eval_ sCode
}

Note that I am currently quite unsure, if this code will work reliably on DOORS 64 bit (64 bit memory addresses will not go to the code alright). There are probably ways to work around that, but consider the above code for "informational purposes".

So hardcoding the function names is definitively the way to go here. After all, you will put this in a library and there are only few system attributes you want to query.

Regards, Mathias

Re: Function Name in String -> call function possible?
Mathias Mamsch - Wed Jan 13 03:14:53 EST 2016

by the way: I think probeAttr_ already supports at least "identifier" when you use "Object Identifier" ... Maybe it also supports "Object URL" ? Regards, Mathias

Re: Function Name in String -> call function possible?
JörgWerner - Wed Jan 13 03:48:12 EST 2016

Mathias Mamsch - Wed Jan 13 03:14:53 EST 2016

by the way: I think probeAttr_ already supports at least "identifier" when you use "Object Identifier" ... Maybe it also supports "Object URL" ? Regards, Mathias

print probeAttr_(current Object,"Object Identifier") "\n" // okay
print probeAttr_(current Object,"Object URL") "\n"  // no (empty) result

Object URL is not working. Thanks, Jörg

Re: Function Name in String -> call function possible?
JörgWerner - Wed Jan 13 03:51:11 EST 2016

Mathias Mamsch - Wed Jan 13 03:09:00 EST 2016

Not a nice way. You could use eval_ though which would allow you to generate DXL code on the fly. For this you would need to pass the Object reference to the eval code. Something like this:

string callFuncOnObject(string funcName, Object o) {
    string sCode = "
    Object oEval = addr_ " ((addr_ o) int) "
    return_ " funcName " oEval
    "
    
    return eval_ sCode
}

Note that I am currently quite unsure, if this code will work reliably on DOORS 64 bit (64 bit memory addresses will not go to the code alright). There are probably ways to work around that, but consider the above code for "informational purposes".

So hardcoding the function names is definitively the way to go here. After all, you will put this in a library and there are only few system attributes you want to query.

Regards, Mathias

Perfect!

Yes I think I will continue to hard code the functions, especially since the code is not 64bit proofed.

  Thanks, Jörg