From 4486c36ea3cb4aca7d5c8131ffd7c2513c715e12 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Senior=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 3 Apr 2018 22:50:43 +0900 Subject: [PATCH] Introduce nnfw::util::env::IntAccessor (#392) This commit introduces nnfw::util::env::IntAccessor which makes it easy to access environmets (for configuration). Signed-off-by: Jonghyun Park --- include/util/environment.h | 32 ++++++++++++++++++++++++++++++++ src/util/src/environment.cpp | 29 +++++++++++++++++++++++++++++ tools/nnapi_unittests/CMakeLists.txt | 1 + tools/nnapi_unittests/lib/env.cpp | 9 ++------- 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/include/util/environment.h b/include/util/environment.h index 256a66c..ba022b4 100644 --- a/include/util/environment.h +++ b/include/util/environment.h @@ -12,4 +12,36 @@ bool get_env_bool(const char* name); } } +#include + +namespace nnfw +{ +namespace util +{ +namespace env +{ + +template struct Accessor +{ + virtual ~Accessor() = default; + + virtual bool access(T &out) const = 0; +}; + +class IntAccessor : public Accessor +{ +public: + IntAccessor(const std::string &tag); + +public: + bool access(int &out) const override; + +private: + std::string _tag; +}; + +} // namespace env +} // namespace util +} // namespace nnfw + #endif // __UTIL_ENVIRONMENT_H__ diff --git a/src/util/src/environment.cpp b/src/util/src/environment.cpp index 1fbfba1..5a4f0a2 100644 --- a/src/util/src/environment.cpp +++ b/src/util/src/environment.cpp @@ -32,3 +32,32 @@ bool get_env_bool(const char* name) } // namespace util } // namespace nnfw + +namespace nnfw +{ +namespace util +{ +namespace env +{ + +IntAccessor::IntAccessor(const std::string &tag) : _tag{tag} +{ + // DO NOTHING +} + +bool IntAccessor::access(int &out) const +{ + auto value = std::getenv(_tag.c_str()); + + if (value == nullptr) + { + return false; + } + + out = std::stoi(value); + return true; +} + +} // namespace env +} // namespace util +} // namespace nnfw diff --git a/tools/nnapi_unittests/CMakeLists.txt b/tools/nnapi_unittests/CMakeLists.txt index e5742a8..5b3b5dc 100644 --- a/tools/nnapi_unittests/CMakeLists.txt +++ b/tools/nnapi_unittests/CMakeLists.txt @@ -2,6 +2,7 @@ file(GLOB_RECURSE NNAPI_UNITTEST_LIB_SOURCES "lib/*.cpp") add_library(nnapi_unittest_common ${NNAPI_UNITTEST_LIB_SOURCES}) target_include_directories(nnapi_unittest_common PUBLIC "inc") +target_link_libraries(nnapi_unittest_common nnfw_util) target_link_libraries(nnapi_unittest_common tensorflow_lite) function(add_nnapi_unittest NAME) diff --git a/tools/nnapi_unittests/lib/env.cpp b/tools/nnapi_unittests/lib/env.cpp index c1f1e92..1638dce 100644 --- a/tools/nnapi_unittests/lib/env.cpp +++ b/tools/nnapi_unittests/lib/env.cpp @@ -1,16 +1,11 @@ #include "env.h" -#include +#include "util/environment.h" // // Integer variable // IntVar::IntVar(const std::string &name, int32_t value) : _value{value} { - auto env = std::getenv(name.c_str()); - - if (env != nullptr) - { - _value = std::atoi(env); - } + nnfw::util::env::IntAccessor{name}.access(value); } -- 2.7.4