1 #pragma once 2 3 #include "interfaces/sensor.hpp" 4 5 #include <boost/container/flat_map.hpp> 6 #include <boost/system/error_code.hpp> 7 8 #include <iostream> 9 #include <memory> 10 #include <string_view> 11 12 class SensorCache 13 { 14 public: 15 template <class SensorType, class... Args> 16 std::shared_ptr<SensorType> makeSensor(std::string_view service, 17 std::string_view path, 18 Args&&... args) 19 { 20 cleanupExpiredSensors(); 21 22 auto id = SensorType::makeId(service, path); 23 auto it = sensors.find(id); 24 25 if (it == sensors.end()) 26 { 27 auto sensor = std::make_shared<SensorType>( 28 std::move(id), std::forward<Args>(args)...); 29 30 sensors[sensor->id()] = sensor; 31 32 return sensor; 33 } 34 35 return std::static_pointer_cast<SensorType>(it->second.lock()); 36 } 37 38 private: 39 using SensorsContainer = 40 boost::container::flat_map<interfaces::Sensor::Id, 41 std::weak_ptr<interfaces::Sensor>>; 42 43 SensorsContainer sensors; 44 45 SensorsContainer::iterator findExpiredSensor(SensorsContainer::iterator); 46 void cleanupExpiredSensors(); 47 }; 48