Thread overview
Rotating though an enum idiom
Sep 06, 2008
Spacen Jasset
Sep 06, 2008
Spacen Jasset
Sep 06, 2008
Denis Koroskin
Sep 06, 2008
Manfred_Nowak
September 06, 2008
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.
September 06, 2008
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));
September 06, 2008
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)

September 06, 2008
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