From patchwork Thu Jan 31 23:47:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 463 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 73FE960DC0 for ; Fri, 1 Feb 2019 00:47:37 +0100 (CET) Received: from pendragon.ideasonboard.com (85-76-34-136-nat.elisa-mobile.fi [85.76.34.136]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B04B1324; Fri, 1 Feb 2019 00:47:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1548978457; bh=QXaS7jOZnhNoLqFpuZVS4Ixfx5GhgoGKq/YpMIJT6+k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q4JqPC9/NgL7WDTIKgYXJ+3oiWp83GgKqsC1aDgVPsjQnJgCopMaECBnp1Mcp+7RS fy7UOjuaeeYqVsvOBO3Hs3kWO4C3cz6pMTCu4FXKJoO/c56n3qzK56HsC1Oye85+Si LDZmQVBCRpJ8vGKSFfdL/KKXNiK1gmoPA37ca41w= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 1 Feb 2019 01:47:16 +0200 Message-Id: <20190131234721.22606-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190131234721.22606-1-laurent.pinchart@ideasonboard.com> References: <20190131234721.22606-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/8] 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: Thu, 31 Jan 2019 23:47:37 -0000 From: Niklas Söderlund 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 Signed-off-by: Laurent Pinchart --- Changes since v1: - Don't inline the OptionsBase member functions - Replace class with typename in the OptionsBase template definition - Pass a const T & to isSet() and operator[]() --- src/cam/options.cpp | 50 +++++++++++++++++++++++---------------------- src/cam/options.h | 28 ++++++++++++++----------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 83601270207b..f48bd1fcf1b4 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -12,6 +12,32 @@ #include "options.h" +template +bool OptionsBase::valid() const +{ + return !values_.empty(); +} + +template +bool OptionsBase::isSet(const T &opt) const +{ + return values_.find(opt) != values_.end(); +} + +template +const std::string &OptionsBase::operator[](const T &opt) const +{ + return values_.find(opt)->second; +} + +template +void OptionsBase::clear() +{ + values_.clear(); +} + +template class OptionsBase; + void OptionsParser::addOption(int opt, const char *help, const char *name, OptionArgument argument, const char *argumentName) { @@ -166,27 +192,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 6d1b12d235d8..2bf1f160f3c0 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -28,21 +28,25 @@ struct Option { bool hasLongOption() const { return name != nullptr; } }; +template +class OptionsBase +{ +public: + bool valid() const; + bool isSet(const T &opt) const; + const std::string &operator[](const T &opt) const; + +private: + friend class OptionsParser; + std::map values_; + void 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,