Jump to content
  • Advertisement
Sign in to follow this  
soconne

C++ #define macros

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to take the input of a macro, and if its a string, change the first letter to uppercase? For instance, I want to do the following: #define METHODS(name) int name##() { return 0; }void set##name() { } When I call it like this: METHODS(value) It will create the following functions: int value() { return 0; } void setvalue() { } But I would like setvalue to be named setValue, with an uppercase V. Is it possible to have the macro automatically change the v to uppercase? I know I can simply redefine the METHODS macro to take 2 parameters, the last being what to call the set method, but I'm wondering if there's a way around it.

Share this post


Link to post
Share on other sites
Advertisement
This is C++. Avoid macros like the plague. There is probably a template solution to your problem, but I am still pretty unclear about what you're trying to achieve.

Perhaps you could explain your problem in terms of what you're trying to solve instead of in terms of your partial solution.

Share this post


Link to post
Share on other sites
I don't think this is possible.

You may of course adopt the convention of using under_scores instead of camelCase :)

Share this post


Link to post
Share on other sites
Quote:
if its a string

Macros are verbatim text replacement. What you call string is just 'something'.

Perhaps there is a hack somewhere, somehow, but I doubt it.

For what you want, there's also a perfect C++ replacement (which is also somewhat pointless, but so are most applications of accessors).

Share this post


Link to post
Share on other sites
Getters and setters which have prefixes are pointless and I do not really see the point of using camel case.
If a macro was a requirement and if you were using GCC then you would do something like the following, which uses a gcc extension.

#define METHODS(name)__typeof__(this->name) name()const{ return this->name;}void name(__typeof__(this->name) n){ this->name = n;}


Without using the extension you would have to supply the type which name is and could get into trouble if the type is
a template as this causes problems with the number of parameters.

EDIT the forum steals the the backslashes and messes with the indentations

Share this post


Link to post
Share on other sites
Quote:
Original post by soconne
Is there a way to take the input of a macro, and if its a string, change the first letter to uppercase? For instance, I want to do the following:

#define METHODS(name) int name##() { return 0; }void set##name() { }
When I call it like this:

METHODS(value)

It will create the following functions:

int value() {
return 0;
}

void setvalue() {
}

But I would like setvalue to be named setValue, with an uppercase V. Is it possible to have the macro automatically change the v to uppercase? I know I can simply redefine the METHODS macro to take 2 parameters, the last being what to call the set method, but I'm wondering if there's a way around it.


The fact that you want to do this suggests that you want to make accessor/mutator pairs for a large amount of values. Possibly even automate making them for each member of a class.

The solution to your problem is to realize that this is an incredibly bad idea, which accomplishes nothing except to allow programmers to keep their old disorganized habits while lying to themselves that they are doing OO.

Share this post


Link to post
Share on other sites
Well I'm doing this for a GUI library I'm writing and the base Control class has a lot of properties: backgroundColor, text, border, padding, margin, font, ect...

Hence, instead of typing out:

void setBackgroundColor(Color color);
Color backgroundColor();
void setText(std::string text);
std::string text();
...


I'm trying to create macros which will shorten the process. I'd like to only have to write:

PROPERTY(Color, backgroundColor)

And then it creates the setBackgroundColor and backgroundColor methods. As I stated before, I would need to change the 'b' in backgroundColor to uppercase.

Furthermore, as to whether or not using get/set accessors for property values is good design or not, I'm simply following the coding styles Trolltech uses in QT.
http://doc.trolltech.com/qq/qq13-apis.html

Share this post


Link to post
Share on other sites
Quote:
Original post by soconne
Well I'm doing this for a GUI library I'm writing and the base Control class has a lot of properties: backgroundColor, text, border, padding, margin, font, ect...

Hence, instead of typing out:

void setBackgroundColor(Color color);
Color backgroundColor();
void setText(std::string text);
std::string text();
...


I'm trying to create macros which will shorten the process. I'd like to only have to write:

PROPERTY(Color, backgroundColor)

And then it creates the setBackgroundColor and backgroundColor methods. As I stated before, I would need to change the 'b' in backgroundColor to uppercase.


Soo, (apart from the your get/setters still seeming to be pointless), you could solve the problem simply by getting rid of the superfluous "set"-prefix.
Instead of setBackgroundColor and backgroundColor, have these two functions:


void backgroundColor(Color);
Color backgroundColor();


And create them by using your macro like so:

PROPERTY(Color, backgroundColor)


using a set-prefix, but no get-prefix is inconsistent. Either get rid of both (recommended), or use both. In either case, you end up using the *same* capitalization for both functions.

Problem solved?

Share this post


Link to post
Share on other sites
Well not necessarily. Having the same function name for a property creates ambiguity, since in some cases it may not be clear exactly what the function is doing. Obviously for backgroundColor it may seem obvious, but in many other cases it would not.

And why do you think that using accessor functions is pointless? When any of thee properties changes, more than just storing a new value is performed. Many other pieces of code are executed as well, which could not be done without using accessors.

Share this post


Link to post
Share on other sites
Quote:
Original post by soconne
Well I'm doing this for a GUI library I'm writing and the base Control class has a lot of properties: backgroundColor, text, border, padding, margin, font, ect...

Hence, instead of typing out:

void setBackgroundColor(Color color);
Color backgroundColor();
void setText(std::string text);
std::string text();
...


I'm trying to create macros which will shorten the process. I'd like to only have to write:

PROPERTY(Color, backgroundColor)

And then it creates the setBackgroundColor and backgroundColor methods. As I stated before, I would need to change the 'b' in backgroundColor to uppercase.

Furthermore, as to whether or not using get/set accessors for property values is good design or not, I'm simply following the coding styles Trolltech uses in QT.
http://doc.trolltech.com/qq/qq13-apis.html


Be careful using Trolltech for coding standards. While, I love the work they've done with Qt, I abhor some of their coding practices, particularly their macro abuse, excessive use of mutators, and excessive use of pointers, instead of references.

In particular, you can see that their macro abuse was uncalled for, since they made their whole framework work in Java with Qt Jambi without the ability to use macros.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!