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 ScopedUpdate #550

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 28 additions & 4 deletions include/llama/Concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#if __has_include(<concepts>)
# include <concepts>
#endif
#ifdef __cpp_lib_concepts
namespace llama
{
#ifdef __cpp_lib_concepts
// clang-format off
template <typename M>
concept Mapping = requires(M m) {
Expand Down Expand Up @@ -50,7 +50,7 @@ namespace llama
concept ProxyReference = requires(R r) {
typename R::value_type;
{ static_cast<typename R::value_type>(r) } -> std::same_as<typename R::value_type>;
{ r = typename R::value_type{} } -> std::same_as<R&>;
{ r = std::declval<typename R::value_type>() } -> std::same_as<R&>;
};

template <typename R>
Expand Down Expand Up @@ -100,7 +100,31 @@ namespace llama
concept BlobAllocator = requires(BA ba, std::integral_constant<std::size_t, 16> alignment, std::size_t size) {
{ ba(alignment, size) } -> Blob;
};
// clang-format on
} // namespace llama
// clang-format on
#endif

namespace internal
{
template<typename R, typename = void>
struct IsProxyReferenceImpl : std::false_type
{
};

template<typename R>
struct IsProxyReferenceImpl<
R,
std::void_t<
typename R::value_type,
decltype(static_cast<typename R::value_type>(std::declval<R&>())),
decltype(std::declval<R&>() = std::declval<typename R::value_type>())>> : std::true_type
{
};
} // namespace internal

template<typename R>
#ifdef __cpp_lib_concepts
inline constexpr bool isProxyReference = ProxyReference<R>;
#else
inline constexpr bool isProxyReference = internal::IsProxyReferenceImpl<R>::value;
#endif
} // namespace llama
162 changes: 162 additions & 0 deletions include/llama/VirtualRecord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

#pragma once

#include "Concepts.hpp"
#include "HasRanges.hpp"
#include "ProxyRefOpMixin.hpp"
#include "StructName.hpp"
#include "View.hpp"

#include <iosfwd>
Expand Down Expand Up @@ -783,6 +786,165 @@ namespace llama
[functor = std::forward<Functor>(functor), &vr = vr](auto rc)
LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS(constexpr mutable) { std::forward<Functor>(functor)(vr(rc)); });
}

namespace internal
{
// gets the value type for a given T, where T models a reference type. T is either an l-value reference, a
// proxy reference or a VirtualRecord
template<typename T, typename = void>
struct ValueOf
{
static_assert(sizeof(T) == 0, "T does not model a reference");
};

template<typename T>
struct ValueOf<T, std::enable_if_t<is_VirtualRecord<T>>>
{
using type = One<typename T::AccessibleRecordDim>;
};

#ifdef __cpp_lib_concepts
template<ProxyReference T>
#else
template<typename T>
#endif
struct ValueOf<T, std::enable_if_t<isProxyReference<T>>>
{
using type = typename T::value_type;
};

template<typename T>
struct ValueOf<T&>
{
using type = T;
};
} // namespace internal

/// Scope guard type. ScopedUpdate takes a copy of a value through a reference and stores it internally during
/// construction. The stored value is written back when ScopedUpdate is destroyed. ScopedUpdate tries to act like
/// the stored value as much as possible, exposing member functions of the stored value and acting like a proxy
/// reference if the stored value is a primitive type.
template<typename Reference, typename = void>
struct ScopedUpdate : internal::ValueOf<Reference>::type
{
using value_type = typename internal::ValueOf<Reference>::type;

/// Loads a copy of the value referenced by r. Stores r and the loaded value.
LLAMA_FN_HOST_ACC_INLINE ScopedUpdate(Reference r) : value_type(r), ref(r)
{
}

ScopedUpdate(const ScopedUpdate&) = delete;
auto operator=(const ScopedUpdate&) -> ScopedUpdate& = delete;

ScopedUpdate(ScopedUpdate&&) noexcept = default;
auto operator=(ScopedUpdate&&) noexcept -> ScopedUpdate& = default;

using value_type::operator=;

/// Stores the internally stored value back to the referenced value.
LLAMA_FN_HOST_ACC_INLINE ~ScopedUpdate()
{
ref = static_cast<value_type&>(*this);
}

/// Get access to the stored value.
LLAMA_FN_HOST_ACC_INLINE auto get() -> value_type&
{
return *this;
}

/// Get access to the stored value.
LLAMA_FN_HOST_ACC_INLINE auto get() const -> const value_type&
{
return *this;
}

private:
Reference ref;
};

