[coco] Add 'PtrList<T>' class (#691)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 18 Jul 2018 00:53:59 +0000 (09:53 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 18 Jul 2018 00:53:59 +0000 (09:53 +0900)
Coco IR often uses a list of object pointers. To reduce code
duplication, this commit introduces 'PtrList<T>' helper class
which manages a list of object pointers (using std::vector).

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/CMakeLists.txt [new file with mode: 0644]
contrib/coco/core/CMakeLists.txt [new file with mode: 0644]
contrib/coco/core/include/coco/ADT/PtrList.h [new file with mode: 0644]
contrib/coco/core/src/ADT/PtrList.cpp [new file with mode: 0644]
contrib/coco/core/src/ADT/PtrList.test.cpp [new file with mode: 0644]

diff --git a/contrib/coco/CMakeLists.txt b/contrib/coco/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ad6d478
--- /dev/null
@@ -0,0 +1 @@
+add_subdirectory(core)
diff --git a/contrib/coco/core/CMakeLists.txt b/contrib/coco/core/CMakeLists.txt
new file mode 100644 (file)
index 0000000..e3f6da3
--- /dev/null
@@ -0,0 +1,18 @@
+file(GLOB_RECURSE SOURCES "src/*.cpp")
+file(GLOB_RECURSE TESTS "src/*.test.cpp")
+list(REMOVE_ITEM SOURCES ${TESTS})
+
+add_library(coco_core STATIC ${SOURCES})
+set_target_properties(coco_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
+target_include_directories(coco_core PUBLIC include)
+
+nncc_find_package(GTest QUIET)
+
+if(NOT GTest_FOUND)
+  return()
+endif(NOT GTest_FOUND)
+
+add_executable(coco_core_test ${TESTS})
+target_link_libraries(coco_core_test gtest_main)
+target_link_libraries(coco_core_test coco_core)
+add_test(coco_core_test coco_core_test)
diff --git a/contrib/coco/core/include/coco/ADT/PtrList.h b/contrib/coco/core/include/coco/ADT/PtrList.h
new file mode 100644 (file)
index 0000000..5485c58
--- /dev/null
@@ -0,0 +1,38 @@
+#ifndef __COCO_ADT_PTR_LIST_H__
+#define __COCO_ADT_PTR_LIST_H__
+
+#include <vector>
+
+#include <cstdint>
+
+namespace coco
+{
+
+template<typename T> class PtrList
+{
+public:
+  PtrList() = default;
+
+public:
+  PtrList(const PtrList &) = delete;
+  PtrList(PtrList &&) = delete;
+
+public:
+  virtual ~PtrList() = default;
+
+public:
+  uint32_t size(void) const { return _ptrs.size(); }
+
+public:
+  T *at(uint32_t n) const { return _ptrs.at(n); }
+
+public:
+  void insert(T *ptr) { _ptrs.emplace_back(ptr); }
+
+private:
+  std::vector<T *> _ptrs;
+};
+
+} // namespace coco
+
+#endif // __COCO_ADT_PTR_LIST_H__
diff --git a/contrib/coco/core/src/ADT/PtrList.cpp b/contrib/coco/core/src/ADT/PtrList.cpp
new file mode 100644 (file)
index 0000000..d6f753c
--- /dev/null
@@ -0,0 +1,3 @@
+#include "coco/ADT/PtrList.h"
+
+// NOTE Do NOT delete this file; this file checks the completeness of 'PtrList.h'
diff --git a/contrib/coco/core/src/ADT/PtrList.test.cpp b/contrib/coco/core/src/ADT/PtrList.test.cpp
new file mode 100644 (file)
index 0000000..6493569
--- /dev/null
@@ -0,0 +1,29 @@
+#include "coco/ADT/PtrList.h"
+
+#include <memory>
+
+#include <gtest/gtest.h>
+
+namespace
+{
+struct Object { };
+}
+
+TEST(ADT_PTR_LIST, ctor)
+{
+  coco::PtrList<Object> l;
+
+  ASSERT_EQ(l.size(), 0);
+}
+
+TEST(ADT_PTR_LIST, insert)
+{
+  coco::PtrList<Object> l;
+
+  std::unique_ptr<Object> ptr{new Object};
+
+  l.insert(ptr.get());
+
+  ASSERT_EQ(l.size(), 1);
+  ASSERT_EQ(l.at(0), ptr.get());
+}