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