使用枚举使用按位参数组合的最佳实践?
我正在实现一个屏幕上的视图。根据以下配置参数,每当屏幕尺寸发生变化时,视图都会被定位和调整大小。
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
};
我想知道在什么情况下按位 OR
组合枚举值来传递视图状态才有意义。
int viewState = DLViewLayoutSolo | DLViewFixedHorizontalPropertyWidth | DLViewFixedVerticalPropertyMargin | DLViewHorizonalAlignmentCenter | DLViewVerticalAlignmentMiddle;
// viewState = 1189
我希望该类的用户配置所有参数。我该如何强制执行?
在类中,我可以通过按位 AND
组合状态来检索各个设置。
if (viewState & DLViewLayoutSolo)
{
// Set up solo view layout.
}
else if (viewState & DLViewLayoutDual)
{
// Set up dual view layout.
}
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)
{
// Set up dual view layout.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想使用不同枚举中的值来组合标志吗?
这永远不会是最佳实践,事实上,我从未在任何实践中见过它,无论好坏。这样,除其他问题外,任何使用公开这些标志属性的实例的元素都必须知道或应该知道它是来自这些不同
enum
类型的值的合并。既然值相应地上升,为什么不简单地使用一个由所有成员组成的适当命名的枚举呢?
另外,如果我们在这里讨论 .NET(虽然合法,但分号告诉我可能不合法),这样的
enum
类型应该用[Flags]
属性修饰,但仍然只组合相同enum
类型的值。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 sameenum
type.听起来好像您想要其值采用每个子条件的状态的字段:
如果您担心内存消耗,并且有充分的理由这样做,那么您可以使用不可移植的位字段构造:
It sounds as though you want fields whose values take on the state of each sub-condition:
If you are concerned about memory consumption, and have valid reason to be, then you can use the non-portable bit-field construct: