C ++ source code sharing (4): Object attribute generic base class

Source: Internet
Author: User

The object attribute generic base class can be used to save the attributes of an object. It is similar to the attributes of a DOM node and provides property traversal. The usage is as follows:

 

Class cview: public base: cobjectattributesbase

{

...

};

 

 

//************************************** ********************************
// Copy right (c) 2010
// Dester software development team.
// File: objectattributebase. HPP
// Content:
// History:
// Modified by the sequence number modification time
// 1 2010-8-2 hlq First Generation
//************************************** *******************************

// Declare the macro of this header file
# Ifndef _ objectattributebase_hpp
# DEFINE _ objectattributebase_hpp

// Contains the header file
# Include <string>
# Include <list>

// Base namespace starts
Namespace Base
{

//************************************** ********************************
// Class Name: cobjectattributesbase
// Purpose: Object Property set base class
//************************************** *******************************
Class cobjectattributesbase
{
//// Type declaration
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;

//// Basic query
Public:
// Whether the specified attribute exists
Bool hasattribute (const string_type & szname) const {return attribute_find (szname )! = Attribute_end ();}
// Obtain the attribute quantity
Size_type getattributescount (void) const {return static_cast <size_type> (m_aattributes.size ());}
// Obtain the attribute name.
Const string_type & getattributename (index_type nindex) const;
// Obtain the property value
Const string_type & getattribute (const string_type & szname) const;
// Obtain the property value with the default value
Const string_type & getattribute (const string_type & szname, const string_type & szdefaultvalue) const;
// Get the property value (Template)
Template <typename T> void getattribute (const string_type & szname, T & avalue) const {convert (getattribute (szname), avalue );}
// Get the property value with the default value (Template)
Template <typename T, typename T1> void getattribute (const string_type & szname, T & avalue, const T1 & adefaultvalue) const;
// Whether the property event has been frozen
Bool isattributeeventfreezeed (void) const {return m_bfreezeattributeevent ;}

//// Command
Public:
// Insert attributes
Void insertattribute (index_type nindex, const string_type & szname, const string_type & szvalue );
// Insert attributes (Template)
Template <typename T> void insertattribute (index_type nindex, const string_type & szname, const T & avalue) {insertattribute (nindex, szname, convert <string_type> (avalue ));}
// Add attributes
Void appendattribute (const string_type & szname, const string_type & szvalue) {insertattribute (m_aattributes.size (), szname, szvalue );}
// Add attributes (Template)
Template <typename T> void appendattribute (const string_type & szname, const T & szvalue) {appendattribute (szname, convert <string_type> (avalue ));}
// Modify attributes
Void modifyattribute (const string_type & szname, const string_type & szvalue );
// Modify attributes (Template)
Template <typename T> void modifyattribute (const string_type & szname, const T & avalue) {modifyattribute (szname, convert <string_type> (avalue ));}
// Set attributes
Void setattribute (const string_type & szname, const string_type & szvalue );
// Set attributes (Template)
Template <typename T> void setattribute (const string_type & szname, const T & avalue) {setattribute (szname, convert <string_type> (avalue ));}
// Delete attributes
Void removeattribute (const string_type & szname );
// Clear attributes
Void clearattributes (void );
// Notification of property freezing event
Void freezeattributeevent (bool bfreeze) {m_bfreezeattributeevent = bfreeze ;}

//// Event overload Function
Public:
// Attribute event to be added
Virtual void onattributewilladd (const string_type & szname, const string_type & szvalue, bool & bpermit ){}
// An event has been added to the property.
Virtual void onattributeadded (const string_type & szname, const string_type & szvalue ){}
// Attribute event to be modified
Virtual void onattributewillmodify (const string_type & szname, const string_type & szvalue, bool & bpermit ){}
// The attribute has been modified.
Virtual void onattributemodified (const string_type & szname, const string_type & szvalue ){}
// Attribute event to be deleted
Virtual void onattributewillremove (const string_type & szname, bool & bpermit ){}
// The property has been deleted.
Virtual void onattributeremoved (const string_type & szname ){}
// All attribute events to be cleared
Virtual void onattributeswillclear (bool & bpermit ){}
// The property has been cleared.
Virtual void onattributescleared (void ){}

//// Constructor and destructor
Public:
// Constructor
Cobjectattributesbase (void): m_bfreezeattributeevent (false ){}

//// Iterator implementation
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;

//// Execute the Function
Public:
// Convert the Data Type
Template <typename fromtype, typename totype> static void convert (const fromtype & afromvalue, totype & atovalue );
// Convert the Data Type
Template <typename fromtype, typename totype> static const totype convert (const fromtype & afromvalue) {totype avalue; convert (afromvalue, avalue); Return avalue ;}

/// Data member
PRIVATE:
Attribute_container_type m_aattributes; // Attribute Set
Bool m_bfreezeattributeevent; // freeze attribute events
};

//************************************** ********************************
// Function: attribute_find
// Function: Search for Attributes
//************************************** *******************************
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;
}

