Author Topic: How (where?) to model 'global' functions  (Read 11726 times)

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
How (where?) to model 'global' functions
« on: June 04, 2007, 10:44:12 pm »
I have a couple of mathematical classes that I defined operators for. For these operators it doesn't neccessarily make sense to model them as class methods, unless they're unary. The typical approach would be to implement them as global functions, while at the same time declaring them friend to the class(es) they takes their parameters from.

I only started using EA a lot later, and when I tried importing that code, I realized the operators (declared in the header of the appropriate classes) got lost on import. I now wonder
a) is there a standard to model global functions that are not class methods, but strongly related (friend) to a particular class?
b) is it possible to import code that already defines such functions?
« Last Edit: June 04, 2007, 10:44:52 pm by sl@sh »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #1 on: June 05, 2007, 02:16:14 am »
Here's a possible approach,

If you mean something like Math.Max() in .Net, then you've got your answer. Create a utility class, perhaps a set of them in a generally accessible namespace. Now create various shared (static if you prefer) methods with suitable overrides for the various parameter types that would be applicable.

In some cases the utility classes themselves may be shared, and are not things you can actually create, just holders for the methods.

There is nothing quite like 'global' for visibility. Make the classes public, and ensure that you have access to the containing namespace from wherever you intend to call them.

Now the fun part. In .Net and Java, just import the runtime namespaces from the binaries. For your own stuff, see if you can find an executable (dll) and do the same.

David
No, you can't have it!

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #2 on: June 05, 2007, 02:49:59 am »
I was actually thinking of overloading standard operators '+', '-', '*', '/', etc.

I could certainly put them into a namespace and then add a 'using' statement whereever these operators would be used (talking about C++ here). A Utility class definitely won't work though.

The difficult thing would be the fact that currently all these operators and methods are spread over the various class files. I wouldn't want to pick them from the headers they naturally reside in and put them somewhere else, while a number of similar functions (e. g. the unary '-') would remain with the class definitions! So the only feasible way would be to introduce namespaces for each and every class that I have specialized global methods for!? :o

I would have liked a less clumsy method. :( Apart from the effort of manually introducing 30+ namespaces for all the classes involved, I also need to make sure every object that deals with those classes automatically 'uses' those namespaces.

Seems to me namespaces are a rather heavyweight method of modelling simple out-of-class methods. Isn't there any easier way? ???

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #3 on: June 05, 2007, 03:04:51 am »
Somewhere in the back of my mind I think I remember that there is a stereotype that can be applied in EA, indicating that a method is an operator overload.

If that works, see if you can overload the operator in a base class or interface, then inherit.

You'll need to search the documentation and forum for overload.

I also don't remember if this was specific to C++. It seems to me that there are a few such features that don't propagate to the other languages, but with luck they are only on the code generation side.

David
No, you can't have it!

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #4 on: June 05, 2007, 04:18:36 am »
Well, I have very specific reasons not to put certain operators and methods into the class*. That's why I was asking about 'global' functions in the first place! I am asking about this not in spite of global functions being bad practice, but because in this case they are in fact good practice! I wonder why UML - and EA - wouldn't support it. Or does it?

I searched for operator overloading but those few entries that did turn up dealt with in-class operators only. Those I have no issues with.

* If you really want to know, check out B. Stroustrup, 'The C++ programming language'

P.S.: for a stereotype to be applied to a method, said method first would have to be part of the model - my problem is that I don't even see a way to achieve that!
« Last Edit: June 05, 2007, 04:24:29 am by sl@sh »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #5 on: June 05, 2007, 04:36:17 am »
Reply to the first point - and taking the excellent reference you cite into account - is that this is a convenience that does not always fit into an OO paradigm. Still, it would be nice to see an accommodation.

Meanwhile, the most recent reference I found in the forum - that does not mean there are not others - was a vague indication that this would likely be added to EA in the future. The context was specifically C++.

The post was from circa August 2006. I don't know if there's been any action at all since then.
No, you can't have it!

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #6 on: June 05, 2007, 05:42:42 am »
Quote
Reply to the first point - and taking the excellent reference you cite into account - is that this is a convenience that does not always fit into an OO paradigm. Still, it would be nice to see an accommodation.

Well, is it a convenience? Consider two geometry classes, a CPoint and a CVector. While both might use the same representation and functionality inside, they represent different objects. Now consider an operator+() that adds a CVector to a CPoint. Or in a more abstract view: a CPoint is being moved to a new location with the relative movement denoted by a CVector object. Does this operator belong to the CPoint class or to the CVector class? One could argue either way. But the true answer is: it belongs to neither! And that is what I'm trying to model.

The references I did find in my search for 'operator overload' either dealt with in-class operators which I have no issues with (but apparently there had been a few issues which were fixed in EA 6.0), or not with C++ at all.

I did a rather more general search, looking for 'global' and finally found sth. useful: in a related topic simonm suggested importing such a file as C file. I tried that, but unfortunately the mix-in of C++ features results in parser errors when I try to import the files I currently have as C. However, I learned something interesting in trying this: C-Import generates class objects for each header which in turn contain global functions declared therein as 'member' functions. Weird...

Anyway, now I have something to play with, I suppose I can find a suitable workaround for my problem.  :)

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #7 on: June 05, 2007, 10:45:42 am »
Sorry about the term, it was just what came out as I was typing.