template<typename Reference>
struct ScopedUpdate<
Reference,
std::enable_if_t<std::is_fundamental_v<typename internal::ValueOf<Reference>::type>>>
: ProxyRefOpMixin<ScopedUpdate<Reference>, typename internal::ValueOf<Reference>::type>
{
using value_type = typename internal::ValueOf<Reference>::type;

LLAMA_FN_HOST_ACC_INLINE ScopedUpdate(Reference r) : value(r), ref(r)
{
}

ScopedUpdate(const ScopedUpdate&) = delete;
auto operator=(const ScopedUpdate&) -> ScopedUpdate& = delete;

ScopedUpdate(ScopedUpdate&&) noexcept = default;
auto operator=(ScopedUpdate&&) noexcept -> ScopedUpdate& = default;

LLAMA_FN_HOST_ACC_INLINE auto get() -> value_type&
{
return value;
}

LLAMA_FN_HOST_ACC_INLINE auto get() const -> const value_type&
{
return value;
}

LLAMA_FN_HOST_ACC_INLINE operator const value_type&() const
{
return value;
}

LLAMA_FN_HOST_ACC_INLINE operator value_type&()
{
return value;
}

LLAMA_FN_HOST_ACC_INLINE auto operator=(value_type v) -> ScopedUpdate&
{
value = v;
return *this;
}

LLAMA_FN_HOST_ACC_INLINE ~ScopedUpdate()
{
ref = value;
}

private:
value_type value;
Reference ref;
};

namespace internal
{
template<typename T, typename = void>
struct ReferenceTo
{
using type = T&;
};

template<typename T>
struct ReferenceTo<T, std::enable_if_t<is_VirtualRecord<T> && !is_One<T>>>
{
using type = T;
};

#ifdef __cpp_lib_concepts
template<ProxyReference T>
#else
template<typename T>
#endif
struct ReferenceTo<T, std::enable_if_t<isProxyReference<T>>>
{
using type = T;
};
} // namespace internal

template<typename T>
ScopedUpdate(T) -> ScopedUpdate<typename internal::ReferenceTo<std::remove_reference_t<T>>::type>;
} // namespace llama

template<typename View, typename BoundRecordCoord, bool OwnView>
Expand Down
24 changes: 24 additions & 0 deletions tests/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,27 @@ TEST_CASE("divCeil")
STATIC_REQUIRE(llama::divCeil(17, 16) == 2);
STATIC_REQUIRE(llama::divCeil(300, 16) == 19);
}

TEST_CASE("isProxyReference")
{
STATIC_REQUIRE(!llama::internal::IsProxyReferenceImpl<int&>::value);
STATIC_REQUIRE(!llama::isProxyReference<int&>);
STATIC_REQUIRE(!llama::internal::IsProxyReferenceImpl<std::string>::value);
STATIC_REQUIRE(!llama::isProxyReference<std::string>);
STATIC_REQUIRE(!llama::internal::IsProxyReferenceImpl<std::vector<int>>::value);
STATIC_REQUIRE(!llama::isProxyReference<std::vector<int>>);
STATIC_REQUIRE(!llama::internal::IsProxyReferenceImpl<std::vector<bool>::reference>::value);
STATIC_REQUIRE(!llama::isProxyReference<std::vector<bool>::reference>); // misses a value_type alias

using One = llama::One<Vec3I>;
STATIC_REQUIRE(!llama::internal::IsProxyReferenceImpl<One>::value);
STATIC_REQUIRE(!llama::isProxyReference<One>);
STATIC_REQUIRE(!llama::internal::IsProxyReferenceImpl<decltype(One{}())>::value);
STATIC_REQUIRE(!llama::isProxyReference<decltype(One{}())>);

auto mapping = llama::mapping::BitPackedIntSoA<llama::ArrayExtents<int, 4>, Vec3I>{{}, 17};
auto v = llama::allocView(mapping);
[[maybe_unused]] auto ref = v(1)(tag::X{});
STATIC_REQUIRE(llama::internal::IsProxyReferenceImpl<decltype(ref)>::value);
STATIC_REQUIRE(llama::isProxyReference<decltype(ref)>);
}
115 changes: 115 additions & 0 deletions tests/virtualrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,3 +1083,118 @@ TEST_CASE("VirtualRecord.reference_to_One")
CHECK(v(tag::Y{}) == 22);
CHECK(v(tag::Z{}) == 3);
}

