From: 박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 Date: Fri, 14 Sep 2018 09:40:30 +0000 (+0900) Subject: [stdex] Introduce Memory.h (#1494) X-Git-Tag: nncc_backup~1814 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e95841739d8ecb6c62b9d010366357c81f506221;p=platform%2Fcore%2Fml%2Fnnfw.git [stdex] Introduce Memory.h (#1494) This commit introduces Memory.h to stdex library, which includes make_unique implementation (not present in C++ 11). Signed-off-by: Jonghyun Park --- diff --git a/contrib/stdex/include/stdex/Memory.h b/contrib/stdex/include/stdex/Memory.h new file mode 100644 index 0000000..3de992d --- /dev/null +++ b/contrib/stdex/include/stdex/Memory.h @@ -0,0 +1,17 @@ +#ifndef __STDEX_MEMORY_H__ +#define __STDEX_MEMORY_H__ + +#include + +namespace stdex +{ + +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 stdex + +#endif // __STDEX_MEMORY_H__ diff --git a/contrib/stdex/src/Memory.test.cpp b/contrib/stdex/src/Memory.test.cpp new file mode 100644 index 0000000..3a95ddd --- /dev/null +++ b/contrib/stdex/src/Memory.test.cpp @@ -0,0 +1,44 @@ +#include "stdex/Memory.h" + +#include + +namespace +{ + +struct Stat +{ + unsigned allocated = 0; + unsigned freed = 0; +}; + +struct Counter +{ +public: + Counter(Stat *stat) : _stat{stat} { _stat->allocated += 1; } + +public: + ~Counter() { _stat->freed += 1; } + +private: + Stat *_stat; +}; + +} // namespace + +TEST(MemoryTest, make_unique) +{ + Stat stat; + + ASSERT_EQ(stat.allocated, 0); + ASSERT_EQ(stat.freed, 0); + + auto o = stdex::make_unique<::Counter>(&stat); + + ASSERT_EQ(stat.allocated, 1); + ASSERT_EQ(stat.freed, 0); + + o.reset(); + + ASSERT_EQ(stat.allocated, 1); + ASSERT_EQ(stat.freed, 1); +}