123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /*
- * Copyright (C) =USTC= Fu Li
- *
- * Author : Fu Li
- * Create : 2005-1-23
- * Home : http://www.crazy-bit.com/
- * Mail : crazybit@263.net
- * History :
- */
- #ifndef __PCL_INTERFACE_COMPOSITE__2005_01_23__H__
- #define __PCL_INTERFACE_COMPOSITE__2005_01_23__H__
- #include <assert.h>
- #include <deque>
- template<class T> class PCL_Interface_Composite ;
- //=============================================================================
- /**
- composite support.
- enable your class contain composite object, for example:
- @code
- class CMultiImage : public CObject,
- private PCL_Interface_Composite<CImage>
- {
- int GetImageCount() const { return PCL_GetObjectCount(); }
- CImage* GetImage (int nIndex) const { return PCL_GetObject(nIndex); }
- }
- @endcode
- */
- template<class T>
- class PCL_Interface_Composite
- {
- public:
- /// Deconstructor will call all PCL_DeleteAllObjects to delete all objects.
- virtual ~PCL_Interface_Composite() {PCL_DeleteAllObjects();}
- /// Get number of object.
- int PCL_GetObjectCount() const {return (int)m_objList.size();}
- /**
- * Add an object (created by new) into list (add a NULL object is permitted).
- * after the object be added, you can't delete it any more.
- */
- void PCL_PushObject (T* pObj) {m_objList.push_back (pObj);} // push NULL is permitted
- /**
- * Insert an object (created by new) into list (add a NULL object is permitted).
- * after the object be added, you can't delete it any more.
- */
- void PCL_InsertObject (int nIndex, T* pObj)
- {
- if ((nIndex >= 0) && (nIndex <= PCL_GetObjectCount()))
- m_objList.insert (m_objList.begin()+nIndex, pObj) ; // push NULL is permitted
- else
- {assert(false);}
- }
- /// Get object in list, nIndex is a zero-based index.
- T* PCL_GetObject (int nIndex) const
- {
- if ((nIndex >= 0) && (nIndex < PCL_GetObjectCount()))
- return m_objList[nIndex] ;
- else
- {assert(false); return 0;}
- }
- /// Get zero-based index which pObj in list.
- int PCL_GetObjectIndex (const T* pObj) const
- {
- for (size_t i=0 ; i < m_objList.size() ; i++)
- {
- if (m_objList[i] == pObj)
- return (int)i ;
- }
- return -1 ;
- }
- /// Throw objects' ownership. you must delete objects yourself.
- void PCL_ThrowOwnership (std::deque<T*>& listObj)
- {
- listObj = m_objList ;
- m_objList.clear() ;
- }
- /// Delete all objects in list.
- void PCL_DeleteAllObjects()
- {
- while (!m_objList.empty())
- {
- T * p = m_objList.back() ;
- m_objList.pop_back() ;
- if (p)
- delete p ;
- }
- }
- /// Remove & delete object from list.
- void PCL_DeleteObject (int nIndex)
- {
- if ((nIndex >= 0) && (nIndex < PCL_GetObjectCount()))
- {
- T * p = m_objList[nIndex] ;
- m_objList.erase(m_objList.begin() + nIndex) ;
- if (p)
- delete p ;
- }
- else
- {
- assert(false) ;
- }
- }
- /// Delete object in list.
- void PCL_DeleteObject (T* pObj)
- {
- if (pObj)
- {
- PCL_DeleteObject (PCL_GetObjectIndex(pObj)) ;
- }
- assert (PCL_GetObjectIndex(pObj) == -1) ;
- }
- private:
- std::deque<T*> m_objList ;
- };
- //=============================================================================
- // inline implement
- //=============================================================================
- #endif
|