473,399 Members | 2,146 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,399 software developers and data experts.

multi block macro

Is it any possibility to define a macro that produces several blocks.
For example the input is :

BEFIN
ITEM(one)
ITEM(two)
ITEM(three)
END

or, alternatively,

ITEMS(one, two, three)

After preprocessing I need:

enum {one, two, three};

void on_one();
void on_two();
void on_three();

int map(char* id)
{
if (strcmp(id, "one") == 0) return one;
if (strcmp(id, "two") == 0) return two;
if (strcmp(id, "three") == 0) return three;
}

May 30 '07 #1
8 1971
ge****@gmail.com wrote:
Is it any possibility to define a macro that produces several blocks.
For example the input is :

BEFIN
ITEM(one)
ITEM(two)
ITEM(three)
END

or, alternatively,

ITEMS(one, two, three)

After preprocessing I need:

enum {one, two, three};

void on_one();
void on_two();
void on_three();

int map(char* id)
{
if (strcmp(id, "one") == 0) return one;
if (strcmp(id, "two") == 0) return two;
if (strcmp(id, "three") == 0) return three;
}
What problem are you encountering while trying to accomplish your
goal? Let's start with

#define ITEMS(a,b,c) enum { a, b, c }; \
void on_##a(); void on_##b(); void on_##c(); \
int map(char const* id) { \
return 42; \
}

Now's your turn.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 30 '07 #2
On May 30, 10:17 am, gel...@gmail.com wrote:
Is it any possibility to define a macro that produces several blocks.
For example the input is :

BEFIN
ITEM(one)
ITEM(two)
ITEM(three)
END

or, alternatively,

ITEMS(one, two, three)

After preprocessing I need:

enum {one, two, three};

void on_one();
void on_two();
void on_three();

int map(char* id)
{
if (strcmp(id, "one") == 0) return one;
if (strcmp(id, "two") == 0) return two;
if (strcmp(id, "three") == 0) return three;}
Hello.

Actually, yes, it is, but only in the alternative form used by you:

#define ITEMS(one, two, three) \
enum { one, two, three }; \
void on_ ## one (); \
void on_ ## two (); \
void on_ ## three ();

[]s

================
Wanderley Caloni
http://www.cthings.org

May 30 '07 #3
What problem are you encountering while trying to accomplish your
goal? Let's start with
Well, I'm implementing an interface between activex javascript and c++
backend. Every javascript call is translated into a query to a
function that retuns this call id, i.e., it converts a function name
into some unique integer. Second a function is invoked, and this
functions is called with this id. For example, let's assume, there are
two javascript functions: read and write. C++ code should be (for the
sake of simplicity I ommit a lot of unnecisesary details):

enum {read, write}

int get_id(char* name)
{
if (strcmp(name, "read")==0) return read;
if (strcmp(name, "write")==0) return write;
}

void invoke(int id)
{
if (id == read) on_read();
if (id == write) on_write;
}

I want to define macroses that generate this code automatically.

May 30 '07 #4
Sorry, I forgot to note, the number of arguments is not known ion
advance.

May 30 '07 #5
ge****@gmail.com wrote:
Sorry, I forgot to note, the number of arguments is not known ion
advance.
Variadic macros are coming in the next Standard, IIRC. At this
time you'll have to introduce all 1 through N macros yourself,
manually.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 30 '07 #6
Actually, visual c++ already has got __VA_ARGS__, but to the best of
my knowladge there is no way to iterate through the args list.

BTW, I found a different solution. ITEM(one), ITEM(two), ... may be
put into an additional header and it should be includede several times
with different enviroment.

May 30 '07 #7
On May 30, 8:17 pm, gel...@gmail.com wrote:
Is it any possibility to define a macro that produces several blocks.
For example the input is :

BEFIN
ITEM(one)
ITEM(two)
ITEM(three)
END

or, alternatively,

ITEMS(one, two, three)

After preprocessing I need:

enum {one, two, three};

void on_one();
void on_two();
void on_three();

int map(char* id)
{
if (strcmp(id, "one") == 0) return one;
if (strcmp(id, "two") == 0) return two;
if (strcmp(id, "three") == 0) return three;

}
You can reformulate this so that you're doing it in a different way.
This is the way that I've done this sort of thing in the past.

Use a global for the next id number:

int g_nextID = 1;

Use a map to store the function to run given an id or a name (use
wstring because JavaScript is UTF-16):

std::map< int, boost::function< void () g_byID;
std::map< std::wstring, boost::function< void () g_byName;

A superclass can now handle the registration:

class Function {
public:
Function( const std::wstring &name )
: m_id( g_nextID++ ), m_name( name ) {
g_byID[ m_id ] = boost::bind( &Function::execute, this );
g_byName[ m_name ] = g_byID[ m_id ];
}
virtual void execute() const = 0;
};

Now to add a "read" function:

class Read : public Function {
public:
Read() : Function( L"read" ) {}
void execute() const {
// Code to execute
}
} g_doRead;

To execute then by name or by id you simply execute the thing in the
map:

g_byID[ theID ]();

or

g_byName[ theName ]();

You can use boost::lambda to bind extra parameters and then pass the
missing ones in at invocation.

It looks different, but works just the same. If the ID numbers must be
g'teed the same all the time then issue the ID numbers yourself and
pass them from the constructor like the name. Or consider generating
the ID as a hash of the name.

A big advantage of this is that you can late load libraries (using
something like LoadLibrary in Windows) so the range of functions
supported can be extended much more neatly than otherwise possible as
you don't have an enum to edit.
K

May 31 '07 #8
On May 30, 3:17 pm, gel...@gmail.com wrote:
Is it any possibility to define a macro that produces several blocks.
For example the input is :
BEFIN
ITEM(one)
ITEM(two)
ITEM(three)
END
or, alternatively,
ITEMS(one, two, three)
After preprocessing I need:
enum {one, two, three};
void on_one();
void on_two();
void on_three();
int map(char* id)
{
if (strcmp(id, "one") == 0) return one;
if (strcmp(id, "two") == 0) return two;
if (strcmp(id, "three") == 0) return three;

}
Is there any reason this has to be done with macros. Victor has
pretty much covered the situation from within C++, but for
something like this, I'd normally use AWK to generate the code
from lines that look like:
one two three
It would only take about five minutes to write the AWK, and it
would be easily understood at a glance.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 31 '07 #9

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

Similar topics

16
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
4
by: Bob | last post by:
I'm running Access 97 and have a sub-form within a main form. The subform allows data entry by the user. Now, I would also like to enter a row from a macro, but when I try, the entry always goes...
6
by: Everton da Silva Marques | last post by:
Hi, I need to allocate, using malloc(), a single memory block to hold two structures and a variable length string. Is it safe (portable, alignment-wise) to sum up the sizeof's of those...
3
by: Joe Befumo | last post by:
This is my first attempt at multi-thread programming, and I'm encountering a program-logic problem that I can't quite put my finger on. The program is pretty simple. I'm trying to validate a...
8
by: cj | last post by:
Has MS included in VB2005 any multi-line comment methods like in C? /* This is a multi-line comment in C */ It's something I'd like to have. I did read somewhere that I could use Ctrl+K,...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
Sorry this is so long winded, but here goes. Following the model of http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.channels.ipc.ipcchannel.aspx I made a remote object using the...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
5
by: =?Utf-8?B?amM=?= | last post by:
Hello, This compiles OK using Multi-Byte character set, but when I switch to Unicode I get an error. char reply = _T("olleh"); I know this will fix the error with the Unicode compile, wchar_t...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.