Browse Source

fix handling of QList<X> in the reflection subsytem

assuming that <prefix> is an item which supports QList<X>
e.g. "load.cookies" then the following actions are supported:

<prefix>.length   get the length of the list ("count" and "size" also work)
<prefix>.first    get the first element or blank if list is empty
<prefix>.last     get the last element  or blank if list is empty
<prefix>[index]   gets the element specified by index

For setting the values, you can do the following:

<prefix>.clear    clears the list, value is ignored
<prefix>.append   appends  a blank item to the list, value is ignored
<prefix>.prepend  prepends a blank item to the list, value is ignored
<prefix>.delete   deletes an item (list index is specified via the value)
<prefix>[index]   sets the element specified by index to the given value

Error handling was also improved to return a failure when setting a value
or returning a blank string when trying to get a value. Fixes #1944
Ashish Kulkarni 10 years ago
parent
commit
cd80b56ee7
2 changed files with 20 additions and 12 deletions
  1. 1 0
      CHANGELOG.md
  2. 19 12
      src/lib/reflect.hh

+ 1 - 0
CHANGELOG.md

@@ -17,6 +17,7 @@ v0.12.2 (unreleased)
 * **#1825**: fix handling of non-ASCII characters in command-line arguments
 * **#1863**: **[qt]** blank page or crash with low DPI on Windows
 * **#1906**: fix wrong comparison when parsing list parameters
+* **#1944**: **[breaking change]** fix the reflection subsystem to fix the non-functional API
 * **#1949**: fix generation of tarball in the posix-local build
 * **#1955**: installer does not work on 32-bit OS X (10.6.x or 10.7.x)
 * **#1961**: add explicit dependency on minimal font packages for all linux targets

+ 19 - 12
src/lib/reflect.hh

@@ -148,9 +148,10 @@ struct DLL_LOCAL ReflectImpl< QList< X> >: public Reflect {
 
 	virtual QString get(const char * name) {
 		int ps, next, elm;
-		if (!strcmp(name,"size")) return QString::number(l.size());
-		parse(name, ps, next, elm);
-		if (ps > 0 || !strncmp(name, "last", ps)) elm = l.size() -1;
+		if (!strcmp(name,"size") || !strcmp(name,"length") || !strcmp(name,"count")) return QString::number(l.size());
+		if (!parse(name, ps, next, elm)) return QString();
+		if (ps > 0 && !l.isEmpty() && !strncmp(name, "first", ps)) elm = 0;
+		if (ps > 0 && !l.isEmpty() && !strncmp(name, "last",  ps)) elm = l.size() - 1;
 		if (elm < 0 || elm >= l.size()) return QString();
 		ReflectImpl<X> impl(l[elm]);
 		return static_cast<Reflect*>(&impl)->get(name+next);
@@ -160,17 +161,23 @@ struct DLL_LOCAL ReflectImpl< QList< X> >: public Reflect {
 		int ps, next, elm;
 		if (!strcmp(name,"clear"))
 			l.clear();
-		else if (!strcmp(name,"pop"))
-			l.pop_back();
 		else if (!strcmp(name,"append"))
-			l.push_front(X());
-		else {
-			parse(name, ps, next, elm);
-			if (ps > 0 || !strncmp(name, "last", ps)) elm = l.size() -1;
-			if (ps > 0 || !strncmp(name, "append", ps)) {
-				l.push_front(X());
-				elm = l.size() -1;
+			l.append(X());
+		else if (!strcmp(name,"prepend"))
+			l.prepend(X());
+		else if (!strcmp(name,"delete")) {
+			bool ok = true;
+			int idx = value.toInt(&ok);
+			if (ok && idx >= 0 && idx < l.size()) {
+				l.removeAt(idx);
+				return true;
 			}
+			return false;
+		}
+		else {
+			if (!parse(name, ps, next, elm)) return false;
+			if (ps > 0 && !l.isEmpty() && !strncmp(name, "first", ps)) elm = 0;
+			if (ps > 0 && !l.isEmpty() && !strncmp(name, "last",  ps)) elm = l.size() - 1;
 			ReflectImpl<X> impl(l[elm]);
 			return static_cast<Reflect *>(&impl)->set(name+next, value);
 		}