|
Boost : |
From: Sebastien Martel (smartel_at_[hidden])
Date: 2005-06-17 11:18:29
Maksym Motornyy wrote:
Is there a way to integrate a solution to this problem :
[google groups] http://tinyurl.com/dtnx3
-seb
> Hello, all.
>
> I want to propose typesafe enums. Sources are available in Boost Sandbox
> (typesafe_enums.zip). Were tested under VC 7.1.
>
> Main features are:
> - type safety;
> - meaningful printable names;
> - ability to use in switch-case constructs;
> - iteration through enumerators;
> - enumerator-specific methods;
>
> Basic usage
> ===========
>
> class season_policy: public boost::simple_enumeration_policy
> {
> public:
> BOOST_DEFINE_ENUMERATOR( WINTER )
> BOOST_DEFINE_ENUMERATOR( SPRING )
> BOOST_DEFINE_ENUMERATOR( SUMMER )
> BOOST_DEFINE_ENUMERATOR( AUTUMN )
>
> typedef boost::mpl::vector< WINTER, SPRING, SUMMER, AUTUMN >
> enumerators;
> };
>
> typedef boost::enumeration< season_policy > season;
>
> Type safety
> -----------
>
> season s = season::WINTER();
>
> if ( s ) // accidentally forgotten comparison, wouldn't compile
> BOOST_ASSERT( false );
>
> if ( s == season::SUMMER() )
> std::cout << "Let's go in for fishing!";
>
> Meaningful printable names
> --------------------------
>
> season s = season::WINTER();
> BOOST_CHECK_EQUAL( s->get_name(), "WINTER" );
>
> Ability to use in switch-case constructs
> ----------------------------------------
>
> season s = season::SUMMER();
>
> switch ( s.get_id() )
> {
> case BOOST_ENUMERATOR_ID( season, SUMMER ):
> break;
> default:
> BOOST_ERROR( "Should not fall here." );
> break;
> }
>
> Iteration through enumerators
> -----------------------------
>
> for ( season::iterator i = season::begin(); i != season::end(); ++i )
> std::cout << ( *i )->get_name() << " ";
>
> Advanced usage
> ==============
>
> class operation_policy
> {
> public:
> class base_enumerator
> {
> public:
> virtual double eval(
> double operand1, double operand2 ) const = 0;
> };
>
> template < typename Functor >
> class enumerator_impl: public base_enumerator
> {
> public:
> double eval( double operand1, double operand2 ) const
> {
> return Functor()( operand1, operand2 );
> }
> };
>
> class PLUS: public enumerator_impl< std::plus< double > >
> {
> };
>
> class MINUS: public enumerator_impl< std::minus< double > >
> {
> };
>
> class TIMES: public enumerator_impl< std::multiplies< double > >
> {
> };
>
> class DIVIDE: public enumerator_impl< std::divides< double > >
> {
> };
>
> typedef boost::mpl::vector<
> PLUS, MINUS, TIMES, DIVIDE > enumerators;
> };
>
> typedef boost::enumeration< operation_policy > operation;
>
> Enumerator-specific methods
> ---------------------------
>
> for ( season::iterator i = season::begin(); i != season::end(); ++i )
> std::cout << ( *i )->eval( 4, 2 ) << " ";
> // expected output is: 6 2 8 2
>
> Is anybody found it useful?
>
> Sincerely,
> Maksym Motornyy.
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk