Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2004-04-21 18:23:34


> [mailto:boost-bounces_at_[hidden]] On Behalf Of Arkadiy Vertleyb

> Actually I don't need to have many things on the same line,
> but I do want to
> handle inclusions from several headers... Maybe it's somehow
> possible to
> generate a unique number from the header file name? Or use
> this #include
> mechanism you mentioned?

It depends on what exactly you need the value for. Boost.Preprocessor contains
a mechanism that is capable of storing/setting a value (technically, more than
one):

#define BOOST_PP_VALUE 1
#include BOOST_PP_ASSIGN_SLOT(1)

BOOST_PP_SLOT(1) // 1

#define BOOST_PP_VALUE BOOST_PP_SLOT(1) + 1
#include BOOST_PP_ASSIGN_SLOT(1)

BOOST_PP_SLOT(1) // 2

// etc.

Doing the incrementation manually is tedious, so you'd want to make a separate
file just to do that (and I'd recommend also defining a macro that expands to
that filename just so it is really obvious that you're using the #include
mechanism in an abnormal way):

# // detail/counter.hpp
# ifndef DETAIL_COUNTER_HPP
# define DETAIL_COUNTER_HPP
#
# include <boost/preprocessor/slot/slot.hpp>
#
# define BOOST_PP_VALUE 1
# include BOOST_PP_ASSIGN_SLOT(1)
#
# else
#
# define BOOST_PP_VALUE BOOST_PP_SLOT(1) + 1
# include BOOST_PP_ASSIGN_SLOT(1)
#
# endif

# // counter.hpp
# ifndef COUNTER_HPP
# define COUNTER_HPP
#
# define COUNTER BOOST_PP_SLOT(1)
# define NEXT() "detail/update.hpp"
#
# endif

And then use it like this:

#include "counter.hpp"

#include NEXT()
COUNTER // 1

#include NEXT()
COUNTER // 2

#include NEXT()
COUNTER // 3

// etc.

It is better, IMO, to do the update immediately before using the value rather
than immediately after because that localizes mistakes. If, on the other hand,
you don't need to use the resulting value as a macro argument (which is
unlikely), you can encapsulate the above process to only a single header
inclusion:

#include COUNTER() // 1
#include COUNTER() // 2
#include COUNTER() // 3

// etc.

Do that help?

Regards,
Paul Mensonides


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk