473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Macro to iteratively generate variable names

I am designing a GUI (my question is not about GUIs) and I have named my
variables using a regular naming scheme. However, in order to simplify
the code using these variables, I have created an array of non-owning
pointers to these variables. I am trying to write a macro to generate
these variable names for me, but I am not sure if what I want to do is
possible.

The code below demonstrates what I want to do (except it is generating
function names instead of variable names). I would like to have a macro
that I can put in a loop to generate the various names. However, since
macros are expanded before the compilation phase, I know this won't work
like this (in the loop, the macro gets expanded to method_i_do() since
the actual values for 'i' do not get set until runtime).

If it is possible at all, I am seeking a simple solution. There are 45
variables, which can be grouped into sets of 5 that I will apply the
solution to. It's not a matter of laziness (I have already typed the
variable names explicitly); I would just like to avoid having lots of
lines of trivial code that are all very similar, but only if the
overhead of implementing the solution is not significantly greater than
listing all 45 variables.

Is this possible using templates, or any other standard technique?
#include <iostream>

void method_1_do() { std::cout << "Method 1\n"; }
void method_2_do() { std::cout << "Method 2\n"; }
void method_3_do() { std::cout << "Method 3\n"; }

#define METHOD_DO(pre, i, post) pre ## i ## post()

int main()
{
METHOD_DO(metho d_, 1, _do);
METHOD_DO(metho d_, 2, _do);
METHOD_DO(metho d_, 3, _do);

/*
for (int i = 0; i < 3; ++i) {
METHOD_DO(metho d_, i, _do);
}
*/

return 0;
}
--
Marcus Kwok
Apr 4 '06 #1
6 6161

Marcus Kwok wrote:
I am designing a GUI (my question is not about GUIs) and I have named my
variables using a regular naming scheme. However, in order to simplify
the code using these variables, I have created an array of non-owning
pointers to these variables. I am trying to write a macro to generate
these variable names for me, but I am not sure if what I want to do is
possible.

The code below demonstrates what I want to do (except it is generating
function names instead of variable names). I would like to have a macro
that I can put in a loop to generate the various names. However, since
macros are expanded before the compilation phase, I know this won't work
like this (in the loop, the macro gets expanded to method_i_do() since
the actual values for 'i' do not get set until runtime).

If it is possible at all, I am seeking a simple solution. There are 45
variables, which can be grouped into sets of 5 that I will apply the
solution to. It's not a matter of laziness (I have already typed the
variable names explicitly); I would just like to avoid having lots of
lines of trivial code that are all very similar, but only if the
overhead of implementing the solution is not significantly greater than
listing all 45 variables.

Is this possible using templates, or any other standard technique?
#include <iostream>

void method_1_do() { std::cout << "Method 1\n"; }
void method_2_do() { std::cout << "Method 2\n"; }
void method_3_do() { std::cout << "Method 3\n"; }

#define METHOD_DO(pre, i, post) pre ## i ## post()

int main()
{
METHOD_DO(metho d_, 1, _do);
METHOD_DO(metho d_, 2, _do);
METHOD_DO(metho d_, 3, _do);

/*
for (int i = 0; i < 3; ++i) {
METHOD_DO(metho d_, i, _do);
}
*/

return 0;
}
--
Marcus Kwok


I think this is the closest you can get to what you want:

#define VARLIST \
X(int, a) \
X(char, b) \
X(double, c)

// Define vars.
#undef X
#define X(TYPE, NAME) TYPE NAME;
VARLIST

// Define ptrs.
#undef X
#define X(TYPE, NAME) TYPE * p_##NAME = &NAME;
VARLIST

Apr 4 '06 #2
"Marcus Kwok" <ri******@gehen nom.net.invalid > wrote in message
news:e0******** **@news-int2.gatech.edu ...
:I am designing a GUI (my question is not about GUIs) and I have named my
: variables using a regular naming scheme. However, in order to simplify
: the code using these variables, I have created an array of non-owning
: pointers to these variables. I am trying to write a macro to generate
: these variable names for me, but I am not sure if what I want to do is
: possible.
[...]

I'm not an expert in this, but you probably should look into the Boost
Preprocessor Metaprogramming library:
http://www.boost.org/libs/preprocessor/doc/index.html
The provided examples are likely to inspire you towards a solution
if a macro-based solution is what you are looking for.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Apr 4 '06 #3
Marcus Kwok wrote:
I am designing a GUI (my question is not about GUIs)
You are allowed to ask on-topic questions about GUIs here.
void method_1_do() { std::cout << "Method 1\n"; }
void method_2_do() { std::cout << "Method 2\n"; }
void method_3_do() { std::cout << "Method 3\n"; }


Why so many methods?

Why aren't they members of a class?

Have you researched how other C++ GUIs do this?

Can't you find a leaner abstraction with less duplicated code?

If not, here's a good technique to inflate code around macros:

http://www.codeproject.com/macro/metamacros.asp

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 4 '06 #4
Ivan Vecerina <IN************ *****@ivan.vece rina.com> wrote:
"Marcus Kwok" <ri******@gehen nom.net.invalid > wrote in message
news:e0******** **@news-int2.gatech.edu ...
:I am designing a GUI (my question is not about GUIs) and I have named my
: variables using a regular naming scheme. However, in order to simplify
: the code using these variables, I have created an array of non-owning
: pointers to these variables. I am trying to write a macro to generate
: these variable names for me, but I am not sure if what I want to do is
: possible.
[...]

