Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Efficient E...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 13 of 13 Topic 9544 of 10094
Post > Topic >>

Re: Efficient Enum Access...

by Marsh Ray <marsh527@[EMAIL PROTECTED] > May 2, 2008 at 12:48 PM

On Apr 25, 11:48 am, Thomas Lehmann <t.lehm...@[EMAIL PROTECTED]
> wrote:
> How can I check wether a given value is
> contained by a given enum?

For enums for which the integer values are not significant,  I often
do something similar to:

enum color
{
   red, green, blue,
   // add more here
   color_cnt // keep me last
};

void f(int raw_color)
{
   if (raw_color < 0 || raw_color >= color_cnt)
      throw std::runtime_error("Unexpected data.");

   color c = static_cast<color>(raw_color);

   // ...
}

It seems like that 'if' range assertion could be made to work in one
of the enum_cast function templates suggested by others, even without
specialization, if one were willing to commit to a little discipline
in naming:

struct color {
   enum enum_type // always named this
   {
     red, green, blue,
     // add more here
     count_of_enumerators // always named this
   };
};

template <class enum_wrapper_T, class conv_to_integral_T>
typename enum_wrapper_T::enum_type enum_cast(conv_to_integral_T
raw_val)
{
   int n = raw_val;

   if (n < 0 || n >= enum_wrapper_T::count_of_enumerators)
      throw std::runtime_error("Unexpected data.");

   return static_cast<enum_wrapper_T::enum_type>(n);
}

This has a side effect (one which I like) of hiding the enum
identifiers inside the struct color:

void f(int raw_color)
{
   switch (enum_cast<color>(raw_color))
   {
   case color::red:   /* ... */ break;
   case color::green: /* ... */ break;
   case color::blue:  /* ... */ break;
   //default: Really can't happen if cases cover all enumerators.
   }
}

- Marsh

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 13 Posts in Topic:
Efficient Enum Access...
Thomas Lehmann <t.lehm  2008-04-25 10:48:42 
Re: Efficient Enum Access...
nickf3 <nickf3@[EMAIL   2008-04-25 15:47:51 
Re: Efficient Enum Access...
Thiago Adams <thiago.a  2008-04-25 15:48:52 
Re: Efficient Enum Access...
brangdon@[EMAIL PROTECTED  2008-04-26 08:47:57 
Re: Efficient Enum Access...
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-26 04:43:31 
Re: Efficient Enum Access...
brangdon@[EMAIL PROTECTED  2008-04-26 08:47:32 
Re: Efficient Enum Access...
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-26 16:31:39 
Re: Efficient Enum Access...
brangdon@[EMAIL PROTECTED  2008-04-27 12:39:04 
Re: Efficient Enum Access...
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-28 01:55:51 
Re: Efficient Enum Access...
Seungbeom Kim <musiphi  2008-04-26 08:42:16 
Re: Efficient Enum Access...
Alberto Ganesh Barbati &l  2008-04-26 08:43:12 
Re: Efficient Enum Access...
Tony Delroy <tony_in_d  2008-04-28 02:13:22 
Re: Efficient Enum Access...
Marsh Ray <marsh527@[E  2008-05-02 12:48:54 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 7:56:09 CDT 2008.