Change static flat_map to constexpr array of pairs
This changes a hardcoded static flat_map to a compile-time defined
constexpr array of pairs to reduce binary space.
Tested:
Confirmed that 'ipmitool sdr elist' shows the correct units
Change-Id: Id66d8c3f7324c39360ccb083fb3b5f3d154ac215
Signed-off-by: Jason M. Bills <jason.m.bills@intel.com>
diff --git a/src/sensorcommands.cpp b/src/sensorcommands.cpp
index 0fb04f7..d1c4b9d 100644
--- a/src/sensorcommands.cpp
+++ b/src/sensorcommands.cpp
@@ -78,20 +78,12 @@
static boost::container::flat_map<std::string, ManagedObjectType> SensorCache;
-// Specify the comparison required to sort and find char* map objects
-struct CmpStr
-{
- bool operator()(const char* a, const char* b) const
- {
- return std::strcmp(a, b) < 0;
- }
-};
-const static boost::container::flat_map<const char*, SensorUnits, CmpStr>
- sensorUnits{{{"temperature", SensorUnits::degreesC},
- {"voltage", SensorUnits::volts},
- {"current", SensorUnits::amps},
- {"fan_tach", SensorUnits::rpm},
- {"power", SensorUnits::watts}}};
+constexpr static std::array<std::pair<const char*, SensorUnits>, 5> sensorUnits{
+ {{"temperature", SensorUnits::degreesC},
+ {"voltage", SensorUnits::volts},
+ {"current", SensorUnits::amps},
+ {"fan_tach", SensorUnits::rpm},
+ {"power", SensorUnits::watts}}};
void registerSensorFunctions() __attribute__((constructor));
@@ -1357,13 +1349,13 @@
record.body.sensor_capabilities = 0x68; // auto rearm - todo hysteresis
record.body.sensor_type = getSensorTypeFromPath(path);
std::string type = getSensorTypeStringFromPath(path);
- auto typeCstr = type.c_str();
- auto findUnits = sensorUnits.find(typeCstr);
- if (findUnits != sensorUnits.end())
+ for (const auto& [unitsType, units] : sensorUnits)
{
- record.body.sensor_units_2_base =
- static_cast<uint8_t>(findUnits->second);
- } // else default 0x0 unspecified
+ if (type == unitsType)
+ {
+ record.body.sensor_units_2_base = static_cast<uint8_t>(units);
+ }
+ }
record.body.event_reading_type = getSensorEventTypeFromPath(path);