Best practice to use bitwise parameter combinations using enums?
I am implementing a view that lives on a screen. Based on the following configuration parameters the view will be positioned and resized whenever the screen size changes.
enum DLViewLayout
{
DLViewLayoutSolo = 1,
DLViewLayoutDual = 2
};
enum DLViewFixedHorizontalProperty
{
DLViewFixedHorizontalPropertyWidth = 4, // View margins scale with the screen width.
DLViewFixedHorizontalPropertyMargin = 8 // View width scales with the screen width.
};
enum DLViewFixedVerticalProperty
{
DLViewFixedVerticalPropertyHeight = 16,
DLViewFixedVerticalPropertyMargin = 32
};
enum DLViewHorizonalAlignment
{
DLViewHorizonalAlignmentLeft = 64,
DLViewHorizonalAlignmentCenter = 128,
DLViewHorizonalAlignmentRight = 256
};
enum DLViewVerticalAlignment
{
DLViewVerticalAlignmentTop = 512,
DLViewVerticalAlignmentMiddle = 1024,
DLViewVerticalAlignmentBottom = 2048
};
I wonder in what situation it would make sense to bitwise OR
combine the enum values to pass a view state.
int viewState = DLViewLayoutSolo | DLViewFixedHorizontalPropertyWidth | DLViewFixedVerticalPropertyMargin | DLViewHorizonalAlignmentCenter | DLViewVerticalAlignmentMiddle;
// viewState = 1189
I want the user of the class to configure all parameter. How can I enforce this?
Within the class I can retrieve the individual settings by bitwise AND
combining the state.
if (viewState & DLViewLayoutSolo)
{
// Set up solo view layout.
}
else if (viewState & DLViewLayoutDual)
{
//开发者_StackOverflow Set up dual view layout.
}
You want to combine flags using values from differing enumerations?
This is never going to be best practice, in fact, I've never seen it in any practice, good or bad. This way, among other issues, any element utilising an instance which exposes a property of these flags must know, or be expected to know, that it's an amalgamation of values from these differing enum
types.
Since the values ascend accordinly, why not simply use an aptly named enumeration consisting of all members?
Also, if we're talking .NET here (although legal, the semi-colons tell me maybe not), such enum
types should be decorated with the [Flags]
attribute, but still, only combine values of the same enum
type.
It sounds as though you want fields whose values take on the state of each sub-condition:
enum DLViewLayout
{
DLViewLayoutSolo,
DLViewLayoutDual
};
enum DLViewFixedHorizontalProperty
{
DLViewFixedHorizontalPropertyWidth, // View margins scale with the screen width.
DLViewFixedHorizontalPropertyMargin // View width scales with the screen width.
};
enum DLViewFixedVerticalProperty
{
DLViewFixedVerticalPropertyHeight,
DLViewFixedVerticalPropertyMargin
};
enum DLViewHorizonalAlignment
{
DLViewHorizonalAlignmentLeft,
DLViewHorizonalAlignmentCenter,
DLViewHorizonalAlignmentRight
};
enum DLViewVerticalAlignment
{
DLViewVerticalAlignmentTop,
DLViewVerticalAlignmentMiddle,
DLViewVerticalAlignmentBottom
};
struct DLView {
DLViewLayout layout;
DLViewFixedHorizontalProperty fixed_horizontal;
DLViewFixedVerticalProperty fixed_vertical;
DLViewHorizonalAlignment horizontal;
DLViewVerticalAlignment vertical;
};
// ...
DLView viewState;
// ...
if ( viewState.layout == DLViewLayoutSolo ) { ... }
else if ( viewState.layout == DLViewLayoutDual ) { ... }
}
/// repeat per field
If you are concerned about memory consumption, and have valid reason to be, then you can use the non-portable bit-field construct:
struct DLView {
DLViewLayout layout : 1;
DLViewFixedHorizontalProperty fixed_horizontal : 1;
DLViewFixedVerticalProperty fixed_vertical : 1;
DLViewHorizonalAlignment horizontal : 2;
DLViewVerticalAlignment vertical : 2;
};
精彩评论