BTW, I assume your global '+' would correctly handle adding a vector to a color. If not, there would seem to be some contextual framework within which the operator was meaningful. That is what I meant when I assumed the operator would be owned by something. Still, this does nothing to solve your problem.

Simon's suggestion is probably a good place to start.

David
No, you can't have it!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8063
  • Karma: +118/-20
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #8 on: June 05, 2007, 01:11:28 pm »
I have another possible suggestion.  But first, EA does not currently handle this at all.  This brings me to the bad news.  The C++ parser can't import your global functions.  EA only handles the Object Oriented part of your C++ code.

The good news though is that the code templates can be modified to generate global functions from inside a class stereotyped as a container for global functions.

The even better news is that quite some time ago I posted to the EA user group a set of templates to do this.  You can access this file from http://sharepoint.standardcase.com/external/eaug/EA%20Code%20Generation%20Templates/namespace_members.xml and import it into your EA model using Tools | Import Reference Data.

The classes to contain your global functions will be stereotyped "namespace members", although in principle the namespace it is generating to could be the global namespace.  (Put the class in a namespace root).

The main limitation that I am aware of for these templates, is that forward synchronisation of this class will not work.  It will insert the functions newly into the class each time.

If you can live with that, and not being able to import them automatically, you can model them and generate them.

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #9 on: June 06, 2007, 10:39:53 pm »
Thanks, Simon for your support. I will definitely look into these templates, although I'm not sure that is what I was looking for. Fortunately the number of functions (operators all) that are concerned is still limited at the moment.

My main reason for posting was that I got the impression that:
Quote
The C++ parser can't import your global functions.

I find this odd - the complete C Syntax is part of the C++ Syntax and even though none of it was designed with OO in mind, C++ objects couldn't exist without the C Syntax it is built upon. The policy of ignoring code that is not inside classes (or structs, typedefs, etc.) appears wrong to me. But then I never designed an UML tool, maybe I am judging this by false premises.

The reason I posted this problem into the UML forum and not into the general forum was that I wondered if I was wrong in assuming UML should provide a means to model global functions. While I appreciate the suggestions to solve this on a technical level, I am also interested in the opinions of the UML gurus around here, whether or not global functions (or global operators at least) should be considered UML objects.

@David: your explanation makes sense. I am considering creating a context "Matrix Calculus" or something on that line to put my operators in. The more I think about it the more I like it  :)

It does create the problem that I need to include 'uses <namespace>' statements everywhere I want to use those operators, but then I need to include whatever header(s) contain(s) those operators anyway.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #10 on: June 07, 2007, 02:19:23 am »
It sounds like you're moving forward in any case. The main thing is to maintain some kind of momentum rather than getting the entire project bogged down because of this snag.

As to the namespace requirement, we have the same kind of problem in the .Net and Java worlds (or Python or whatever). It rankled me a bit at the beginning, but the scars are hardly noticeable now.

Please keep us informed on which way you go, and how it works for you.
No, you can't have it!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8063
  • Karma: +118/-20
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #11 on: June 07, 2007, 01:29:08 pm »
Let me explain why the technical solution I've given from a pure UML point of view.

If I was looking to add attributes or operations directly to a namespace the logical container for them would be the package.  This doesn't work though as none of the parent classes of package contains the ownedAttribute and ownedOperation properties.

As a result we need to provide an extension to UML to handle this.  The preferred extension mechanism for UML is to use profiles.  That would mean a stereotype applied to one of the types that can contain attributes and operations.  For this I have used a class, and defined the semantics of that stereotype (via the code templates) to be that what is owned by that class is actually contained by the containing package.

My final note on this is that although there is no profile because EA permits stereotypes independently from profiles, this is valid as long as you consider that it is in an implicit profile, instead of within no profile.

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +54/-3
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #12 on: June 07, 2007, 02:05:46 pm »
Quote
If I was looking to add attributes or operations directly to a namespace the logical container for them would be the package.  This doesn't work though as none of the parent classes of package contains the ownedAttribute and ownedOperation properties.

And because neither Kernel::Property nor Kernel::Operation specializes Kernel::PackageableElement.
« Last Edit: June 07, 2007, 02:06:00 pm by KP »
The Sparx Team
[email protected]

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #13 on: June 07, 2007, 10:57:03 pm »
[edit]removed issues that have been resolved in the meantime[/edit]

I did check out the existing template packages and found a number of Templates under the names of "Namespace xxx" What are these for, and how do I invoke them?

P.S.: I tried setting a class method's scope to "package, but apparently that's equivalent to "public" (the templates always check for scope=='public' or scope=='package') . Wouldn't this be a good place to modify?
« Last Edit: June 08, 2007, 02:27:10 am by sl@sh »

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How (where?) to model 'global' functions
« Reply #14 on: June 07, 2007, 11:32:13 pm »
Talking about templates: When I checked out the existing ones I didn't have too much problems interpreting their meaning. However, the formatting is awful!

If I start modifying or adding templates, can I indent parts of it, or does the parser not like that? Indenting some of the nested if-statements would greatly increase readability...