用于组合信用卡网络的位掩码枚举
Bitmask enum for combining credit card network
我试图将卡提供商表示为 enum
。基本上信用卡(基于 BIN 编号)可以是 Visa 或 Mastercard,子类型可以是 Credit 或 Debit(简化示例,但我想处理所有情况(Maestro、Visa Electron、UATP...)
这似乎可行,但看到最后一个案例可以做到 CardProvider Visa | CardProvider MasterCard
我们知道这是不正确的。
我有办法防止这种情况发生吗?除此之外,当前的 enum
结构是否正确?
enum CardProvider
{
CardProviderNone = 0,
CardProviderMasterCard = 1 << 0,
CardProviderMasterCardDebit = 1 << 1,
CardProviderMasterCardCredit = 1 << 2,
CardProviderVisa = 2 << 0,
CardProviderVisaDebit = 2 << 1,
CardProviderVisaCredit = 2 << 2
};
CardProvider cardType1 = CardProviderMasterCard;
CardProvider cardType2 = CardProviderMasterCard | CardProviderMasterCardCredit;
CardProvider cardType3 = CardProviderMasterCard | CardProviderMasterCardDebit;
CardProvider cardType4 = CardProviderVisa | CardProviderVisaDebit;
// possible to prevent?
CardProvider cardType5 = CardProvider Visa | CardProvider MasterCard;
// works as expected:
assert(cardType1 & CardProviderMasterCard);
assert(cardType2 & CardProviderMasterCardCredit);
assert(cardType2 & CardProviderMasterCard);
assert(cardType3 & CardProviderMasterCard);
assert(!(cardType4 & CardProviderMasterCard));
assert(cardType4 & CardProviderVisa);
// works but shouldn't be allowed
assert(cardType5 & CardProviderVisa);
您可能需要为提供者和类型使用单独的枚举,并使用序号而不是位掩码:
enum CardProvider
{
CardProviderNone = 0,
CardProviderMasterCard = 1,
CardProviderVisa = 2
};
enum CardType
{
CardTypeNone = 0,
CardTypeDebit = 1,
CardTypeCredit = 2
};
您的卡类型重叠(2<<0 == 1<<1)
您只需要调整它们就不会重叠。有许多不同的方法,但这就是您尝试做的事情。
typedef enum CardProvider {
CardTypeNone = 0,
CardTypeCredit = 1<<0,
CardTypeDebit = 1<<1,
/* you may want to add padding here for extra card types */
CardProviderMasterCard = 1<<2,
CardProviderMasterCardDebit = CardTypeDebit | CardProviderMasterCard,
CardProviderMasterCardCredit = CardTypeCredit | CardProviderMasterCard,
CardProviderVisa = 1<<3,
CardProviderVisaDebit = CardTypeDebit | CardProviderVisa,
CardProviderVisaCredit = CardTypeCredit | CardProviderVisa,
//future providers here
};
int card_transaction(CardProvider, float amount, struct card_data);
要使编译器在误用时出错,请参阅
我试图将卡提供商表示为 enum
。基本上信用卡(基于 BIN 编号)可以是 Visa 或 Mastercard,子类型可以是 Credit 或 Debit(简化示例,但我想处理所有情况(Maestro、Visa Electron、UATP...)
这似乎可行,但看到最后一个案例可以做到 CardProvider Visa | CardProvider MasterCard
我们知道这是不正确的。
我有办法防止这种情况发生吗?除此之外,当前的 enum
结构是否正确?
enum CardProvider
{
CardProviderNone = 0,
CardProviderMasterCard = 1 << 0,
CardProviderMasterCardDebit = 1 << 1,
CardProviderMasterCardCredit = 1 << 2,
CardProviderVisa = 2 << 0,
CardProviderVisaDebit = 2 << 1,
CardProviderVisaCredit = 2 << 2
};
CardProvider cardType1 = CardProviderMasterCard;
CardProvider cardType2 = CardProviderMasterCard | CardProviderMasterCardCredit;
CardProvider cardType3 = CardProviderMasterCard | CardProviderMasterCardDebit;
CardProvider cardType4 = CardProviderVisa | CardProviderVisaDebit;
// possible to prevent?
CardProvider cardType5 = CardProvider Visa | CardProvider MasterCard;
// works as expected:
assert(cardType1 & CardProviderMasterCard);
assert(cardType2 & CardProviderMasterCardCredit);
assert(cardType2 & CardProviderMasterCard);
assert(cardType3 & CardProviderMasterCard);
assert(!(cardType4 & CardProviderMasterCard));
assert(cardType4 & CardProviderVisa);
// works but shouldn't be allowed
assert(cardType5 & CardProviderVisa);
您可能需要为提供者和类型使用单独的枚举,并使用序号而不是位掩码:
enum CardProvider
{
CardProviderNone = 0,
CardProviderMasterCard = 1,
CardProviderVisa = 2
};
enum CardType
{
CardTypeNone = 0,
CardTypeDebit = 1,
CardTypeCredit = 2
};
您的卡类型重叠(2<<0 == 1<<1)
您只需要调整它们就不会重叠。有许多不同的方法,但这就是您尝试做的事情。
typedef enum CardProvider {
CardTypeNone = 0,
CardTypeCredit = 1<<0,
CardTypeDebit = 1<<1,
/* you may want to add padding here for extra card types */
CardProviderMasterCard = 1<<2,
CardProviderMasterCardDebit = CardTypeDebit | CardProviderMasterCard,
CardProviderMasterCardCredit = CardTypeCredit | CardProviderMasterCard,
CardProviderVisa = 1<<3,
CardProviderVisaDebit = CardTypeDebit | CardProviderVisa,
CardProviderVisaCredit = CardTypeCredit | CardProviderVisa,
//future providers here
};
int card_transaction(CardProvider, float amount, struct card_data);
要使编译器在误用时出错,请参阅