summaryrefslogtreecommitdiffstats
path: root/apt-pkg/contrib/sptr.h
blob: 77806d94dc8a6c4a16d72cf9568908deb5ca9e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
/* ######################################################################
   
   Trivial non-ref counted 'smart pointer'
   
   This is really only good to eliminate 
     { 
       delete Foo;
       return;
     }
   
   Blocks from functions.
   
   I think G++ has become good enough that doing this won't have much
   code size implications.
   
   ##################################################################### */
									/*}}}*/
#ifndef SMART_POINTER_H
#define SMART_POINTER_H
#include <apt-pkg/macros.h>

template <class T>
class APT_DEPRECATED_MSG("use std::unique_ptr instead") SPtr
{
   public:
   T *Ptr;
   
   inline T *operator ->() {return Ptr;};
   inline T &operator *() {return *Ptr;};
   inline operator T *() {return Ptr;};
   inline operator void *() {return Ptr;};
   inline T *UnGuard() {T *Tmp = Ptr; Ptr = 0; return Tmp;};
   inline void operator =(T *N) {Ptr = N;};      
   inline bool operator ==(T *lhs) const {return Ptr == lhs;};
   inline bool operator !=(T *lhs) const {return Ptr != lhs;};
   inline T*Get() {return Ptr;};
      
   inline SPtr(T *Ptr) : Ptr(Ptr) {};
   inline SPtr() : Ptr(0) {};
   inline ~SPtr() {delete Ptr;};
};

template <class T>
class APT_DEPRECATED_MSG("use std::unique_ptr instead") SPtrArray
{
   public:
   T *Ptr;
   
   //inline T &operator *() {return *Ptr;};
   inline operator T *() {return Ptr;};
   inline operator void *() {return Ptr;};
   inline T *UnGuard() {T *Tmp = Ptr; Ptr = 0; return Tmp;};
   //inline T &operator [](signed long I) {return Ptr[I];};
   inline void operator =(T *N) {Ptr = N;};
   inline bool operator ==(T *lhs) const {return Ptr == lhs;};
   inline bool operator !=(T *lhs) const {return Ptr != lhs;};
   inline T *Get() {return Ptr;};
   
   inline SPtrArray(T *Ptr) : Ptr(Ptr) {};
   inline SPtrArray() : Ptr(0) {};
#if __GNUC__ >= 4
	#pragma GCC diagnostic push
	#pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations"
	// gcc warns about this, but we can do nothing here…
#endif
   inline ~SPtrArray() {delete [] Ptr;};
#if __GNUC__ >= 4
	#pragma GCC diagnostic pop
#endif
};

#endif