diff options
author | Jan Eilers <jan.eilers@arm.com> | 2021-10-26 16:57:34 +0100 |
---|---|---|
committer | Jan Eilers <jan.eilers@arm.com> | 2021-11-02 11:49:32 +0000 |
commit | f39f8d8597c59057118f67cacf70d246f95fea9b (patch) | |
tree | 35c6f5b1b641a143d29e1ce478877f9e2b251569 /include/armnn | |
parent | 32c7527df494a99a97283fe09580671e10f8d398 (diff) | |
download | armnn-f39f8d8597c59057118f67cacf70d246f95fea9b.tar.gz |
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 <jan.eilers@arm.com>
Change-Id: I0ada17f511027dd3f47a85142cae346464682f5a
Diffstat (limited to 'include/armnn')
-rw-r--r-- | include/armnn/utility/StringUtils.hpp | 44 |
1 files changed, 44 insertions, 0 deletions
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 <iostream> #include <sstream> +#include <algorithm> +#include <vector> +#include <armnn/Exceptions.hpp> 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 |