Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an etl::nullptr_t type to <etl/nullptr.h> #924

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions include/etl/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ namespace etl
}

//*********************************
void reset(pointer p_ = pointer()) ETL_NOEXCEPT
void reset(pointer p_) ETL_NOEXCEPT
tigran2008 marked this conversation as resolved.
Show resolved Hide resolved
{
assert(p_ != p);

Expand All @@ -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
{
Expand All @@ -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
//*********************************
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
Loading