diff options
author | Sven Gothel <[email protected]> | 2021-08-05 08:40:18 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-08-05 08:40:18 +0200 |
commit | d3f225b2bf987c27802cc70183ec500e20349ea8 (patch) | |
tree | c7ac8d4695a145f3566af87d43d6b561241d3df9 /api | |
parent | 387647583ff2dd6a420ab60dd1f0225898f8b1c5 (diff) |
BTDeviceRegistry::DeviceQuery: Use explicit enum type distinguishing address or name to allow empty EUI48Sub for all devices
Diffstat (limited to 'api')
-rw-r--r-- | api/direct_bt/BTDeviceRegistry.hpp | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/api/direct_bt/BTDeviceRegistry.hpp b/api/direct_bt/BTDeviceRegistry.hpp index 1ddae11b..d07af857 100644 --- a/api/direct_bt/BTDeviceRegistry.hpp +++ b/api/direct_bt/BTDeviceRegistry.hpp @@ -42,15 +42,31 @@ namespace direct_bt { * Specifies devices queries to act upon. */ struct DeviceQuery { + /** + * DeviceQuery type, i.e. EUI48Sub or a std::string name. + */ + enum class Type : int { + /** DeviceQuery type, using a sensor device EUI48Sub. */ + EUI48SUB, + /** DeviceQuery type, using a sensor device std::string name. */ + NAME + }; + + Type type; EUI48Sub addressSub; std::string nameSub; - DeviceQuery(const EUI48Sub& as, const std::string& ns) : addressSub(as), nameSub(ns) {} - DeviceQuery() : addressSub(), nameSub() {} + + DeviceQuery(const EUI48Sub& as) : type(Type::EUI48SUB), addressSub(as), nameSub(as.toString()) {} + + DeviceQuery(const std::string& ns) : type(Type::NAME), addressSub(EUI48Sub::ANY_DEVICE), nameSub(ns) {} + + bool isEUI48Sub() const noexcept { return Type::EUI48SUB == type; } + std::string toString() const { - if( addressSub.length>0 ) { - return addressSub.toString(); + if( Type::EUI48SUB == type ) { + return "[a: "+addressSub.toString()+"]"; } else { - return "'"+nameSub+"'"; + return "[n: '"+nameSub+"']"; } } }; @@ -136,8 +152,7 @@ namespace direct_bt { * Example (lambda): * <pre> * [](const EUI48& a, const std::string& n, const DeviceQuery& q)->bool { - * return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - * ( q.nameSub.length()>0 && n.find(q.nameSub) != std::string::npos ); + * return q.isEUI48Sub() ? a.contains(q.addressSub) : n.find(q.nameSub) != std::string::npos; * }); * </pre> * </p> @@ -169,8 +184,7 @@ namespace direct_bt { */ bool isWaitingForDevice(const EUI48 &address, const std::string &name) { return isWaitingForDevice(address, name, [](const EUI48& a, const std::string& n, const DeviceQuery& q)->bool { - return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - ( q.nameSub.length()>0 && n.find(q.nameSub) != std::string::npos ); + return q.isEUI48Sub() ? a.contains(q.addressSub) : n.find(q.nameSub) != std::string::npos; }); } @@ -199,8 +213,7 @@ namespace direct_bt { */ bool areAllDevicesProcessed() { return areAllDevicesProcessed( [](const EUI48& a, const std::string& n, const DeviceQuery& q)->bool { - return ( q.addressSub.length>0 && a.contains(q.addressSub) ) || - ( q.nameSub.length()>0 && n.find(q.nameSub) != std::string::npos ); + return q.isEUI48Sub() ? a.contains(q.addressSub) : n.find(q.nameSub) != std::string::npos; }); } |