473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to do a macro that creates other macros and...

Using the GNU C preprocessor, I want to declare a macro able to declare other macros for me, so that I can do:

Expand|Select|Wrap|Line Numbers
  1. declare( ONE );
  2. declare( TWO );
  3. declare( THREE );
and I get following macros declared:

NUMBER_ONE (which substitutes 1)
NUMBER_TWO (which substitutes 2)
NUMBER_THREE (which substitutes 3)

e.g., right like if I had done:

#define NUMBER_ONE 1
#define NUMBER_ONE 2
#define NUMBER_ONE 3

this is, the macro would get the number accordingly to the order in which it has been declared.

I know I can do:

Expand|Select|Wrap|Line Numbers
  1. #define declare(NAME)  { #define NUMBER_ ## NAME 1 }
But I don't know how can I get the order in which the macros have been declared instead of the "1", e.g. something that could retrieve the order in which they have been declared.

I have gone through http://gcc.gnu.org/onlinedocs/gcc-4.3.0/cpp/ but couldn't figure out a way using that.

Anyone has got any idea?

Thanks.
Jun 19 '08 #1
3 1961
RRick
463 Expert 256MB
I doubt you will be able to define a macro to define another macro, because who is going to read/decode the second macro? The second macro will need a separate pass to decode it. This is a chicken and the egg problem.

Take a look at this link for more info on preprocessors. The section on macros is cumbersome, but readable.
http://www.delorie.com/gnu/docs/gcc/cpp.html#SEC_Top
Jun 19 '08 #2
Yes, right.

I really thought it was possible to define macros that defined other macros, but I see your point now.

Thanks.-
Jun 19 '08 #3
Banfa
9,065 Expert Mod 8TB
You are doing it the wrong way round a trick, albeit one that can be confusing and make the code hard (impossible) to read, is to define a table of data and then redefine the defining macro to make use of the table to produce the effects you want.

so my table in a header called Table.h (for example)

Expand|Select|Wrap|Line Numbers
  1. #ifdef TABLE_DATA
  2.  
  3. TABLE_DATA(ONE, 1)
  4. TABLE_DATA(TWO, 2)
  5. TABLE_DATA(THREE, 3)
  6.  
  7. #undef TABLE_DATA
  8. #endif
  9.  
Note do not use inclusion protection round these lines because you may actually want to include the into another file more than once.

In a header somewhere

Expand|Select|Wrap|Line Numbers
  1. #define TABLE_DATA(name, value) extern const int NUMBER_ ## name;
  2. #include "Table.h"
  3.  
  4. extern void PrintConstants(void);
  5.  
in a c file somewhere


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define TABLE_DATA(name, value) const int NUMBER_ ## name = value;
  4. #include "Table.h"
  5.  
  6. void PrintConstants(void)
  7. {
  8. #define TABLE_DATA(name, value) printf("NUMBER_" #name " = %d\n", NUMBER_ ## name);
  9. #include "Table.h"
  10. }
  11.  

This is a trivial example that could probably be done better a different way but I have worked with examples where the table has contained 10+ different values and the symbol has been defined 5 or 6 ways in the same file sometimes with as many as 10 lines of code per definition.

It isn't pretty, it isn't easy to understand but if the code is working, tested and robust then it does allow people using that module (rather than developing it) to have control by just altering the table data.

There are times when it is very useful.
Jun 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

25
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...
699
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...
3
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
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....
8
by: David | last post by:
Hi, I am using header file for driver so Can I define macro to call Kernel function ? What are the generatl rules for defining macro. - David
14
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any ideas - its not vital but would make the code a...
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
9
by: jedi200581 | last post by:
Hello, I have a singleton class that I'm using through macros: //snippet #define DO_SOMETHING ( myArgument) \ { \ MyClass *myObject = MyClass::getInstance(); \...
3
MMcCarthy
by: MMcCarthy | last post by:
Although some users find Macros simple and easy to use, there are some major limitations to using them. Although you can use macros to perform tasks, there is limited control on when and how those...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.