From patchwork Mon Jan 28 00:41:04 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: 422 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B218360DB6 for ; Mon, 28 Jan 2019 01:41:32 +0100 (CET) X-Halon-ID: 65db9697-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 65db9697-2295-11e9-911a-0050569116f7; Mon, 28 Jan 2019 01:41:07 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 28 Jan 2019 01:41:04 +0100 Message-Id: <20190128004109.25860-2-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 1/6] cam: options: move enum OptionArgument 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 The enumeration of the different possibilities for arguments can be used by other parser then OptionsParser. Move it outside the class to make it ready to be used by other parsers. Signed-off-by: Niklas Söderlund Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/cam/main.cpp | 3 +-- src/cam/options.h | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/cam/main.cpp b/src/cam/main.cpp index cb98d302dcf30331..bde47a8f17983912 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -38,8 +38,7 @@ static int parseOptions(int argc, char *argv[]) OptionsParser parser; parser.addOption(OptCamera, "Specify which camera to operate on", - "camera", OptionsParser::ArgumentRequired, - "camera"); + "camera", ArgumentRequired, "camera"); parser.addOption(OptHelp, "Display this help message", "help"); parser.addOption(OptList, "List all cameras", "list"); diff --git a/src/cam/options.h b/src/cam/options.h index f99ea7300a71c24f..491f6a316fffbe5b 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -11,15 +11,15 @@ #include #include +enum OptionArgument { + ArgumentNone, + ArgumentRequired, + ArgumentOptional, +}; + class OptionsParser { public: - enum OptionArgument { - ArgumentNone, - ArgumentRequired, - ArgumentOptional, - }; - class Options { public: Options(); From patchwork Mon Jan 28 00:41:05 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: 423 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CF3F560B2D for ; Mon, 28 Jan 2019 01:41:32 +0100 (CET) X-Halon-ID: 67576cce-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 67576cce-2295-11e9-911a-0050569116f7; Mon, 28 Jan 2019 01:41:09 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 28 Jan 2019 01:41:05 +0100 Message-Id: <20190128004109.25860-3-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 2/6] cam: options: create a template class for 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 In preparation to adding more parsers create a template class to hold the parsed information. The rational for making it a template are that different parsers can index the options using different data types. The OptionsParser index its options using an int while the upcoming KeyValyeParser will index its options using strings for example. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/cam/options.cpp | 24 ------------------------ src/cam/options.h | 28 ++++++++++++++++------------ 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 83601270207b67b3..b24964a8ce413a85 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -166,27 +166,3 @@ void OptionsParser::usage() } } } - -OptionsParser::Options::Options() -{ -} - -bool OptionsParser::Options::valid() const -{ - return !values_.empty(); -} - -bool OptionsParser::Options::isSet(int opt) const -{ - return values_.find(opt) != values_.end(); -} - -const std::string &OptionsParser::Options::operator[](int opt) const -{ - return values_.find(opt)->second; -} - -void OptionsParser::Options::clear() -{ - values_.clear(); -} diff --git a/src/cam/options.h b/src/cam/options.h index 491f6a316fffbe5b..a08bfea1ba74c96b 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -17,21 +17,25 @@ enum OptionArgument { ArgumentOptional, }; +template +class OptionsBase +{ +public: + bool valid() const { return !values_.empty(); }; + bool isSet(T opt) const { return values_.find(opt) != values_.end(); }; + const std::string &operator[](T opt) const { return values_.find(opt)->second; }; + +private: + friend class OptionsParser; + std::map values_; + void clear() { values_.clear(); }; +}; + class OptionsParser { public: - class Options { - public: - Options(); - - bool valid() const; - bool isSet(int opt) const; - const std::string &operator[](int opt) const; - - private: - friend class OptionsParser; - std::map values_; - void clear(); + class Options : public OptionsBase + { }; void addOption(int opt, const char *help, const char *name = nullptr, From patchwork Mon Jan 28 00:41:06 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: 424 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EC67560DB9 for ; Mon, 28 Jan 2019 01:41:32 +0100 (CET) X-Halon-ID: 67a9eeb8-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 67a9eeb8-2295-11e9-911a-0050569116f7; Mon, 28 Jan 2019 01:41:09 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 28 Jan 2019 01:41:06 +0100 Message-Id: <20190128004109.25860-4-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 3/6] cam: options: return if addOption() succeeds or not 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 To later extend the options handling to cover subparsing of arguments it will be needed to know if the addition of the option itself was successful or not. The information is already present in addOption() this change just makes it available. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/cam/options.cpp | 11 ++++++----- src/cam/options.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index b24964a8ce413a85..c9ca017b4cf3fa3d 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -12,7 +12,7 @@ #include "options.h" -void OptionsParser::addOption(int opt, const char *help, const char *name, +bool OptionsParser::addOption(int opt, const char *help, const char *name, OptionArgument argument, const char *argumentName) { /* @@ -20,18 +20,19 @@ void OptionsParser::addOption(int opt, const char *help, const char *name, * If an argument is accepted, it must be described by argumentName. */ if (!isalnum(opt) && !name) - return; + return false; if (!help || help[0] == '\0') - return; + return false; if (argument != ArgumentNone && !argumentName) - return; + return false; /* Reject duplicate options. */ if (optionsMap_.find(opt) != optionsMap_.end()) - return; + return false; options_.push_back(Option({ opt, name, argument, argumentName, help })); optionsMap_[opt] = &options_.back(); + return true; } OptionsParser::Options OptionsParser::parse(int argc, char **argv) diff --git a/src/cam/options.h b/src/cam/options.h index a08bfea1ba74c96b..dfb7fcc9f6fa3324 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -38,7 +38,7 @@ public: { }; - void addOption(int opt, const char *help, const char *name = nullptr, + bool addOption(int opt, const char *help, const char *name = nullptr, OptionArgument argument = ArgumentNone, const char *argumentName = nullptr); 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