woo smallification!

Wed, 21 Aug 2013 01:30:38 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 21 Aug 2013 01:30:38 +0300
changeset 468
7b3159f1a9e2
parent 467
59549731151a
child 469
f2faed654633

woo smallification!

src/config.cpp file | annotate | diff | comparison | revisions
src/config.h file | annotate | diff | comparison | revisions
--- a/src/config.cpp	Wed Aug 21 01:13:52 2013 +0300
+++ b/src/config.cpp	Wed Aug 21 01:30:38 2013 +0300
@@ -58,7 +58,8 @@
 		if (!cfg)
 			break;
 		
-		cfg->loadFromConfig (settings);
+		QVariant val = settings->value (cfg->name, cfg->defaultVariant());
+		cfg->loadFromVariant (val);
 	}
 	
 	settings->deleteLater();
@@ -67,48 +68,6 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-void IntConfig::loadFromConfig (const QSettings* cfg) {
-	QVariant val = cfg->value (name, str::number (defval));
-	value = val.toInt();
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void FloatConfig::loadFromConfig (const QSettings* cfg) {
-	QVariant val = cfg->value (name, str::number (defval));
-	value = val.toFloat();
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void StringConfig::loadFromConfig (const QSettings* cfg) {
-	QVariant val = cfg->value (name, defval);
-	value = val.toString();
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void BoolConfig::loadFromConfig (const QSettings* cfg) {
-	QVariant val = cfg->value (name, str::number (defval));
-	value = val.toBool();
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void KeySequenceConfig::loadFromConfig (const QSettings* cfg) {
-	QVariant val = cfg->value (name, defval.toString());
-	value = QKeySequence (val.toString());
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void ListConfig::loadFromConfig (const QSettings* cfg) {
-	QVariant val = cfg->value (name, defval);
-	value = val.toList();
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
 // Save the configuration to disk
 bool Config::save() {
 	QSettings* settings = getSettingsObject();
@@ -166,6 +125,9 @@
 }
 
 // =============================================================================
+// We cannot just add config objects to a list or vector because that would rely
+// on the vector's c-tor being called before the configs' c-tors. With global
+// variables we cannot assume that!! Therefore we need to use a C-style array here.
 // -----------------------------------------------------------------------------
 void Config::addToArray (Config* ptr) {
 	if (g_cfgPointerCursor == 0)
--- a/src/config.h	Wed Aug 21 01:13:52 2013 +0300
+++ b/src/config.h	Wed Aug 21 01:30:38 2013 +0300
@@ -57,9 +57,10 @@
 	
 	str defaultString() const;
 	virtual void resetValue() {}
-	virtual void loadFromConfig (const QSettings* cfg) { (void) cfg; }
+	virtual void loadFromVariant (const QVariant& val) { (void) val; }
 	virtual bool isDefault() const { return false; }
 	virtual QVariant toVariant() const { return QVariant(); }
+	virtual QVariant defaultVariant() const { return QVariant(); }
 	
 	// ------------------------------------------
 	static bool load();
@@ -84,36 +85,37 @@
 	\
 	operator const T&() const { return value; } \
 	Config::Type getType() const override { return Config::NAME; } \
-	virtual void resetValue() { value = defval; } \
-	virtual bool isDefault() const { return value == defval; } \
-	virtual void loadFromConfig (const QSettings* cfg) override; \
-	virtual QVariant toVariant() const { return QVariant::fromValue<T> (value); }
-	
+	virtual void resetValue() override { value = defval; } \
+	virtual bool isDefault() const override { return value == defval; } \
+	virtual QVariant toVariant() const override { return QVariant::fromValue<T> (value); } \
+	virtual QVariant defaultVariant() const override { return QVariant::fromValue<T> (defval); } \
+	virtual void loadFromVariant (const QVariant& val) override { value = val.value<T>(); } \
+
 #define DEFINE_UNARY_OPERATOR(T, OP) \
 	T operator OP() { \
 		return OP value; \
 	}
-	
+
 #define DEFINE_BINARY_OPERATOR(T, OP) \
 	T operator OP (const T other) { \
 		return value OP other; \
 	}
-	
+
 #define DEFINE_ASSIGN_OPERATOR(T, OP) \
 	T& operator OP (const T other) { \
 		return value OP other; \
 	}
-	
+
 #define DEFINE_COMPARE_OPERATOR(T, OP) \
 	bool operator OP (const T other) { \
 		return value OP other; \
 	}
-	
+
 #define DEFINE_CAST_OPERATOR(T) \
 	operator T() { \
 		return (T) value; \
 	}
-	
+
 #define DEFINE_ALL_COMPARE_OPERATORS(T) \
 	DEFINE_COMPARE_OPERATOR (T, ==) \
 	DEFINE_COMPARE_OPERATOR (T, !=) \
@@ -121,7 +123,7 @@
 	DEFINE_COMPARE_OPERATOR (T, <) \
 	DEFINE_COMPARE_OPERATOR (T, >=) \
 	DEFINE_COMPARE_OPERATOR (T, <=) \
-	 
+
 #define DEFINE_INCREMENT_OPERATORS(T) \
 	T operator++() { return ++value; } \
 	T operator++(int) { return value++; } \

mercurial