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>
--- /dev/null
+#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
--- /dev/null
+#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__
--- /dev/null
+#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);
+}