multi block macro

G

geloli

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;
}
 
V

Victor Bazarov

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
 
W

Wanderley Caloni

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
 
G

geloli

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.
 
V

Victor Bazarov

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
 
G

geloli

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.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

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
 
J

James Kanze

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top