@@ -305,39 +305,14 @@ bool MediaDevice::match(const DeviceMatch &device) const
if (driver_ != device.driver())
return false;
- for (const std::string &name : device.entitiesNames()) {
- bool found = false;
-
- for (const MediaEntity *entity : entities()) {
- if (name == entity->name()) {
- if (!entity->deviceNode().empty()) {
- found = true;
- break;
- } else {
- LOG(MediaDevice, Debug)
- << "Skip " << entity->name()
- << ": no device node";
- }
- }
- }
-
- if (!found)
- return false;
- }
-
- for (const std::regex &nameRegex : device.entitiesRegexs()) {
+ const auto searchEntities = [&](const auto &searchPattern,
+ const auto &searchFn, bool breakEarly) {
bool found = false;
for (const MediaEntity *entity : entities()) {
- if (!std::regex_search(entity->name(), nameRegex))
+ if (!searchFn(searchPattern, entity->name()))
continue;
- if (found) {
- LOG(MediaDevice, Error)
- << "Multiple entities match regex";
- return false;
- }
-
if (entity->deviceNode().empty()) {
LOG(MediaDevice, Debug)
<< "Skip " << entity->name()
@@ -345,10 +320,34 @@ bool MediaDevice::match(const DeviceMatch &device) const
continue;
}
+ if (found) {
+ LOG(MediaDevice, Error)
+ << "Multiple entities match "
+ << entity->name();
+ return false;
+ }
+
found = true;
+ if (breakEarly)
+ break;
}
- if (!found)
+ return found;
+ };
+
+ for (const std::string &name : device.entitiesNames()) {
+ if (!searchEntities(name, [](const std::string &s,
+ const std::string &e) {
+ return s == e;
+ }, true))
+ return false;
+ }
+
+ for (const std::regex &nameRegex : device.entitiesRegexs()) {
+ if (!searchEntities(nameRegex, [](const std::regex &r,
+ const std::string &e) {
+ return std::regex_search(e, r);
+ }, false))
return false;
}
Align the implementations of the two search loops to be identical by using a lambda function. The only difference between the two existing implementation is that with string-based matching as soon as we find a match we can break from the loop, while regex-based matching needs to walk all entities even if a match is found to make sure no multiple matches happen. To support both behaviours pass to the lambda a boolean flag that allows to interrupt the loop as soon as a match is found. Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> --- src/libcamera/media_device.cpp | 57 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 29 deletions(-)