Skip to content

Commit

Permalink
Add filtering option for add_database (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
DefaultRyan authored Mar 11, 2021
1 parent 878f8de commit 1cd14e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/impl/winmd_reader/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ namespace winmd::reader

// This won't invalidate any existing database or row_base (e.g. TypeDef) instances
// However, it may invalidate iterators and references to namespace_members, because those are stored in std::vector
void add_database(std::string_view const& file)
template <typename TypeFilter>
void add_database(std::string_view const& file, TypeFilter filter)
{
auto& db = m_databases.emplace_back(file, this);
for (auto&& type : db.TypeDef)
{
if (type.Flags().value == 0 || is_nested(type))
if (type.Flags().value == 0 || is_nested(type) || !filter(type))
{
continue;
}
Expand All @@ -182,6 +183,11 @@ namespace winmd::reader
}
}

void add_database(std::string_view const& file)
{
add_database(file, default_type_filter{});
}

std::vector<TypeDef> const& nested_types(TypeDef const& enclosing_type) const
{
auto it = m_nested_types.find(enclosing_type);
Expand Down
26 changes: 26 additions & 0 deletions test/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ TEST_CASE("cache_add")
REQUIRE(JsonValue.TypeNamespace() == "Windows.Data.Json");
}

TEST_CASE("cache_add_filter")
{
std::filesystem::path winmd_dir = get_local_winmd_path();
auto file_path = winmd_dir;
file_path.append("Windows.Foundation.winmd");

cache c(file_path.string());

TypeDef JsonValue = c.find("Windows.Data.Json", "JsonValue");
REQUIRE(!JsonValue);

file_path = winmd_dir;
file_path.append("Windows.Data.winmd");
c.add_database(file_path.string(), [](TypeDef const& type)
{
return !(type.TypeNamespace() == "Windows.Data.Json" && type.TypeName() == "JsonArray");
});

JsonValue = c.find("Windows.Data.Json", "JsonValue");
REQUIRE(!!JsonValue);
REQUIRE(JsonValue.TypeName() == "JsonValue");
REQUIRE(JsonValue.TypeNamespace() == "Windows.Data.Json");

REQUIRE(!c.find("Windows.Data.Json", "JsonArray"));
}

TEST_CASE("cache_add_duplicate")
{
std::filesystem::path winmd_dir = get_local_winmd_path();
Expand Down

0 comments on commit 1cd14e6

Please sign in to comment.