Added pointer serializing so I can keep track of all LDObject* members. This way I can replace them all properly when needed.

Sat, 16 Mar 2013 14:21:31 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 16 Mar 2013 14:21:31 +0200
changeset 19
6c5977e43e73
parent 18
a6732098fed8
child 20
2ca638886082

Added pointer serializing so I can keep track of all LDObject* members. This way I can replace them all properly when needed.

common.h file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
io.cpp file | annotate | diff | comparison | revisions
io.h file | annotate | diff | comparison | revisions
ldforge.pro file | annotate | diff | comparison | revisions
ldtypes.h file | annotate | diff | comparison | revisions
model.cpp file | annotate | diff | comparison | revisions
pointer.cpp file | annotate | diff | comparison | revisions
pointer.h file | annotate | diff | comparison | revisions
zz_setContentsDialog.cpp file | annotate | diff | comparison | revisions
zz_setContentsDialog.h file | annotate | diff | comparison | revisions
--- a/common.h	Sat Mar 16 13:08:24 2013 +0200
+++ b/common.h	Sat Mar 16 14:21:31 2013 +0200
@@ -131,4 +131,6 @@
 typedef uint32_t xulong;
 typedef uint64_t xulonglong;
 
+#include "pointer.h"
+
 #endif
\ No newline at end of file
--- a/gui.cpp	Sat Mar 16 13:08:24 2013 +0200
+++ b/gui.cpp	Sat Mar 16 14:21:31 2013 +0200
@@ -227,6 +227,7 @@
 	line->dColor = 24;
 	
 	g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + ulSpot, line);
+	g_CurrentFile->objects[ulSpot].serialize ();
 	
 	buildObjList ();
 	R->hardRefresh ();
--- a/io.cpp	Sat Mar 16 13:08:24 2013 +0200
+++ b/io.cpp	Sat Mar 16 14:21:31 2013 +0200
@@ -2,7 +2,6 @@
 
 #include "common.h"
 #include "io.h"
-#include "ldtypes.h"
 #include "misc.h"
 #include "gui.h"
 #include "bbox.h"
@@ -73,6 +72,10 @@
 		}
 	}
 	
+	// Serialize all of the objects
+	for (ulong i = 0; i < load->objects.size(); ++i)
+		load->objects[i].serialize ();
+	
 	g_LoadedFiles.push_back (load);
 	g_CurrentFile = g_LoadedFiles[g_LoadedFiles.size() - 1];
 	
--- a/io.h	Sat Mar 16 13:08:24 2013 +0200
+++ b/io.h	Sat Mar 16 14:21:31 2013 +0200
@@ -8,7 +8,7 @@
 class OpenFile {
 public:
 	str zFileName, zTitle;
-	vector<LDObject*> objects;
+	vector<objPointer> objects;
 };
 
 // PROTOTYPES
--- a/ldforge.pro	Sat Mar 16 13:08:24 2013 +0200
+++ b/ldforge.pro	Sat Mar 16 14:21:31 2013 +0200
@@ -20,6 +20,7 @@
 	str.h \
 	config.h \
 	cfgdef.h \
+	pointer.h \
 	zz_setContentsDialog.h
 
 SOURCES += bbox.cpp \
@@ -33,6 +34,7 @@
 	scanner.cpp \
 	str.cpp \ 
 	config.cpp \
+	pointer.cpp \
 	zz_setContentsDialog.cpp
 
 QMAKE_CXXFLAGS += -std=c++0x
--- a/ldtypes.h	Sat Mar 16 13:08:24 2013 +0200
+++ b/ldtypes.h	Sat Mar 16 14:21:31 2013 +0200
@@ -2,7 +2,6 @@
 #define __LDTYPES_H__
 
 #include "common.h"
-#include "str.h"
 
 #define IMPLEMENT_LDTYPE(N) \
 	LD##N (); \
@@ -45,7 +44,7 @@
 	LDObject ();
 	
 	// Index (i.e. line number) of this object
-	ulong getIndex ();
+	unsigned long getIndex ();
 	
 	// Type enumerator of this object
 	virtual LDObjectType_e getType () const {
@@ -203,7 +202,7 @@
 	
 	vertex vPos;
 	bearing gAngle3D;
-	ulong ulLength;
+	unsigned long ulLength;
 };
 
 // =============================================================================
