Skip to content

Commit

Permalink
Use the pointer to value directly when using construct_at()/destroy_at()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkaratarakis committed May 4, 2024
1 parent 081c253 commit 4d53055
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
14 changes: 6 additions & 8 deletions include/fixed_containers/fixed_deque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,13 @@ class FixedDequeBase
}
constexpr void set_size(const std::size_t size) { starting_index_and_size().distance = size; }

constexpr const OptionalT& array_unchecked_at(const std::size_t i) const { return array()[i]; }
constexpr OptionalT& array_unchecked_at(const std::size_t i) { return array()[i]; }
constexpr const T& unchecked_at(const std::size_t i) const
{
return optional_storage_detail::get(array_unchecked_at(i));
return optional_storage_detail::get(array()[i]);
}
constexpr T& unchecked_at(const std::size_t i)
{
return optional_storage_detail::get(array_unchecked_at(i));
return optional_storage_detail::get(array()[i]);
}

constexpr void destroy_at(std::size_t)
Expand All @@ -742,7 +740,7 @@ class FixedDequeBase
constexpr void destroy_at(std::size_t i)
requires NotTriviallyDestructible<T>
{
std::destroy_at(&array_unchecked_at(i).value);
std::destroy_at(&unchecked_at(i));
}

constexpr void destroy_range(iterator /*first*/, iterator /*last*/)
Expand All @@ -760,17 +758,17 @@ class FixedDequeBase

constexpr void place_at(const std::size_t i, const value_type& v)
{
std::construct_at(&array_unchecked_at(i), v);
std::construct_at(&unchecked_at(i), v);
}
constexpr void place_at(const std::size_t i, value_type&& v)
{
std::construct_at(&array_unchecked_at(i), std::move(v));
std::construct_at(&unchecked_at(i), std::move(v));
}

template <class... Args>
constexpr void emplace_at(const std::size_t i, Args&&... args)
{
optional_storage_detail::construct_at(&array_unchecked_at(i), std::forward<Args>(args)...);
std::construct_at(&unchecked_at(i), std::forward<Args>(args)...);
}

// [WORKAROUND-1] - Needed by the non-trivially-copyable flavor of FixedDeque
Expand Down
15 changes: 7 additions & 8 deletions include/fixed_containers/fixed_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,14 @@ class FixedVectorBase
{
IMPLEMENTATION_DETAIL_DO_NOT_USE_size_ = size;
}
constexpr const OptionalT& array_unchecked_at(const std::size_t i) const { return array()[i]; }
constexpr OptionalT& array_unchecked_at(const std::size_t i) { return array()[i]; }

constexpr const T& unchecked_at(const std::size_t i) const
{
return optional_storage_detail::get(array_unchecked_at(i));
return optional_storage_detail::get(array()[i]);
}
constexpr T& unchecked_at(const std::size_t i)
{
return optional_storage_detail::get(array_unchecked_at(i));
return optional_storage_detail::get(array()[i]);
}

constexpr void destroy_at(std::size_t)
Expand All @@ -721,7 +720,7 @@ class FixedVectorBase
constexpr void destroy_at(std::size_t i)
requires NotTriviallyDestructible<T>
{
std::destroy_at(&array_unchecked_at(i).value);
std::destroy_at(&unchecked_at(i));
}

constexpr void destroy_range(iterator /*first*/, iterator /*last*/)
Expand All @@ -739,17 +738,17 @@ class FixedVectorBase

constexpr void place_at(const std::size_t i, const value_type& v)
{
std::construct_at(&array_unchecked_at(i), v);
std::construct_at(&unchecked_at(i), v);
}
constexpr void place_at(const std::size_t i, value_type&& v)
{
std::construct_at(&array_unchecked_at(i), std::move(v));
std::construct_at(&unchecked_at(i), std::move(v));
}

template <class... Args>
constexpr void emplace_at(const std::size_t i, Args&&... args)
{
optional_storage_detail::construct_at(&array_unchecked_at(i), std::forward<Args>(args)...);
std::construct_at(&unchecked_at(i), std::forward<Args>(args)...);
}

// [WORKAROUND-1] - Needed by the non-trivially-copyable flavor of FixedVector
Expand Down
12 changes: 0 additions & 12 deletions include/fixed_containers/optional_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,6 @@ constexpr T&& get(T&& value)
return value;
}

template <typename T, typename... Args>
constexpr void construct_at(OptionalStorage<T>* p, Args&&... args)
{
std::construct_at(p, std::in_place, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
constexpr void construct_at(T* p, Args&&... args)
{
std::construct_at(p, std::forward<Args>(args)...);
}

// "Transparent" here means there will be no wrapping for simple types.
template <typename T>
using OptionalStorageTransparent =
Expand Down

0 comments on commit 4d53055

Please sign in to comment.