From d7585508da860616403ec0e2b84fbcf0877b6244 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 16 Aug 2025 10:12:41 +0100 Subject: [PATCH] Changed unique() to underlying_type() Added underlying_type() to etl::not_null --- include/etl/not_null.h | 45 ++++++++++++++++++--------- test/test_not_null_pointer.cpp | 34 ++++++++++++++++++++ test/test_not_null_unique_pointer.cpp | 8 ++--- test/vs2022/etl.vcxproj | 1 + 4 files changed, 70 insertions(+), 18 deletions(-) diff --git a/include/etl/not_null.h b/include/etl/not_null.h index 0387a35c..54a5be6d 100644 --- a/include/etl/not_null.h +++ b/include/etl/not_null.h @@ -87,12 +87,13 @@ namespace etl typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; + typedef pointer underlying_type; //********************************* /// Constructs a not_null from a pointer. /// Asserts if the pointer is null. //********************************* - explicit not_null(pointer ptr_) + explicit not_null(underlying_type ptr_) : ptr(ptr_) { ETL_ASSERT(ptr_ != ETL_NULLPTR, ETL_ERROR(not_null_contains_null)); @@ -120,7 +121,7 @@ namespace etl /// Assignment from a pointer. /// Asserts if the pointer is null. //********************************* - not_null& operator =(pointer rhs) + not_null& operator =(underlying_type rhs) { ETL_ASSERT_OR_RETURN_VALUE(rhs != ETL_NULLPTR, ETL_ERROR(not_null_contains_null), *this); @@ -161,6 +162,22 @@ namespace etl return ptr; } + //********************************* + /// Gets a reference to the underlying pointer. + //********************************* + underlying_type& underlying() + { + return ptr; + } + + //********************************* + /// Gets a const_reference to the underlying pointer. + //********************************* + const underlying_type& underlying() const + { + return ptr; + } + private: /// The underlying pointer. @@ -182,14 +199,14 @@ namespace etl typedef T& reference; typedef const T& const_reference; - typedef etl::unique_ptr unique_ptr_type; + typedef etl::unique_ptr underlying_type;; #if ETL_USING_CPP11 //********************************* /// Constructs a not_null from a unique_ptr. /// Asserts if the unique_ptr is null. //********************************* - explicit not_null(unique_ptr_type&& u_ptr_) + explicit not_null(underlying_type&& u_ptr_) : u_ptr(etl::move(u_ptr_)) { ETL_ASSERT(u_ptr.get() != ETL_NULLPTR, ETL_ERROR(not_null_contains_null)); @@ -198,7 +215,7 @@ namespace etl //********************************* /// Constructs a not_null from a unique_ptr. //********************************* - not_null(etl::not_null&& other) + not_null(etl::not_null&& other) : u_ptr(etl::move(other.u_ptr)) { } @@ -207,7 +224,7 @@ namespace etl /// Assign from a unique_ptr. /// Asserts if the unique_ptr is null. //********************************* - not_null& operator =(unique_ptr_type&& rhs) + not_null& operator =(underlying_type&& rhs) { ETL_ASSERT_OR_RETURN_VALUE(rhs.get() != ETL_NULLPTR, ETL_ERROR(not_null_contains_null), *this); @@ -219,7 +236,7 @@ namespace etl //********************************* /// Assign from a not_null. //********************************* - not_null& operator =(etl::not_null&& rhs) + not_null& operator =(etl::not_null&& rhs) { u_ptr = etl::move(rhs.u_ptr); @@ -260,25 +277,25 @@ namespace etl } //********************************* - /// Gets the underlying unique_ptr. + /// Gets a reference to the underlying unique_ptr. //********************************* - unique_ptr_type& unique() + underlying_type& underlying() { - return u_ptr; + return etl::move(u_ptr); } //********************************* - /// Gets the underlying unique_ptr. + /// Gets a const_reference to the underlying unique_ptr. //********************************* - const unique_ptr_type& unique() const + const underlying_type& underlying() const { - return u_ptr; + return etl::move(u_ptr); } private: /// The underlying unique_ptr. - unique_ptr_type u_ptr; + underlying_type u_ptr; }; } diff --git a/test/test_not_null_pointer.cpp b/test/test_not_null_pointer.cpp index fd9d1683..59a34038 100644 --- a/test/test_not_null_pointer.cpp +++ b/test/test_not_null_pointer.cpp @@ -79,6 +79,40 @@ namespace CHECK_EQUAL(456, *nn2); } + //************************************************************************* + TEST(test_underlying) + { + int value1 = 123; + etl::not_null nn1(&value1); + + int* p = nn1.underlying(); + + CHECK_EQUAL(123, *p); + } + + //************************************************************************* + TEST(test_underlying_const) + { + int value1 = 123; + const etl::not_null nn1(&value1); + + const int* p = nn1.underlying(); + + CHECK_EQUAL(123, *p); + } + + //************************************************************************* + TEST(test_unique_const) + { + using up_t = etl::unique_ptr; + up_t up1(new int{ 123 }); + const etl::not_null nn(etl::move(up1)); + + const up_t& up2 = nn.underlying(); + + CHECK_EQUAL(123, *up2.get()); + } + //************************************************************************* TEST(test_implicit_conversion) { diff --git a/test/test_not_null_unique_pointer.cpp b/test/test_not_null_unique_pointer.cpp index 8ad0f4b4..eef5fc00 100644 --- a/test/test_not_null_unique_pointer.cpp +++ b/test/test_not_null_unique_pointer.cpp @@ -55,25 +55,25 @@ namespace } //************************************************************************* - TEST(test_unique) + TEST(test_underlying) { using up_t = etl::unique_ptr; up_t up1(new int{ 123 }); etl::not_null nn(etl::move(up1)); - up_t up2 = etl::move(nn.unique()); + up_t up2 = etl::move(nn.underlying()); CHECK_EQUAL(123, *up2.get()); } //************************************************************************* - TEST(test_unique_const) + TEST(test_underlying_const) { using up_t = etl::unique_ptr; up_t up1(new int{ 123 }); const etl::not_null nn(etl::move(up1)); - const up_t& up2 = nn.unique(); + const up_t& up2 = etl::move(nn.underlying()); CHECK_EQUAL(123, *up2.get()); } diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index 294096af..7e15bc66 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -3362,6 +3362,7 @@ +