It is currently Wed Feb 08, 2012 4:46 pm

All times are UTC



Welcome
Welcome to RHAPSODY4YOU

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, respond to polls, upload and download content, and access many other special features. Registration is fast, simple, and absolutely free, so please, register to join our community today.





 Page 2 of 2 [ 21 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Thu May 21, 2009 2:00 pm 

Joined: Wed May 07, 2008 3:50 pm
Posts: 148
Location: Horsham, W Sussex, England
Examples

Example: Avoid const pass-by-value function parameters in function declarations. The following two declarations are exactly equivalent:

void Fun( int x );

void Fun( const int x ); // redeclares the same function: top-level const is ignored


In the second declaration, the const is redundant. We recommend declaring functions without such top-level consts, so that readers of your header files won't get confused. However, the top-level const does make a difference in a function's definition and can be sensible there to catch unintended changes to the parameter:

void Fun( const int x ) { // Fun's actual definition
// …

++x; // error: cannot modify a const value

// …
}


Last edited by shanz on Thu May 21, 2009 2:03 pm, edited 1 time in total.

Offline
 Profile  
 
 Post subject:
PostPosted: Thu May 21, 2009 8:24 pm 
User avatar

Joined: Thu Sep 13, 2007 7:34 pm
Posts: 397
Location: London
OK, I did read that but I didn't quite understand it. Having re-read it, I understand it now and I hadn't previously thought about it but it makes logical sense. Basically the const in the declaration doesn't make any difference when passing by value so if you want to prevent the implementation from accidentally changing the passed in value then the const only has to be applied in the definition.

Notwithstanding this, in the case of Rhapsody, there is only one place to define the parameter type. You can't therefore accidentally put const in the declaration and then forget to add it to the definition and therefore "confuse the reader".


Offline
 Profile  
 
 Post subject: No!
PostPosted: Fri May 22, 2009 12:00 pm 

Joined: Wed May 07, 2008 3:50 pm
Posts: 148
Location: Horsham, W Sussex, England
Farquad,

The idea is to have the const in the definition but not the declaration.
So it sounds like you think it would be difficullt or impossible to achieve within Rhapasody.


Offline
 Profile  
 
 Post subject:
PostPosted: Fri May 22, 2009 6:05 pm 
User avatar

Joined: Thu Sep 13, 2007 7:34 pm
Posts: 397
Location: London
Pretty much anything can be achieved in terms of code gen in Rhapsody but some are easier than others. In this case I suspect you'd have to resort to writing a postprocessor to hook in.

I'm still failing to see the point of doing it though. Am I wrong in saying that the 'confusion' issue that you're trying to avoid occurs when you've put const in the dec but not in the def? That can't happen in rhapsody code gen so what's the point?


Offline
 Profile  
 
 Post subject: Re: Is there a way to define function arguments as const?
PostPosted: Thu Jan 14, 2010 4:19 pm 

Joined: Wed May 07, 2008 3:50 pm
Posts: 148
Location: Horsham, W Sussex, England
Well, I'm currently creating a ProductTypesPkg and including it in CommonTypes for drop-down selection of existing types.

For each type, you can define the ReturnType which defaults to $type*.

If you have one operation which returns the type directly then the type's ReturnType needs to be defined as $type.

However if another operation returns a pointer to the type instead, how can that be achieved?
I know I can untick 'use existing types' and enter it manually but I've noted the following ...

The advantages of explicitly modelling the types rather than unticking the 'use existing type' box and entering a type manually are as follows :-

1) Existing types make use of the In, Out, InOut argument specifiers.
2) The use of const and pointers and/or references can be controlled by the types' properties.
Eg: CPP_CG::Type::In
CPP_CG::Type::InOut
CPP_CG::Type::Out
CPP_CG::Type::ReturnType
3) Test Conductor's animation relies on using existing-types selected via the drop-down.

Point 3) here seems to be the killer. If a type is returned differently from the ReturnType pattern, then TestConductor won't be able to test it.

One horrible solution would be to duplicate each type as follows:-

typedef int myType;
typedef int myType*;
typedef int myType&;

Anyone got any better ideas?


Offline
 Profile  
 
 Post subject: Re: Is there a way to define function arguments as const?
PostPosted: Tue Jan 19, 2010 11:09 am 

Joined: Wed May 07, 2008 3:50 pm
Posts: 148
Location: Horsham, W Sussex, England
I think I've found it.
The operation's own property -
CPP_CG:Type:ReturnType

It appears that this only overrides the type's equivalent property when it is non-default.

Thanks.

*UPDATE*

I've just discovered the argument properties. I had no idea there were extra properties.
To clarify what I've just discovered :-
Select argument
Click icon - Invoke Feature Dialog
Click icon - Invoke Feature Dialog (again)
Previously invisible properties appear!


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 2 of 2 [ 21 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:


suspicion-preferred