为数据结构添加setProperty和getProperty(二)

2014-11-24 10:39:57 · 作者: · 浏览: 1
t val) {return "float";}
std::string getvalType(bool val) {return "bool";}
std::string getvalType(std::string val) {return "string";}


public:

std::string _name; // 属性的名称
std::string _defaultVal; // 默认值
std::string _describe; // 描述
std::vector _possibleva l; // 可用的值
};

#endif

myProperty.cpp
[cpp]
#include "myProperty.h"

myPropertySet.h
[cpp]
// 王智泉
// 属性集
#ifndef __myPropertySet__H__
#define __myPropertySet__H__
#include
#include

// 关闭自动排序(还未用)
template
struct DisableCompare
{
bool operator()(const T& lhs, const T& rhs) const
{
return true;
}

bool operator<(const T& rhs) const
{
return true;
}
};

// 属性集
// ======================================================================
class myProperty;
class myPropertySet
{
public:

typedef std::map PropertyList;

myPropertySet(void);

virtual ~myPropertySet(void);

/* 注册属性
@param pty: 属性
**/
void registerProperty_impl(myProperty* pty);

/* 设置属性
@param name: 属性名
@param val: 属性值
**/
void setProperty(const std::string& name, const std::string& val);

/* 获得属性值信息
@param name: 属性名
@return: 返回属性名对应的值
**/
std::string getProperty(const std::string& name) const;

/* 获得属性
@param name: 属性名
@return: 返回属性名对应的属性指针
*/
myProperty* getPropertyPtr(const std::string& name);

/* 获得指定属性的数据类型
@param name: 属性名
@return: 返回指定属性的数据类型
**/
std::string getPropertyValueType(const std::string& name) const;

public:

PropertyList _ptysList;
};


#endif

myPropertySet.cpp

[cpp]
#include "myPropertySet.h"
#include "myProperty.h"
#include

// ================================================================
myPropertySet::myPropertySet(void)
{
}

// ================================================================
myPropertySet::~myPropertySet(void)
{
}

// ================================================================
void myPropertySet::registerProperty_impl(myProperty * pty)
{
_ptysList[pty->_name] = pty;
}

// ================================================================
void myPropertySet::setProperty(const std::string& name, const std::string& val)
{
myPropertySet::PropertyList::iterator pos = _ptysList.find(name);
if (pos != _ptysList.end())
{
pos->second->setter(this, val);
}
}

// ================================================================
std::string myPropertySet::getProperty(const std::string& name) const
{
myPropertySet::PropertyList::const_iterator pos = _ptysList.find(name);
assert(pos != _ptysList.end());
return pos->second->getter(this);
}

// ================================================================
myProperty* myPropertySet::getPropertyPtr(const std::string& name)
{
myPropertySet::PropertyList::const_iterator pos = _ptysList.find(name);
if (pos != _ptysList.end())
return pos->second;
return NULL;
}

// ================================================================
std::string myPropertySet::getPropertyValueType(const std::string& name) const
{
myPropertySet::PropertyList::const_iterator pos = _ptysList.find(name);
as