Merge lp:~aacid/unity-scopes-shell/no_contains_before_value into lp:unity-scopes-shell

Proposed by Albert Astals Cid
Status: Merged
Approved by: Paweł Stołowski
Approved revision: 166
Merged at revision: 171
Proposed branch: lp:~aacid/unity-scopes-shell/no_contains_before_value
Merge into: lp:unity-scopes-shell
Diff against target: 88 lines (+38/-33)
1 file modified
src/Unity/resultsmodel.cpp (+38/-33)
To merge this branch: bzr merge lp:~aacid/unity-scopes-shell/no_contains_before_value
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Paweł Stołowski (community) Approve
Review via email: mp+242750@code.launchpad.net

Commit message

Don't call Result::contains and then Result::value

Doing that means two searches in the map for every query, we can just call value()
and in the exceptional case it throws catch the exception and return QVariant()

To post a comment you must log in.
Revision history for this message
Paweł Stołowski (stolowski) wrote :

Looks, good, +1

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Unity/resultsmodel.cpp'
2--- src/Unity/resultsmodel.cpp 2014-08-27 17:54:20 +0000
3+++ src/Unity/resultsmodel.cpp 2014-11-25 10:04:47 +0000
4@@ -112,15 +112,17 @@
5 return QVariant();
6 }
7 std::string const& realFieldName = mappingIt->second;
8- if (!result->contains(realFieldName)) {
9- return QVariant();
10- }
11- scopes::Variant const& v = result->value(realFieldName);
12- if (v.which() != scopes::Variant::Type::String) {
13- return QVariant();
14- }
15-
16- return QString::fromStdString(v.get_string());
17+ try {
18+ scopes::Variant const& v = result->value(realFieldName);
19+ if (v.which() != scopes::Variant::Type::String) {
20+ return QVariant();
21+ }
22+ return QString::fromStdString(v.get_string());
23+ } catch (...) {
24+ // value() throws if realFieldName is empty or the result
25+ // doesn't have a value for it
26+ return QVariant();
27+ }
28 }
29
30 QVariant
31@@ -130,30 +132,33 @@
32 if (mappingIt == m_componentMapping.end()) {
33 return QVariant();
34 }
35- std::string const& realFieldName = mappingIt->second;
36- if (!result->contains(realFieldName)) {
37- return QVariant();
38- }
39- scopes::Variant const& v = result->value(realFieldName);
40- if (v.which() != scopes::Variant::Type::Array) {
41- return QVariant();
42- }
43-
44- QVariantList attributes;
45- scopes::VariantArray arr(v.get_array());
46- for (unsigned i = 0; i < arr.size(); i++) {
47- if (arr[i].which() != scopes::Variant::Type::Dict) {
48- continue;
49- }
50- QVariantMap attribute(scopeVariantToQVariant(arr[i]).toMap());
51- attributes << QVariant(attribute);
52- // we'll limit the number of attributes
53- if (attributes.size() >= m_maxAttributes) {
54- break;
55- }
56- }
57-
58- return attributes;
59+ try {
60+ std::string const& realFieldName = mappingIt->second;
61+ scopes::Variant const& v = result->value(realFieldName);
62+ if (v.which() != scopes::Variant::Type::Array) {
63+ return QVariant();
64+ }
65+
66+ QVariantList attributes;
67+ scopes::VariantArray arr(v.get_array());
68+ for (unsigned i = 0; i < arr.size(); i++) {
69+ if (arr[i].which() != scopes::Variant::Type::Dict) {
70+ continue;
71+ }
72+ QVariantMap attribute(scopeVariantToQVariant(arr[i]).toMap());
73+ attributes << QVariant(attribute);
74+ // we'll limit the number of attributes
75+ if (attributes.size() >= m_maxAttributes) {
76+ break;
77+ }
78+ }
79+
80+ return attributes;
81+ } catch (...) {
82+ // value() throws if realFieldName is empty or the result
83+ // doesn't have a value for it
84+ return QVariant();
85+ }
86 }
87
88 QHash<int, QByteArray> ResultsModel::roleNames() const

Subscribers

People subscribed via source and target branches

to all changes: