sptr.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sptr.h,v 1.3 2001/03/11 07:22:19 jgg Exp $
  4. /* ######################################################################
  5. Trivial non-ref counted 'smart pointer'
  6. This is really only good to eliminate
  7. {
  8. delete Foo;
  9. return;
  10. }
  11. Blocks from functions.
  12. I think G++ has become good enough that doing this won't have much
  13. code size implications.
  14. ##################################################################### */
  15. /*}}}*/
  16. #ifndef SMART_POINTER_H
  17. #define SMART_POINTER_H
  18. #include <apt-pkg/macros.h>
  19. template <class T>
  20. class APT_DEPRECATED_MSG("use std::unique_ptr instead") SPtr
  21. {
  22. public:
  23. T *Ptr;
  24. inline T *operator ->() {return Ptr;};
  25. inline T &operator *() {return *Ptr;};
  26. inline operator T *() {return Ptr;};
  27. inline operator void *() {return Ptr;};
  28. inline T *UnGuard() {T *Tmp = Ptr; Ptr = 0; return Tmp;};
  29. inline void operator =(T *N) {Ptr = N;};
  30. inline bool operator ==(T *lhs) const {return Ptr == lhs;};
  31. inline bool operator !=(T *lhs) const {return Ptr != lhs;};
  32. inline T*Get() {return Ptr;};
  33. inline SPtr(T *Ptr) : Ptr(Ptr) {};
  34. inline SPtr() : Ptr(0) {};
  35. inline ~SPtr() {delete Ptr;};
  36. };
  37. template <class T>
  38. class APT_DEPRECATED_MSG("use std::unique_ptr instead") SPtrArray
  39. {
  40. public:
  41. T *Ptr;
  42. //inline T &operator *() {return *Ptr;};
  43. inline operator T *() {return Ptr;};
  44. inline operator void *() {return Ptr;};
  45. inline T *UnGuard() {T *Tmp = Ptr; Ptr = 0; return Tmp;};
  46. //inline T &operator [](signed long I) {return Ptr[I];};
  47. inline void operator =(T *N) {Ptr = N;};
  48. inline bool operator ==(T *lhs) const {return Ptr == lhs;};
  49. inline bool operator !=(T *lhs) const {return Ptr != lhs;};
  50. inline T *Get() {return Ptr;};
  51. inline SPtrArray(T *Ptr) : Ptr(Ptr) {};
  52. inline SPtrArray() : Ptr(0) {};
  53. #if __GNUC__ >= 4
  54. #pragma GCC diagnostic push
  55. #pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations"
  56. // gcc warns about this, but we can do nothing here…
  57. #endif
  58. inline ~SPtrArray() {delete [] Ptr;};
  59. #if __GNUC__ >= 4
  60. #pragma GCC diagnostic pop
  61. #endif
  62. };
  63. #endif