Skip to content

Commit

Permalink
Add an etl::nullptr_t type to <etl/nullptr.h> (#924)
Browse files Browse the repository at this point in the history
* Add an etl::nullptr_t type

* etlcpp/etl issue #921 (etl::unique_ptr reset): add etl::unique_ptr(...)::reset(ETL_NULLPTR)

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

* Add member pointer support and delete the addressof operator

* "Delete" etl::addressof(ETL_NULLPTR)

* Ensure compatibility with C++98

* ACTUALLY ensure compatibility with C++98

I'm stupid :/

* Correct definition according to cppreference
  • Loading branch information
tigran2008 authored Jul 19, 2024
1 parent 744d54c commit 22dc6b2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
40 changes: 11 additions & 29 deletions include/etl/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,8 @@ namespace etl
//*********************************
void reset(pointer p_ = pointer()) ETL_NOEXCEPT
{
assert(p_ != p);

assert(p_ == ETL_NULLPTR || p_ != p);
pointer value = p;
p = p_;

Expand All @@ -1420,29 +1420,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
//*********************************
Expand Down Expand Up @@ -1622,6 +1609,11 @@ namespace etl
}
}

void reset(etl::nullptr_t = ETL_NULLPTR) ETL_NOEXCEPT
{
reset(pointer());
}

//*********************************
void swap(unique_ptr& v) ETL_NOEXCEPT
{
Expand All @@ -1636,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
//*********************************
Expand Down
24 changes: 22 additions & 2 deletions include/etl/nullptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,33 @@ SOFTWARE.

#include <stddef.h>

namespace etl
{
#if ETL_CPP11_NOT_SUPPORTED
// Use the old style C++ NULL definition.
#define ETL_NULLPTR 0
class nullptr_t
{
public:
template <class T>
inline operator T*() const { return 0; }

template <class C, class T>
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();

#define ETL_NULLPTR (etl::_nullptr)
#else
// Use the new style nullptr.
typedef decltype(nullptr) nullptr_t;
#define ETL_NULLPTR nullptr
#endif
}

#endif

3 changes: 2 additions & 1 deletion include/etl/private/addressof.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>
Expand All @@ -48,7 +49,7 @@ namespace etl
///\ingroup memory
//*****************************************************************************
template <typename T>
ETL_CONSTEXPR17 T* addressof(T& t)
ETL_CONSTEXPR17 typename etl::enable_if<!etl::is_same<T, etl::nullptr_t>::value, T>::type* addressof(T& t)
{
#if ETL_USING_STL && ETL_USING_CPP11
return std::addressof(t);
Expand Down

0 comments on commit 22dc6b2

Please sign in to comment.