Book a Demo

Author Topic: Modeling Pointers to Functions in C++  (Read 5925 times)

jjyoung

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Modeling Pointers to Functions in C++
« on: January 19, 2007, 02:10:33 pm »
I need to model an interface to external software which contains structures that contain pointers to functions as in the following:

typedef struct _MyStruct {
 int  arg1;
 int  arg2;
 int  (*func1)(int p1, int p2);
 int  (*func2)(int p1, int p2);
} MyStruct;

I'm having a hard time figuring out how to do this.  I've tried creating a Class with a <<struct>> stereotype, buy can't figure out how to define the attributes corresponding to the function pointers.  I've tried importing the source code, but in doesn't include the fields for the function pointers.

Anyone have a way to do this?

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Modeling Pointers to Functions in C++
« Reply #1 on: January 19, 2007, 02:45:17 pm »
Try searching the forum for the keywords C++ and typedef; go back about 1000 days. You'll find some information about the limits of EA in this area, as well as some possible workarounds.
No, you can't have it!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Modeling Pointers to Functions in C++
« Reply #2 on: January 21, 2007, 01:10:15 pm »
As you've noticed, EA doesn't import C++ function pointers.

However, if I was just interested in modelling them and possible generation I would create stereotyped operations.

So you would get something like the following.

Code: [Select]
+ <<funcPtr>> func1(p1: int, p2: int): int
+ <<funcPtr>> func2(p1: int, p2: int): int

You could then modify the code generation templates to handle this stereotype.

jjyoung

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Modeling Pointers to Functions in C++
« Reply #3 on: January 22, 2007, 06:58:16 am »
Thanks.  That'll help with the model representation.  However, it won't work with code generation because modeling them as operations will result in loss of control of the function pointer within the structure.  In this case, I'm having to implement a long existing interface and the position of the function pointers within the struct have been fixed, and they're intermixed with the other attributes.  So I guess I'll need to generate the code manually for this.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Modeling Pointers to Functions in C++
« Reply #4 on: January 22, 2007, 01:26:40 pm »
You can modify the templates to generate this stereotyped operation as a function pointer.  Here's how you do it.

Open the code template editor.
Set the language to C++.
Select the Operation template.
Click 'Add New Stereotyped Override'.
Select 'funcPtr' as the feature stereotype.  (If it's not there then use it somewhere in your model or add it in Settings | UML | Stereotypes)
Enter the following in that template.

Code: [Select]
%OperationNotes%
%OperationDeclaration%

Add another stereotype override for the Operation Impl template.

Code: [Select]
%endTemplate%
Those two just ensure that no implementation or body will be generated.  The declaration is changed by adding one more stereotype override to the Operation Declaration template.

Code: [Select]
%PI=" "%
%RESOLVE_QUALIFIED_TYPE("::")%
%opMacros%
%PI=""%
(*%opName%)
(%list="Parameter" @separator=", "%)
%opIsQuery=="T"? " const"%
;

If you follow those instructions you'll be able to generate function pointers from operations stereotyped as <<funcPtr>>, without effecting any other generation.  Note that the templates that I just gave a pretty much just cut down versions of the default templates with only a slight change to the declaration template.

jjyoung

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Modeling Pointers to Functions in C++
« Reply #5 on: January 24, 2007, 07:53:59 am »
Thanks for your responses.  I'll want to learn more about the code generation templates then I'll try your suggestion.

However, there's one thing I still don't know if this will handle.  The interface I need to work with is pre-defined, and the function pointer is mixed in with the other data members of the structure.  If I modify the code generator as you say, is there also a way to modify the ordering of placement of the generated members of the structure to match the current interface?

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Modeling Pointers to Functions in C++
« Reply #6 on: January 24, 2007, 01:17:06 pm »
Well, you could encode an arbitrary ordering into the template.  But I doubt that it's worth it.