Sparx Systems Forum

Enterprise Architect => Uml Process => Topic started by: umlinfo on April 11, 2007, 05:13:16 am

Title: Callback function as a parameter
Post by: umlinfo on April 11, 2007, 05:13:16 am
Hi,
  I have a problem in representing a callback function as one of the parameters to another function.
Right now, I am just using a void pointer.

Alternately,
I would define
  typedef void (*pCallback)(unsigned long events);
How do I represent this typedef using EA, so that I get pCallback in the "Type" listing.

Thanks.
Title: Re: Callback function as a parameter
Post by: «Midnight» on April 11, 2007, 11:35:20 am
Try reverse engineering one into EA. Then examine the resulting elements to see if there is a stereotype or tagged value that indicates this is a callback.
Title: Re: Callback function as a parameter
Post by: umlinfo on April 12, 2007, 11:13:17 am
Hi, good idea about revese engg. Assuming I will implement a typedef in the code, I can specify the Type of the parameter as a function pointer pCallback*, which solve my problem.
Thanks.
Title: Re: Callback function as a parameter
Post by: «Midnight» on April 13, 2007, 02:19:47 pm
Don't know for sure, and typedefs have to be handled 'just so' in EA.

Do a thorough search of this forum for more information. Pay attention to those threads where Sparx personnel have participated.
Title: Re: Callback function as a parameter
Post by: sl@sh on April 15, 2007, 10:40:06 pm
As another thought: in a recent project we were using callback mechanisms extensively. We modelled this as a call providing a callback object as a parameter. This object was of type class, or more precisely an abstract class (an interface). This class provided a pure virtual function which the called object then could use to notify the caller once it was done, and to pass any resulting values of the operation.

There were basically two approaches to pass such a callback object: either there was a seperate class, implementing the interface class, which was instantiated by the caller and passed to the called object, or the caller was derived from the interface class itself, so it could pass itself as the callback object. The latter apparently is the easier approach, but in some cases, when certain classes would call a lot of others using the callback mechanism, using seperate callback class implementations proved to provide a much cleaner and easier to maintain code. Also in some cases it turned out we could use one callback implementation class for several different classes, avoiding redundancy.

There are multiple advantages to this approach:


HTH,
Stefan