Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.7 KiB

  1. // Copyright (C) 1999 Microsoft Corporation. All rights reserved.
  2. //This pointer class wraps a pointer so that delete happens automatically when the TSmartPointer goes out of scope
  3. //If this is not the behavior you want then don't use this wrapper class
  4. #ifndef __SMARTPOINTER_H__
  5. #define __SMARTPOINTER_H__
  6. #ifndef ASSERT
  7. #define ASSERT(x)
  8. #endif
  9. //Destructor is NOT virtual bacause I don't see why anyone would ever treat a TSmartPointerArray as a TSmartPointer
  10. #pragma warning(disable : 4284)//It's OK to use this SmartPointer class with native types; but the -> operator doesn't make sense. Compiler warns of this.
  11. template <class T> class TSmartPointer
  12. {
  13. public:
  14. TSmartPointer() : m_p(0) {}
  15. TSmartPointer(T* p) : m_p(p) {}
  16. ~TSmartPointer() { Delete();}
  17. operator T*() const { return m_p; }
  18. T& operator*() const { ASSERT(0!=m_p); return *m_p; }
  19. T** operator&() { ASSERT(0==m_p); return &m_p; }
  20. T* operator->() const { ASSERT(0!=m_p); return m_p; }
  21. T* operator=(T* p) { return (m_p = p); }
  22. bool operator!() const { return (0 == m_p); }
  23. bool operator==(const T* p) const { return m_p == p; }
  24. void Delete() { delete m_p; m_p=0; }
  25. T* m_p;
  26. private:
  27. TSmartPointer(const TSmartPointer<T>& p) {}//This is private bacause it doesn't make sense to automatically clean up a copy of the pointer
  28. };
  29. template <class T> class TSmartPointerArray : public TSmartPointer<T>
  30. {
  31. public:
  32. TSmartPointerArray(T *p) : TSmartPointer<T>(p) {}
  33. TSmartPointerArray() : TSmartPointer<T>() {}
  34. ~TSmartPointerArray() { Delete();}
  35. T* operator++() { ASSERT(0!=m_p); return ++m_p; }
  36. T* operator+=(int n) { ASSERT(0!=m_p); return (m_p+=n); }
  37. T* operator-=(int n) { ASSERT(0!=m_p); return (m_p-=n); }
  38. T* operator--() { ASSERT(0!=m_p); return --m_p; }
  39. // T& operator[](int n) const { ASSERT(0!=m_p); return m_p[n]; }
  40. bool operator<(const T* p) const { return m_p < p; }
  41. bool operator>(const T* p) const { return m_p > p; }
  42. bool operator<=(const T* p) const { return m_p <= p; }
  43. bool operator>=(const T* p) const { return m_p >= p; }
  44. T* operator=(T p[]) { return (m_p = p); }
  45. void Delete() { delete [] m_p; m_p=0; }
  46. private:
  47. TSmartPointerArray(const TSmartPointerArray<T>& p) {}//This is private bacause it doesn't make sense to automatically clean up a copy of the pointer
  48. };
  49. #endif //__SMARTPOINTER_H__