ptrvec.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**********************************************************************
  2. *<
  3. FILE: ptrvec.h
  4. DESCRIPTION: An variable length array of pointers
  5. CREATED BY: Dan Silva
  6. HISTORY:
  7. *> Copyright (c) 1994, All Rights Reserved.
  8. **********************************************************************/
  9. #ifndef __PTRVEC__H
  10. #define __PTRVEC__H
  11. class PtrVector {
  12. protected:
  13. int size;
  14. int nused;
  15. void** data;
  16. PtrVector() { size = nused = 0; data = NULL; }
  17. UtilExport ~PtrVector();
  18. UtilExport PtrVector(const PtrVector& v);
  19. UtilExport PtrVector& operator=(const PtrVector& v);
  20. UtilExport void append(void * ptr , int extra);
  21. UtilExport void insertAt(void * ptr , int at, int extra);
  22. UtilExport void* remove(int i);
  23. UtilExport void* removeLast();
  24. void* operator[](int i) const { return data[i]; }
  25. void*& operator[](int i) { return data[i]; }
  26. public:
  27. UtilExport void reshape(int i); // sets capacity
  28. UtilExport void setLength(int i); // sets length, capacity if necessary
  29. UtilExport void clear(); // deletes the ptr array, but not the objects
  30. void shrink() { reshape(nused); }
  31. int length() const { return nused; }
  32. int capacity() const { return size; }
  33. };
  34. template <class T> class PtrVec: public PtrVector {
  35. public:
  36. PtrVec():PtrVector() {}
  37. T* operator[](int i) const { return (T*)PtrVector::operator[](i); }
  38. T*& operator[](int i) { return (T*&)PtrVector::operator[](i); }
  39. PtrVec<T>& operator=(const PtrVec<T>& v) { return (PtrVec<T>&)PtrVector::operator=(v); }
  40. void append(T *ptr, int extra = 10) { PtrVector::append(ptr,extra); }
  41. void insertAt(T* ptr, int at, int extra=10) { PtrVector::insertAt(ptr,at,extra); }
  42. T* remove(int i) { return (T *)PtrVector::remove(i); }
  43. T* removeLast() { return (T *)PtrVector::removeLast(); }
  44. void deleteAll(); // deletes all the objects
  45. };
  46. template <class T>
  47. void PtrVec<T>::deleteAll() {
  48. while (length()) {
  49. T* p = removeLast();
  50. delete p;
  51. }
  52. }
  53. #endif