From 754874dcf305069993f7d2d44ce9470707141f2d Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Wed, 18 Oct 2017 13:00:16 +0200 Subject: [PATCH] More explicit use of "Optional". --- src/Options.cpp | 17 +++++++++++++---- src/Options.hpp | 9 ++++++--- src/PNGDumper.hpp | 2 ++ src/main.cpp | 25 +++++++++---------------- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index bb04560..459ea97 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -3,6 +3,7 @@ #include #include #include "Options.hpp" +#include "utils.hpp" using namespace fmri; using namespace std; @@ -127,12 +128,20 @@ const string& Options::means() const return meansPath; } -const string& Options::labels() const +optional> Options::labels() const { - return labelsPath; + if (labelsPath.empty()) { + return nullopt; + } else { + return read_vector(labelsPath); + } } -const string &Options::imageDump() const +std::optional Options::imageDumper() const { - return dumpPath; + if (dumpPath.empty()) { + return nullopt; + } else { + return move(PNGDumper(dumpPath)); + } } diff --git a/src/Options.hpp b/src/Options.hpp index 296e4e3..570ccfa 100644 --- a/src/Options.hpp +++ b/src/Options.hpp @@ -1,7 +1,10 @@ #pragma once -#include +#include #include +#include + +#include "PNGDumper.hpp" namespace fmri { @@ -15,8 +18,8 @@ namespace fmri { const string& model() const; const string& weights() const; const string& means() const; - const string& labels() const; - const string& imageDump() const; + std::optional> labels() const; + std::optional imageDumper() const; const vector& inputs() const; diff --git a/src/PNGDumper.hpp b/src/PNGDumper.hpp index 9d4fa7f..b8bc3be 100644 --- a/src/PNGDumper.hpp +++ b/src/PNGDumper.hpp @@ -15,6 +15,8 @@ namespace fmri { public: PNGDumper(string_view baseDir); + PNGDumper(PNGDumper&&) = default; + PNGDumper(const PNGDumper&) = default; void dump(const LayerData& layerData); diff --git a/src/main.cpp b/src/main.cpp index f1b3154..a147171 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,9 @@ #include #include -#include #include #include "Options.hpp" #include "Simulator.hpp" -#include "PNGDumper.hpp" using namespace std; using namespace fmri; @@ -14,15 +12,8 @@ int main(int argc, char *const argv[]) { google::InitGoogleLogging(argv[0]); Options options = Options::parse(argc, argv); - vector labels; - if (!options.labels().empty()) { - labels = read_vector(options.labels()); - } - - unique_ptr pngDumper; - if (!options.imageDump().empty()) { - pngDumper.reset(new PNGDumper(options.imageDump())); - } + auto labels = options.labels(); + auto dumper = options.imageDumper(); Simulator simulator(options.model(), options.weights(), options.means()); @@ -31,9 +22,9 @@ int main(int argc, char *const argv[]) { LOG(INFO) << "Result for " << image << ":" << endl; const auto& resultRow = res[res.size() - 1]; - if (!labels.empty()) { + if (labels) { vector weights(resultRow.data(), resultRow.data() + resultRow.numEntries()); - auto scores = combine(weights, labels); + auto scores = combine(weights, *labels); sort(scores.begin(), scores.end(), greater<>()); for (unsigned int i = 0; i < scores.size() && i < 5; ++i) { LOG(INFO) << scores[i].first << " " << scores[i].second << endl; @@ -42,9 +33,11 @@ int main(int argc, char *const argv[]) { LOG(INFO) << "Best result: " << *(resultRow.data(), resultRow.data() + resultRow.numEntries()) << endl; } - for (auto& layer : res) { - pngDumper->dump(layer); - } + if (dumper) { + for (const auto& layer : res) { + dumper->dump(layer); + } + } } google::ShutdownGoogleLogging();