From 5da3bbed6e923ef7816c41222ba1e4193e32dcf6 Mon Sep 17 00:00:00 2001 From: Jihoon Lee Date: Wed, 1 Dec 2021 17:53:23 +0900 Subject: [PATCH] [Header] Remove nntrainer_log.h from app_context.h This patch removes nntrainer_log.h from app_context.h and implement additional safecheck **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Jihoon Lee --- nntrainer/app_context.cpp | 56 ++++++++++++++++++++++++++++++++++++ nntrainer/app_context.h | 73 ++++++++++++++++++++++++----------------------- 2 files changed, 93 insertions(+), 36 deletions(-) diff --git a/nntrainer/app_context.cpp b/nntrainer/app_context.cpp index 7a52e70..66c930f 100644 --- a/nntrainer/app_context.cpp +++ b/nntrainer/app_context.cpp @@ -21,8 +21,10 @@ #include #include +#include #include #include +#include #include #include @@ -484,4 +486,58 @@ AppContext::registerPluggableFromDirectory(const std::string &base_path) { return keys; } +template +const int AppContext::registerFactory(const FactoryType factory, + const std::string &key, + const int int_key) { + static_assert(isSupported::value, + "given type is not supported for current app context"); + + auto &index = std::get>(factory_map); + auto &str_map = std::get>(index); + auto &int_map = std::get(index); + + std::string assigned_key = key == "" ? factory({})->getType() : key; + + std::transform(assigned_key.begin(), assigned_key.end(), assigned_key.begin(), + [](unsigned char c) { return std::tolower(c); }); + + const std::lock_guard lock(factory_mutex); + if (str_map.find(assigned_key) != str_map.end()) { + std::stringstream ss; + ss << "cannot register factory with already taken key: " << key; + throw std::invalid_argument(ss.str().c_str()); + } + + if (int_key != -1 && int_map.find(int_key) != int_map.end()) { + std::stringstream ss; + ss << "cannot register factory with already taken int key: " << int_key; + throw std::invalid_argument(ss.str().c_str()); + } + + int assigned_int_key = int_key == -1 ? str_map.size() + 1 : int_key; + + str_map[assigned_key] = factory; + int_map[assigned_int_key] = assigned_key; + + ml_logd("factory has registered with key: %s, int_key: %d", + assigned_key.c_str(), assigned_int_key); + + return assigned_int_key; +} + +/** + * @copydoc const int AppContext::registerFactory + */ +template const int AppContext::registerFactory( + const FactoryType factory, const std::string &key, + const int int_key); + +/** + * @copydoc const int AppContext::registerFactory + */ +template const int AppContext::registerFactory( + const FactoryType factory, const std::string &key, + const int int_key); + } // namespace nntrainer diff --git a/nntrainer/app_context.h b/nntrainer/app_context.h index 105ad8c..9b14b5d 100644 --- a/nntrainer/app_context.h +++ b/nntrainer/app_context.h @@ -20,7 +20,9 @@ #include #include #include +#include #include +#include #include #include @@ -29,11 +31,11 @@ #include #include -#include namespace nntrainer { extern std::mutex factory_mutex; +namespace {} // namespace /** * @class AppContext contains user-dependent configuration @@ -188,41 +190,7 @@ public: template const int registerFactory(const FactoryType factory, const std::string &key = "", - const int int_key = -1) { - - auto &index = std::get>(factory_map); - auto &str_map = std::get>(index); - auto &int_map = std::get(index); - - std::string assigned_key = key == "" ? factory({})->getType() : key; - - std::transform(assigned_key.begin(), assigned_key.end(), - assigned_key.begin(), - [](unsigned char c) { return std::tolower(c); }); - - const std::lock_guard lock(factory_mutex); - if (str_map.find(assigned_key) != str_map.end()) { - std::stringstream ss; - ss << "cannot register factory with already taken key: " << key; - throw std::invalid_argument(ss.str().c_str()); - } - - if (int_key != -1 && int_map.find(int_key) != int_map.end()) { - std::stringstream ss; - ss << "cannot register factory with already taken int key: " << int_key; - throw std::invalid_argument(ss.str().c_str()); - } - - int assigned_int_key = int_key == -1 ? str_map.size() + 1 : int_key; - - str_map[assigned_key] = factory; - int_map[assigned_int_key] = assigned_key; - - ml_logd("factory has registered with key: %s, int_key: %d", - assigned_key.c_str(), assigned_int_key); - - return assigned_int_key; - } + const int int_key = -1); /** * @brief Create an Object from the integer key @@ -235,6 +203,8 @@ public: template PtrType createObject(const int int_key, const PropsType &props = {}) const { + static_assert(isSupported::value, + "given type is not supported for current app context"); auto &index = std::get>(factory_map); auto &int_map = std::get(index); @@ -295,8 +265,39 @@ public: private: FactoryMap factory_map; std::string working_path_base; + + template struct isSupportedHelper; + + /** + * @brief supportHelper to check if given type is supported within appcontext + */ + template + struct isSupportedHelper> { + static constexpr bool value = + (std::is_same_v, std::decay_t> || ...); + }; + + /** + * @brief supportHelper to check if given type is supported within appcontext + */ + template + struct isSupported : isSupportedHelper {}; }; +/** + * @copydoc const int AppContext::registerFactory + */ +extern template const int AppContext::registerFactory( + const FactoryType factory, const std::string &key, + const int int_key); + +/** + * @copydoc const int AppContext::registerFactory + */ +extern template const int AppContext::registerFactory( + const FactoryType factory, const std::string &key, + const int int_key); + namespace plugin {} } // namespace nntrainer -- 2.7.4