diff --git a/ememory/Counter.cpp b/ememory/Counter.cpp index 1a7e536..aab1569 100644 --- a/ememory/Counter.cpp +++ b/ememory/Counter.cpp @@ -26,7 +26,7 @@ ememory::Counter::~Counter() { int64_t ememory::Counter::incrementShared(bool _fromWeak) { int64_t out; { - std::unique_lock lock(m_mutex); + ethread::UniqueLock lock(m_mutex); EMEMORY_DBG("shared++ (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); if (m_counterShared != 0 || _fromWeak == false) { m_counterShared++; @@ -38,7 +38,7 @@ int64_t ememory::Counter::incrementShared(bool _fromWeak) { } ememory::Counter::remove ememory::Counter::decrementShared() { - std::unique_lock lock(m_mutex); + ethread::UniqueLock lock(m_mutex); EMEMORY_DBG("shared-- (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); if (m_counterShared != 0) { m_counterShared--; @@ -62,7 +62,7 @@ ememory::Counter::remove ememory::Counter::decrementShared() { int64_t ememory::Counter::incrementWeak() { int64_t out; { - std::unique_lock lock(m_mutex); + ethread::UniqueLock lock(m_mutex); EMEMORY_DBG("weak++ (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); m_counterWeak++; out = m_counterWeak; @@ -72,7 +72,7 @@ int64_t ememory::Counter::incrementWeak() { } ememory::Counter::remove ememory::Counter::decrementWeak() { - std::unique_lock lock(m_mutex); + ethread::UniqueLock lock(m_mutex); EMEMORY_DBG("weak-- (stop) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); if (m_counterWeak != 0) { m_counterWeak--; @@ -92,7 +92,7 @@ ememory::Counter::remove ememory::Counter::decrementWeak() { int64_t ememory::Counter::getCountWeak() const { int64_t out; { - std::unique_lock lock(m_mutex); + ethread::UniqueLock lock(m_mutex); out = m_counterWeak; } return out; @@ -101,7 +101,7 @@ int64_t ememory::Counter::getCountWeak() const { int64_t ememory::Counter::getCountShared() const { int64_t out; { - std::unique_lock lock(m_mutex); + ethread::UniqueLock lock(m_mutex); out = m_counterShared; } return out; @@ -110,7 +110,7 @@ int64_t ememory::Counter::getCountShared() const { int64_t ememory::Counter::getCount() const { int64_t out; { - std::unique_lock lock(m_mutex); + ethread::UniqueLock lock(m_mutex); out = m_counterWeak + m_counterShared; } return out; diff --git a/ememory/Counter.hpp b/ememory/Counter.hpp index 7eedc97..bfc5f6c 100644 --- a/ememory/Counter.hpp +++ b/ememory/Counter.hpp @@ -5,12 +5,13 @@ */ #pragma once -#include +#include // define type : uintXX_t and intXX_t #define __STDC_LIMIT_MACROS // note in android include the macro of min max are overwitten -#include - +extern "C" { + #include +} namespace ememory { /** * @brief Couter is an important part of the SharedPtr/WeakPtr implementation. This use a simple refcounting method dut thread-safe @@ -29,7 +30,7 @@ namespace ememory { private: int64_t m_counterShared; //!< Count of the active SharedPtr int64_t m_counterWeak; //!< Count of the active WeakPtr - mutable std::mutex m_mutex; //!< local counter mutex to prevent the thread concurent removing + mutable ethread::Mutex m_mutex; //!< local counter mutex to prevent the thread concurent removing public: /** * @brief Contructor diff --git a/ememory/EnableSharedFromThis.hpp b/ememory/EnableSharedFromThis.hpp index b2343ec..819bd50 100644 --- a/ememory/EnableSharedFromThis.hpp +++ b/ememory/EnableSharedFromThis.hpp @@ -6,7 +6,7 @@ #pragma once #include -#include +#include #include #include diff --git a/ememory/SharedPtr.hpp b/ememory/SharedPtr.hpp index 470cbb7..de47d1f 100644 --- a/ememory/SharedPtr.hpp +++ b/ememory/SharedPtr.hpp @@ -5,8 +5,7 @@ */ #pragma once -#include -#include +#include #include #include #include @@ -14,7 +13,7 @@ namespace ememory { template class WeakPtr; template class EnableSharedFromThis; - using deleterCall = std::function; + using deleterCall = etk::Function; /** * @brief ememory::SharedPtr is a smart pointer that retains shared ownership of an object through a pointer. * Several SharedPtr objects may own the same object. The object is destroyed and its memory deallocated when @@ -47,13 +46,13 @@ namespace ememory { public: #ifndef PARSE_DOXYGEN template::value - && std::is_base_of::value + typename etk::EnableIf< etk::IsSame::value + && etk::IsBaseOf::value , int>::type = 0> SharedPtr(EMEMORY_TYPE2* _element); template::value - && !std::is_base_of::value + typename etk::EnableIf< etk::IsSame::value + && !etk::IsBaseOf::value , int>::type = 0> SharedPtr(EMEMORY_TYPE2* _element); #else @@ -67,7 +66,7 @@ namespace ememory { /** * @brief Contructor on nullptr */ - SharedPtr(std::nullptr_t); + SharedPtr(etk::NullPtr); /** * @brief Contructor empty */ @@ -102,15 +101,15 @@ namespace ememory { * @brief Asignement operator (asign nullptr) * @return Reference on this */ - SharedPtr& operator= (std::nullptr_t); + SharedPtr& operator= (etk::NullPtr); public: #ifndef PARSE_DOXYGEN template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type = 0> SharedPtr(const SharedPtr& _obj); template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type = 0> SharedPtr& operator= (const SharedPtr& _obj); #endif @@ -128,7 +127,7 @@ namespace ememory { * @brief Check if the SharedPtr have an internal data (not nullptr) * @return true The pointer is not asigned, false otherwise */ - bool operator==(std::nullptr_t) const; + bool operator==(etk::NullPtr) const; /** * @brief Check if two SharedPtr are the same data (maybe not the same cast) * @param[in] _obj Object to compare @@ -140,7 +139,7 @@ namespace ememory { * @brief Check if the SharedPtr have NOT an internal data (nullptr) * @return true The pointer is asigned, false otherwise */ - bool operator!=(std::nullptr_t) const; + bool operator!=(etk::NullPtr) const; /** * @brief Check if two SharedPtr are NOT the same data (maybe not the same cast) * @param[in] _obj Object to compare diff --git a/ememory/UniquePtr.hpp b/ememory/UniquePtr.hpp index d041ba0..c527599 100644 --- a/ememory/UniquePtr.hpp +++ b/ememory/UniquePtr.hpp @@ -17,6 +17,10 @@ namespace ememory { UniquePtr() : m_pointer(nullptr) { + } + UniquePtr(etk::NullPtr) : + m_pointer(nullptr) { + } explicit UniquePtr(EMEM_UPTR_TYPE* _obj) : m_pointer(_obj) @@ -26,14 +30,19 @@ namespace ememory { ~UniquePtr() { reset(); } + UniquePtr& operator=(etk::NullPtr) { + reset(); + return *this; + } UniquePtr& operator=(UniquePtr _obj) { - reset(_obj.release()); + reset(); + m_pointer = etk::move(_obj.release()); return *this; } template UniquePtr& operator=(UniquePtr _obj){ - reset(_obj.release()); - m_pointer = etk::move(_obj.pointer); + reset(); + m_pointer = etk::move(_obj.m_pointer); return *this; } EMEM_UPTR_TYPE operator*() const{ @@ -47,7 +56,7 @@ namespace ememory { } EMEM_UPTR_TYPE *release(){ EMEM_UPTR_TYPE *tmp = m_pointer; - m_pointer = 0; + m_pointer = nullptr; return tmp; } void reset(){ @@ -55,7 +64,7 @@ namespace ememory { m_pointer = nullptr; } void swap(UniquePtr &_obj){ - std::swap(m_pointer, _obj.m_pointer); + etk::swap(m_pointer, _obj.m_pointer); } }; diff --git a/ememory/WeakPtr.hpp b/ememory/WeakPtr.hpp index e51c151..eb9d6e4 100644 --- a/ememory/WeakPtr.hpp +++ b/ememory/WeakPtr.hpp @@ -6,7 +6,7 @@ #pragma once #include -#include +#include #include #include @@ -31,7 +31,7 @@ namespace ememory { /** * @brief nullptr contructor */ - WeakPtr(std::nullptr_t); + WeakPtr(etk::NullPtr); private: /** * @brief Contructor of the Weak Ptr (specific for EnableSharedFromThis) @@ -71,14 +71,14 @@ namespace ememory { * @brief nullptr asignement * @return Reference on this */ - WeakPtr& operator= (std::nullptr_t); + WeakPtr& operator= (etk::NullPtr); /** * @brief Copy contuctor of herited WeakPtr * @param[in] _obj Object to copy */ template::value - && !std::is_void::value + typename etk::EnableIf< etk::IsVoid::value + && !etk::IsVoid::value , int>::type = 0> WeakPtr(const SharedPtr& _obj); /** @@ -87,18 +87,18 @@ namespace ememory { * @return Reference on this */ template::value - && !std::is_void::value + typename etk::EnableIf< etk::IsVoid::value + && !etk::IsVoid::value , int>::type = 0> WeakPtr& operator= (const SharedPtr& _obj); public: /* template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type = 0> WeakPtr(const WeakPtr& _obj); template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type = 0> WeakPtr& operator= (const WeakPtr& _obj); */ @@ -133,7 +133,7 @@ namespace ememory { * @brief Check if the WeakPtr have an internal data (not nullptr) * @return true The pointer is not asigned, false otherwise */ - bool operator==(std::nullptr_t) const; + bool operator==(etk::NullPtr) const; /** * @brief Check if two WeakPtr are different data * @param[in] _obj Object to compare @@ -144,7 +144,7 @@ namespace ememory { * @brief Check if the SharedPtr have NOT an internal data (nullptr) * @return true The pointer is asigned, false otherwise */ - bool operator!=(std::nullptr_t) const; + bool operator!=(etk::NullPtr) const; /** * @brief Swap 2 Object inside the WeakPtr * @param[in] _obj Object to swap with diff --git a/ememory/details/SharedPtr.hxx b/ememory/details/SharedPtr.hxx index 06b03f4..64a3e35 100644 --- a/ememory/details/SharedPtr.hxx +++ b/ememory/details/SharedPtr.hxx @@ -5,15 +5,13 @@ */ #pragma once -#include -#include #include #include template template::value - && std::is_base_of::value + typename etk::EnableIf< etk::IsSame::value + && etk::IsBaseOf::value , int>::type> ememory::SharedPtr::SharedPtr(EMEMORY_TYPE2* _element): m_element(_element), @@ -35,8 +33,8 @@ ememory::SharedPtr::SharedPtr(EMEMORY_TYPE2* _element): template template::value - && !std::is_base_of::value + typename etk::EnableIf< etk::IsSame::value + && !etk::IsBaseOf::value , int>::type> ememory::SharedPtr::SharedPtr(EMEMORY_TYPE2* _element): m_element(_element), @@ -58,7 +56,7 @@ ememory::SharedPtr::SharedPtr(): } template -ememory::SharedPtr::SharedPtr(std::nullptr_t): +ememory::SharedPtr::SharedPtr(etk::NullPtr): m_element(nullptr), m_counter(nullptr), m_deleter(createDeleter()) { @@ -123,7 +121,7 @@ ememory::SharedPtr& ememory::SharedPtr::operator= (c } template -ememory::SharedPtr& ememory::SharedPtr::operator= (std::nullptr_t) { +ememory::SharedPtr& ememory::SharedPtr::operator= (etk::NullPtr) { reset(); return *this; } @@ -142,7 +140,7 @@ ememory::SharedPtr::SharedPtr(ememory::SharedPtr&& _ template template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type> ememory::SharedPtr::SharedPtr(const ememory::SharedPtr& _obj): m_element(const_cast(_obj.get())), @@ -162,7 +160,7 @@ ememory::SharedPtr::SharedPtr(const ememory::SharedPtr template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type> ememory::SharedPtr& ememory::SharedPtr::operator= (const SharedPtr& _obj) { reset(); @@ -234,7 +232,7 @@ int64_t ememory::SharedPtr::useCount() const { } template -bool ememory::SharedPtr::operator==(std::nullptr_t) const { +bool ememory::SharedPtr::operator==(etk::NullPtr) const { return m_counter == nullptr; } @@ -245,7 +243,7 @@ bool ememory::SharedPtr::operator==(const SharedPtr } template -bool ememory::SharedPtr::operator!=(std::nullptr_t) const { +bool ememory::SharedPtr::operator!=(etk::NullPtr) const { return m_counter != nullptr; } @@ -329,14 +327,14 @@ namespace ememory { class SharedPtr { friend class WeakPtr; public: - using deleterCall = std::function; + using deleterCall = etk::Function; private: void* m_element; ememory::Counter* m_counter; public: SharedPtr(void* _element); public: - SharedPtr(std::nullptr_t): + SharedPtr(etk::NullPtr): m_element(nullptr), m_counter(nullptr) { EMEMORY_DBG("new shared"); @@ -360,7 +358,7 @@ namespace ememory { } m_counter->incrementShared(); } - SharedPtr& operator= (std::nullptr_t) { + SharedPtr& operator= (etk::NullPtr) { reset(); return *this; } @@ -434,14 +432,14 @@ namespace ememory { } return m_counter->getCountShared(); } - bool operator==(std::nullptr_t) const { + bool operator==(etk::NullPtr) const { return m_counter == nullptr; } template bool operator==(const SharedPtr& _obj) const { return m_counter == _obj.m_counter; } - bool operator!=(std::nullptr_t) const { + bool operator!=(etk::NullPtr) const { return m_counter != nullptr; } template diff --git a/ememory/details/WeakPtr.hxx b/ememory/details/WeakPtr.hxx index 265d72f..63b97d9 100644 --- a/ememory/details/WeakPtr.hxx +++ b/ememory/details/WeakPtr.hxx @@ -6,7 +6,7 @@ #pragma once #include -#include +#include #include #include @@ -18,7 +18,7 @@ ememory::WeakPtr::WeakPtr(): } template -ememory::WeakPtr::WeakPtr(std::nullptr_t): +ememory::WeakPtr::WeakPtr(etk::NullPtr): m_element(nullptr), m_counter(nullptr) { @@ -75,7 +75,7 @@ ememory::WeakPtr& ememory::WeakPtr::operator= (const } template -ememory::WeakPtr& ememory::WeakPtr::operator= (std::nullptr_t) { +ememory::WeakPtr& ememory::WeakPtr::operator= (etk::NullPtr) { reset(); return *this; } @@ -126,8 +126,8 @@ ememory::WeakPtr& ememory::WeakPtr::operator= (const template template::value - && !std::is_void::value + typename etk::EnableIf< etk::IsVoid::value + && !etk::IsVoid::value , int>::type> ememory::WeakPtr::WeakPtr(const ememory::SharedPtr& _obj): m_element((void*)_obj.get()), @@ -147,8 +147,8 @@ ememory::WeakPtr::WeakPtr(const ememory::SharedPtr& template template::value - && !std::is_void::value + typename etk::EnableIf< etk::IsVoid::value + && !etk::IsVoid::value , int>::type> ememory::WeakPtr& ememory::WeakPtr::operator= (const ememory::SharedPtr& _obj) { reset(); @@ -170,7 +170,7 @@ ememory::WeakPtr& ememory::WeakPtr::operator= (const /* template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type> WeakPtr(const WeakPtr& _obj): m_element(_obj.m_element), @@ -188,7 +188,7 @@ WeakPtr(const WeakPtr& _obj): } template::value + typename etk::EnableIf< etk::IsBaseOf::value , int>::type> WeakPtr& operator= (const WeakPtr& _obj) { reset(); @@ -268,7 +268,7 @@ bool ememory::WeakPtr::operator==(const ememory::WeakPtr -bool ememory::WeakPtr::operator==(std::nullptr_t) const { +bool ememory::WeakPtr::operator==(etk::NullPtr) const { return m_counter == nullptr; } @@ -278,7 +278,7 @@ bool ememory::WeakPtr::operator!=(const ememory::WeakPtr -bool ememory::WeakPtr::operator!=(std::nullptr_t) const { +bool ememory::WeakPtr::operator!=(etk::NullPtr) const { return m_counter != nullptr; } diff --git a/ememory/memory.hpp b/ememory/memory.hpp index dc70018..98deadb 100644 --- a/ememory/memory.hpp +++ b/ememory/memory.hpp @@ -6,7 +6,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/lutin_ememory-test.py b/lutin_ememory-test.py index 1f341b8..ab95266 100644 --- a/lutin_ememory-test.py +++ b/lutin_ememory-test.py @@ -34,9 +34,10 @@ def configure(target, my_module): 'test/testCasts.cpp' ]) my_module.add_depend([ + 'cxx', 'ememory', - 'gtest', - 'test-debug' + 'test-debug', + 'etest', ]) return True diff --git a/lutin_ememory.py b/lutin_ememory.py index b7ac91e..21bf008 100644 --- a/lutin_ememory.py +++ b/lutin_ememory.py @@ -49,7 +49,7 @@ def configure(target, my_module): # build in C++ mode my_module.compile_version("c++", 2011) # add dependency of the generic C++ library: - my_module.add_depend('cxx') + my_module.add_depend('etk-base') my_module.add_optionnal_depend('elog') my_module.add_path(".") diff --git a/test/main.cpp b/test/main.cpp index 829e28a..6eadbc7 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -6,13 +6,13 @@ #include #include -#include -#include +#include +//#include int main(int _argc, const char *_argv[]) { - ::testing::InitGoogleTest(&_argc, const_cast(_argv)); - etk::init(_argc, _argv); + //etk::init(_argc, _argv); + etest::init(_argc, _argv); for (int32_t iii=0; iii<_argc ; ++iii) { etk::String data = _argv[iii]; if ( data == "-h" diff --git a/test/testCasts.cpp b/test/testCasts.cpp index 3410215..ca9b1df 100644 --- a/test/testCasts.cpp +++ b/test/testCasts.cpp @@ -4,7 +4,7 @@ * @license MPL v2.0 (see license file) */ -#include +#include #include #include "main.hpp" diff --git a/test/testEnableSharedFromThis.cpp b/test/testEnableSharedFromThis.cpp index 61aa569..cb2ec59 100644 --- a/test/testEnableSharedFromThis.cpp +++ b/test/testEnableSharedFromThis.cpp @@ -4,7 +4,7 @@ * @license MPL v2.0 (see license file) */ -#include +#include #include #include "main.hpp" diff --git a/test/testShared.cpp b/test/testShared.cpp index f65e5e4..8344726 100644 --- a/test/testShared.cpp +++ b/test/testShared.cpp @@ -4,7 +4,7 @@ * @license MPL v2.0 (see license file) */ -#include +#include #include #include "main.hpp" diff --git a/test/testUnique.cpp b/test/testUnique.cpp index b96ee08..ac136db 100644 --- a/test/testUnique.cpp +++ b/test/testUnique.cpp @@ -6,11 +6,10 @@ * @license MPL v2.0 (see license file) */ -#include +#include #include #include -#define NAME "UNIQUE-PTR" TEST(TestEmemoryUniquePtr, Creation_1) { ememory::UniquePtr testData; @@ -44,7 +43,7 @@ class testCreateAndDestroy { }; TEST(TestEmemoryUniquePtr, reset) { - vals = {0,0,0,0,0,0,0,0,0,0,0,0,0}; + memset(vals, 0, sizeof(vals)); EXPECT_EQ(vals[1], 0); ememory::UniquePtr testData = ememory::makeUniquePtr(1); EXPECT_NE(testData.get(), nullptr); @@ -53,8 +52,18 @@ TEST(TestEmemoryUniquePtr, reset) { EXPECT_EQ(testData.get(), nullptr); EXPECT_EQ(vals[1], -1); } +TEST(TestEmemoryUniquePtr, reset_2) { + memset(vals, 0, sizeof(vals)); + EXPECT_EQ(vals[1], 0); + ememory::UniquePtr testData = ememory::makeUniquePtr(1); + EXPECT_NE(testData.get(), nullptr); + EXPECT_EQ(vals[1], 1); + testData = nullptr; + EXPECT_EQ(testData.get(), nullptr); + EXPECT_EQ(vals[1], -1); +} TEST(TestEmemoryUniquePtr, overwrite) { - vals = {0,0,0,0,0,0,0,0,0,0,0,0,0}; + memset(vals, 0, sizeof(vals)); ememory::UniquePtr testData = ememory::makeUniquePtr(1); EXPECT_NE(testData.get(), nullptr); EXPECT_EQ(vals[1], 1); diff --git a/test/testWeak.cpp b/test/testWeak.cpp index 9bb80e0..c9cebb1 100644 --- a/test/testWeak.cpp +++ b/test/testWeak.cpp @@ -4,7 +4,7 @@ * @license MPL v2.0 (see license file) */ -#include +#include #include #include "main.hpp"