From 5193f7d03ea6cfbf597de56106119f0caab2f66d Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 31 Dec 2016 15:55:06 -0500 Subject: [PATCH] Task: Improved method signature --- src/DOM.cpp | 6 ++---- src/Task.cpp | 12 ++++++------ src/Task.h | 2 +- src/Variant.cpp | 10 ++-------- src/columns/ColDescription.cpp | 16 ++++------------ src/commands/CmdDenotate.cpp | 3 +-- src/commands/CmdEdit.cpp | 4 +--- src/commands/CmdInfo.cpp | 4 +--- src/commands/CmdStats.cpp | 5 +---- src/commands/CmdTimesheet.cpp | 8 ++------ 10 files changed, 21 insertions(+), 49 deletions(-) diff --git a/src/DOM.cpp b/src/DOM.cpp index eba6b30aa..1d5f43519 100644 --- a/src/DOM.cpp +++ b/src/DOM.cpp @@ -300,8 +300,7 @@ bool getDOM (const std::string& name, const Task& task, Variant& value) if (ref.data.size () && size == 3 && elements[0] == "annotations") { - std::map annos; - ref.getAnnotations (annos); + auto annos = ref.getAnnotations (); int a = strtol (elements[1].c_str (), NULL, 10); int count = 0; @@ -329,8 +328,7 @@ bool getDOM (const std::string& name, const Task& task, Variant& value) if (ref.data.size () && size == 4 && elements[0] == "annotations" && elements[2] == "entry") { - std::map annos; - ref.getAnnotations (annos); + auto annos = ref.getAnnotations (); int a = strtol (elements[1].c_str (), NULL, 10); int count = 0; diff --git a/src/Task.cpp b/src/Task.cpp index 5ed1a2219..a6c82c5e6 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -1081,13 +1081,14 @@ void Task::removeAnnotations () } //////////////////////////////////////////////////////////////////////////////// -void Task::getAnnotations (std::map & annotations) const +std::map Task::getAnnotations () const { - annotations.clear (); - + std::map a; for (auto& ann : data) if (! ann.first.compare (0, 11, "annotation_", 11)) - annotations.insert (ann); + a.insert (ann); + + return a; } //////////////////////////////////////////////////////////////////////////////// @@ -1357,8 +1358,7 @@ void Task::substitute ( // Get the data to modify. std::string description = get ("description"); - std::map annotations; - getAnnotations (annotations); + auto annotations = getAnnotations (); // Count the changes, so we know whether to proceed to annotations, after // modifying description. diff --git a/src/Task.h b/src/Task.h index c64e083e1..72939ad4a 100644 --- a/src/Task.h +++ b/src/Task.h @@ -129,7 +129,7 @@ public: void removeTag (const std::string&); bool hasAnnotations () const; - void getAnnotations (std::map &) const; + std::map getAnnotations () const; void setAnnotations (const std::map &); void addAnnotation (const std::string&); void removeAnnotations (); diff --git a/src/Variant.cpp b/src/Variant.cpp index 0bcdbe898..aac28b53e 100644 --- a/src/Variant.cpp +++ b/src/Variant.cpp @@ -874,10 +874,7 @@ bool Variant::operator_match (const Variant& other, const Task& task) const // in the annotations. if (left.source () == "description") { - std::map annotations; - task.getAnnotations (annotations); - - for (auto& a : annotations) + for (auto& a : task.getAnnotations ()) if (r.match (a.second)) return true; } @@ -909,10 +906,7 @@ bool Variant::operator_match (const Variant& other, const Task& task) const // in the annotations. if (left.source () == "description") { - std::map annotations; - task.getAnnotations (annotations); - - for (auto& a : annotations) + for (auto& a : task.getAnnotations ()) if (find (a.second, pattern, searchCaseSensitive) != std::string::npos) return true; } diff --git a/src/columns/ColDescription.cpp b/src/columns/ColDescription.cpp index 7706c861c..db7fa387c 100644 --- a/src/columns/ColDescription.cpp +++ b/src/columns/ColDescription.cpp @@ -102,9 +102,7 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int if (min_anno > minimum) minimum = min_anno; - std::map annos; - task.getAnnotations (annos); - for (auto& i : annos) + for (auto& i : task.getAnnotations ()) { unsigned int len = min_anno + 1 + utf8_width (i.second); if (len > maximum) @@ -129,9 +127,7 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int if (task.annotation_count) { auto min_anno = Datetime::length (_dateformat); - std::map annos; - task.getAnnotations (annos); - for (auto& i : annos) + for (auto& i : task.getAnnotations ()) maximum += min_anno + 1 + utf8_width (i.second); } } @@ -179,9 +175,7 @@ void ColumnDescription::render ( { if (task.annotation_count) { - std::map annos; - task.getAnnotations (annos); - for (const auto& i : annos) + for (const auto& i : task.getAnnotations ()) { Datetime dt (strtol (i.first.substr (11).c_str (), NULL, 10)); description += '\n' + std::string (_indent, ' ') + dt.toString (_dateformat) + ' ' + i.second; @@ -210,9 +204,7 @@ void ColumnDescription::render ( { if (task.annotation_count) { - std::map annos; - task.getAnnotations (annos); - for (const auto& i : annos) + for (const auto& i : task.getAnnotations ()) { Datetime dt (strtol (i.first.substr (11).c_str (), NULL, 10)); description += ' ' + dt.toString (_dateformat) + ' ' + i.second; diff --git a/src/commands/CmdDenotate.cpp b/src/commands/CmdDenotate.cpp index 7bd040aa5..3b9fd42db 100644 --- a/src/commands/CmdDenotate.cpp +++ b/src/commands/CmdDenotate.cpp @@ -90,8 +90,7 @@ int CmdDenotate::execute (std::string&) { Task before (task); - std::map annotations; - task.getAnnotations (annotations); + auto annotations = task.getAnnotations (); if (annotations.size () == 0) throw std::string (STRING_CMD_DENO_NONE); diff --git a/src/commands/CmdEdit.cpp b/src/commands/CmdEdit.cpp index a38d6403f..b669286c7 100644 --- a/src/commands/CmdEdit.cpp +++ b/src/commands/CmdEdit.cpp @@ -245,9 +245,7 @@ std::string CmdEdit::formatTask (Task task, const std::string& dateformat) << "# " << STRING_EDIT_HEADER_14 << '\n' << "# " << STRING_EDIT_HEADER_15 << '\n'; - std::map annotations; - task.getAnnotations (annotations); - for (auto& anno : annotations) + for (auto& anno : task.getAnnotations ()) { Datetime dt (strtol (anno.first.substr (11).c_str (), NULL, 10)); before << " Annotation: " << dt.toString (dateformat) diff --git a/src/commands/CmdInfo.cpp b/src/commands/CmdInfo.cpp index 7b8c5a047..3aeb4148a 100644 --- a/src/commands/CmdInfo.cpp +++ b/src/commands/CmdInfo.cpp @@ -134,9 +134,7 @@ int CmdInfo::execute (std::string& output) std::string description = task.get ("description"); int indent = context.config.getInteger ("indent.annotation"); - std::map annotations; - task.getAnnotations (annotations); - for (auto& anno : annotations) + for (auto& anno : task.getAnnotations ()) description += '\n' + std::string (indent, ' ') + Datetime (anno.first.substr (11)).toString (dateformatanno) diff --git a/src/commands/CmdStats.cpp b/src/commands/CmdStats.cpp index 84695e9fd..6942f3ea9 100644 --- a/src/commands/CmdStats.cpp +++ b/src/commands/CmdStats.cpp @@ -140,10 +140,7 @@ int CmdStats::execute (std::string& output) daysPending += (now.toEpoch () - entry) / 86400.0; descLength += task.get ("description").length (); - - std::map annotations; - task.getAnnotations (annotations); - annotationsT += annotations.size (); + annotationsT += task.getAnnotations ().size (); auto tags = task.getTags (); if (tags.size ()) diff --git a/src/commands/CmdTimesheet.cpp b/src/commands/CmdTimesheet.cpp index f9673d294..899a23eef 100644 --- a/src/commands/CmdTimesheet.cpp +++ b/src/commands/CmdTimesheet.cpp @@ -142,9 +142,7 @@ int CmdTimesheet::execute (std::string& output) std::string description = task.get ("description"); int indent = context.config.getInteger ("indent.annotation"); - std::map annotations; - task.getAnnotations (annotations); - for (auto& ann : annotations) + for (auto& ann : task.getAnnotations ()) description += '\n' + std::string (indent, ' ') + Datetime (ann.first.substr (11)).toString (context.config.get ("dateformat")) @@ -198,9 +196,7 @@ int CmdTimesheet::execute (std::string& output) std::string description = task.get ("description"); int indent = context.config.getInteger ("indent.annotation"); - std::map annotations; - task.getAnnotations (annotations); - for (auto& ann : annotations) + for (auto& ann : task.getAnnotations ()) description += '\n' + std::string (indent, ' ') + Datetime (ann.first.substr (11)).toString (context.config.get ("dateformat"))