www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Rotating though an enum idiom

reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Is there a way to "rotate" though and enum; So that:


enum RenderMode { POINT, LINE, FILL }

renderMode = POINT;
fn(renderMode) gives LINE
fn(renderMode) gives FILL
fn(renderMode) gives POINT
and so on, in rotation.

I would normally use some sort of modulo operation on an int to do this, 
but I wonder if there is another way.
Sep 06 2008
next sibling parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Spacen Jasset wrote:
 Is there a way to "rotate" though and enum; So that:
 
 
 enum RenderMode { POINT, LINE, FILL }
 
 renderMode = POINT;
 fn(renderMode) gives LINE
 fn(renderMode) gives FILL
 fn(renderMode) gives POINT
 and so on, in rotation.
 
 I would normally use some sort of modulo operation on an int to do this, 
 but I wonder if there is another way.
Like this: mRenderMode = cast(RenderMode)((mRenderMode + 1) % (RenderMode.max + 1));
Sep 06 2008
parent "Denis Koroskin" <2korden gmail.com> writes:
On Sun, 07 Sep 2008 02:18:32 +0400, Spacen Jasset  
<spacenjasset yahoo.co.uk> wrote:

 Spacen Jasset wrote:
 Is there a way to "rotate" though and enum; So that:
   enum RenderMode { POINT, LINE, FILL }
  renderMode = POINT;
 fn(renderMode) gives LINE
 fn(renderMode) gives FILL
 fn(renderMode) gives POINT
 and so on, in rotation.
  I would normally use some sort of modulo operation on an int to do  
 this, but I wonder if there is another way.
Like this: mRenderMode = cast(RenderMode)((mRenderMode + 1) % (RenderMode.max + 1));
T rotate(T)(T elem) { return cast(T)((elem + 1) % (T.max + 1)); } enum RenderMode { POINT, LINE, FILL } RenderMode mRenderMode = RenderMode.POINT; mRenderMode = rotate(mRenderMode); // returns RenderMode.LINE
Sep 06 2008
prev sibling parent "Manfred_Nowak" <svv1999 hotmail.com> writes:
Spacen Jasset wrote:

 use some sort of modulo operation
Might be dependent on what you know about the enum: enum E{ A , B=42 , C=int.max} -manfred -- If life is going to exist in this Universe, then the one thing it cannot afford to have is a sense of proportion. (Douglas Adams)
Sep 06 2008