使用枚举使用按位参数组合的最佳实践?

发布于 2024-11-03 03:26:38 字数 1341 浏览 0 评论 0原文

我正在实现一个屏幕上的视图。根据以下配置参数,每当屏幕尺寸发生变化时,视图都会被定位和调整大小。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-11-10 03:26:38

您想使用不同枚举中的值来组合标志吗?

永远不会是最佳实践,事实上,我从未在任何实践中见过它,无论好坏。这样,除其他问题外,任何使用公开这些标志属性的实例的元素都必须知道或应该知道它是来自这些不同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 same enum type.

妳是的陽光 2024-11-10 03:26:38

听起来好像您想要其值采用每个子条件的状态的字段:

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

如果您担心内存消耗,并且有充分的理由这样做,那么您可以使用不可移植的位字段构造:

struct DLView {
    DLViewLayout layout : 1;
    DLViewFixedHorizontalProperty fixed_horizontal : 1;
    DLViewFixedVerticalProperty fixed_vertical : 1;
    DLViewHorizonalAlignment horizontal : 2;
    DLViewVerticalAlignment vertical : 2;
};

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;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文