Skip to content

Commit

Permalink
Add flexibilty to object value IDs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Oct 1, 2020
1 parent a308e39 commit b9c87ce
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/flow-tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ underlying data associated with each item will change when the item moves.*
Depending on what you're associating with your items, the effects of this can
vary from slight inefficiencies to complete discontinuities in your interface.
You can override the default association by defining a `get_alia_id` function
You can override the default association by defining a `get_alia_item_id` function
for your item's type. (It should be accessible from within the `alia` namespace,
either via [argument-dependent
lookup](https://en.cppreference.com/w/cpp/language/adl) or because it's defined
Expand Down
27 changes: 25 additions & 2 deletions src/alia/context/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,37 @@ struct has_value_id<
{
};

} // namespace detail

template<class Object>
std::enable_if_t<detail::has_value_id<Object>::value, id_interface const&>
get_alia_value_id(Object const& object)
{
return object.value_id();
}

namespace detail {

template<class Object, class = void_t<>>
struct has_alia_value_id : std::false_type
{
};
template<class Object>
struct has_alia_value_id<
Object,
void_t<decltype(static_cast<id_interface const&>(
get_alia_value_id(std::declval<Object>())))>> : std::true_type
{
};

void
fold_in_content_id(context ctx, id_interface const& id);

template<class Object>
std::enable_if_t<has_value_id<Object>::value>
std::enable_if_t<has_alia_value_id<Object>::value>
fold_in_object_id(context ctx, Object const& object)
{
fold_in_content_id(ctx, object.value_id());
fold_in_content_id(ctx, get_alia_value_id(object));
}

template<class Object>
Expand Down
22 changes: 11 additions & 11 deletions src/alia/flow/for_each.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct is_vector_like<

template<class Item>
auto
get_alia_id(Item const&)
get_alia_item_id(Item const&)
{
return null_id;
}
Expand Down Expand Up @@ -243,7 +243,7 @@ for_each(Context ctx, ContainerSignal const& container_signal, Fn&& fn)
ctx,
nc,
[&](named_block& nb) {
auto iteration_id = get_alia_id(container[index]);
auto iteration_id = get_alia_item_id(container[index]);
if (iteration_id != null_id)
nb.begin(nc, iteration_id);
else
Expand Down Expand Up @@ -283,9 +283,9 @@ for_each(Context ctx, Container&& container, Fn&& fn)
ctx,
nc,
[&](named_block& nb) {
// We don't try to use get_alia_id() here because we want to
// support the use case where the UI is present even when the
// item isn't available, and we want to keep the block ID
// We don't try to use get_alia_item_id() here because we want
// to support the use case where the UI is present even when
// the item isn't available, and we want to keep the block ID
// stable in that scenario.
nb.begin(nc, make_id(index));
},
Expand Down Expand Up @@ -322,7 +322,7 @@ for_each(Context ctx, Container&& container, Fn&& fn)
ctx,
nc,
[&](named_block& nb) {
auto iteration_id = get_alia_id(item);
auto iteration_id = get_alia_item_id(item);
if (iteration_id != null_id)
nb.begin(nc, iteration_id);
else
Expand Down Expand Up @@ -416,7 +416,7 @@ for_each(Context ctx, ContainerSignal const& container_signal, Fn&& fn)
ctx,
nc,
[&](named_block& nb) {
auto iteration_id = get_alia_id(item);
auto iteration_id = get_alia_item_id(item);
if (iteration_id != null_id)
nb.begin(nc, iteration_id);
else
Expand Down Expand Up @@ -457,9 +457,9 @@ for_each(Context ctx, Container&& container, Fn&& fn)
ctx,
nc,
[&](named_block& nb) {
// We don't try to use get_alia_id() here because we want to
// support the use case where the UI is present even when the
// item isn't available, and we want to keep the block ID
// We don't try to use get_alia_item_id() here because we want
// to support the use case where the UI is present even when
// the item isn't available, and we want to keep the block ID
// stable in that scenario.
nb.begin(nc, make_id(&item));
},
Expand Down Expand Up @@ -496,7 +496,7 @@ for_each(Context ctx, Container&& container, Fn&& fn)
ctx,
nc,
[&](named_block& nb) {
auto iteration_id = get_alia_id(item);
auto iteration_id = get_alia_item_id(item);
if (iteration_id != null_id)
nb.begin(nc, iteration_id);
else
Expand Down
8 changes: 4 additions & 4 deletions unit_tests/flow/for_each.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ operator<(my_item a, my_item b)
}

auto
get_alia_id(my_item const& item)
get_alia_item_id(my_item const& item)
{
return make_id(item.id);
}
Expand Down Expand Up @@ -327,7 +327,7 @@ TEST_CASE("item vector", "[flow][for_each]")

std::reverse(container.begin(), container.end());

// Since my_item defines get_alia_id(), the graph data properly follows
// Since my_item defines get_alia_item_id(), the graph data properly follows
// the items around, so there are no additional calls.
check_traversal(sys, controller, "cherry;banana;apple;");
REQUIRE(call_count == 3);
Expand Down Expand Up @@ -568,7 +568,7 @@ TEST_CASE("item list", "[for_each][list]")

std::reverse(container.begin(), container.end());

// Since my_item defines get_alia_id(), the graph data properly follows
// Since my_item defines get_alia_item_id(), the graph data properly follows
// the items around, so there are no additional calls.
check_traversal(sys, controller, "cherry;banana;apple;");
REQUIRE(call_count == 3);
Expand Down Expand Up @@ -654,7 +654,7 @@ TEST_CASE("for_each over a list of raw items", "[for_each][list]")

std::reverse(container.begin(), container.end());

// Since my_item defines get_alia_id(), the graph data properly follows
// Since my_item defines get_alia_item_id(), the graph data properly follows
// the items around, so there are no additional calls.
check_traversal(sys, controller, "cherry;banana;apple;");
REQUIRE(call_count == 3);
Expand Down

0 comments on commit b9c87ce

Please sign in to comment.