[C++] 根据运算符获取对应枚举

// app::DepthTest::Func::Type
enum class Type : GLenum 
{
	Always = GL_ALWAYS,
	Never = GL_NEVER,
	Less = GL_LESS,
	Equal = GL_EQUAL,
	LEqual = GL_LEQUAL,
	Greater = GL_GREATER,
	NotEqual = GL_NOTEQUAL,
	GEqual = GL_GEQUAL,

	Default = Less,
};
#define APP_DEPTH_FUNC_OP(op)\
(([] {\
	struct { void operator##op (int) { } };/* X APP_DEPTH_FUNC_OP(== 1 && 2 ==) X */\
	static_assert(std::is_same_v<bool, decltype(0 op 0)>, "Comparison operator only.");\
	switch (((1 op 2) << 2) | ((3 op 3) << 1) | (5 op 4))\
	{\
	case 0b001: return app::DepthTest::Func::Type::Greater;\
	case 0b010: return app::DepthTest::Func::Type::Equal;\
	case 0b011: return app::DepthTest::Func::Type::GEqual;\
	case 0b100: return app::DepthTest::Func::Type::Less;\
	case 0b101: return app::DepthTest::Func::Type::NotEqual;\
	case 0b110: return app::DepthTest::Func::Type::LEqual;\
	}\
})())

用例:

APP_DEPTH_FUNC_OP(==) == Type::Equal;// (bool)true

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据