[{"id":719,"web_url":"https://patchwork.libcamera.org/comment/719/","msgid":"<20190131101340.GH4197@pendragon.ideasonboard.com>","date":"2019-01-31T10:13:40","subject":"Re: [libcamera-devel] [PATCH 4/6] cam: options: remove\n\tOptionsParser::options_","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Mon, Jan 28, 2019 at 01:41:07AM +0100, Niklas Söderlund wrote:\n> It's unsafe to keep a pointer to an object inside a vector if one keeps\n> adding and modifying the vectors content.\n\nVery good point.\n\n> There are also little need to keep the two data structures around when\n> one map can solve the problem. Remove the vector and update all loops\n> to iterate over the map instead of the vector.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/cam/options.cpp | 18 +++++++++++-------\n>  src/cam/options.h   |  3 +--\n>  2 files changed, 12 insertions(+), 9 deletions(-)\n> \n> diff --git a/src/cam/options.cpp b/src/cam/options.cpp\n> index c9ca017b4cf3fa3d..d3bff1cd897a5cfb 100644\n> --- a/src/cam/options.cpp\n> +++ b/src/cam/options.cpp\n> @@ -30,8 +30,8 @@ bool OptionsParser::addOption(int opt, const char *help, const char *name,\n>  \tif (optionsMap_.find(opt) != optionsMap_.end())\n>  \t\treturn false;\n>  \n> -\toptions_.push_back(Option({ opt, name, argument, argumentName, help }));\n> -\toptionsMap_[opt] = &options_.back();\n> +\toptionsMap_[opt] = Option({ opt, name, argument, argumentName, help });\n> +\n>  \treturn true;\n>  }\n>  \n> @@ -43,14 +43,16 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv)\n>  \t * Allocate short and long options arrays large enough to contain all\n>  \t * options.\n>  \t */\n> -\tchar shortOptions[options_.size() * 3 + 2] = {};\n> -\tstruct option longOptions[options_.size() + 1] = {};\n> +\tchar shortOptions[optionsMap_.size() * 3 + 2] = {};\n> +\tstruct option longOptions[optionsMap_.size() + 1] = {};\n>  \tunsigned int ids = 0;\n>  \tunsigned int idl = 0;\n>  \n>  \tshortOptions[ids++] = ':';\n>  \n> -\tfor (const Option &option : options_) {\n> +\tfor (auto const &iter : optionsMap_) {\n> +\t\tconst Option &option = iter.second;\n> +\n>  \t\tif (option.hasShortOption()) {\n>  \t\t\tshortOptions[ids++] = option.opt;\n>  \t\t\tif (option.argument != ArgumentNone)\n> @@ -112,7 +114,8 @@ void OptionsParser::usage()\n>  \n>  \tunsigned int indent = 0;\n>  \n> -\tfor (const Option &option : options_) {\n> +\tfor (auto const &iter : optionsMap_) {\n> +\t\tconst Option &option = iter.second;\n\nThis will result in options being sorted base on their value. Do we\nreally want this ?\n\nAn alternative would be to replace the std::vector options_ with an\nstd::list that will not invalidate iterators and references when\ninserting an element at the end.\n\n>  \t\tunsigned int length = 14;\n>  \t\tif (option.hasLongOption())\n>  \t\t\tlength += 2 + strlen(option.name);\n> @@ -127,7 +130,8 @@ void OptionsParser::usage()\n>  \n>  \tindent = (indent + 7) / 8 * 8;\n>  \n> -\tfor (const Option &option : options_) {\n> +\tfor (auto const &iter : optionsMap_) {\n> +\t\tconst Option &option = iter.second;\n>  \t\tstd::string argument;\n>  \t\tif (option.hasShortOption())\n>  \t\t\targument = std::string(\"  -\")\n> diff --git a/src/cam/options.h b/src/cam/options.h\n> index dfb7fcc9f6fa3324..cb7286a0a8005579 100644\n> --- a/src/cam/options.h\n> +++ b/src/cam/options.h\n> @@ -57,8 +57,7 @@ private:\n>  \t\tbool hasLongOption() const { return name != nullptr; }\n>  \t};\n>  \n> -\tstd::vector<Option> options_;\n> -\tstd::map<unsigned int, Option *> optionsMap_;\n> +\tstd::map<unsigned int, Option> optionsMap_;\n\nYou can rename optionsMap_ to options_, it will simplify the patch a\nbit.\n\n>  };\n>  \n>  #endif /* __CAM_OPTIONS_H__ */","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F2E2F60B10\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 31 Jan 2019 11:13:46 +0100 (CET)","from pendragon.ideasonboard.com (85-76-34-136-nat.elisa-mobile.fi\n\t[85.76.34.136])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8968E41;\n\tThu, 31 Jan 2019 11:13:45 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1548929626;\n\tbh=3V0UL+ElRykOiUu5wvnmOfCGTU99IgVDkRoq6eo/zwU=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=OWVXNulop4i5GOb4/p+/VFzIb3N/g2/kedOi9P7f79LbzkKOb0F4qNrk/cB52DBq+\n\tkfx30ZpQtGsnI5GfqNQiSEcItXpbld5fwzMASSvhvmM25zqxDaHjQfUBmwpzgIM2fp\n\tnvwrbtXwoY6/G6zLGVeHl0BY9Px8sejZm6Gn4O2E=","Date":"Thu, 31 Jan 2019 12:13:40 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190131101340.GH4197@pendragon.ideasonboard.com>","References":"<20190128004109.25860-1-niklas.soderlund@ragnatech.se>\n\t<20190128004109.25860-5-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190128004109.25860-5-niklas.soderlund@ragnatech.se>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 4/6] cam: options: remove\n\tOptionsParser::options_","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Thu, 31 Jan 2019 10:13:47 -0000"}}]