Skip to content

Commit 9f2a3d7

Browse files
committed
--[BE] - Add ability to filter Configuration subconfigs (#2471)
* --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
1 parent 0620632 commit 9f2a3d7

File tree

6 files changed

+621
-356
lines changed

6 files changed

+621
-356
lines changed

Diff for: src/esp/bindings/ConfigBindings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void initConfigBindings(py::module& m) {
152152
R"(Returns whether or not this Configuration has the passed key. Does not check subconfigurations.)",
153153
"key"_a)
154154
.def(
155-
"has_key_to_type", &Configuration::hasKeyOfType,
155+
"has_key_to_type", &Configuration::hasKeyToValOfType,
156156
R"(Returns whether passed key points to a value of specified ConfigValType)",
157157
"key"_a, "value_type"_a)
158158
.def(

Diff for: src/esp/core/Configuration.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,52 @@ std::vector<std::string> Configuration::findValue(
783783
return breadcrumbs;
784784
}
785785

786+
void Configuration::overwriteWithConfig(
787+
const std::shared_ptr<const Configuration>& src) {
788+
if (src->getNumEntries() == 0) {
789+
return;
790+
}
791+
// copy every element over from src
792+
for (const auto& elem : src->valueMap_) {
793+
valueMap_[elem.first] = elem.second;
794+
}
795+
// merge subconfigs
796+
for (const auto& subConfig : src->configMap_) {
797+
const auto name = subConfig.first;
798+
// make if DNE and merge src subconfig
799+
addOrEditSubgroup<Configuration>(name).first->second->overwriteWithConfig(
800+
subConfig.second);
801+
}
802+
} // Configuration::overwriteWithConfig
803+
804+
void Configuration::filterFromConfig(
805+
const std::shared_ptr<const Configuration>& src) {
806+
if (src->getNumEntries() == 0) {
807+
return;
808+
}
809+
// filter out every element that is present with the same value in both src
810+
// and this.
811+
for (const auto& elem : src->valueMap_) {
812+
ValueMapType::const_iterator mapIter = valueMap_.find(elem.first);
813+
// if present and has the same data, erase this configuration's data
814+
if ((mapIter != valueMap_.end()) && (mapIter->second == elem.second)) {
815+
valueMap_.erase(mapIter);
816+
}
817+
}
818+
// repeat process on all subconfigs of src that are present in this.
819+
for (const auto& subConfig : src->configMap_) {
820+
// find if this has subconfig of same name
821+
ConfigMapType::iterator mapIter = configMap_.find(subConfig.first);
822+
if (mapIter != configMap_.end()) {
823+
mapIter->second->filterFromConfig(subConfig.second);
824+
// remove the subconfig if it has no entries after filtering
825+
if (mapIter->second->getNumEntries() == 0) {
826+
configMap_.erase(mapIter);
827+
}
828+
}
829+
}
830+
} // Configuration::filterFromConfig
831+
786832
Configuration& Configuration::operator=(const Configuration& otr) {
787833
if (this != &otr) {
788834
configMap_.clear();

Diff for: src/esp/core/Configuration.h

+17-20
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ class Configuration {
697697
: configMap_(std::move(otr.configMap_)),
698698
valueMap_(std::move(otr.valueMap_)) {} // move ctor
699699

700-
// virtual destructor set to that pybind11 recognizes attributes inheritance
700+
// virtual destructor set so that pybind11 recognizes attributes inheritance
701701
// from Configuration to be polymorphic
702702
virtual ~Configuration() = default;
703703

@@ -1163,7 +1163,8 @@ class Configuration {
11631163
* @param desiredType the @ref ConfigValType to compare the value's type to
11641164
* @return Whether @p key references a value that is of @p desiredType.
11651165
*/
1166-
bool hasKeyOfType(const std::string& key, ConfigValType desiredType) {
1166+
bool hasKeyToValOfType(const std::string& key,
1167+
ConfigValType desiredType) const {
11671168
ValueMapType::const_iterator mapIter = valueMap_.find(key);
11681169
return (mapIter != valueMap_.end() &&
11691170
(mapIter->second.getType() == desiredType));
@@ -1247,7 +1248,6 @@ class Configuration {
12471248
* @return A pointer to a copy of the Configuration having the requested
12481249
* name, cast to the appropriate type, or nullptr if not found.
12491250
*/
1250-
12511251
template <typename T>
12521252
std::shared_ptr<T> getSubconfigCopy(const std::string& cfgName) const {
12531253
static_assert(std::is_base_of<Configuration, T>::value,
@@ -1371,27 +1371,24 @@ class Configuration {
13711371

13721372
/**
13731373
* @brief Merges Configuration pointed to by @p src into this
1374-
* Configuration, including all subconfigs. Passed config overwrites
1374+
* Configuration, including all subconfigs. Passed config overwrites
13751375
* existing data in this config.
13761376
* @param src The source of Configuration data we wish to merge into this
13771377
* Configuration.
13781378
*/
1379-
void overwriteWithConfig(const std::shared_ptr<const Configuration>& src) {
1380-
if (src->getNumEntries() == 0) {
1381-
return;
1382-
}
1383-
// copy every element over from src
1384-
for (const auto& elem : src->valueMap_) {
1385-
valueMap_[elem.first] = elem.second;
1386-
}
1387-
// merge subconfigs
1388-
for (const auto& subConfig : src->configMap_) {
1389-
const auto name = subConfig.first;
1390-
// make if DNE and merge src subconfig
1391-
addOrEditSubgroup<Configuration>(name).first->second->overwriteWithConfig(
1392-
subConfig.second);
1393-
}
1394-
}
1379+
void overwriteWithConfig(const std::shared_ptr<const Configuration>& src);
1380+
1381+
/**
1382+
* @brief Performs the opposite operation to @ref Configuration::overwriteWithConfig.
1383+
* All values and subconfigs in the passed Configuration will be removed from
1384+
* this config unless the data they hold is different. Any empty subconfigs
1385+
* will be removed as well.
1386+
*
1387+
* @param src The source of Configuration data we wish to prune from this
1388+
* Configuration.
1389+
*/
1390+
1391+
void filterFromConfig(const std::shared_ptr<const Configuration>& src);
13951392

13961393
/**
13971394
* @brief Returns a const iterator across the map of values.

Diff for: src/tests/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ if(MagnumPlugins_KtxImageConverter_FOUND)
7474
target_link_libraries(GfxBatchRendererTest PRIVATE MagnumPlugins::KtxImageConverter)
7575
endif()
7676

77-
corrade_add_test(CoreTest CoreTest.cpp LIBRARIES core io)
77+
corrade_add_test(ConfigurationTest ConfigurationTest.cpp LIBRARIES core io)
7878

7979
corrade_add_test(CullingTest CullingTest.cpp LIBRARIES gfx)
8080
target_include_directories(CullingTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

0 commit comments

Comments
 (0)