--- a/model.cpp	Sat Mar 16 13:08:24 2013 +0200
+++ b/model.cpp	Sat Mar 16 14:21:31 2013 +0200
@@ -11,8 +11,10 @@
 	for (ushort i = 0; i < g_LoadedFiles.size(); i++) {
 		OpenFile* f = g_LoadedFiles[i];
 		
-		for (ushort j = 0; j < f->objects.size(); ++j)
-			delete f->objects[j];
+		for (ushort j = 0; j < f->objects.size(); ++j) {
+			f->objects[i].unSerialize ();
+			delete (LDObject*)f->objects[j];
+		}
 		
 		delete f;
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pointer.cpp	Sat Mar 16 14:21:31 2013 +0200
@@ -0,0 +1,54 @@
+#include "pointer.h"
+
+vector<objPointer*> g_pObjectPointers;
+
+// #define POINTER_DEBUG
+
+#ifdef POINTER_DEBUG
+#include <stdio.h>
+#define DEBUGMESSAGE printf
+#else
+#define DEBUGMESSAGE
+#endif
+
+void objPointer::replacePointers (LDObject* old, LDObject* repl) {
+	DEBUGMESSAGE ("replacing %p with %p\n", old, repl);
+	DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size());
+	
+	for (ulong i = 0; i < g_pObjectPointers.size(); ++i) {
+		objPointer* ptrptr = g_pObjectPointers[i];
+		DEBUGMESSAGE ("%lu. (%p <-> %p)\n", i, (*ptrptr).ptr, old);
+		if ((*ptrptr).ptr == old) {
+			DEBUGMESSAGE ("\treplacing... %p -> %p\n", (*ptrptr).ptr, repl);
+			(*ptrptr).ptr = repl;
+			
+			DEBUGMESSAGE ("%lu. (%p <-> %p)\n", i, (*ptrptr).ptr, old);
+		}
+	}
+}
+
+objPointer::~objPointer () {
+	
+}
+
+objPointer::objPointer () {
+	ptr = nullptr;
+}
+
+objPointer::objPointer (LDObject* _ptr) {
+	ptr = _ptr;
+}
+
+void objPointer::serialize () {
+	DEBUGMESSAGE ("Adding %p to pointers (ptr = %p)... ", this, ptr);
+	g_pObjectPointers.push_back (this);
+	DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size());
+}
+
+void objPointer::unSerialize () {
+	DEBUGMESSAGE ("Removing %p from pointers... ", this);
+	for (ulong i = 0; i < g_pObjectPointers.size(); ++i)
+		if (g_pObjectPointers[i] == this)
+			g_pObjectPointers.erase (g_pObjectPointers.begin() + i);
+	DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size());
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pointer.h	Sat Mar 16 14:21:31 2013 +0200
@@ -0,0 +1,57 @@
+#ifndef __POINTER_H__
+#define __POINTER_H__
+
+#include "ldtypes.h"
+#include <vector>
+using std::vector;
+
+class objPointer;
+extern vector<objPointer*> g_pObjectPointers;
+
+#define POINTER_LDCAST(T) \
+	operator T* () { \
+		return static_cast<T*> (ptr); \
+	}
+
+class objPointer {
+public:
+	LDObject* ptr;
+	
+	objPointer ();
+	objPointer (LDObject*);
+	~objPointer ();
+	
+	void serialize ();
+	void unSerialize ();
+	static void replacePointers (LDObject* old, LDObject* repl);
+	
+	LDObject& operator* () {
+		return *ptr;
+	}
+	
+	LDObject* operator-> () {
+		return ptr;
+	}
+	
+	operator LDObject* () {
+		return ptr;
+	}
+	
+	POINTER_LDCAST (LDComment)
+	POINTER_LDCAST (LDCondLine)
+	POINTER_LDCAST (LDQuad)
+	POINTER_LDCAST (LDTriangle)
+	POINTER_LDCAST (LDSubfile)
+	POINTER_LDCAST (LDGibberish)
+	POINTER_LDCAST (LDVector)
+	POINTER_LDCAST (LDVertex)
+	POINTER_LDCAST (LDLine)
+	
+	LDObject* operator= (LDObject* repl) {
+		ptr = repl;
+		return ptr;
+	}
+};
+
+#endif // __POINTER_H__
+class LDLine;
--- a/zz_setContentsDialog.cpp	Sat Mar 16 13:08:24 2013 +0200
+++ b/zz_setContentsDialog.cpp	Sat Mar 16 14:21:31 2013 +0200
@@ -70,12 +70,7 @@
 		delete oldobj;
 		
 		// Replace all instances of the old object with the new object
-		for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
-			if (g_CurrentFile->objects[i] == oldobj) {
-				g_CurrentFile->objects[i] = obj;
-				break;
-			}
-		}
+		objPointer::replacePointers (oldobj, obj);
 		
 		// Rebuild stuff after this
 		parent->buildObjList ();
--- a/zz_setContentsDialog.h	Sat Mar 16 13:08:24 2013 +0200
+++ b/zz_setContentsDialog.h	Sat Mar 16 14:21:31 2013 +0200
@@ -2,7 +2,7 @@
 #include <qlineedit.h>
 #include <qlabel.h>
 #include <qdialogbuttonbox.h>
-#include "ldtypes.h"
+#include "common.h"
 
 class Dialog_SetContents : public QDialog {
 public:

mercurial