//************************************** ********************************
// Function: attribute_find
// Function: Search for Attributes
//************************************** *******************************
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;
}

//************************************** ********************************
// Function: Convert
// Function: convert data types
//************************************** *******************************
Template <typename fromtype, typename totype>
Inline void cobjectattributesbase: Convert (const fromtype & afromvalue, totype & atovalue)
{
String_typestream SS;
SS <afromvalue;
SS> atovalue;
Return atovalue;
}

//************************************** ********************************
// Function: getattributename
// Function: Get the property name
//************************************** *******************************
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;
}
}

//************************************** ********************************
// Function: getattribute
// Function: Get the property value
//************************************** *******************************
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;
}

//************************************** ********************************
// Function: getattribute
// Function: Get the property value with the default value
//************************************** *******************************
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;
}
}

//************************************** ********************************
// Function: getattribute
// Function: Get the property value with the default value (Template)
//************************************** *******************************
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;
}

//************************************** ********************************
// Function: insertattribute
// Function: insert attributes
//************************************** *******************************
Inline void cobjectattributesbase: insertattribute (index_type nindex, const string_type & szname, const string_type & szvalue)
{
// Prerequisites
Assert (! Hasattribute (szname ));

// Glow event
If (! Isattributeeventfreezeed ())
{
Bool bpermit = true;
Onattributewilladd (szname, szvalue, bpermit );
If (! Bpermit)
Return;
}

// Add attributes
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 ));

// Glow event
If (! Isattributeeventfreezeed ())
Onattributeadded (szname, szvalue );
}

//************************************** ********************************
// Function: modifyattribute
// Function: modify attributes
//************************************** *******************************
Inline void cobjectattributesbase: modifyattribute (const string_type & szname, const string_type & szvalue)
{
Assert (hasattribute (szname ));
Attribute_iterator P = attribute_find (szname );
If (! Isattributeeventfreezeed ())
{
// Glow event
Bool bpermit = true;
Onattributewillmodify (szname, szvalue, bpermit );
If (! Bpermit)
Return;
}

// Modify attributes
P-> second = szvalue;

// Glow event
If (! Isattributeeventfreezeed ())
Onattributemodified (szname, szvalue );
}

//************************************** ********************************
// Function: setattribute
// Function: Set attributes
//************************************** *******************************
Inline void cobjectattributesbase: setattribute (const string_type & szname, const string_type & szvalue)
{
If (hasattribute (szname ))
Modifyattribute (szname, szvalue );
Else
Appendattribute (szname, szvalue );
}

//************************************** ********************************
// Function: removeattribute
// Function: delete attributes
//************************************** *******************************
Inline void cobjectattributesbase: removeattribute (const string_type & szname)
{
Attribute_iterator P = attribute_find (szname );
If (P! = Attribute_end ())
{
If (! Isattributeeventfreezeed ())
{
// Glow event
Bool bpermit = true;
Onattributewillremove (szname, bpermit );
If (! Bpermit)
Return;
}

// Remove attributes
M_aattributes.erase (P );

// Glow event
If (! Isattributeeventfreezeed ())
Onattributeremoved (szname );
}
}

//************************************** ********************************
// Function: clearattributes
// Function: Clear attributes
//************************************** *******************************
Inline void cobjectattributesbase: clearattributes (void)
{
// Glow event
If (! Isattributeeventfreezeed ())
{
Bool bpermit = true;
Onattributeswillclear (bpermit );
If (! Bpermit)
Return;
}

// Clear attributes
M_aattributes.clear ();

// Glow event
If (! Isattributeeventfreezeed ())
Onattributescleared ();
}

} // End of base namespace
# Endif // If the _ objectattributebase_hpp macro is not defined

 

 

Hu leqiu

2010/8/3

Http://blog.csdn.net/hlqyq

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.