如何C++代表与操作顺序相关的状态标志?
我现在正在研究运动控制系统。我想使运动控制接口更加灵活,因此我想提供一个可以指定受控轴的API。
就像。
void moveTo(/*data*/, Axis which);
轴参数用于定义应如何处理运动数据,并且内部实施将根据请求的轴检查数据。
因此,我想在轴参数上定义操作规则。
我尝试了位标志,定义为如下。
enum class Axis : std::uint_fast16_t {
X = 1l << 0,
Y = 1l << 1,
Z = 1l << 2
};
// operator | and & and so on are also defined.
它可以用如下。
const auto which = Axis::X | Axis::Y;
但是我想要的是, axis :: x | Axis :: y 和 Axis :: y | Axis :: X 应该有所不同,但是现在它们具有相同的价值。无法在功能中区分它们。
我应该如何定义似乎与“操作顺序”相关的蒙版?
似乎是通过在单个枚举中列出所有允许的合并状态来做到这一点。但是我仍然想问是否有更优雅和清晰的解决方案。
还是更好地实施此类功能的建议?
我希望最终用法仍然像咬面罩一样简单,我不介意我是否在幕后做一些非常复杂的工作,我不介意使用任何第三方库。
I am now working on a motion control system. I want to make the motion control interface more flexible, so I want to provide an api that can specifiy the controlled axis.
It just like.
void moveTo(/*data*/, Axis which);
Axis parameter is used to define how the motion data should be treated as and the internal implement will check the data according the requested axis.
So i want to define a operation rules on Axis parameters.
I tried the bit flags, defined as follow.
enum class Axis : std::uint_fast16_t {
X = 1l << 0,
Y = 1l << 1,
Z = 1l << 2
};
// operator | and & and so on are also defined.
It can be used as follow.
const auto which = Axis::X | Axis::Y;
But what i want is that, Axis::X | Axis::Y and Axis::Y | Axis::X should be different, but now they have same value. There is no way to distinguish them inside the function.
How should I define such a bit mask that appears to be related to "order of operations"?
It seems to do this by listing all allowed combined states in a single enumeration. But I still want to ask if there is a more elegant and clear solution.
Or any suggestion for a better implementation of such functionality?
I hope the final usage is still as simple as the bit mask, I don't mind if I do some really complicated work behind the scenes, I don't mind using any 3rd party library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于您只有3个轴和有限的组合,我建议只定义所有轴。
如果您想以编程方式进行操作,那么您可以做的是每次添加新标志时将值移动2来制作标志,因为每个值都由2位表示。
Given that you just have 3 axes and a limited amount of combinations, I'd suggest just defining them all.
If you want to do it programmatically, what you can do is to make the flag by shifting the value by 2 every time you add a new flag, as each value is represented by 2 bits.