SenseDevSpec: add NULL checks stringify, ostream::<<

This commit is contained in:
2025-01-13 07:05:05 -04:00
parent 3f9b406fb2
commit 8e94e829d0
2 changed files with 52 additions and 23 deletions
+2 -19
View File
@@ -15,33 +15,16 @@ std::vector<std::unique_ptr<ExtrospectorDeviceSpec>>
std::vector<std::reference_wrapper<SenseDeviceSpec>>
DeviceManager::senseDeviceSpecs;
std::ostream& operator<<(
std::ostream& os, const SenseDeviceSpec& spec)
{
os << "Device: " << spec.sensorType << ", Implexor: "
<< spec.implexor << ", API: " << spec.api
<< ", API Params: (";
for (auto it = spec.apiParams.begin(); it != spec.apiParams.end(); ++it) {
os << *it << (it + 1 == spec.apiParams.end() ? "" : " ");
}
os << "), Provider: " << spec.provider << ", Provider Params: (";
for (auto it = spec.providerParams.begin(); it != spec.providerParams.end(); ++it) {
os << *it << (it + 1 == spec.providerParams.end() ? "" : " ");
}
os << "), Device Selector: " << spec.deviceSelector << std::endl;
return os;
}
const std::string DeviceManager::stringifyDeviceSpecs(void)
{
std::ostringstream oss;
for (const auto& spec : DeviceManager::interoceptorDeviceSpecs) {
oss << "Interoceptor " << *spec;
oss << "Interoceptor " << CSenseDeviceSpec(*spec);
}
for (const auto& spec : DeviceManager::extrospectorDeviceSpecs) {
oss << "Extrospector " << *spec;
oss << "Extrospector " << CSenseDeviceSpec(*spec);
}
return oss.str();
+50 -4
View File
@@ -8,6 +8,7 @@
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#endif // __cplusplus
#ifdef __cplusplus
@@ -23,6 +24,13 @@ struct SenseDeviceSpec
friend std::ostream& operator<<(
std::ostream& os, const SenseDeviceSpec& spec);
bool operator==(const SenseDeviceSpec& other) const
{
return sensorType == other.sensorType &&
provider == other.provider &&
deviceSelector == other.deviceSelector;
}
};
struct InteroceptorDeviceSpec : public SenseDeviceSpec
@@ -70,18 +78,50 @@ struct CSenseDeviceSpec
free(provider);
free(deviceSelector);
for (size_t i = 0; apiParams[i] != nullptr; ++i)
if (apiParams)
{
free(apiParams[i]);
for (size_t i = 0; apiParams[i] != nullptr; ++i) {
free(apiParams[i]);
}
}
delete[] apiParams;
for (size_t i = 0; providerParams[i] != nullptr; ++i)
if (providerParams)
{
free(providerParams[i]);
for (size_t i = 0; providerParams[i] != nullptr; ++i) {
free(providerParams[i]);
}
}
delete[] providerParams;
}
std::string stringify() const
{
std::ostringstream os;
os << "Device: " << sensorType << ", Implexor: "
<< (implexor ? implexor : "NULL") << ", API: "
<< (api ? api : "NULL") << ", API Params: (";
if (apiParams)
{
for (size_t i = 0; apiParams[i] != nullptr; ++i)
{
os << apiParams[i] << (apiParams[i + 1] == nullptr ? "" : " ");
}
}
os << "), Provider: " << (provider ? provider : "NULL")
<< ", Provider Params: (";
if (providerParams)
{
for (size_t i = 0; providerParams[i] != nullptr; ++i)
{
os << providerParams[i] << (providerParams[i + 1] == nullptr ? "" : " ");
}
}
os << "), Device Selector: "
<< (deviceSelector ? deviceSelector : "NULL") << std::endl;
return os.str();
}
#endif
char sensorType;
@@ -93,6 +133,12 @@ struct CSenseDeviceSpec
char *deviceSelector;
};
inline std::ostream& operator<<(std::ostream& os, const CSenseDeviceSpec& spec)
{
os << spec.stringify();
return os;
}
#ifdef __cplusplus
}
#endif