[英]C, proper use of enums, unions and function return
My goal is to achieve best code / design practices:我的目标是实现最佳代码/设计实践:
I have functions that return 2 possible enums which can be a (state enum) or an (error enum).我有返回 2 个可能的枚举的函数,它们可以是(状态枚举)或(错误枚举)。
in order to distinguish between the two enums, they have to be far values from each other and never overlap for example:为了区分两个枚举,它们必须是彼此相距很远的值并且永远不会重叠,例如:
other modules has function are expecting the (error enum).其他模块有 function 期待(错误枚举)。 and i do not want to make mapping function from state to error.
而且我不想将 function 从 state 映射到错误。 so i have to contain (enum error) inside (enum state).
所以我必须在(枚举状态)内包含(枚举错误)。
Enum eSomeState{
enum1Value = 0,
...
enum1Value99 = 99, // <<max
};
//defined in different module:
Enum eSomeError{
enum2Value = 100, // <<min
...
enum2Value99 = 199,
};
as if they overlapped, it will be impossible to know which one was returned from the called function (without extra flag).好像它们重叠了,不可能知道哪个是从被调用的 function 返回的(没有额外的标志)。
So, earlier in my code i used to define two enums, (combining all error in error enum) into (state enum).. like following所以,在我的代码的前面,我曾经定义两个枚举,(将错误枚举中的所有错误组合)到(状态枚举)中......就像下面
Version 1:版本 1:
/* used by this module */
enum eSomeState{
eSomeState_A = 1,
eSomeState_B = 2,
eSomeState_C = 3,
eSomeState_D = 4,
/* do not reach 100 */
/* DO NOT GO BELOW 100 */ <------------- copied from other module's enum
/* every time i update eSomeErrorFromOtherModule, i have to come here and update this as well */
eSomeStateError_ErrA = 100,
eSomeStateError_ErrB = 101,
eSomeStateError_ErrC = 102,
};
/* used by this module and other module that cares about error handling */
/* defined in different scope */
enum eSomeErrorFromOtherModule{
eSomeError_none = 0,
/* DO NOT GO BELOW 100 */
eSomeError_ErrA = 100,
eSomeError_ErrB = 101,
eSomeError_ErrC = 102,
};
bool doSomthingA( enum eSomeState* const state){
Assert(*state == eSomeState_A);
//do stuff
if (success){
*state = eSomeState_B;
return true;
}else{
*state = err;
}
return false;
}
functions from other modules are expecting (Error Enum) type so i used to cast the (State Enum) to (Error Enum)来自其他模块的函数需要 (Error Enum) 类型,所以我曾经将 (State Enum) 转换为 (Error Enum)
after some time, i noticed that its hard to keep copying the (Error Enum) to (State Enum) and keep them identical everytime i need to modify one of them.一段时间后,我注意到每次我需要修改其中一个时,很难将(错误枚举)复制到(状态枚举)并保持它们相同。
so i moved to Version 2:所以我搬到了第 2 版:
/* used by this module */
enum eSomeState{
eSomeState_A = 1,
eSomeState_B = 2,
eSomeState_C = 3,
eSomeState_D = 4,
/* do not reach 100 */
}
union uSomeState{
enum eSomeState state;
enum eSomeErrorFromOtherModule err;
}
/* functions updated to */
bool doSomthingA( union eSomeState* const state){
Assert(state->state == eSomeState_A);
//do stuff
if (success){
state->state = eSomeState_B;
return true;
}else{
state->err = err;
}
return false;
}
version 2 [i didnt test it as i barely used union but i expect it to work] have reduced my work to go copy enum from another module to this module.. but i still have to make sure that (state enum) doesnt overlap (error enum)..版本 2 [我没有测试它,因为我几乎没有使用 union,但我希望它能够工作] 已将我的工作减少到 go 将枚举从另一个模块复制到该模块..但我仍然必须确保(状态枚举)不重叠(错误枚举)..
I like to think of enums as a set (list) of constants.我喜欢将枚举视为一组(列表)常量。 If i have to store a state, then i introduce a new type, eg bitset_t or state_t .
如果我必须存储 state,那么我会引入一种新类型,例如bitset_t或state_t 。
Example:例子:
typedef int state_t;
enum someStates {};
enum someErrorStates {};
bool doSomething(state_t state) {}
Because sometimes it is necessary to 'combine' two states together (eg by the operator '|'), which is not in the set of either enums.因为有时有必要将两个状态“组合”在一起(例如,通过运算符“|”),这不在任一枚举的集合中。
Just an opinion.只是一个意见。
Supplement:补充:
Basically, an enum is an int (unsigned?) and you can store upto INT_MAX values in one set.基本上,枚举是一个 int(无符号?),您可以在一组中存储最多 INT_MAX 值。 But if you want a 'bitset', then you are restricted to 32 (if int are 32 bits) distinct values.
但是,如果您想要一个“位集”,那么您将被限制为 32 个(如果 int 是 32 位)不同的值。 If you want to have more, then you have to implement a bitset, an array of words.
如果你想拥有更多,那么你必须实现一个位集,一个单词数组。
Example:例子:
#define word_size sizeof(unsigned int)
#define word_width (word_size * CHAR_BIT) // defined in limits.h
#define word_index(Pos) ((Pos) / word_width)
#define bit_index(Pos) ((Pos) % word_width)
unsigned int bitset[N];
bool isSet(unsigned int bs[N], unsigned int pos)
{
return ((bs[word_index(pos)] >> bit_index(pos)) & 1);
}
void setBit(unsigned int bs[N], unsigned int pos)
{
bs[word_index(pos)] |= (1 << bit_index(pos));
}
void clearBit(unsigned int bs[N], unsigned int pos)
{
bs[word_index(pos)] &= ~(1 << bit_index(pos));
}
void toggleBit(unsigned int bs[N], unsigned int pos)
{
bs[word_index(pos)] ^= (1 << bit_index(pos));
}
And then you could define your enums like that:然后你可以像这样定义你的枚举:
enum someState {
state1 = 1,
state2 = 2
//...
};
setBit(bitset, state2);
isSet(bitset, state2);
// ... and so on
Basically, the enum constants will describe the (bit) position in the bitset.基本上,枚举常量将描述位集中的(位)position。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.