From 00ba1b62a013ab7b4e30fbd5a8138e2fd80e25c7 Mon Sep 17 00:00:00 2001 From: tigran2008 Date: Wed, 17 Jul 2024 15:40:46 +0400 Subject: [PATCH 1/7] Add an etl::nullptr_t type --- include/etl/nullptr.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/etl/nullptr.h b/include/etl/nullptr.h index 8d2e99512..2fbced497 100644 --- a/include/etl/nullptr.h +++ b/include/etl/nullptr.h @@ -35,13 +35,18 @@ SOFTWARE. #include +namespace etl +{ #if ETL_CPP11_NOT_SUPPORTED // Use the old style C++ NULL definition. - #define ETL_NULLPTR 0 + typedef enum { _nullptr = 0 } nullptr_t; + #define ETL_NULLPTR (etl::_nullptr) #else // Use the new style nullptr. + typedef decltype(nullptr) nullptr_t; #define ETL_NULLPTR nullptr #endif +} #endif From af40b7f318a862cf50a01d50dee7638e3c42fb8f Mon Sep 17 00:00:00 2001 From: tigran2008 Date: Wed, 17 Jul 2024 15:55:53 +0400 Subject: [PATCH 2/7] etlcpp/etl issue #921 (etl::unique_ptr reset): add etl::unique_ptr(...)::reset(ETL_NULLPTR) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove default argument for the normal reset method of etl::unique_ptr (sorry, didn't notice 😬) Silence the unused argument warning Fix operator =(nullptr) Replace the nullptr_t enum with a class which acts more similar to C++11 nullptr --- include/etl/memory.h | 25 +++++++++---------------- include/etl/nullptr.h | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/etl/memory.h b/include/etl/memory.h index 1abcf6380..21411e477 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1393,7 +1393,7 @@ namespace etl } //********************************* - void reset(pointer p_ = pointer()) ETL_NOEXCEPT + void reset(pointer p_) ETL_NOEXCEPT { assert(p_ != p); @@ -1406,6 +1406,12 @@ namespace etl } } + void reset(etl::nullptr_t p_ = ETL_NULLPTR) ETL_NOEXCEPT + { + (void)p_; // unused argument + reset(pointer()); + } + //********************************* void swap(unique_ptr& value) ETL_NOEXCEPT { @@ -1420,29 +1426,16 @@ namespace etl return (p != ETL_NULLPTR); } -#if ETL_USING_STL && ETL_USING_CPP11 //********************************* - unique_ptr& operator =(std::nullptr_t) ETL_NOEXCEPT + unique_ptr& operator =(etl::nullptr_t) ETL_NOEXCEPT { if (p) { - reset(nullptr); + reset(ETL_NULLPTR); } return *this; } -#else - //********************************* - unique_ptr& operator =(void*) ETL_NOEXCEPT - { - if (p) - { - reset(NULL); - } - - return *this; - } -#endif #if ETL_USING_CPP11 //********************************* diff --git a/include/etl/nullptr.h b/include/etl/nullptr.h index 2fbced497..a7756ccc6 100644 --- a/include/etl/nullptr.h +++ b/include/etl/nullptr.h @@ -38,8 +38,18 @@ SOFTWARE. namespace etl { #if ETL_CPP11_NOT_SUPPORTED - // Use the old style C++ NULL definition. - typedef enum { _nullptr = 0 } nullptr_t; + class nullptr_t + { + public: + template + inline operator T*() const { return 0; } + + inline bool operator==(nullptr_t) const { return true; } + inline bool operator!=(nullptr_t) const { return false; } + }; + + static const nullptr_t _nullptr = nullptr_t(); + #define ETL_NULLPTR (etl::_nullptr) #else // Use the new style nullptr. From ec0938c3b2a158a1f25097fbf3a52b8ddf87839c Mon Sep 17 00:00:00 2001 From: Tigran Khachatryan <39616689+tigran2008@users.noreply.github.com> Date: Wed, 17 Jul 2024 22:15:22 +0400 Subject: [PATCH 3/7] Add member pointer support and delete the addressof operator --- include/etl/nullptr.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/etl/nullptr.h b/include/etl/nullptr.h index a7756ccc6..df52ea80f 100644 --- a/include/etl/nullptr.h +++ b/include/etl/nullptr.h @@ -43,9 +43,14 @@ namespace etl public: template inline operator T*() const { return 0; } + + template + inline operator T C::* () const { return 0; } inline bool operator==(nullptr_t) const { return true; } inline bool operator!=(nullptr_t) const { return false; } + private: + void operator&() const ETL_DELETE; // cannot take the address of ETL_NULLPTR }; static const nullptr_t _nullptr = nullptr_t(); From 9d2261d158577a5ba00becf53979e5c7e4bd47a4 Mon Sep 17 00:00:00 2001 From: Tigran Khachatryan <39616689+tigran2008@users.noreply.github.com> Date: Wed, 17 Jul 2024 22:41:04 +0400 Subject: [PATCH 4/7] "Delete" etl::addressof(ETL_NULLPTR) --- include/etl/private/addressof.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/etl/private/addressof.h b/include/etl/private/addressof.h index 67fc8d2f6..929e06498 100644 --- a/include/etl/private/addressof.h +++ b/include/etl/private/addressof.h @@ -32,6 +32,7 @@ SOFTWARE. #define ETL_ADDRESSOF_INCLUDED #include "../platform.h" +#include "../type_traits.h" #if defined(ETL_IN_UNIT_TEST) || ETL_USING_STL #include @@ -47,7 +48,7 @@ namespace etl /// https://en.cppreference.com/w/cpp/memory/addressof ///\ingroup memory //***************************************************************************** - template + template ::value>::type> ETL_CONSTEXPR17 T* addressof(T& t) { #if ETL_USING_STL && ETL_USING_CPP11 From dc7c1ce0af8cb466e570ab6d289a3c3a70eab9d1 Mon Sep 17 00:00:00 2001 From: Tigran Khachatryan <39616689+tigran2008@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:00:13 +0400 Subject: [PATCH 5/7] Ensure compatibility with C++98 --- include/etl/private/addressof.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/etl/private/addressof.h b/include/etl/private/addressof.h index 929e06498..0b5bc7c73 100644 --- a/include/etl/private/addressof.h +++ b/include/etl/private/addressof.h @@ -48,8 +48,8 @@ namespace etl /// https://en.cppreference.com/w/cpp/memory/addressof ///\ingroup memory //***************************************************************************** - template ::value>::type> - ETL_CONSTEXPR17 T* addressof(T& t) + template + ETL_CONSTEXPR17 typename etl::enable_if::value>::type* addressof(T& t) { #if ETL_USING_STL && ETL_USING_CPP11 return std::addressof(t); From a2a80daa24ddc2a8d8112746da3a03366cbdfeb7 Mon Sep 17 00:00:00 2001 From: Tigran Khachatryan <39616689+tigran2008@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:05:21 +0400 Subject: [PATCH 6/7] ACTUALLY ensure compatibility with C++98 I'm stupid :/ --- include/etl/private/addressof.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/etl/private/addressof.h b/include/etl/private/addressof.h index 0b5bc7c73..fc864e0cd 100644 --- a/include/etl/private/addressof.h +++ b/include/etl/private/addressof.h @@ -49,7 +49,7 @@ namespace etl ///\ingroup memory //***************************************************************************** template - ETL_CONSTEXPR17 typename etl::enable_if::value>::type* addressof(T& t) + ETL_CONSTEXPR17 typename etl::enable_if::value, T>::type* addressof(T& t) { #if ETL_USING_STL && ETL_USING_CPP11 return std::addressof(t); From 8a517d517ea3f66134709450fa677a160a8114dd Mon Sep 17 00:00:00 2001 From: tigran2008 Date: Thu, 18 Jul 2024 15:05:08 +0400 Subject: [PATCH 7/7] Correct definition according to cppreference --- include/etl/memory.h | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/include/etl/memory.h b/include/etl/memory.h index 21411e477..692b102ec 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1393,10 +1393,10 @@ namespace etl } //********************************* - void reset(pointer p_) ETL_NOEXCEPT + void reset(pointer p_ = pointer()) ETL_NOEXCEPT { - assert(p_ != p); - + assert(p_ == ETL_NULLPTR || p_ != p); + pointer value = p; p = p_; @@ -1406,12 +1406,6 @@ namespace etl } } - void reset(etl::nullptr_t p_ = ETL_NULLPTR) ETL_NOEXCEPT - { - (void)p_; // unused argument - reset(pointer()); - } - //********************************* void swap(unique_ptr& value) ETL_NOEXCEPT { @@ -1615,6 +1609,11 @@ namespace etl } } + void reset(etl::nullptr_t = ETL_NULLPTR) ETL_NOEXCEPT + { + reset(pointer()); + } + //********************************* void swap(unique_ptr& v) ETL_NOEXCEPT { @@ -1629,23 +1628,13 @@ namespace etl return (p != ETL_NULLPTR); } -#if ETL_USING_STL && ETL_USING_CPP11 - //********************************* - unique_ptr& operator =(std::nullptr_t) ETL_NOEXCEPT - { - reset(nullptr); - - return *this; - } -#else //********************************* - unique_ptr& operator =(void*) ETL_NOEXCEPT + unique_ptr& operator =(etl::nullptr_t) ETL_NOEXCEPT { - reset(NULL); + reset(ETL_NULLPTR); return *this; } -#endif #if ETL_USING_CPP11 //*********************************