Introduce 'nncc_foundation' (#93)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 19 Apr 2018 02:04:28 +0000 (11:04 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 19 Apr 2018 02:04:28 +0000 (11:04 +0900)
This commit introduces 'nncc_foundation' library which includes
functions and classes not directly related with compiler itself, but
useful when implementing it.

The initial version includes the implementation of 'make_unique' which
makes it easy to use 'std::unique_ptr'.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
libs/CMakeLists.txt
libs/foundation/CMakeLists.txt [new file with mode: 0644]
libs/foundation/include/nncc/foundation/Memory.h [new file with mode: 0644]
libs/foundation/src/Memory.test.cpp [new file with mode: 0644]

index ec62eec..9583add 100644 (file)
@@ -1,2 +1,3 @@
+add_subdirectory(foundation)
 add_subdirectory(core)
 add_subdirectory(frontend)
diff --git a/libs/foundation/CMakeLists.txt b/libs/foundation/CMakeLists.txt
new file mode 100644 (file)
index 0000000..9d7169c
--- /dev/null
@@ -0,0 +1,11 @@
+file(GLOB_RECURSE HEADERS "include/*.h")
+file(GLOB_RECURSE SOURCES "src/*.cpp")
+file(GLOB_RECURSE TESTS "src/*.test.cpp")
+list(REMOVE_ITEM SOURCES ${TESTS})
+
+add_nncc_library(nncc_foundation SHARED ${HEADERS} ${SOURCES})
+set_target_properties(nncc_foundation PROPERTIES LINKER_LANGUAGE CXX)
+target_include_directories(nncc_foundation PUBLIC include)
+
+add_nncc_test(nncc_foundation_test ${TESTS})
+nncc_test_link_libraries(nncc_foundation_test nncc_foundation)
diff --git a/libs/foundation/include/nncc/foundation/Memory.h b/libs/foundation/include/nncc/foundation/Memory.h
new file mode 100644 (file)
index 0000000..5ba7a6a
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef __NNCC_FOUNDATION_MEMORY_H__
+#define __NNCC_FOUNDATION_MEMORY_H__
+
+#include <memory>
+
+namespace nncc
+{
+namespace foundation
+{
+
+template <typename T, typename... Args> std::unique_ptr<T> make_unique(Args &&... args)
+{
+  // NOTE std::make_unique is missing in C++11 standard
+  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+} // namespace foundation
+} // namespace nncc
+
+#endif // __NNCC_FOUNDATION_MEMORY_H__
diff --git a/libs/foundation/src/Memory.test.cpp b/libs/foundation/src/Memory.test.cpp
new file mode 100644 (file)
index 0000000..29043cd
--- /dev/null
@@ -0,0 +1,30 @@
+#include <nncc/foundation/Memory.h>
+
+#include <gtest/gtest.h>
+
+class Tracker
+{
+public:
+  Tracker(bool &allocated, bool &freed) : _freed{freed} { allocated = true; }
+
+public:
+  ~Tracker() { _freed = true; }
+
+private:
+  bool &_freed;
+};
+
+TEST(FOUNDATION_MEMORY, unique_ptr)
+{
+  bool allocated = false;
+  bool freed = false;
+
+  {
+    auto o = nncc::foundation::make_unique<Tracker>(allocated, freed);
+
+    ASSERT_TRUE(allocated);
+    ASSERT_FALSE(freed);
+  }
+
+  ASSERT_TRUE(freed);
+}