[Props] Separate base_propeties to a source
authorJihoon Lee <jhoon.it.lee@samsung.com>
Mon, 3 May 2021 05:08:47 +0000 (14:08 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 11 May 2021 06:02:21 +0000 (15:02 +0900)
This patch separate base_properties into some specialization to source
file

The purpose is to save compile time and redundant doxygen, and clean up
length header.

**Self evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test: [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
jni/Android.mk
nntrainer/utils/base_properties.cpp [new file with mode: 0644]
nntrainer/utils/base_properties.h
nntrainer/utils/meson.build

index 21d475a..dbe1b1e 100644 (file)
@@ -132,6 +132,7 @@ NNTRAINER_SRCS := $(NNTRAINER_ROOT)/nntrainer/models/neuralnet.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/utils/parse_util.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/utils/profiler.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/utils/node_exporter.cpp \
+                  $(NNTRAINER_ROOT)/nntrainer/utils/base_properties.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/compiler/ini_interpreter.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/compiler/tflite_interpreter.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/app_context.cpp
diff --git a/nntrainer/utils/base_properties.cpp b/nntrainer/utils/base_properties.cpp
new file mode 100644 (file)
index 0000000..6443541
--- /dev/null
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file base_properties.cpp
+ * @date 03 May 2021
+ * @brief Convenient property type definition for automated serialization
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+#include <base_properties.h>
+
+#include <string>
+
+namespace nntrainer {
+
+template <>
+std::string
+str_converter<str_prop_tag, std::string>::to_string(const std::string &value) {
+  return value;
+}
+
+template <>
+std::string str_converter<str_prop_tag, std::string>::from_string(
+  const std::string &value) {
+  return value;
+}
+
+template <>
+std::string str_converter<int_prop_tag, int>::to_string(const int &value) {
+  return std::to_string(value);
+}
+
+template <>
+int str_converter<int_prop_tag, int>::from_string(const std::string &value) {
+  return std::stoi(value);
+}
+
+template <>
+std::string str_converter<uint_prop_tag, unsigned int>::to_string(
+  const unsigned int &value) {
+  return std::to_string(value);
+}
+
+template <>
+unsigned int str_converter<uint_prop_tag, unsigned int>::from_string(
+  const std::string &value) {
+  return std::stoul(value);
+}
+} // namespace nntrainer
index 22bfa9a..5b9d5ed 100644 (file)
@@ -9,8 +9,7 @@
  * @author Jihoon Lee <jhoon.it.lee@samsung.com>
  * @bug No known bugs except for NYI items
  */
-#include <iostream>
-#include <sstream>
+#include <nntrainer_error.h>
 #include <string>
 
 #ifndef __BASE_PROPERTIES_H__
@@ -64,7 +63,7 @@ struct double_prop_tag {};
 struct str_prop_tag {};
 
 /**
- * @brief base property class, inherit this to make a convinient property
+ * @brief base property class, inherit this to make a convenient property
  *
  * @tparam T
  */
@@ -112,9 +111,8 @@ public:
    * @throw std::invalid_argument if argument is not valid
    */
   void set(const T &v) {
-    if (!is_valid(v)) {
-      throw std::invalid_argument("argument is not valid");
-    }
+    NNTR_THROW_IF(is_valid(v) == false, std::invalid_argument)
+      << "argument is not valid";
     value = v;
   }
 
@@ -267,84 +265,25 @@ struct tag_cast<Tag, BaseTag, Others...> {
  * This structure defines how to convert to convert from/to string
  *
  * @tparam Tag tag type for the converter
+ * @tparam DataType underlying datatype
  */
-template <typename Tag> struct str_converter {};
-
-/**
- * @brief struct converter
- *
- * @copydoc template<typename Tag> struct_converter;
- */
-template <> struct str_converter<str_prop_tag> {
-
-  /**
-   * @brief string converter to string
-   *
-   * @param value value to convert
-   * @return std::string to_string
-   */
-  static std::string to_string(const std::string &value) { return value; }
-
-  /**
-   * @brief string converter from string
-   *
-   * @param str value to convert
-   * @return std::string converted value
-   */
-  static std::string from_string(const std::string &str) { return str; }
-};
-
-/**
- * @brief struct converter
- *
- * @copydoc template<typename Tag> struct_converter;
- */
-template <> struct str_converter<int_prop_tag> {
-  /**
-   * @brief string converter to string
-   *
-   * @param value value to convert
-   * @return std::string to_string
-   */
-  static std::string to_string(const int value) {
-    return std::to_string(value);
-  }
+template <typename Tag, typename DataType> struct str_converter {
 
   /**
-   * @brief string converter from string
+   * @brief convert underlying value to string
    *
-   * @param str value to convert
-   * @return std::string converted value
+   * @param value value to convert to string
+   * @return std::string string
    */
-  static int from_string(const std::string &value) { return std::stoi(value); }
-};
-
-/**
- * @brief struct converter
- *
- * @copydoc template<typename Tag> struct_converter;
- */
-template <> struct str_converter<uint_prop_tag> {
+  static std::string to_string(const DataType &value);
 
   /**
-   * @brief string converter to string
+   * @brief convert string to underlying value
    *
-   * @param value value to convert
-   * @return std::string to_string
+   * @param value value to convert to string
+   * @return DataType converted type
    */
-  static std::string to_string(const unsigned int value) {
-    return std::to_string(value);
-  }
-
-  /**
-   * @brief string converter from string
-   *
-   * @param str value to convert
-   * @return std::string converted value
-   */
-  static unsigned int from_string(const std::string &value) {
-    return std::stoul(value);
-  }
+  static DataType from_string(const std::string &value);
 };
 
 /**
@@ -359,7 +298,10 @@ template <typename T> std::string to_string(const T &property) {
     typename tag_cast<typename prop_tag<T>::type, int_prop_tag, uint_prop_tag,
                       vector_prop_tag, dimension_prop_tag, double_prop_tag,
                       str_prop_tag>::type;
-  return str_converter<tag_type>::to_string(property.get());
+
+  using data_type = std::remove_cv_t<
+    std::remove_reference_t<decltype(std::declval<T>().get())>>;
+  return str_converter<tag_type, data_type>::to_string(property.get());
 }
 
 /**
@@ -374,7 +316,11 @@ template <typename T> void from_string(const std::string &str, T &property) {
     typename tag_cast<typename prop_tag<T>::type, int_prop_tag, uint_prop_tag,
                       vector_prop_tag, dimension_prop_tag, double_prop_tag,
                       str_prop_tag>::type;
-  property.set(str_converter<tag_type>::from_string(str));
+
+  using data_type = std::remove_cv_t<
+    std::remove_reference_t<decltype(std::declval<T>().get())>>;
+
+  property.set(str_converter<tag_type, data_type>::from_string(str));
 }
 
 } // namespace nntrainer
index 5c4cbe2..b6eb05f 100644 (file)
@@ -3,7 +3,8 @@ util_sources = [
   'util_func.cpp',
   'profiler.cpp',
   'ini_wrapper.cpp',
-  'node_exporter.cpp'
+  'node_exporter.cpp',
+  'base_properties.cpp'
 ]
 
 util_headers = [