万能类型的设计

/**
万能类型
AnyType.h
**/
#include <string>

#define AnyType_FOREACH_TYPES(Operator) \
	AnyType_FOREACH_TYPES3(Operator, int, double, bool)


#define AnyType_FOREACH_TYPES3(Operator, Type1, Type2, Type3) \
	AnyType_FOREACH_TYPES2(Operator, Type2, Type3)\
	AnyType_FOREACH_TYPES1(Operator, Type1)
#define AnyType_FOREACH_TYPES2(Operator, Type1, Type2) \
	AnyType_FOREACH_TYPES1(Operator, Type2)\
	AnyType_FOREACH_TYPES1(Operator, Type1)
#define AnyType_FOREACH_TYPES1(Operator, Type) \
	Operator(Type)
static void AnyType_throw(std::string exception)
{
	throw exception;
}
#define AnyType_THROW(exception) AnyType_throw(exception);

class DllClass CAnyType
{
public:
	//定义枚举
#define AnyType_DEFINE_ENUM_TYPE(Type) Type ## Enum
#define AnyType_DEFINE_TYPE_ENUMS(Type) AnyType_DEFINE_ENUM_TYPE(Type),
	enum AnyTypeEnum
	{
		AnyType_FOREACH_TYPES(AnyType_DEFINE_TYPE_ENUMS)
		AnyTypeEnumBuffer
	};
public:
	CAnyType(void);
	CAnyType(const CAnyType& other);
	~CAnyType(void);
	//定义构造
#define AnyType_DEFINE_CONSTRUCTOR(Type) \
	CAnyType(Type value);
#define AnyType_CREATE_CONSTRUCTOR(Type) \
	CAnyType::CAnyType(Type value)\
	{\
		m_type = AnyType_DEFINE_ENUM_TYPE(Type);\
		m_data = new Type(value);\
	}
	AnyType_FOREACH_TYPES(AnyType_DEFINE_CONSTRUCTOR);

	//定义类型运算符
#define AnyType_DEFINE_TYPE_OPERATOR(Type) \
	operator Type()const;
#define AnyType_CREATE_TYPE_OPERATOR(Type) \
	CAnyType::operator Type()const\
	{\
		if (m_type == AnyType_DEFINE_ENUM_TYPE(Type))\
		{\
			return *((Type*)m_data);\
		}\
		AnyType_THROW("类型不匹配");\
		return Type();\
	}
	AnyType_FOREACH_TYPES(AnyType_DEFINE_TYPE_OPERATOR);

	//定义赋值运算符
	CAnyType& operator=(const CAnyType& other);
#define AnyType_DEFINE_ASSIGNMENT(Type) \
	CAnyType& operator =(const Type& other);
#define AnyType_CREATE_ASSIGNMENT(Type) \
	CAnyType& CAnyType::operator =(const Type& other)\
	{\
		*this = CAnyType(other);\
		return *this;\
	};
	AnyType_FOREACH_TYPES(AnyType_DEFINE_ASSIGNMENT);

	//类型说明
	std::string GetTypeString()const;
	CAnyType::AnyTypeEnum GetTypeEnum()const;
private:
	void * m_data;
	AnyTypeEnum m_type;
	void ClearData();
};
/**
万能类型
AnyType.cpp
**/
#include "AnyType.h"

CAnyType::CAnyType(void)
{
	m_type = /*CAnyType::AnyTypeEnum::*/AnyTypeEnumBuffer;
	m_data = 0;
}

CAnyType::CAnyType( const CAnyType& other )
{
	*this = other;
}

CAnyType& CAnyType::operator=( const CAnyType& other )
{
	if (m_type != other.m_type)
	{
		ClearData();
	}
#define AnyType_OPERATOR_DENGYU(Type) \
	case AnyType_DEFINE_ENUM_TYPE(Type):\
		if (m_type == other.m_type)\
		{\
			*((Type*)m_data) = *((Type*)(other.m_data));\
		}\
		else\
		{\
			m_data = new Type(*((Type*)(other.m_data)));\
		}\
		break;
	switch(other.m_type)
	{
		AnyType_FOREACH_TYPES(AnyType_OPERATOR_DENGYU);
	default:
		break;
	}
	m_type = other.m_type;
	return *this;
}


CAnyType::~CAnyType(void)
{
	ClearData();
}

std::string CAnyType::GetTypeString()const
{
	std::string ret;
#define AnyType_GETTYPESTRING(Type) \
	case AnyType_DEFINE_ENUM_TYPE(Type):\
		ret = #Type;\
		break;
	switch(m_type)
	{
		AnyType_FOREACH_TYPES(AnyType_GETTYPESTRING);
	default:
		break;
	}
	return ret;
}
CAnyType::AnyTypeEnum CAnyType::GetTypeEnum()const
{
	return m_type;
}

void CAnyType::ClearData()
{
	if (m_data != 0)
	{
#define AnyType_DELETE_DATA(Type) \
	if (m_type == AnyType_DEFINE_ENUM_TYPE(Type))\
		{\
			delete (Type*)m_data;\
			break;\
		}
		while(true)
		{
			AnyType_FOREACH_TYPES(AnyType_DELETE_DATA);
			break;
		}
	}
	m_type = AnyTypeEnumBuffer;
}



AnyType_FOREACH_TYPES(AnyType_CREATE_CONSTRUCTOR);
AnyType_FOREACH_TYPES(AnyType_CREATE_TYPE_OPERATOR);
AnyType_FOREACH_TYPES(AnyType_CREATE_ASSIGNMENT);

大量使用C语言的宏,省去了非常多的麻烦,在头文件文件的最前面可以注册各种类型,

你可能感兴趣的:(宏,万能类型)