From 5307bc10ac488261e84ac76b2dede6039ea3fe96 Mon Sep 17 00:00:00 2001 From: telsoa01 Date: Fri, 9 Mar 2018 13:51:08 +0000 Subject: Release 18.02 Change-Id: I41a89c149534a7c354a58e2c66a32cba572fc0c1 --- SystemPropertiesUtils.hpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 SystemPropertiesUtils.hpp (limited to 'SystemPropertiesUtils.hpp') diff --git a/SystemPropertiesUtils.hpp b/SystemPropertiesUtils.hpp new file mode 100644 index 00000000..57aa98ca --- /dev/null +++ b/SystemPropertiesUtils.hpp @@ -0,0 +1,83 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// See LICENSE file in the project root for full license information. +// + +#pragma once + +#include +#include +#include +#include + +namespace { +template +struct ConvStringTo; + +template<> +struct ConvStringTo +{ + static float Func(std::string s) { return std::stof(s); } +}; + +template<> +struct ConvStringTo +{ + static int Func(std::string s) { return std::stoi(s); } +}; + +template<> +struct ConvStringTo +{ + static bool Func(std::string s) { return !!std::stoi(s); } +}; + +template +void GetCapabilitiesProperties([[maybe_unused]]void* cookie, + [[maybe_unused]]const char *name, + [[maybe_unused]]const char *value, + [[maybe_unused]]uint32_t serial) +{ + T &prop = *reinterpret_cast(cookie); + prop = ConvStringTo::Func(std::string(value)); +} + +template +T ParseSystemProperty(const char* name, T defaultValue) +{ + try + { + const prop_info *pInfo = __system_property_find(name); + if (!pInfo) + { + ALOGW("ArmnnDriver::ParseSystemProperty(): Could not find property [%s].", name); + } else + { + T property; + __system_property_read_callback(pInfo, &GetCapabilitiesProperties, &property); + std::stringstream messageBuilder; + messageBuilder << "ArmnnDriver::ParseSystemProperty(): Setting [" << name << "]=[" << property << "]."; + ALOGD("%s", messageBuilder.str().c_str()); + return property; + } + } + catch(const std::invalid_argument& e) + { + ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] has invalid data type.", name); + } + catch(const std::out_of_range& e) + { + ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] out of range for the data type.", name); + } + catch (...) + { + ALOGD("ArmnnDriver::ParseSystemProperty(): Unexpected exception reading system " + "property [%s].", name); + } + + std::stringstream messageBuilder; + messageBuilder << "ArmnnDriver::ParseSystemProperty(): Falling back to default value [" << defaultValue << "]"; + ALOGD("%s", messageBuilder.str().c_str()); + return defaultValue; +} +} //namespace \ No newline at end of file -- cgit v1.2.1