Skip to content

Commit

Permalink
Merge branch 'pull-request/#924-Add-an-etl--nullptr_t-type-to-etl-nul…
Browse files Browse the repository at this point in the history
…lptr.h' into development
  • Loading branch information
John Wellbelove committed Jul 20, 2024
2 parents 2e30aa6 + e71596a commit 5885433
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
68 changes: 26 additions & 42 deletions include/etl/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -1395,14 +1395,15 @@ namespace etl
//*********************************
void reset(pointer p_ = pointer()) ETL_NOEXCEPT
{
assert(p_ != p);

pointer value = p;
p = p_;

if (value != ETL_NULLPTR)
if (p_ == ETL_NULLPTR || p_ != p)
{
deleter(value);
pointer value = p;
p = p_;

if (value != ETL_NULLPTR)
{
deleter(value);
}
}
}

Expand All @@ -1420,29 +1421,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 @@ -1605,23 +1593,29 @@ namespace etl
{
pointer value = p;
p = ETL_NULLPTR;
return value;
return value;
}

//*********************************
void reset(pointer p_) ETL_NOEXCEPT
{
assert(p_ != p);

pointer value = p;
p = p_;

if (value != ETL_NULLPTR)
if (p_ != p)
{
deleter(value);
pointer value = p;
p = p_;

if (value != ETL_NULLPTR)
{
deleter(value);
}
}
}

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

//*********************************
void swap(unique_ptr& v) ETL_NOEXCEPT
{
Expand All @@ -1636,23 +1630,13 @@ 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
{
reset(nullptr);
reset(ETL_NULLPTR);

return *this;
}
#else
//*********************************
unique_ptr& operator =(void*) ETL_NOEXCEPT
{
reset(NULL);

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 5885433

Please sign in to comment.