From patchwork Mon Jan 28 00:41:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 425 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6BD0A60DBF for ; Mon, 28 Jan 2019 01:41:33 +0100 (CET) X-Halon-ID: 67fa1ef5-2295-11e9-911a-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (unknown [89.233.230.99]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id 67fa1ef5-2295-11e9-911a-0050569116f7; Mon, 28 Jan 2019 01:41:10 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 28 Jan 2019 01:41:07 +0100 Message-Id: <20190128004109.25860-5-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128004109.25860-1-niklas.soderlund@ragnatech.se> References: <20190128004109.25860-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 4/6] cam: options: remove OptionsParser::options_ X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Jan 2019 00:41:33 -0000 It's unsafe to keep a pointer to an object inside a vector if one keeps adding and modifying the vectors content. There are also little need to keep the two data structures around when one map can solve the problem. Remove the vector and update all loops to iterate over the map instead of the vector. Signed-off-by: Niklas Söderlund --- src/cam/options.cpp | 18 +++++++++++------- src/cam/options.h | 3 +-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index c9ca017b4cf3fa3d..d3bff1cd897a5cfb 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -30,8 +30,8 @@ bool OptionsParser::addOption(int opt, const char *help, const char *name, if (optionsMap_.find(opt) != optionsMap_.end()) return false; - options_.push_back(Option({ opt, name, argument, argumentName, help })); - optionsMap_[opt] = &options_.back(); + optionsMap_[opt] = Option({ opt, name, argument, argumentName, help }); + return true; } @@ -43,14 +43,16 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv) * Allocate short and long options arrays large enough to contain all * options. */ - char shortOptions[options_.size() * 3 + 2] = {}; - struct option longOptions[options_.size() + 1] = {}; + char shortOptions[optionsMap_.size() * 3 + 2] = {}; + struct option longOptions[optionsMap_.size() + 1] = {}; unsigned int ids = 0; unsigned int idl = 0; shortOptions[ids++] = ':'; - for (const Option &option : options_) { + for (auto const &iter : optionsMap_) { + const Option &option = iter.second; + if (option.hasShortOption()) { shortOptions[ids++] = option.opt; if (option.argument != ArgumentNone) @@ -112,7 +114,8 @@ void OptionsParser::usage() unsigned int indent = 0; - for (const Option &option : options_) { + for (auto const &iter : optionsMap_) { + const Option &option = iter.second; unsigned int length = 14; if (option.hasLongOption()) length += 2 + strlen(option.name); @@ -127,7 +130,8 @@ void OptionsParser::usage() indent = (indent + 7) / 8 * 8; - for (const Option &option : options_) { + for (auto const &iter : optionsMap_) { + const Option &option = iter.second; std::string argument; if (option.hasShortOption()) argument = std::string(" -") diff --git a/src/cam/options.h b/src/cam/options.h index dfb7fcc9f6fa3324..cb7286a0a8005579 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -57,8 +57,7 @@ private: bool hasLongOption() const { return name != nullptr; } }; - std::vector