PCL_interface_composite.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) =USTC= Fu Li
  3. *
  4. * Author : Fu Li
  5. * Create : 2005-1-23
  6. * Home : http://www.crazy-bit.com/
  7. * Mail : crazybit@263.net
  8. * History :
  9. */
  10. #ifndef __PCL_INTERFACE_COMPOSITE__2005_01_23__H__
  11. #define __PCL_INTERFACE_COMPOSITE__2005_01_23__H__
  12. #include <assert.h>
  13. #include <deque>
  14. template<class T> class PCL_Interface_Composite ;
  15. //=============================================================================
  16. /**
  17. composite support.
  18. enable your class contain composite object, for example:
  19. @code
  20. class CMultiImage : public CObject,
  21. private PCL_Interface_Composite<CImage>
  22. {
  23. int GetImageCount() const { return PCL_GetObjectCount(); }
  24. CImage* GetImage (int nIndex) const { return PCL_GetObject(nIndex); }
  25. }
  26. @endcode
  27. */
  28. template<class T>
  29. class PCL_Interface_Composite
  30. {
  31. public:
  32. /// Deconstructor will call all PCL_DeleteAllObjects to delete all objects.
  33. virtual ~PCL_Interface_Composite() {PCL_DeleteAllObjects();}
  34. /// Get number of object.
  35. int PCL_GetObjectCount() const {return (int)m_objList.size();}
  36. /**
  37. * Add an object (created by new) into list (add a NULL object is permitted).
  38. * after the object be added, you can't delete it any more.
  39. */
  40. void PCL_PushObject (T* pObj) {m_objList.push_back (pObj);} // push NULL is permitted
  41. /**
  42. * Insert an object (created by new) into list (add a NULL object is permitted).
  43. * after the object be added, you can't delete it any more.
  44. */
  45. void PCL_InsertObject (int nIndex, T* pObj)
  46. {
  47. if ((nIndex >= 0) && (nIndex <= PCL_GetObjectCount()))
  48. m_objList.insert (m_objList.begin()+nIndex, pObj) ; // push NULL is permitted
  49. else
  50. {assert(false);}
  51. }
  52. /// Get object in list, nIndex is a zero-based index.
  53. T* PCL_GetObject (int nIndex) const
  54. {
  55. if ((nIndex >= 0) && (nIndex < PCL_GetObjectCount()))
  56. return m_objList[nIndex] ;
  57. else
  58. {assert(false); return 0;}
  59. }
  60. /// Get zero-based index which pObj in list.
  61. int PCL_GetObjectIndex (const T* pObj) const
  62. {
  63. for (size_t i=0 ; i < m_objList.size() ; i++)
  64. {
  65. if (m_objList[i] == pObj)
  66. return (int)i ;
  67. }
  68. return -1 ;
  69. }
  70. /// Throw objects' ownership. you must delete objects yourself.
  71. void PCL_ThrowOwnership (std::deque<T*>& listObj)
  72. {
  73. listObj = m_objList ;
  74. m_objList.clear() ;
  75. }
  76. /// Delete all objects in list.
  77. void PCL_DeleteAllObjects()
  78. {
  79. while (!m_objList.empty())
  80. {
  81. T * p = m_objList.back() ;
  82. m_objList.pop_back() ;
  83. if (p)
  84. delete p ;
  85. }
  86. }
  87. /// Remove & delete object from list.
  88. void PCL_DeleteObject (int nIndex)
  89. {
  90. if ((nIndex >= 0) && (nIndex < PCL_GetObjectCount()))
  91. {
  92. T * p = m_objList[nIndex] ;
  93. m_objList.erase(m_objList.begin() + nIndex) ;
  94. if (p)
  95. delete p ;
  96. }
  97. else
  98. {
  99. assert(false) ;
  100. }
  101. }
  102. /// Delete object in list.
  103. void PCL_DeleteObject (T* pObj)
  104. {
  105. if (pObj)
  106. {
  107. PCL_DeleteObject (PCL_GetObjectIndex(pObj)) ;
  108. }
  109. assert (PCL_GetObjectIndex(pObj) == -1) ;
  110. }
  111. private:
  112. std::deque<T*> m_objList ;
  113. };
  114. //=============================================================================
  115. // inline implement
  116. //=============================================================================
  117. #endif