Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-05-28 10:54:35


John Torjo wrote
> > I have adopted a Java-style approach to enumerated-style values (don't
>know
> > if this is the best approach, just the way that I do it now). For
>example:

> > class WhatIsTheMatrix
> > {
> > public:
> > typedef unsigned char type;
> > public:
> > static const type TheWhat = 0;
> > static const type Matrix = 1;
> > static const type AniMatrix = 2;
> > static const type MatrixReloaded = 3;
> > static const type MatrixRevolutions = 4;
> > };

>I think this is really cool!

Thanks!

>Basically, you should be able to do
>WhatIsTheMatrix::type m = WhatIsTheMatrix::TheWhat;
>++m;
>switch( m) {
>case WhatIsTheMatrix::TheWhat: //...
>case WhatIsTheMatrix::AniMatrix: //...
>...
>}

>The unfortunate thing is that you lose compile-time checking. You'll be
>able
>to say something like
>WhatIsTheMatrix::type m = 20;

This could be resolved using something like the ideas by Dave Harris and
Daniel Frey. This would then make WhatIsTheMatrix the enumerated type, thus
allowing you to do operator overloading on the class.

The class could then be defined in terms of something like:
   boost::enumeration::range< type, lower_value, upper_value >
to restrict the values within a given range. Thus:

WhatIsTheMatrix m = WhatIsTheMatrix::TheWhat;
switch( m )
{
   case WhatIsTheMatrix::TheWhat: //...
   case WhatIsTheMatrix::AniMatrix: //...
}

The bitflag enumeration usage would have the form:
   boost::enumeration::bitflags< type, mask >
where mask will be used to check if a flag is valid or not.

>What I would really love is some library to allow me to do something
>similar
>to this:

>ENUM(Variant) { VT_I1, VT_I2, VT_I4, VT_BSTR, BIT_FLAGS( VT_BYREF,
>VT_ARRAY) };

This would be a combined range-bitflag enumerated type:
   boost::enumeration::range_bitflag< type, lower_value, upper_value, mask >

>[snip]

>I'm not sure operators like ++,--, +,-, etc. should be made workable for
>enums.

I agree. An enumerated type should have the form:

template< typename EnumType > class enumerated
{
   public:
      typedef EnumType enum_type;
   public:
      inline enumerated & operator=( const enumerated & );
      inline enumerated & operator=( enum_type ); // may throw if invalid
value
   public:
      inline enumerated(); // some default value
      inline enumerated( enum_type ); // may throw if invalid value
      inline enumerated( const enumerated & );
};

with == and != defined for
   ( enumerated, enumerated )
   ( enumerated, enumerated::enum_type )
   ( enumerated::enum_type, enumerated )

I have said that assignment or construction to an invalid enum_type will
throw an exception, because although this means that checking occurs at
run-time (harder to debug), it is easier to implement.

NOTE: These are just suggestions and may not be the best way to go about
this.

Regards,
Reece

_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today!
http://www.msn.co.uk/messenger


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