TEST_CASE("ValueOf")
{
STATIC_REQUIRE(std::is_same_v<llama::internal::ValueOf<int&>::type, int>);

using One = llama::One<Vec3I>;
STATIC_REQUIRE(std::is_same_v<llama::internal::ValueOf<decltype(One{}())>::type, One>);

auto mapping = llama::mapping::BitPackedIntSoA<llama::ArrayExtents<int, 4>, Vec3I>{{}, 17};
auto v = llama::allocView(mapping);
[[maybe_unused]] auto ref = v(1)(tag::X{});
#ifdef __cpp_lib_concepts
STATIC_REQUIRE(llama::ProxyReference<decltype(ref)>);
#endif
STATIC_REQUIRE(std::is_same_v<llama::internal::ValueOf<decltype(ref)>::type, int>);
}
TEST_CASE("ScopedUpdate.Fundamental")
{
int i = 1;
{
llama::ScopedUpdate u(i);
STATIC_REQUIRE(std::is_same_v<decltype(u), llama::ScopedUpdate<int&>>);
u = 23;
CHECK(u == 23);
CHECK(u.get() == 23);
CHECK(i == 1);
u = 24;
CHECK(u == 24);
CHECK(u.get() == 24);
CHECK(i == 1);
}
CHECK(i == 24);
}

TEST_CASE("ScopedUpdate.Object")
{
std::vector v = {1};
{
llama::ScopedUpdate u(v);
STATIC_REQUIRE(std::is_same_v<decltype(u), llama::ScopedUpdate<std::vector<int>&>>);
u.push_back(2);
CHECK(u == std::vector{1, 2});
CHECK(u.get() == std::vector{1, 2});
CHECK(v == std::vector{1});
u = std::vector{3, 4, 5};
CHECK(u == std::vector{3, 4, 5});
CHECK(u.get() == std::vector{3, 4, 5});
CHECK(v == std::vector{1});
}
CHECK(v == std::vector{3, 4, 5});
}

TEST_CASE("ScopedUpdate.ProxyRef")
{
auto mapping = llama::mapping::BitPackedIntSoA<llama::ArrayExtents<int, 4>, Vec3I>{{}, 17};
auto v = llama::allocView(mapping);
auto i = v(1)(tag::X{});
i = 1;
{
llama::ScopedUpdate u(i);
STATIC_REQUIRE(std::is_same_v<decltype(u), llama::ScopedUpdate<decltype(i)>>);
u = 23;
CHECK(u == 23);
CHECK(u.get() == 23);
CHECK(i == 1);
u = 24;
CHECK(u == 24);
CHECK(u.get() == 24);
CHECK(i == 1);
}
CHECK(i == 24);
}

TEST_CASE("ScopedUpdate.VirtualRecord")
{
auto test = [](auto&& v)
{
llama::forEachLeaf(v, [i = 0](auto& field) mutable { field = ++i; });
{
llama::ScopedUpdate u(v);
if constexpr(llama::is_One<std::remove_reference_t<decltype(v)>>)
{
STATIC_REQUIRE(
std::is_same_v<decltype(u), llama::ScopedUpdate<std::remove_reference_t<decltype(v)>&>>);
}
else
{
STATIC_REQUIRE(std::is_same_v<decltype(u), llama::ScopedUpdate<decltype(v())>>);
}
u(tag::X{}) = 11;
CHECK(u(tag::X{}) == 11);
CHECK(u(tag::Y{}) == 2);
CHECK(u(tag::Z{}) == 3);
CHECK(u.get()(tag::Z{}) == 3);
CHECK(v(tag::X{}) == 1);
CHECK(v(tag::Y{}) == 2);
CHECK(v(tag::Z{}) == 3);
CHECK(v(tag::Z{}) == 3);
u = 24;
CHECK(u(tag::X{}) == 24);
CHECK(u(tag::Y{}) == 24);
CHECK(u(tag::Z{}) == 24);
CHECK(u.get()(tag::Z{}) == 24);
CHECK(v(tag::X{}) == 1);
CHECK(v(tag::Y{}) == 2);
CHECK(v(tag::Z{}) == 3);
}
CHECK(v(tag::X{}) == 24);
CHECK(v(tag::Y{}) == 24);
CHECK(v(tag::Z{}) == 24);
};
llama::One<Vec3I> v;
test(v);
test(v());
}