Skip to content

Commit

Permalink
Move Subdivide method implementations of CopyElements, FillElements, …
Browse files Browse the repository at this point in the history
…FillIds and CopyIndices to below the declarations for C++ ≥ 20 compatibility.
  • Loading branch information
stripe2933 authored and guybrush77 committed Sep 5, 2023
1 parent 580910c commit 430ad3b
Showing 1 changed file with 63 additions and 52 deletions.
115 changes: 63 additions & 52 deletions include/rapidobj/rapidobj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4590,10 +4590,6 @@ using FillFloats = FillElements<float>;
using FillMaterialIds = FillIds<int32_t>;
using FillSmoothingGroupIds = FillIds<uint32_t>;

using MergeTask =
std::variant<CopyBytes, CopyInts, CopyFloats, CopyIndices, FillFloats, FillMaterialIds, FillSmoothingGroupIds>;
using MergeTasks = std::vector<MergeTask>;

template <typename T>
struct CopyElements final {
CopyElements(T* dst, const T* src, size_t size) noexcept : m_dst(dst), m_src(src), m_size(size) {}
Expand All @@ -4610,18 +4606,7 @@ struct CopyElements final {
return rapidobj_errc::Success;
}

auto Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(CopyElements(m_dst + begin, m_src + begin, end - begin));
begin = end;
}
return tasks;
}
inline auto Subdivide(size_t num) const noexcept;

private:
T* m_dst{};
Expand All @@ -4645,18 +4630,7 @@ struct FillElements final {
return rapidobj_errc::Success;
}

auto Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(FillElements(m_dst + begin, m_value, end - begin));
begin = end;
}
return tasks;
}
inline auto Subdivide(size_t num) const noexcept;

private:
T* m_dst{};
Expand Down Expand Up @@ -4701,18 +4675,7 @@ struct FillIds final {
return rapidobj_errc::Success;
}

auto Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(FillIds(m_dst + begin, *m_src, end - begin, m_start + begin));
begin = end;
}
return tasks;
}
inline auto Subdivide(size_t num) const noexcept;

private:
T* m_dst{};
Expand Down Expand Up @@ -4777,18 +4740,7 @@ struct CopyIndices final {
return rapidobj_errc::Success;
}

auto Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(CopyIndices(m_dst + begin, m_src + begin, m_offset_flags, end - begin, m_offset, m_count));
begin = end;
}
return tasks;
}
inline auto Subdivide(size_t num) const noexcept;

private:
Index* m_dst{};
Expand All @@ -4799,6 +4751,65 @@ struct CopyIndices final {
AttributeInfo m_count{};
};

using MergeTask =
std::variant<CopyBytes, CopyInts, CopyFloats, CopyIndices, FillFloats, FillMaterialIds, FillSmoothingGroupIds>;
using MergeTasks = std::vector<MergeTask>;

template <typename T>
auto CopyElements<T>::Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(CopyElements(m_dst + begin, m_src + begin, end - begin));
begin = end;
}
return tasks;
}

template <typename T>
auto FillElements<T>::Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(FillElements(m_dst + begin, m_value, end - begin));
begin = end;
}
return tasks;
}

template <typename T>
auto FillIds<T>::Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(FillIds(m_dst + begin, *m_src, end - begin, m_start + begin));
begin = end;
}
return tasks;
}

auto CopyIndices::Subdivide(size_t num) const noexcept
{
auto begin = size_t{ 0 };
auto tasks = MergeTasks();
tasks.reserve(num);
for (size_t i = 0; i != num; ++i) {
auto end = (1 + i) * m_size / num;
tasks.push_back(CopyIndices(m_dst + begin, m_src + begin, m_offset_flags, end - begin, m_offset, m_count));
begin = end;
}
return tasks;
}

struct Reader {
struct ReadResult final {
std::size_t bytes_read;
Expand Down

0 comments on commit 430ad3b

Please sign in to comment.