现在的位置: 首页 > 综合 > 正文

C++ 属性(二)

2013年09月24日 ⁄ 综合 ⁄ 共 3347字 ⁄ 字号 评论关闭
#include <iostream>

template<typename T, typename U>
class PropertyType
{
public:
	//指向类成员函数的指针类型
	typedef void (U::*SetProc)(const T&);
	typedef const T& (U::*GetProc)();        
};

//属性基类
template<typename U>
class PropertyBase
{
protected:
	//拥有这个属性的类的指针
	U* obj_;
public:
	void SetObj(U *obj) 
	{
		this->obj_ = obj;
	}
protected:
};

//只写属性过程定义
template<typename T, typename U, typename PropertyType<T, U>::SetProc Set>
class WriteProperty: public PropertyBase<U>
{
protected:
	//定义属性set的函数指针
	typename PropertyType<T, U>::SetProc SetValue;
public:
	WriteProperty()
	{
		this->SetValue = Set;
	}

	void operator= (const T &value) const
	{
		(obj_->*SetValue)(value);
	}
};

//只读属性过程定义
template<typename T, typename U, typename PropertyType<T, U>::GetProc Get>
class ReadProperty: public PropertyBase<U>
{
private:
	//避免让只读属性可写
	void operator= (const T&) {}
	void operator= (const ReadProperty<T, U, Get>&) {}
protected:
	//定义属性get的函数指针
	typename PropertyType<T, U>::GetProc GetValue;
public:
	ReadProperty()
	{
		this->GetValue = Get;
	}

	operator T() const
	{
		return (obj_->*GetValue)();
	}
};

template<typename T, typename U, typename PropertyType<T, U>::GetProc Get>
std::ostream& operator << (std::ostream &out, const ReadProperty<T, U, Get>& rv) 
{
	out << rv.operator T();
	return out;
}

//读写属性过程定义
template<typename T, typename U, typename PropertyType<T, U>::SetProc Set, typename PropertyType<T, U>::GetProc Get>
class ReadWriteProperty: public PropertyBase<U>
{
private:
	typename PropertyType<T, U>::SetProc SetValue;
	typename PropertyType<T, U>::GetProc GetValue;

	//禁用赋值和拷贝构造
	const ReadWriteProperty<T, U, Set, Get>& operator= (const ReadWriteProperty<T, U, Set, Get>&) {}
	ReadWriteProperty(ReadWriteProperty<T, U, Set, Get>&) {}
public:
	ReadWriteProperty()
	{
		SetValue = Set;
		GetValue = Get;
	}

	const ReadWriteProperty<T, U, Set, Get>& operator= (const T &value) const
	{
		(obj_->*SetValue)(value);
		return *this;
	}

	operator T() const
	{
		return (obj_->*GetValue)();
	}
};

template<typename T, typename U, typename PropertyType<T, U>::SetProc Set, typename PropertyType<T, U>::GetProc Get>
std::ostream& operator << (std::ostream &out, const ReadWriteProperty<T, U, Set, Get>& rv) 
{
	out << rv.operator T();
	return out;
}

//简化函性定义的宏
//定义读写属性
#define PROPERTY_DECLARE_RW(property_name, type, class_type, set, get) \
	ReadWriteProperty<type, class_type, &class_type::set, &class_type::get> property_name;
//定义只读属性
#define PROPERTY_DECLARE_R(property_name, type, class_type, get) \
	ReadProperty<type, class_type, &class_type::get> property_name;
//定义只写属性
#define PROPERTY_DECLARE_W(property_name, type, class_type, set) \
	WriteProperty<type, class_type, &class_type::set> property_name;

#define INIT_PROPERTY(property_name) property_name.SetObj(this);



//-------------------------华丽的分隔线-----------------------------

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

class Test
{
private:
	int value_;
	string name_;
public:
	Test(int value) 
	{
		INIT_PROPERTY(Value);
		INIT_PROPERTY(Name);
		INIT_PROPERTY(WValue);

		this->value_ = value;
		name_ = "TestClass";
	}

	void SetValue(const int& value) 
	{
		cout << "Set Value: " << value << std::endl;
		this->value_ = value;
	}

	const int& GetValue() 
	{
		cout << "Get Value: " << value_ << std::endl;
		return value_;
	}

	const string& GetName() 
	{
		return name_;
	}

	void ShowValue()
	{
		cout << "Value: " << value_ << std::endl;
	}	

	PROPERTY_DECLARE_RW(Value, int, Test,  SetValue, GetValue);
	PROPERTY_DECLARE_R(Name, string, Test, GetName);
	PROPERTY_DECLARE_W(WValue, int , Test, SetValue);
};




int main() 
{
	Test t(100);
	t.ShowValue();
	t.WValue = 999;            //只写属性可以写入
	//int i = t.WValue;        //只读属性无法读取
	t.Value = 200;            //读写属性可以写入
	int i = t.Value;        //读写属性可以读取
	cout << "i: " << i << std::endl;
	cout << t.Name << std::endl; //只读属性可以读取
	//t.Name = "hello";        //只写属性无法写入

	cin.get();
	return 0;
}

先暂存  留待之后有空合并处理下

抱歉!评论已关闭.