Advanced usage of macros

P

Peter Poulsen

I have to create a lot of similar tables that only differs by type and name of each column
(ok, I guess that _is_ the difference between tables). I create these tables by creating a
class for a row and then letting a std::vector hold objects of these classes.

As I have to write quite a lot I would like to use macros for this. Below I have shown how
most of it works. I just cannot figure out a good way of keeping the name of each column.
I'm hoping somebody have a great idea on how to accomplish this :)



--- table_row.cc ---

#include <iostream>
#include <string>

#define BEGIN_TABLE(name) \
class name \
{ \
public: \

#define END_TABLE(size) \
void set_column(unsigned int column, std::string const& value) { \
fields_[column] = value; \
} \
\
/* Should return the name of the column */ \
std::string const column_name(unsigned int index) const { \
return ""; /* ???? Here is the problem ???? */ \
} \
private: \
std::string fields_[size]; \
}; \

#define ADD_INT_COLUMN(number, name) \
int name() const { \
return strtol(fields_[number].c_str(), NULL, 10); \
} \

#define ADD_STRING_COLUMN(number, name) \
std::string const name() const { \
return fields_[number]; \
} \

// Here I create the first class
BEGIN_TABLE(Table1)
ADD_STRING_COLUMN(0, label)
ADD_INT_COLUMN(1, value)
ADD_STRING_COLUMN(2, name)
END_TABLE(3)

int main()
{
Table1 m; // Strictly speaking this is not at table but just a single row
m.set_column(0, "Entry A");
std::cout << m.label() << std::endl;
std::cout << m.column_name(0) << std::endl; // should be "label"
std::cout << "Exiting..." << std::endl;
return 0;
}
 
P

Phlip

Peter said:
I have to create a lot of similar tables that only differs by type and name of each column
(ok, I guess that _is_ the difference between tables). I create these tables by creating a
class for a row and then letting a std::vector hold objects of these classes.

As I have to write quite a lot I would like to use macros for this. Below I have shown how
most of it works. I just cannot figure out a good way of keeping the name of each column.
I'm hoping somebody have a great idea on how to accomplish this :)
--- table_row.cc ---

#include <iostream>
#include <string>

#define BEGIN_TABLE(name) \
class name \{
\
public: \

#define END_TABLE(size) \
void set_column(unsigned int column, std::string const& value) { \
fields_[column] = value; \
\\
/* Should return the name of the column */ \
std::string const column_name(unsigned int index) const { \
return ""; /* ???? Here is the problem ???? */
\

If you really need this system (and sometimes you do), use "metamacros":

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

They provide multi-pass expansion for macros.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top