Introduce nnfw::util::env::IntAccessor (#392)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 3 Apr 2018 13:50:43 +0000 (22:50 +0900)
committer이성재/동작제어Lab(SR)/Principal Engineer/삼성전자 <sj925.lee@samsung.com>
Tue, 3 Apr 2018 13:50:43 +0000 (22:50 +0900)
This commit introduces nnfw::util::env::IntAccessor which makes it easy
to access environmets (for configuration).

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
include/util/environment.h
src/util/src/environment.cpp
tools/nnapi_unittests/CMakeLists.txt
tools/nnapi_unittests/lib/env.cpp

index 256a66c..ba022b4 100644 (file)
@@ -12,4 +12,36 @@ bool get_env_bool(const char* name);
 }
 }
 
+#include <string>
+
+namespace nnfw
+{
+namespace util
+{
+namespace env
+{
+
+template <typename T> struct Accessor
+{
+  virtual ~Accessor() = default;
+
+  virtual bool access(T &out) const = 0;
+};
+
+class IntAccessor : public Accessor<int>
+{
+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__
index 1fbfba1..5a4f0a2 100644 (file)
@@ -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
index e5742a8..5b3b5dc 100644 (file)
@@ -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)
index c1f1e92..1638dce 100644 (file)
@@ -1,16 +1,11 @@
 #include "env.h"
 
-#include <cstdlib>
+#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);
 }