C++源码分享(四):对象属性泛型基类

   对象属性泛型基类可用于保存对象的属性,类似于DOM节点的属性保存,提供属性遍历,使用方法如下:

 

class CView : public Base::CObjectAttributesBase

{

...

};

 

 

//**********************************************************************
// Copyright (c) 2010
// 迪斯特软件开发小组.
// 文件: ObjectAttributeBase.hpp
// 内容:
// 历史:
//    序号        修改时间        修改人        修改内容
//    1            2010-8-2        hlq            首次生成
//*********************************************************************

//声明本头文件宏   
#ifndef _OBJECTATTRIBUTEBASE_HPP
#define _OBJECTATTRIBUTEBASE_HPP

//包含头文件
#include <string>
#include <list>

//Base名字空间开始
namespace Base
{

//**********************************************************************
// 类名: CObjectAttributesBase
// 目的: 对象属性集基类
//*********************************************************************
class CObjectAttributesBase
{
类型声明
public:
    typedef CObjectAttributesBase                                my_type;
    typedef std::string                                            string_type;
    typedef std::list<std::pair<string_type,string_type> >        attribute_container_type;
    typedef attribute_container_type::value_type                attribute_type;
    typedef attribute_container_type::const_iterator            attribute_const_iterator;
    typedef attribute_container_type::iterator                    attribute_iterator;
    typedef unsigned int                                        size_type;
    typedef size_type                                            index_type;

基本查询
public:
    //是否有指定属性
    bool HasAttribute(const string_type& szName) const {return attribute_find(szName) != attribute_end();}
    //得到属性数量
    size_type GetAttributesCount(void) const {return static_cast<size_type>(m_aAttributes.size());}
    //得到属性名
    const string_type& GetAttributeName(index_type nIndex) const;
    //得到属性值
    const string_type& GetAttribute(const string_type& szName) const;
    //得到带缺省值的属性值
    const string_type& GetAttribute(const string_type& szName,const string_type& szDefaultValue) const;
    //得到属性值(模板)
    template<typename T> void GetAttribute(const string_type& szName,T& aValue) const {Convert(GetAttribute(szName),aValue);}
    //得到带缺省值的属性值(模板)
    template<typename T,typename T1> void GetAttribute(const string_type& szName,T& aValue,const T1& aDefaultValue) const;
    //属性事件是否已经冻结
    bool IsAttributeEventFreezeed(void) const {return m_bFreezeAttributeEvent;}

命令
public:
    //插入属性
    void InsertAttribute(index_type nIndex,const string_type& szName,const string_type& szValue);
    //插入属性(模板)
    template<typename T> void InsertAttribute(index_type nIndex,const string_type& szName,const T& aValue) {InsertAttribute(nIndex,szName,Convert<string_type>(aValue));}
    //添加属性
    void AppendAttribute(const string_type& szName,const string_type& szValue) {InsertAttribute(m_aAttributes.size(),szName,szValue);}
    //添加属性(模板)
    template<typename T> void AppendAttribute(const string_type& szName,const T& szValue) {AppendAttribute(szName,Convert<string_type>(aValue));}
    //修改属性
    void ModifyAttribute(const string_type& szName,const string_type& szValue);
    //修改属性(模板)
    template<typename T> void ModifyAttribute(const string_type& szName,const T& aValue){ModifyAttribute(szName,Convert<string_type>(aValue));}
    //设置属性
    void SetAttribute(const string_type& szName,const string_type& szValue);
    //设置属性(模板)
    template<typename T> void SetAttribute(const string_type& szName,const T& aValue) {SetAttribute(szName,Convert<string_type>(aValue));}
    //删除属性
    void RemoveAttribute(const string_type& szName);
    //清除属性
    void ClearAttributes(void);
    //冻结属性事件通知
    void FreezeAttributeEvent(bool bFreeze) {m_bFreezeAttributeEvent = bFreeze;}

事件重载函数
public:
    //即将添加属性事件
    virtual void OnAttributeWillAdd(const string_type& szName,const string_type& szValue,bool& bPermit) {}
    //属性已经添加事件
    virtual void OnAttributeAdded(const string_type& szName,const string_type& szValue){}
    //即将修改属性事件
    virtual void OnAttributeWillModify(const string_type& szName,const string_type& szValue,bool& bPermit) {}
    //属性已经修改事件
    virtual void OnAttributeModified(const string_type& szName,const string_type& szValue) {}
    //即将删除属性事件
    virtual void OnAttributeWillRemove(const string_type& szName,bool& bPermit) {}
    //属性已经删除事件
    virtual void OnAttributeRemoved(const string_type& szName) {}
    //即将清除所有属性事件
    virtual void OnAttributesWillClear(bool& bPermit) {}
    //属性已经清除事件
    virtual void OnAttributesCleared(void) {}

构造、析构函数
public:
    //构造函数
    CObjectAttributesBase(void) : m_bFreezeAttributeEvent(false) {}

迭代器实现
public:
    attribute_iterator attribute_begin(void) { return m_aAttributes.begin();}
    attribute_const_iterator attribute_begin(void) const { return m_aAttributes.begin();}
    attribute_iterator attribute_end(void) { return m_aAttributes.end();}
    attribute_const_iterator attribute_end(void) const { return m_aAttributes.end();}
    attribute_iterator attribute_find(const string_type& szName);
    attribute_const_iterator attribute_find(const string_type& szName) const;

执行函数
public:
    //转换数据类型
    template<typename FromType,typename ToType>    static void Convert(const FromType& aFromValue,ToType& aToValue);
    //转换数据类型
    template<typename FromType,typename ToType>    static const ToType Convert(const FromType& aFromValue) {ToType aValue;Convert(aFromValue,aValue);return aValue;}

数据成员
private:
    attribute_container_type                m_aAttributes;                    //属性集合
    bool                                    m_bFreezeAttributeEvent;        //冻结属性事件
};

//**********************************************************************
// 函数: attribute_find
// 功能: 查找属性
//*********************************************************************
inline CObjectAttributesBase::attribute_iterator CObjectAttributesBase::attribute_find(const string_type& szName)
{
    attribute_iterator pEnd = attribute_end();
    for(attribute_iterator p = attribute_begin();p != pEnd;++p)
    {
        if(p->first == szName)
            return p;
    }
    return pEnd;
}

//**********************************************************************
// 函数: attribute_find
// 功能: 查找属性
//*********************************************************************
inline CObjectAttributesBase::attribute_const_iterator CObjectAttributesBase::attribute_find(const string_type& szName) const
{
    attribute_const_iterator pEnd = attribute_end();
    for(attribute_const_iterator p = attribute_begin();p != pEnd;++p)
    {
        if(p->first == szName)
            return p;
    }
    return pEnd;
}

//**********************************************************************
// 函数: Convert
// 功能: 转换数据类型
//*********************************************************************
template<typename FromType,typename ToType>   
inline void CObjectAttributesBase::Convert(const FromType& aFromValue,ToType& aToValue)
{
    string_typestream ss;
    ss << aFromValue;
    ss >> aToValue;
    return aToValue;
}

//**********************************************************************
// 函数: GetAttributeName
// 功能: 得到属性名
//*********************************************************************
inline const CObjectAttributesBase::string_type& CObjectAttributesBase::GetAttributeName(index_type nIndex) const
{
    attribute_const_iterator p;
    if(nIndex >= GetAttributesCount())
    {
        static string_type s_szEmptyName;
        return s_szEmptyName;
    }
    else
    {
        p = attribute_begin();
        std::advance(p,nIndex);
        return p->first;
    }
}

//**********************************************************************
// 函数: GetAttribute
// 功能: 得到属性值
//*********************************************************************
inline const CObjectAttributesBase::string_type& CObjectAttributesBase::GetAttribute(const string_type& szName) const
{
    static string_type s_szEmptyValue;
    attribute_const_iterator p = attribute_find(szName);
    return (p != attribute_end()) ? p->second : s_szEmptyValue;
}

//**********************************************************************
// 函数: GetAttribute
// 功能: 得到带缺省值的属性值
//*********************************************************************
inline const CObjectAttributesBase::string_type& CObjectAttributesBase::GetAttribute(const string_type& szName,const string_type& szDefaultValue) const
{
    attribute_const_iterator p = attribute_find(szName);
    if(p != attribute_end())
        return p->second;
    else
    {
        static string_type s_szDefaultValue;
        s_szDefaultValue = szDefaultValue;
        return s_szDefaultValue;
    }
}

//**********************************************************************
// 函数: GetAttribute
// 功能: 得到带缺省值的属性值(模板)
//*********************************************************************
template<typename T,typename T1>
inline void CObjectAttributesBase::GetAttribute(const string_type& szName,T& aValue, const T1& aDefaultValue) const
{
    string_typestream ss;
    attribute_const_iterator p = attribute_find(szName);
    if(p != attribute_end())
        ss << p->second;
    else
        ss << aDefaultValue;
    ss >> aValue;
}

//**********************************************************************
// 函数: InsertAttribute
// 功能: 插入属性
//*********************************************************************
inline void CObjectAttributesBase::InsertAttribute(index_type nIndex,const string_type& szName,const string_type& szValue)
{
    //前条件
    assert(!HasAttribute(szName));

    //焕发事件
    if(!IsAttributeEventFreezeed())
    {
        bool bPermit = true;
        OnAttributeWillAdd(szName,szValue,bPermit);
        if(!bPermit)
            return;
    }

    //添加属性
    if(nIndex > m_aAttributes.size())
        nIndex = m_aAttributes.size();
    attribute_iterator p = attribute_begin();
    std::advance(p,nIndex);
    m_aAttributes.insert(p,attribute_type(szName,szValue));
    assert(HasAttribute(szName));

    //焕发事件
    if(!IsAttributeEventFreezeed())
        OnAttributeAdded(szName,szValue);
}

//**********************************************************************
// 函数: ModifyAttribute
// 功能: 修改属性
//*********************************************************************
inline void CObjectAttributesBase::ModifyAttribute(const string_type& szName,const string_type& szValue)
{
    assert(HasAttribute(szName));
    attribute_iterator p = attribute_find(szName);
    if(!IsAttributeEventFreezeed())
    {
        //焕发事件
        bool bPermit = true;
        OnAttributeWillModify(szName,szValue,bPermit);
        if(!bPermit)
            return;
    }

    //修改属性
    p->second = szValue;

    //焕发事件
    if(!IsAttributeEventFreezeed())
        OnAttributeModified(szName,szValue);
}

//**********************************************************************
// 函数: SetAttribute
// 功能: 设置属性
//*********************************************************************
inline void CObjectAttributesBase::SetAttribute(const string_type& szName,const string_type& szValue)
{
    if(HasAttribute(szName))
        ModifyAttribute(szName,szValue);
    else
        AppendAttribute(szName,szValue);
}

//**********************************************************************
// 函数: RemoveAttribute
// 功能: 删除属性
//*********************************************************************
inline void CObjectAttributesBase::RemoveAttribute(const string_type& szName)
{
    attribute_iterator p = attribute_find(szName);
    if(p != attribute_end())
    {
        if(!IsAttributeEventFreezeed())
        {
            //焕发事件
            bool bPermit = true;
            OnAttributeWillRemove(szName,bPermit);
            if(!bPermit)
                return;
        }

        //移除属性
        m_aAttributes.erase(p);   

        //焕发事件
        if(!IsAttributeEventFreezeed())
            OnAttributeRemoved(szName);
    }
}

//**********************************************************************
// 函数: ClearAttributes
// 功能: 清除属性
//*********************************************************************
inline void CObjectAttributesBase::ClearAttributes(void)
{
    //焕发事件
    if(!IsAttributeEventFreezeed())
    {
        bool bPermit = true;
        OnAttributesWillClear(bPermit);
        if(!bPermit)
            return;
    }

    //清除属性
    m_aAttributes.clear();

    //焕发事件
    if(!IsAttributeEventFreezeed())
        OnAttributesCleared();
}

} // Base名字空间结束
#endif //假如未定义_OBJECTATTRIBUTEBASE_HPP宏

 

 

胡乐秋

2010/8/3

http://blog.csdn.net/hlqyq

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值