Advanced usage of macros

Discussion in 'C++' started by Peter Poulsen, Jul 10, 2004.

  1. 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;
    }

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Peter Poulsen, Jul 10, 2004
    #1
    1. Advertising

  2. Peter Poulsen

    Phlip Guest

    Peter Poulsen wrote:

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

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Jul 10, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    80
    Views:
    2,368
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    429
    Marco Antoniotti
    Oct 7, 2003
  3. Let_Me_Be

    Advanced Macros && Preprocessors

    Let_Me_Be, Apr 28, 2006, in forum: C Programming
    Replies:
    9
    Views:
    364
    Thad Smith
    Apr 29, 2006
  4. Let_Me_Be
    Replies:
    1
    Views:
    333
    Phlip
    Apr 28, 2006
  5. Michele Simionato
    Replies:
    1
    Views:
    587
    Lacrima
    Mar 27, 2010
Loading...

Share This Page