PCL_array.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) =USTC= Fu Li
  3. *
  4. * Author : Fu Li
  5. * Create : 2004-7-19
  6. * Home : http://www.crazy-bit.com/
  7. * Mail : crazybit@263.net
  8. * History :
  9. */
  10. #ifndef __PCL_ARRAY__2004_07_19__H__
  11. #define __PCL_ARRAY__2004_07_19__H__
  12. #include <assert.h>
  13. //=============================================================================
  14. /**
  15. * Auto delete array.
  16. @code
  17. // attach user alloc memory
  18. PCL_array<POINT> arrUser (new POINT[10]) ;
  19. arrUser[0].x = 0 ;
  20. // alloc memory self
  21. PCL_array<POINT> arrOur (10) ;
  22. arrOur[0].x = 0 ;
  23. @endcode
  24. */
  25. template<class T>
  26. class PCL_array
  27. {
  28. public:
  29. /// Attach pArray, pArray must use <B>new []</B> to create (no bound check).
  30. /// After attached, you can't delete pArray any more.
  31. PCL_array (void* pArray)
  32. {
  33. m_pArray = (T*)pArray; assert(pArray);
  34. m_nNumberT = -1 ;
  35. }
  36. /// Alloc nNumberT T array (with DEBUG-time bound check).
  37. PCL_array (int nNumberT)
  38. {
  39. if (nNumberT > 0)
  40. {
  41. m_pArray = new T[nNumberT] ;
  42. m_nNumberT = nNumberT ;
  43. }
  44. else
  45. {
  46. m_pArray = 0 ;
  47. m_nNumberT = -1 ; assert(false);
  48. }
  49. }
  50. /// delete[] array
  51. ~PCL_array()
  52. {
  53. if (m_pArray)
  54. delete[] m_pArray ;
  55. }
  56. /// Get element.
  57. T& operator[](int n) const
  58. {
  59. assert ((m_nNumberT == -1) ? true : (n < m_nNumberT)) ;
  60. return m_pArray[n] ;
  61. }
  62. /// Get array start pointer.
  63. T* get() const {return m_pArray;}
  64. // operator T*() const {return m_pArray;} // don't release, otherwise you can use delete[] to delete this object.
  65. /// Get bytes of array.
  66. int GetArrayBytes() const
  67. {
  68. if (m_nNumberT == -1)
  69. {
  70. // attached, we don't know array's size
  71. assert(false) ;
  72. return 0 ;
  73. }
  74. return (sizeof(T) * m_nNumberT) ;
  75. }
  76. private:
  77. T * m_pArray ;
  78. int m_nNumberT ;
  79. };
  80. #endif