Skip to content

Commit

Permalink
Update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Jan 29, 2024
1 parent 8fd85fe commit 9ccd8a6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
1 change: 1 addition & 0 deletions cpp.hint
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
// See https://go.microsoft.com/fwlink/?linkid=865984

#define PAPILIO_EXPORT
#define PAPILIO_ASSERT(expr)
12 changes: 1 addition & 11 deletions include/papilio/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,6 @@ namespace detail
};
} // namespace detail

#ifdef PAPILIO_COMPILER_MSVC
# pragma warning(push)
# pragma warning(disable : 26495)
#endif

PAPILIO_EXPORT template <typename T, std::size_t Capacity>
class fixed_vector : public detail::fixed_vector_impl
{
Expand Down Expand Up @@ -1016,9 +1011,8 @@ class fixed_vector : public detail::fixed_vector_impl
}
}

value_type tmp(std::forward<Args>(args)...);
iterator nonconst_pos = const_cast<iterator>(pos);
*nonconst_pos = std::move(tmp);
*nonconst_pos = std::move(value_type(std::forward<Args>(args)...));

return nonconst_pos;
}
Expand Down Expand Up @@ -1077,10 +1071,6 @@ class fixed_vector : public detail::fixed_vector_impl
}
};

#ifdef PAPILIO_COMPILER_MSVC
# pragma warning(pop)
#endif

namespace detail
{
template <typename Compare>
Expand Down
2 changes: 1 addition & 1 deletion include/papilio/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#define PAPILIO_ERR_INVALID_STRING 0x7
#define PAPILIO_ERR_UNCLOSED_BRACE 0x8

#define PAPILIO_ERR_UNKNOWN_ERROR -1
#define PAPILIO_ERR_UNKNOWN_ERROR (-1)

#ifdef __cplusplus // C compatibility
# define PAPILIO_NS ::papilio::
Expand Down
56 changes: 36 additions & 20 deletions include/papilio/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ class optional_unique_ptr : public detail::optional_ptr_base<optional_unique_ptr
optional_unique_ptr() noexcept(std::is_nothrow_default_constructible_v<Deleter>)
: m_ptr(), m_del(), m_has_ownership(false) {}

optional_unique_ptr(std::nullptr_t) noexcept {}
optional_unique_ptr(std::nullptr_t) noexcept(std::is_nothrow_default_constructible_v<Deleter>)
: optional_unique_ptr() {}

optional_unique_ptr(const optional_unique_ptr& other) noexcept
optional_unique_ptr(const optional_unique_ptr& other) noexcept(std::is_nothrow_default_constructible_v<Deleter>)
: m_ptr(other.get()),
m_del(other.get_deleter()),
m_has_ownership(false) {}
Expand All @@ -150,15 +151,20 @@ class optional_unique_ptr : public detail::optional_ptr_base<optional_unique_ptr
m_has_ownership(false)
{}

optional_unique_ptr(optional_unique_ptr&& other) noexcept
optional_unique_ptr(optional_unique_ptr&& other) noexcept(std::is_nothrow_default_constructible_v<Deleter>)
: m_ptr(std::exchange(other.m_ptr, pointer())),
m_del(),
m_has_ownership(std::exchange(other.m_has_ownership, false)) {}

explicit optional_unique_ptr(pointer ptr, bool ownership = true) noexcept
: m_ptr(std::move(ptr)), m_has_ownership(ownership) {}
optional_unique_ptr(pointer ptr, bool ownership) noexcept(std::is_nothrow_default_constructible_v<Deleter>)
: m_ptr(std::move(ptr)),
m_del(),
m_has_ownership(ownership) {}

optional_unique_ptr(independent_t, pointer ptr) noexcept
: m_ptr(std::move(ptr)), m_has_ownership(true) {}
: m_ptr(std::move(ptr)),
m_del(),
m_has_ownership(true) {}

template <typename D>
optional_unique_ptr(std::unique_ptr<T, D>&& ptr) noexcept
Expand Down Expand Up @@ -196,7 +202,7 @@ class optional_unique_ptr : public detail::optional_ptr_base<optional_unique_ptr

// modifiers

void reset(pointer ptr, bool ownership = true) noexcept
void reset(pointer ptr, bool ownership) noexcept
{
reset(nullptr);
if(ptr)
Expand All @@ -206,7 +212,7 @@ class optional_unique_ptr : public detail::optional_ptr_base<optional_unique_ptr
}
}

void reset(independent_t, pointer ptr)
void reset(independent_t, pointer ptr) noexcept
{
reset(std::move(ptr), true);
}
Expand All @@ -225,7 +231,10 @@ class optional_unique_ptr : public detail::optional_ptr_base<optional_unique_ptr
void reset(std::nullptr_t) noexcept
{
if(has_ownership())
m_del(get());
{
PAPILIO_ASSERT(static_cast<bool>(*this));
get_deleter()(get());
}
m_ptr = pointer();
m_has_ownership = false;
}
Expand Down Expand Up @@ -290,11 +299,13 @@ class optional_unique_ptr : public detail::optional_ptr_base<optional_unique_ptr

