Skip to content

Commit

Permalink
--[BE] - Add ability to filter Configuration subconfigs (#2471)
Browse files Browse the repository at this point in the history
* --add subconfig filtering process.
This function will remove any values and subconfigs from a Configuration that match those found within a passed Configuration.
* --minor naming clarification
* --expand Configuration tests to test filtering; rename test
Test only tests Configurations, so name appropriately
  • Loading branch information
jturner65 committed Sep 18, 2024
1 parent 0620632 commit 9f2a3d7
Show file tree
Hide file tree
Showing 6 changed files with 621 additions and 356 deletions.
2 changes: 1 addition & 1 deletion src/esp/bindings/ConfigBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void initConfigBindings(py::module& m) {
R"(Returns whether or not this Configuration has the passed key. Does not check subconfigurations.)",
"key"_a)
.def(
"has_key_to_type", &Configuration::hasKeyOfType,
"has_key_to_type", &Configuration::hasKeyToValOfType,
R"(Returns whether passed key points to a value of specified ConfigValType)",
"key"_a, "value_type"_a)
.def(
Expand Down
46 changes: 46 additions & 0 deletions src/esp/core/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,52 @@ std::vector<std::string> Configuration::findValue(
return breadcrumbs;
}

void Configuration::overwriteWithConfig(
const std::shared_ptr<const Configuration>& src) {
if (src->getNumEntries() == 0) {
return;
}
// copy every element over from src
for (const auto& elem : src->valueMap_) {
valueMap_[elem.first] = elem.second;
}
// merge subconfigs
for (const auto& subConfig : src->configMap_) {
const auto name = subConfig.first;
// make if DNE and merge src subconfig
addOrEditSubgroup<Configuration>(name).first->second->overwriteWithConfig(
subConfig.second);
}
} // Configuration::overwriteWithConfig

void Configuration::filterFromConfig(
const std::shared_ptr<const Configuration>& src) {
if (src->getNumEntries() == 0) {
return;
}
// filter out every element that is present with the same value in both src
// and this.
for (const auto& elem : src->valueMap_) {
ValueMapType::const_iterator mapIter = valueMap_.find(elem.first);
// if present and has the same data, erase this configuration's data
if ((mapIter != valueMap_.end()) && (mapIter->second == elem.second)) {
valueMap_.erase(mapIter);
}
}
// repeat process on all subconfigs of src that are present in this.
for (const auto& subConfig : src->configMap_) {
// find if this has subconfig of same name
ConfigMapType::iterator mapIter = configMap_.find(subConfig.first);
if (mapIter != configMap_.end()) {
mapIter->second->filterFromConfig(subConfig.second);
// remove the subconfig if it has no entries after filtering
if (mapIter->second->getNumEntries() == 0) {
configMap_.erase(mapIter);
}
}
}
} // Configuration::filterFromConfig

Configuration& Configuration::operator=(const Configuration& otr) {
if (this != &otr) {
configMap_.clear();
Expand Down
37 changes: 17 additions & 20 deletions src/esp/core/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ class Configuration {
: configMap_(std::move(otr.configMap_)),
valueMap_(std::move(otr.valueMap_)) {} // move ctor

// virtual destructor set to that pybind11 recognizes attributes inheritance
// virtual destructor set so that pybind11 recognizes attributes inheritance
// from Configuration to be polymorphic
virtual ~Configuration() = default;

Expand Down Expand Up @@ -1163,7 +1163,8 @@ class Configuration {
* @param desiredType the @ref ConfigValType to compare the value's type to
* @return Whether @p key references a value that is of @p desiredType.
*/
bool hasKeyOfType(const std::string& key, ConfigValType desiredType) {
bool hasKeyToValOfType(const std::string& key,
ConfigValType desiredType) const {
ValueMapType::const_iterator mapIter = valueMap_.find(key);
return (mapIter != valueMap_.end() &&
(mapIter->second.getType() == desiredType));
Expand Down Expand Up @@ -1247,7 +1248,6 @@ class Configuration {
* @return A pointer to a copy of the Configuration having the requested
* name, cast to the appropriate type, or nullptr if not found.
*/

template <typename T>
std::shared_ptr<T> getSubconfigCopy(const std::string& cfgName) const {
static_assert(std::is_base_of<Configuration, T>::value,
Expand Down Expand Up @@ -1371,27 +1371,24 @@ class Configuration {

/**
* @brief Merges Configuration pointed to by @p src into this
* Configuration, including all subconfigs. Passed config overwrites
* Configuration, including all subconfigs. Passed config overwrites
* existing data in this config.
* @param src The source of Configuration data we wish to merge into this
* Configuration.
*/
void overwriteWithConfig(const std::shared_ptr<const Configuration>& src) {
if (src->getNumEntries() == 0) {
return;
}
// copy every element over from src
for (const auto& elem : src->valueMap_) {
valueMap_[elem.first] = elem.second;
}
// merge subconfigs
for (const auto& subConfig : src->configMap_) {
const auto name = subConfig.first;
// make if DNE and merge src subconfig
addOrEditSubgroup<Configuration>(name).first->second->overwriteWithConfig(
subConfig.second);
}
}
void overwriteWithConfig(const std::shared_ptr<const Configuration>& src);

/**
* @brief Performs the opposite operation to @ref Configuration::overwriteWithConfig.
* All values and subconfigs in the passed Configuration will be removed from
* this config unless the data they hold is different. Any empty subconfigs
* will be removed as well.
*
* @param src The source of Configuration data we wish to prune from this
* Configuration.
*/

void filterFromConfig(const std::shared_ptr<const Configuration>& src);

/**
* @brief Returns a const iterator across the map of values.
Expand Down
2 changes: 1 addition & 1 deletion src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ if(MagnumPlugins_KtxImageConverter_FOUND)
target_link_libraries(GfxBatchRendererTest PRIVATE MagnumPlugins::KtxImageConverter)
endif()

corrade_add_test(CoreTest CoreTest.cpp LIBRARIES core io)
corrade_add_test(ConfigurationTest ConfigurationTest.cpp LIBRARIES core io)

corrade_add_test(CullingTest CullingTest.cpp LIBRARIES gfx)
target_include_directories(CullingTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
Loading

0 comments on commit 9f2a3d7

Please sign in to comment.