2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-07-05 19:15:52 +00:00

Merge pull request #6 from lioncash/view

View: Add load() overload for arbitrary containers
This commit is contained in:
Phillip Stephens 2019-09-01 11:03:59 -07:00 committed by GitHub
commit 953e1a0270

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <iterator>
#include <optional> #include <optional>
#include <type_traits>
#include <utility> #include <utility>
#include <boo/BooObject.hpp> #include <boo/BooObject.hpp>
@ -111,20 +113,19 @@ public:
void load(const VertStruct* data, size_t count) { void load(const VertStruct* data, size_t count) {
if (m_vertsBuf) { if (m_vertsBuf) {
VertStruct* out = m_vertsBuf.access(); VertStruct* const out = m_vertsBuf.access();
for (size_t i = 0; i < count; ++i) std::copy(data, data + count, out);
out[i] = data[i];
} }
} }
template <typename VertArray> template <typename ContiguousContainer>
void load(const VertArray data) { void load(const ContiguousContainer& container) {
static_assert(std::is_same<std::remove_all_extents_t<VertArray>, VertStruct>::value, "mismatched type"); // All contiguous containers (even those that aren't containers like C arrays) are usable
if (m_vertsBuf) { // with std::begin(). Because of that, we can use it to deduce the contained type.
constexpr size_t count = sizeof(VertArray) / sizeof(VertStruct); static_assert(std::is_same_v<std::remove_reference_t<decltype(*std::begin(std::declval<ContiguousContainer&>()))>,
VertStruct* out = m_vertsBuf.access(); VertStruct>,
for (size_t i = 0; i < count; ++i) "Supplied container doesn't contain same type of vertex struct");
out[i] = data[i];
} load(std::data(container), std::size(container));
} }
operator const boo::ObjToken<boo::IShaderDataBinding>&() { return m_shaderBinding; } operator const boo::ObjToken<boo::IShaderDataBinding>&() { return m_shaderBinding; }