[enco] Rewrite constant methods using concat (#1337)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 5 Sep 2018 06:19:51 +0000 (15:19 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 5 Sep 2018 06:19:51 +0000 (15:19 +0900)
Currently, constant methods in Global internally implements string concat
operation.

This commit revises these methods to reuse existing concat method.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/src/CppGen/Global.cpp

index 5385899..889eb4f 100644 (file)
@@ -1,7 +1,49 @@
 #include "Global.h"
 
+#include "String.h"
+
 #include <pp/Format.h>
 
+namespace
+{
+
+template <typename Container> std::string concat(const std::string &sep, Container &&c)
+{
+  std::stringstream ss;
+  enco::concat(ss, sep, c.begin(), c.end());
+  return ss.str();
+}
+
+template <typename Callable, typename InputIt>
+auto transform(Callable &&cb, InputIt beg, InputIt end) -> std::vector<decltype(cb(*beg))>
+{
+  using U = decltype(cb(*beg));
+  std::vector<U> res;
+
+  for (auto it = beg; it != end; ++it)
+  {
+    res.emplace_back(cb(*it));
+  }
+
+  return res;
+}
+
+std::string u32_to_dec(uint32_t value)
+{
+  std::stringstream ss;
+  ss << value;
+  return ss.str();
+}
+
+std::string u08_to_hex(uint8_t value)
+{
+  std::stringstream ss;
+  ss << "0x" << std::hex << value + 0;
+  return ss.str();
+}
+
+} // namespace
+
 namespace enco
 {
 
@@ -21,14 +63,7 @@ template <> std::string Global::constant(const std::vector<uint32_t> &values)
   std::stringstream ss;
 
   ss << "const uint32_t " << name << "[" << values.size() << "] = { ";
-  if (values.size() > 0)
-  {
-    ss << values.at(0);
-    for (uint32_t n = 1; n < values.size(); ++n)
-    {
-      ss << ", " << values.at(n);
-    }
-  }
+  ss << ::concat(", ", transform(u32_to_dec, values.begin(), values.end()));
   ss << " };";
 
   _content.append(ss.str());
@@ -43,17 +78,7 @@ std::string Global::constant(const uint8_t *base, uint32_t size)
   std::stringstream ss;
 
   ss << "const uint8_t " << name << "[" << size << "] = { ";
-
-  if (size > 0)
-  {
-    ss << "0x" << std::hex << (base[0] + 0);
-
-    for (uint32_t n = 1; n < size; ++n)
-    {
-      ss << ", 0x" << std::hex << (base[n] + 0);
-    }
-  }
-
+  ss << ::concat(", ", transform(u08_to_hex, base, base + size));
   ss << " };";
 
   _content.append(ss.str());