private:
pointer m_ptr;
PAPILIO_NO_UNIQUE_ADDRESS Deleter m_del{};
bool m_has_ownership = true;
PAPILIO_NO_UNIQUE_ADDRESS Deleter m_del;
bool m_has_ownership;
};

PAPILIO_EXPORT template <typename T, typename Deleter>
PAPILIO_EXPORT template <
typename T,
typename Deleter>
class optional_unique_ptr<T[], Deleter>
{
public:
Expand All @@ -305,7 +316,8 @@ class optional_unique_ptr<T[], Deleter>
optional_unique_ptr() noexcept(std::is_nothrow_default_constructible_v<Deleter>)
: m_ptr(), m_del(), m_has_ownership(false) {}

optional_unique_ptr(std::nullptr_t) noexcept {}
optional_unique_ptr(std::nullptr_t) noexcept(std::is_nothrow_default_constructible_v<Deleter>)
: optional_unique_ptr() {}

optional_unique_ptr(const optional_unique_ptr& other) noexcept
: m_ptr(other.get()),
Expand All @@ -314,17 +326,18 @@ class optional_unique_ptr<T[], Deleter>

template <typename D>
optional_unique_ptr(const optional_unique_ptr<T, D>& other)
: m_ptr(other.m_ptr), m_has_ownership(false)
: m_ptr(other.m_ptr), m_del(), m_has_ownership(false)
{}

optional_unique_ptr(optional_unique_ptr&& other) noexcept
: m_ptr(std::exchange(other.m_ptr, pointer())),
m_del(),
m_has_ownership(std::exchange(other.m_has_ownership, false)) {}

template <typename U>
requires detail::optional_arr_ptr_acceptable<U, pointer, element_type>
explicit optional_unique_ptr(U ptr, bool ownership = true) noexcept
: m_ptr(ptr), m_has_ownership(ownership)
optional_unique_ptr(U ptr, bool ownership) noexcept
: m_ptr(ptr), m_del(), m_has_ownership(ownership)
{}

template <typename D>
Expand Down Expand Up @@ -363,7 +376,7 @@ class optional_unique_ptr<T[], Deleter>

// modifiers

void reset(pointer ptr, bool ownership = true) noexcept
void reset(pointer ptr, bool ownership) noexcept
{
reset(nullptr);
if(ptr)
Expand All @@ -387,7 +400,10 @@ class optional_unique_ptr<T[], Deleter>
void reset(std::nullptr_t) noexcept
{
if(has_ownership())
{
PAPILIO_ASSERT(static_cast<bool>(*this));
m_del(get());
}
m_ptr = pointer();
m_has_ownership = false;
}
Expand Down Expand Up @@ -447,8 +463,8 @@ class optional_unique_ptr<T[], Deleter>

private:
pointer m_ptr;
PAPILIO_NO_UNIQUE_ADDRESS Deleter m_del;
bool m_has_ownership = true;
PAPILIO_NO_UNIQUE_ADDRESS Deleter m_del{};
};

PAPILIO_EXPORT template <typename T, typename Deleter>
Expand All @@ -458,15 +474,15 @@ PAPILIO_EXPORT template <typename T, typename... Args>
requires(!std::is_array_v<T>)
optional_unique_ptr<T> make_optional_unique(Args&&... args)
{
return optional_unique_ptr<T>(new T(std::forward<Args>(args)...));
return optional_unique_ptr<T>(new T(std::forward<Args>(args)...), true);
}

PAPILIO_EXPORT template <typename T, typename... Args>
requires(std::is_array_v<T> && std::extent_v<T> == 0)
optional_unique_ptr<T> make_optional_unique(std::size_t n)
{
using element_type = std::remove_extent_t<T>;
return optional_unique_ptr<T>(new element_type[n]());
return optional_unique_ptr<T>(new element_type[n](), true);
}

PAPILIO_EXPORT template <typename T, typename... Args>
Expand Down
4 changes: 2 additions & 2 deletions test/test_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ TEST(optional_unique_ptr, compatibility)
EXPECT_EQ(p, nullptr);
EXPECT_EQ(nullptr, p);

p.reset(std::malloc(4));
p.reset(std::malloc(4), true);
std::memset(p.get(), 0, 4);
ASSERT_TRUE(p.has_ownership());

Expand Down Expand Up @@ -110,7 +110,7 @@ TEST(optional_unique_ptr, compatibility)
}

{
optional_unique_ptr<int[]> opt_int_arr(new int[4]{0, 1, 2, 3});
optional_unique_ptr<int[]> opt_int_arr(new int[4]{0, 1, 2, 3}, true);
ASSERT_TRUE(opt_int_arr.has_ownership());

for(std::size_t i = 0; i < 4; ++i)
Expand Down

0 comments on commit 9ccd8a6

Please sign in to comment.