Skip to content

Commit

Permalink
refactor(core): add DictionaryTrait::{View, ConstView}
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 28, 2023
1 parent b81b3b2 commit ea5f292
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 296 deletions.
12 changes: 2 additions & 10 deletions core/include/cubos/core/reflection/external/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,8 @@ CUBOS_REFLECT_EXTERNAL_TEMPLATE((typename K, typename V), (std::map<K, V>))
map->emplace(*static_cast<const K*>(key), std::move(*static_cast<V*>(value)));
});

dictionaryTrait.setErase([](void* instance, const void* iterator, bool writeable) {
auto* map = static_cast<Map*>(instance);
if (writeable)
{
map->erase(*static_cast<const typename Map::iterator*>(iterator));
}
else
{
map->erase(*static_cast<const typename Map::const_iterator*>(iterator));
}
dictionaryTrait.setErase([](void* instance, const void* iterator) {
static_cast<Map*>(instance)->erase(*static_cast<const typename Map::iterator*>(iterator));
});

return Type::create("std::map<" + reflect<K>().name() + ", " + reflect<V>().name() + ">")
Expand Down
14 changes: 3 additions & 11 deletions core/include/cubos/core/reflection/external/unordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,14 @@ CUBOS_REFLECT_EXTERNAL_TEMPLATE((typename K, typename V), (std::unordered_map<K,
});
}

// Since V must be moveable for all std::map<K, V>, we always supply this function.
// Since V must be moveable for all std::unordered_map<K, V>, we always supply this function.
dictionaryTrait.setInsertMove([](void* instance, const void* key, void* value) {
auto* map = static_cast<Map*>(instance);
map->emplace(*static_cast<const K*>(key), std::move(*static_cast<V*>(value)));
});

dictionaryTrait.setErase([](void* instance, const void* iterator, bool writeable) {
auto* map = static_cast<Map*>(instance);
if (writeable)
{
map->erase(*static_cast<const typename Map::iterator*>(iterator));
}
else
{
map->erase(*static_cast<const typename Map::const_iterator*>(iterator));
}
dictionaryTrait.setErase([](void* instance, const void* iterator) {
static_cast<Map*>(instance)->erase(*static_cast<const typename Map::iterator*>(iterator));
});

return Type::create("std::unordered_map<" + reflect<K>().name() + ", " + reflect<V>().name() + ">")
Expand Down
Loading

0 comments on commit ea5f292

Please sign in to comment.