1
- // Emacs style mode select -*- C++ -*-
1
+ // Emacs style mode select -*- C++ -*-
2
2
// -----------------------------------------------------------------------------
3
3
//
4
4
// $Id$
18
18
// DESCRIPTION:
19
19
//
20
20
// denis - szp<T>, the self zeroing pointer
21
- //
22
- // Once upon a time, actors held raw pointers to other actors.
23
- //
24
- // To destroy an object, one cycled though all the others searching for its
25
- // pointer and resetting every copy to NULL. Then one did the cycling for
26
- // the players, then the sector sound origins, and so on; with hack upon
27
- // hack. Ironically, zero dereferencing is what often crashed the
21
+ //
22
+ // Once upon a time, actors held raw pointers to other actors.
23
+ //
24
+ // To destroy an object, one cycled though all the others searching for its
25
+ // pointer and resetting every copy to NULL. Then one did the cycling for
26
+ // the players, then the sector sound origins, and so on; with hack upon
27
+ // hack. Ironically, zero dereferencing is what often crashed the
28
28
// program altogether.
29
- //
30
- // The idea behind szp is that all copies of one szp pointer can be made
31
- // to point to the same object in O(1) time. This means that having a
32
- // single szp of an actor, you can set them all to NULL without iteration.
33
- // And, as a bonus, on every pointer access, a NULL check can throw a
29
+ //
30
+ // The idea behind szp is that all copies of one szp pointer can be made
31
+ // to point to the same object in O(1) time. This means that having a
32
+ // single szp of an actor, you can set them all to NULL without iteration.
33
+ // And, as a bonus, on every pointer access, a NULL check can throw a
34
34
// specific exception. Naturally, you should always be careful with pointers.
35
35
//
36
36
// -----------------------------------------------------------------------------
@@ -51,7 +51,7 @@ class szp
51
51
52
52
// this should never be used
53
53
// spawn from other pointers, or use init()
54
- szp &operator =(T *other) {} ;
54
+ szp &operator =(T *other) = delete ;
55
55
56
56
// utility function to remove oneself from the linked list
57
57
void inline unlink ()
@@ -61,17 +61,17 @@ class szp
61
61
62
62
next->prev = prev;
63
63
prev->next = next;
64
-
64
+
65
65
if (!naive)
66
66
return ;
67
67
68
68
// last in ring?
69
69
if (this == next)
70
70
delete naive;
71
-
71
+
72
72
naive = NULL ;
73
73
}
74
-
74
+
75
75
public:
76
76
77
77
// use as pointer, checking validity
@@ -82,7 +82,7 @@ class szp
82
82
83
83
return *naive;
84
84
}
85
-
85
+
86
86
// use as raw pointer
87
87
inline operator T*()
88
88
{
@@ -97,11 +97,11 @@ class szp
97
97
{
98
98
if (!naive)
99
99
throw CRecoverableError (" szp pointer was NULL on update_all" );
100
-
100
+
101
101
// all copies already have naive, so their pointers will update too
102
102
*naive = target;
103
103
}
104
-
104
+
105
105
// copy a pointer and add self to the "i have this pointer" list
106
106
inline szp &operator =(szp other)
107
107
{
@@ -116,31 +116,31 @@ class szp
116
116
next = prev = this ;
117
117
return *this ;
118
118
}
119
-
119
+
120
120
// link
121
121
naive = other.naive ;
122
122
prev = other.next ->prev ;
123
123
next = other.next ;
124
124
prev->next = next->prev = this ;
125
-
125
+
126
126
return *this ;
127
127
}
128
-
128
+
129
129
// creates the first (original) pointer
130
130
void init (T *target)
131
131
{
132
132
unlink ();
133
-
133
+
134
134
// first link
135
135
naive = new T*(target);
136
136
prev = next = this ;
137
137
}
138
-
138
+
139
139
// cheap constructor
140
140
inline szp ()
141
141
: naive(NULL ), prev(NULL ), next(NULL )
142
142
{ }
143
-
143
+
144
144
// copy constructor
145
145
inline szp (const szp &other)
146
146
: naive(NULL )
@@ -150,7 +150,7 @@ class szp
150
150
prev = next = this ;
151
151
return ;
152
152
}
153
-
153
+
154
154
// link
155
155
naive = other.naive ;
156
156
prev = other.next ->prev ;
0 commit comments