[enco] Introduce Global vairable manager (#1140)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 23 Aug 2018 01:07:26 +0000 (10:07 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 23 Aug 2018 01:07:26 +0000 (10:07 +0900)
This commit introduces Global class which allows us to declare global
variables with its initial value.

The current implemenation supports global string decleration.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/src/CppGen/Global.cpp [new file with mode: 0644]
contrib/enco/core/src/CppGen/Global.h [new file with mode: 0644]
contrib/enco/core/src/CppGen/Global.test.cpp [new file with mode: 0644]

diff --git a/contrib/enco/core/src/CppGen/Global.cpp b/contrib/enco/core/src/CppGen/Global.cpp
new file mode 100644 (file)
index 0000000..5b9e134
--- /dev/null
@@ -0,0 +1,17 @@
+#include "Global.h"
+
+#include <pp/Format.h>
+
+namespace enco
+{
+
+std::string Global::constant(const std::string &s)
+{
+  auto name = pp::fmt("g_", _count++);
+
+  _content.append("const char *", name, " = \"", s, "\";");
+
+  return name;
+}
+
+} // namespace enco
diff --git a/contrib/enco/core/src/CppGen/Global.h b/contrib/enco/core/src/CppGen/Global.h
new file mode 100644 (file)
index 0000000..f23a913
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef __ENCO_GLOBAL_H__
+#define __ENCO_GLOBAL_H__
+
+#include <pp/LinearDocument.h>
+
+#include <cstdint>
+#include <string>
+
+namespace enco
+{
+
+/**
+ * @brief Manage global variable declarations
+ */
+class Global
+{
+public:
+  // @brief Create a global constant string (const char *) literal, and return variable name
+  std::string constant(const std::string &value);
+
+public:
+  const pp::MultiLineText &content(void) const { return _content; }
+
+private:
+  uint32_t _count = 0;
+  pp::LinearDocument _content;
+};
+
+} // namespace enco
+
+#endif // __ENCO_GLOBAL_H__
diff --git a/contrib/enco/core/src/CppGen/Global.test.cpp b/contrib/enco/core/src/CppGen/Global.test.cpp
new file mode 100644 (file)
index 0000000..f2e3c62
--- /dev/null
@@ -0,0 +1,17 @@
+#include "Global.h"
+
+#include <set>
+
+#include <gtest/gtest.h>
+
+TEST(GLOBAL, distinct_id)
+{
+  enco::Global global;
+
+  std::set<std::string> ids;
+
+  ids.insert(global.constant("Hello"));
+  ids.insert(global.constant("Nice to meet you"));
+
+  ASSERT_EQ(ids.size(), 2);
+}