From: 이한종/동작제어Lab(SR)/Engineer/삼성전자 Date: Fri, 6 Jul 2018 02:09:14 +0000 (+0900) Subject: Introduce EnvVar class (#1884) X-Git-Tag: 0.2~501 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=61026418d7794b47543366dca1c1961603780c99;p=platform%2Fcore%2Fml%2Fnnfw.git Introduce EnvVar class (#1884) * Introduce EnvVar class EnvVar reads environment variables from system and can return the value as string, bool or int. Signed-off-by: Hanjoung Lee * Move EnvVar.h to nnfw/include/util Move the file and add namespace Signed-off-by: Hanjoung Lee --- diff --git a/include/util/EnvVar.h b/include/util/EnvVar.h new file mode 100644 index 0000000..ec18678 --- /dev/null +++ b/include/util/EnvVar.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NNFW_UTIL_ENV_VAR__ +#define __NNFW_UTIL_ENV_VAR__ + +#include +#include +#include + + +namespace nnfw +{ +namespace util +{ + +class EnvVar +{ +public: + EnvVar(const std::string &key) + { + const char *value = std::getenv(key.c_str()); + if (value == nullptr) + { + // An empty string is considered as an empty value + _value = ""; + } + else + { + _value = value; + } + } + + std::string asString(const std::string &def) const + { + if (_value.empty()) + return def; + return _value; + } + + bool asBool(bool def) const + { + if (_value.empty()) + return def; + static const std::array false_list{"0", "OFF", "FALSE", "N", "NO"}; + return std::find(false_list.begin(), false_list.end(), _value); + } + + int asInt(int def) const + { + if (_value.empty()) + return def; + return std::stoi(_value); + } + +private: + std::string _value; +}; + +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_ENV_VAR__