Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 06, 2008 Rotating though an enum idiom | ||||
---|---|---|---|---|
| ||||
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 Re: Rotating though an enum idiom | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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 Re: Rotating though an enum idiom | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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 Re: Rotating though an enum idiom | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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
|
Copyright © 1999-2021 by the D Language Foundation