From f39f8d8597c59057118f67cacf70d246f95fea9b Mon Sep 17 00:00:00 2001 From: Jan Eilers Date: Tue, 26 Oct 2021 16:57:34 +0100 Subject: Move command line parsing in external delegate to DelegateOptions * Moves the creation of a DelegateOption object from armnn_external_delegate to DelegateOptions. * This allows this code to be reused elsewhere * Allow boolean values of DelegateOptions to be passed as strings e.g. 'true' or 'false' * Add unit tests Signed-off-by: Jan Eilers Change-Id: I0ada17f511027dd3f47a85142cae346464682f5a --- include/armnn/utility/StringUtils.hpp | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'include/armnn') diff --git a/include/armnn/utility/StringUtils.hpp b/include/armnn/utility/StringUtils.hpp index cc5e8e7349..172b1798c5 100644 --- a/include/armnn/utility/StringUtils.hpp +++ b/include/armnn/utility/StringUtils.hpp @@ -7,6 +7,9 @@ #include #include +#include +#include +#include namespace armnn { @@ -115,6 +118,47 @@ inline void StringReplaceAll(std::string& str, } } +/// +/// Converts a string to bool. +/// Accepts "true", "false" (case-insensitive) and numbers, 1 (true) or 0 (false). +/// +/// \param s String to convert to bool +/// \param throw_on_error Bool variable to suppress error if conversion failed (Will return false in that case) +/// \return bool value +/// +inline bool StringToBool(const std::string& s, bool throw_on_error = true) +{ + // in case of failure to convert returns false + auto result = false; + + // isstringstream fails if parsing didn't work + std::istringstream is(s); + + // try integer conversion first. For the case s is a number + is >> result; + + if (is.fail()) + { + // transform to lower case to make case-insensitive + std::string s_lower = s; + std::transform(s_lower.begin(), + s_lower.end(), + s_lower.begin(), + [](unsigned char c){ return std::tolower(c); }); + is.str(s_lower); + // try boolean -> s="false" or "true" + is.clear(); + is >> std::boolalpha >> result; + } + + if (is.fail() && throw_on_error) + { + throw armnn::InvalidArgumentException(s + " is not convertable to bool"); + } + + return result; +} + } // namespace stringUtils } // namespace armnn \ No newline at end of file -- cgit v1.2.1