|
Boost : |
From: Frank Laub (frank.laub_at_[hidden])
Date: 2005-12-09 00:45:26
Thanks a lot for this suggestion. It inspired me to write version 2. In this
approach, the macro expansion only produces a type that contains the
underlying enum and storage(), which contains all the strings. The trick now
is that there is:
template <typename base>
struct safe_enum : base;
The macro now expands:
BOOST_ENUM(Boolean,
(False)
(True)
)
to:
class Boolean_base
{
public:
enum type
{
False,
True,
size = 2,
Invalid = -1
};
protected:
typedef boost::array<const char*, (size)+1> array_type;
static const array_type& storage()
{
static const array_type names =
{
"False",
"True",
"2"
};
return names;
}
};
typedef safe_enum<Boolean_base> Boolean;
The Boolean now gets the following methods from safe_enum:
begin()
end()
operator[]
str() (CT and RT versions)
parse()
I was thinking of re-naming the other kind of enum that I had before
(BOOST_ENUM_COMMENTS) to something like BOOST_STRINGTABLE, since that's
really the intention.
I've uploaded v2 to the file vault under Preprocessor Metaprogramming. I'd
love to hear more suggestions or even complaints!
Thanks a ton,
-Frank
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk