Use a C-array for config too for the same reasons as with actions

Tue, 21 May 2013 19:07:38 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 21 May 2013 19:07:38 +0300
changeset 239
ea09eeba1c2b
parent 238
3b35f502a6c7
child 240
bdc9d429cdc2

Use a C-array for config too for the same reasons as with actions

src/config.cpp file | annotate | diff | comparison | revisions
src/config.h file | annotate | diff | comparison | revisions
--- a/src/config.cpp	Tue May 21 18:56:38 2013 +0300
+++ b/src/config.cpp	Tue May 21 19:07:38 2013 +0300
@@ -24,7 +24,8 @@
 #include "misc.h"
 #include "gui.h"
 
-std::vector<config*> g_configPointers;
+config* g_configPointers[MAX_CONFIG];
+static ushort g_cfgPointerCursor = 0;
 
 // =============================================================================
 const char* g_WeekdayNames[7] = {
@@ -101,9 +102,13 @@
 		
 		// Find the config entry for this.
 		config* cfg = null;
-		for (config* i : g_configPointers)
+		for (config* i : g_configPointers) {
+			if (!i)
+				break;
+			
 			if (entry == i->name)
 				cfg = i;
+		}
 		
 		if (!cfg) {
 			fprintf (stderr, "unknown config `%s`\n", entry.chars());
@@ -211,6 +216,9 @@
 		timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
 	
 	for (config* cfg : g_configPointers) {
+		if (!cfg)
+			break;
+		
 		str valstring;
 		switch (cfg->getType()) {
 		case CONFIG_int:
@@ -251,8 +259,12 @@
 
 // =============================================================================
 void config::reset () {
-	for (size_t i = 0; i < NUM_CONFIG; i++)
-		g_configPointers[i]->resetValue ();
+	for (config* cfg : g_configPointers) {
+		if (!cfg)
+			break;
+		
+		cfg->resetValue ();
+	}
 }
 
 // =============================================================================
@@ -272,4 +284,12 @@
 #else
 	return fmt ("%s" DIRSLASH APPNAME DIRSLASH, qchars (QDir::homePath ()));
 #endif // _WIN32
+}
+
+void addConfig (config* ptr) {
+	if (g_cfgPointerCursor == 0)
+		memset (g_configPointers, 0, sizeof g_configPointers);
+	
+	assert (g_cfgPointerCursor < MAX_CONFIG);
+	g_configPointers[g_cfgPointerCursor++] = ptr;
 }
\ No newline at end of file
--- a/src/config.h	Tue May 21 18:56:38 2013 +0300
+++ b/src/config.h	Tue May 21 19:07:38 2013 +0300
@@ -26,7 +26,7 @@
 #include <qkeysequence.h>
 
 #define MAX_INI_LINE 512
-#define NUM_CONFIG (g_configPointers.size ())
+#define MAX_CONFIG 512
 
 #define cfg(T, NAME, DEFAULT) \
 	T##config NAME (DEFAULT, #NAME, #T, #DEFAULT)
@@ -63,7 +63,7 @@
 	static str filepath ();
 };
 
-extern std::vector<config*> g_configPointers;
+void addConfig (config* ptr);
 
 // =============================================================================
 #define DEFINE_UNARY_OPERATOR(T, OP) \
@@ -118,7 +118,7 @@
 		name = _name; \
 		typestring = _typestring; \
 		defaultstring = _defaultstring; \
-		g_configPointers.push_back (this); \
+		addConfig (this); \
 	} \
 	operator T () { \
 		return value; \

mercurial