I'm not an expert in this, but you probably should look into the Boost
Preprocessor Metaprogramming library:
http://www.boost.org/libs/preprocessor/doc/index.html
The provided examples are likely to inspire you towards a solution
if a macro-based solution is what you are looking for.


Thanks, I'll take a look.

--
Marcus Kwok
Apr 5 '06 #5
Phlip <ph******@yahoo .com> wrote:
Marcus Kwok wrote:
I am designing a GUI (my question is not about GUIs)
You are allowed to ask on-topic questions about GUIs here.
void method_1_do() { std::cout << "Method 1\n"; }
void method_2_do() { std::cout << "Method 2\n"; }
void method_3_do() { std::cout << "Method 3\n"; }


Why so many methods?

Why aren't they members of a class?


Sorry, this was really just demonstrating the problem of putting the
variable 'i' into the macro, since the literal 'i' instead of the
numerical value of 'i' gets substituted into the macro. Really, my code
is doing something more like this:
// These get generated by the GUI designer
TextBox* textbox_foo_1_l ow;
TextBox* textbox_foo_2_l ow;
TextBox* textbox_foo_3_l ow;
TextBox* textbox_foo_4_l ow;
TextBox* textbox_foo_5_l ow;

// This is an array of non-owning (weak?) pointers to the variables,
// which is only used to simplify code using the textboxes by allowing
// code that has to be applied to all of them to be placed in a loop.
TextBox* foo_textboxes[max];
foo_textboxes[0] = textbox_foo_1_l ow;
foo_textboxes[1] = textbox_foo_2_l ow;
foo_textboxes[2] = textbox_foo_3_l ow;
foo_textboxes[3] = textbox_foo_4_l ow;
foo_textboxes[4] = textbox_foo_5_l ow;

Have you researched how other C++ GUIs do this?

Can't you find a leaner abstraction with less duplicated code?
Actually, this is an abstraction because by doing this, I can now have
code that does, for example,

for (int i = 0; i < max; ++i) {
foo_textboxes[i]->Visible = true;
}

instead of

textbox_foo_1_l ow->Visible = true;
textbox_foo_2_l ow->Visible = true;
textbox_foo_3_l ow->Visible = true;
textbox_foo_4_l ow->Visible = true;
textbox_foo_5_l ow->Visible = true;

It's the initialization of the array of pointers that I was hoping to
abstract more.
If not, here's a good technique to inflate code around macros:

http://www.codeproject.com/macro/metamacros.asp


Thanks, this looks interesting.

--
Marcus Kwok
Apr 5 '06 #6
Marcus Kwok wrote:
I am designing a GUI (my question is not about GUIs)

I looked at that, and read, "I am designing a new GUI toolkit from scratch".

If you were, then a map of pointers to members would be a perfect C++ way to
accomplish the "property bag" feature of all GUI controls.

You are, instead, concocting a single application's GUI. You are automating
the system that plugs the View into the Controller of a
Model-View-Controller pattern. The table of links between fields is the
Controller.

For an example of growing such a system, download the preview PDF from
here...

http://www.zeroplayer.com/cgi-bin/wi...UserInterfaces

....then search for Model View Controller.

My system did not happen to use a batch of macros. Yours still might if your
project needs them.
It's the initialization of the array of pointers that I was hoping to
abstract more.


Right - that's what OO is for. You need View objects that represent your
target view, and that encapsulate all the batches of updates to the target
GUI Toolkit objects themselves. MVC wraps that situation perfectly.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
3256
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a clarification on what 'macro' means. A Lisp macro is a way of modifying code when that code is first defined. It can rearrange the structure of the code, and add and remove parts of it. Unlike C's #define macro language, Lisp macros understand the...
699
34114
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
8
5768
by: rong.guo | last post by:
Greetings! I am now doing one type of analysis every month, and wanted to creat table names in a more efficient way. Here is what happens now, everytime I do the analysis, I will create a table called something like customer_20050930, and then update the table by using several update steps. Then next month I will create a table called customer_20051031. Does anyone know if there is a better way to do it? like using a macro variable...
2
3491
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your menus by customizing a toolbar. With this method you have to create a separate macro for every single menu and sub menu. The old method would allow me to include several menus (File/ print/ page setup ...) all within one macro. Now it seems I...
3
13567
by: arut | last post by:
I would like to know when a const should be used and when a #define is necessary in a program using a constant. What are the pros and cons?
7
23555
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
17
2165
by: Russell Shaw | last post by:
Hi, How do i make an if/then/else macro act as a function so that the whole thing looks like the return value? I tried this lame attempt for starters: #define A_FROM_B(b) \ ( \ if(b < 10) { \
9
2788
by: userblue | last post by:
Hi Does anyone know if there is a way to define what is effectively a single globally visible, enumerated list whilst actually defining the entries across several different modules? or somehow do a similar thing with macros. Details: I have a c project to fit into a small microprocessor and need to save some ram. I have a significant number of flags all over the place that currently use whole byte storage. I thought if I had a way to...
36
2561
by: sh.vipin | last post by:
how to make large macro paste the code as it is Problem Explanation '-- For example in the program below /* a.c - starts here */ #define DECL_VARS() \ unsigned int a0;\ unsigned int a1;\ unsigned int a2;\
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.