From 289035b9a294efd5fbf574e3335e6bb085cbb6d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Senior=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 19 Apr 2018 11:04:28 +0900 Subject: [PATCH] Introduce 'nncc_foundation' (#93) 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 --- libs/CMakeLists.txt | 1 + libs/foundation/CMakeLists.txt | 11 +++++++++ libs/foundation/include/nncc/foundation/Memory.h | 20 ++++++++++++++++ libs/foundation/src/Memory.test.cpp | 30 ++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 libs/foundation/CMakeLists.txt create mode 100644 libs/foundation/include/nncc/foundation/Memory.h create mode 100644 libs/foundation/src/Memory.test.cpp diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt index ec62eec..9583add 100644 --- a/libs/CMakeLists.txt +++ b/libs/CMakeLists.txt @@ -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 index 0000000..9d7169c --- /dev/null +++ b/libs/foundation/CMakeLists.txt @@ -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 index 0000000..5ba7a6a --- /dev/null +++ b/libs/foundation/include/nncc/foundation/Memory.h @@ -0,0 +1,20 @@ +#ifndef __NNCC_FOUNDATION_MEMORY_H__ +#define __NNCC_FOUNDATION_MEMORY_H__ + +#include + +namespace nncc +{ +namespace foundation +{ + +template std::unique_ptr make_unique(Args &&... args) +{ + // NOTE std::make_unique is missing in C++11 standard + return std::unique_ptr(new T(std::forward(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 index 0000000..29043cd --- /dev/null +++ b/libs/foundation/src/Memory.test.cpp @@ -0,0 +1,30 @@ +#include + +#include + +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(allocated, freed); + + ASSERT_TRUE(allocated); + ASSERT_FALSE(freed); + } + + ASSERT_TRUE(freed); +} -- 2.7.4