不互斥的枚举

Enum枚举类中实现:一个枚举组是否包含其中的几个枚举对象

假设我有一个枚举类,这个枚举类用来修饰一个类的各种属性,枚举并不互斥,可以协同修饰一个类。或者说这个枚举用于表明这个类的各个状态。可以将这个状态标记为OnOff

假设我有一个枚举Buff:

public:
	enum Buff
	{
		AttackUp = 0x01,//攻击力上升buff
		HurtReduction = 0x02,//伤害减免buff
		GoldUp = 0x04,//金币增加
		A1 = 0x04, 1<<2
		A2 = 0x08,1<<3
		A3 = 0x10,1<<4
		A4 = 0x20,1<<5
		A5 = 0x40,1<<6
		A6 = 0x80,1<<7
	}

以上的枚举代表了三种不同的增益,用16进制int类型表示。

定义了一个枚举组Buffs

然后绑定Buffs和Buff,预定义为: DECLARE_FLAGS(Buffs, Buff);

枚举对象本质是int类型,因此这个Buffs类需要实现几个int的逻辑运算符号重载。

#ifndef CORE_FLAGS_H
#define CORE_FLAGS_H
#include "core/global.hpp"
#include "qtypeinfo.h"
#include "dtypetraits.h

class DFlag
{
	int i;
public:
	inline DFlag(int ai) : i(ai) {}
	inline DFlag(uint ai) : i(int(ai)) {}
	inline DFlag(short ai) : i(int(ai)) {}
	inline DFlag(ushort ai) : i(int(uint(ai))) {}
	inline operator int() const
	{
		return i;
	}
	inline operator uint() const
	{
		return uint(i);
	}
};
DAN_DECLARE_TYPEINFO(DFlag, D_PRIMITIVE_TYPE);

class IncompatibleFlag
{
	int i;
public:
	inline explicit IncompatibleFlag(int i);
	inline operator int() const
	{
		return i;
	}
};


inline IncompatibleFlag::IncompatibleFlag(int ai) : i(ai) {}

template<typename EnumType>
class LOCALAPI DFlags
{
public:

	typedef EnumType enum_type;

	typedef int Int;
#ifndef SWIG
	typedef int(*Zero);
	inline DFlags(Zero = 0) : i(0) {}
#endif
	inline DFlags(EnumType f) : i(Int(f)) {}

	inline DFlags(DFlag f) : i(f) {}

	inline DFlags &operator&=(int mask)
	{
		i &= mask;
		return *this;
	}
	inline DFlags &operator&=(uint mask)
	{
		i &= mask;
		return *this;
	}
	inline DFlags &operator&=(EnumType mask)
	{
		i &= Int(mask);
		return *this;
	}
	inline DFlags &operator|=(DFlags f)
	{
		i |= f.i;
		return *this;
	}
	inline DFlags &operator|=(EnumType f)
	{
		i |= Int(f);
		return *this;
	}
	inline DFlags &operator^=(DFlags f)
	{
		i ^= f.i;
		return *this;
	}
	inline DFlags &operator^=(EnumType f)
	{
		i ^= Int(f);
		return *this;
	}

	inline operator Int() const
	{
		return i;
	}
	inline int toInt() const
	{
		return i;
	}
	inline DFlags operator|(DFlags f) const
	{
		return DFlags(DFlag(i | f.i));
	}
	inline DFlags operator|(EnumType f) const
	{
		return DFlags(DFlag(i | Int(f)));
	}
	inline DFlags operator^(DFlags f) const
	{
		return DFlags(DFlag(i ^ f.i));
	}
	inline DFlags operator^(EnumType f) const
	{
		return DFlags(DFlag(i ^ Int(f)));
	}
	inline DFlags operator&(int mask) const
	{
		return DFlags(DFlag(i & mask));
	}
	inline DFlags operator&(uint mask) const
	{
		return DFlags(DFlag(i & mask));
	}
	inline DFlags operator&(EnumType f) const
	{
		return DFlags(DFlag(i & Int(f)));
	}
	inline DFlags operator~() const
	{
		return DFlags(DFlag(~i));
	}

	inline bool operator!() const
	{
		return !i;
	}

	inline bool testFlag(EnumType f) const
	{
		return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f));
	}
private:
	Int i;
};


#define DECLARE_FLAGS(Flags, Enum)\
typedef DFlags<Enum> Flags;

#endif

使用方法如下

private:
	Buffs _my_buff;
public:
	void setBuffStatus(Buff buff, bool on /*= true*/)
	{
		if (on)
		{
			_my_buff = _my_buff | buff;
		}
		else {
			_my_buff = _my_buff & ~buff;
		}
	}
	
	bool checkBuffStatus(Buff buff) const
	{
		return  _my_buff & buff;
	}

通过或与非运算,能检测到当前枚举组中是否存在指定枚举对象。
或者换一种说法,能检测到指定枚举对象是否处于激活状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

话与山鬼听

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值