pointer.cpp

changeset 21
9aebaaafa5da
parent 19
6c5977e43e73
equal deleted inserted replaced
20:2ca638886082 21:9aebaaafa5da
1 #include "pointer.h" 1 #include "pointer.h"
2 2
3 vector<objPointer*> g_pObjectPointers; 3 vector<objPointer*> g_pObjectPointers;
4 4
5 // #define POINTER_DEBUG
6
7 #ifdef POINTER_DEBUG
8 #include <stdio.h>
9 #define DEBUGMESSAGE printf
10 #else
11 #define DEBUGMESSAGE
12 #endif
13
14 void objPointer::replacePointers (LDObject* old, LDObject* repl) { 5 void objPointer::replacePointers (LDObject* old, LDObject* repl) {
15 DEBUGMESSAGE ("replacing %p with %p\n", old, repl);
16 DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size());
17
18 for (ulong i = 0; i < g_pObjectPointers.size(); ++i) { 6 for (ulong i = 0; i < g_pObjectPointers.size(); ++i) {
19 objPointer* ptrptr = g_pObjectPointers[i]; 7 objPointer* ptrptr = g_pObjectPointers[i];
20 DEBUGMESSAGE ("%lu. (%p <-> %p)\n", i, (*ptrptr).ptr, old); 8
21 if ((*ptrptr).ptr == old) { 9 if ((*ptrptr).ptr == old)
22 DEBUGMESSAGE ("\treplacing... %p -> %p\n", (*ptrptr).ptr, repl);
23 (*ptrptr).ptr = repl; 10 (*ptrptr).ptr = repl;
24
25 DEBUGMESSAGE ("%lu. (%p <-> %p)\n", i, (*ptrptr).ptr, old);
26 }
27 } 11 }
28 } 12 }
29 13
30 objPointer::~objPointer () { 14 objPointer::~objPointer () {
31 15
38 objPointer::objPointer (LDObject* _ptr) { 22 objPointer::objPointer (LDObject* _ptr) {
39 ptr = _ptr; 23 ptr = _ptr;
40 } 24 }
41 25
42 void objPointer::serialize () { 26 void objPointer::serialize () {
43 DEBUGMESSAGE ("Adding %p to pointers (ptr = %p)... ", this, ptr);
44 g_pObjectPointers.push_back (this); 27 g_pObjectPointers.push_back (this);
45 DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size());
46 } 28 }
47 29
48 void objPointer::unSerialize () { 30 void objPointer::unSerialize () {
49 DEBUGMESSAGE ("Removing %p from pointers... ", this);
50 for (ulong i = 0; i < g_pObjectPointers.size(); ++i) 31 for (ulong i = 0; i < g_pObjectPointers.size(); ++i)
51 if (g_pObjectPointers[i] == this) 32 if (g_pObjectPointers[i] == this)
52 g_pObjectPointers.erase (g_pObjectPointers.begin() + i); 33 g_pObjectPointers.erase (g_pObjectPointers.begin() + i);
53 DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size());
54 } 34 }
35
36 void objPointer::deleteObj (LDObject* obj) {
37 replacePointers (obj, nullptr);
38 delete obj;
39 }

mercurial