From: 박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 Date: Wed, 18 Jul 2018 00:53:59 +0000 (+0900) Subject: [coco] Add 'PtrList' class (#691) X-Git-Tag: nncc_backup~2396 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dcf65043c4e746f7de4f295b6983e20a8853fcdf;p=platform%2Fcore%2Fml%2Fnnfw.git [coco] Add 'PtrList' class (#691) Coco IR often uses a list of object pointers. To reduce code duplication, this commit introduces 'PtrList' helper class which manages a list of object pointers (using std::vector). Signed-off-by: Jonghyun Park --- diff --git a/contrib/coco/CMakeLists.txt b/contrib/coco/CMakeLists.txt new file mode 100644 index 0000000..ad6d478 --- /dev/null +++ b/contrib/coco/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(core) diff --git a/contrib/coco/core/CMakeLists.txt b/contrib/coco/core/CMakeLists.txt new file mode 100644 index 0000000..e3f6da3 --- /dev/null +++ b/contrib/coco/core/CMakeLists.txt @@ -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 index 0000000..5485c58 --- /dev/null +++ b/contrib/coco/core/include/coco/ADT/PtrList.h @@ -0,0 +1,38 @@ +#ifndef __COCO_ADT_PTR_LIST_H__ +#define __COCO_ADT_PTR_LIST_H__ + +#include + +#include + +namespace coco +{ + +template 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 _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 index 0000000..d6f753c --- /dev/null +++ b/contrib/coco/core/src/ADT/PtrList.cpp @@ -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 index 0000000..6493569 --- /dev/null +++ b/contrib/coco/core/src/ADT/PtrList.test.cpp @@ -0,0 +1,29 @@ +#include "coco/ADT/PtrList.h" + +#include + +#include + +namespace +{ +struct Object { }; +} + +TEST(ADT_PTR_LIST, ctor) +{ + coco::PtrList l; + + ASSERT_EQ(l.size(), 0); +} + +TEST(ADT_PTR_LIST, insert) +{ + coco::PtrList l; + + std::unique_ptr ptr{new Object}; + + l.insert(ptr.get()); + + ASSERT_EQ(l.size(), 1); + ASSERT_EQ(l.at(0), ptr.get()); +}