From e824f985e7e32923d9f76e3ad5baf4909880739f Mon Sep 17 00:00:00 2001 From: Matt Spinler Date: Thu, 11 May 2017 10:07:55 -0500 Subject: Use unique_ptr for sd_event object wrapper Convert the sd_event object wrapper from a shared_ptr to a unique_ptr. Requires a new header file. Change-Id: I868a9e88ed93878c2e0bb12e58f8d3a604510da0 Signed-off-by: Matt Spinler --- event.hpp | 41 +++++++++++++++++++++++++++++++++++++++++ monitor/fan.cpp | 2 +- monitor/fan.hpp | 3 ++- monitor/main.cpp | 12 ++++-------- monitor/tach_sensor.cpp | 2 +- monitor/tach_sensor.hpp | 3 ++- test/timertest.cpp | 13 ++++--------- timer.cpp | 2 +- timer.hpp | 19 ++++--------------- 9 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 event.hpp diff --git a/event.hpp b/event.hpp new file mode 100644 index 0000000..71ca6a6 --- /dev/null +++ b/event.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include +#include + +namespace phosphor +{ +namespace fan +{ +namespace event +{ + +/** + * Custom deleter for sd_event_source + */ +struct EventSourceDeleter +{ + void operator()(sd_event_source* eventSource) const + { + sd_event_source_unref(eventSource); + } +}; + +using EventSourcePtr = std::unique_ptr; + +/** + * Customer deleter for sd_event + */ +struct EventDeleter +{ + void operator()(sd_event* event) const + { + sd_event_unref(event); + } +}; + +using EventPtr = std::unique_ptr; + +} +} +} diff --git a/monitor/fan.cpp b/monitor/fan.cpp index 639d88e..17f5812 100644 --- a/monitor/fan.cpp +++ b/monitor/fan.cpp @@ -38,7 +38,7 @@ constexpr auto OPERATIONAL_STATUS_INTF = Fan::Fan(sdbusplus::bus::bus& bus, - std::shared_ptr& events, + phosphor::fan::event::EventPtr& events, const FanDefinition& def) : _bus(bus), _name(std::get(def)), diff --git a/monitor/fan.hpp b/monitor/fan.hpp index 910b4eb..8e7f86f 100644 --- a/monitor/fan.hpp +++ b/monitor/fan.hpp @@ -3,6 +3,7 @@ #include #include #include +#include "event.hpp" #include "tach_sensor.hpp" #include "types.hpp" @@ -72,7 +73,7 @@ class Fan * @param def - the fan definition structure */ Fan(sdbusplus::bus::bus& bus, - std::shared_ptr& events, + phosphor::fan::event::EventPtr& events, const FanDefinition& def); /** diff --git a/monitor/main.cpp b/monitor/main.cpp index 6d51fbb..c10efb9 100644 --- a/monitor/main.cpp +++ b/monitor/main.cpp @@ -15,6 +15,7 @@ */ #include #include +#include "event.hpp" #include "fan.hpp" #include "fan_defs.hpp" @@ -22,16 +23,11 @@ using namespace phosphor::fan::monitor; using namespace phosphor::logging; -void EventDeleter(sd_event* event) -{ - sd_event_unref(event); -} - int main() { auto bus = sdbusplus::bus::new_default(); sd_event* events = nullptr; - std::vector> fans; + std::vector fans; auto r = sd_event_default(&events); if (r < 0) @@ -41,7 +37,7 @@ int main() return -1; } - std::shared_ptr eventPtr{events, EventDeleter}; + phosphor::fan::event::EventPtr eventPtr{events}; //Attach the event object to the bus object so we can //handle both sd_events (for the timers) and dbus signals. @@ -49,7 +45,7 @@ int main() for (const auto& fanDef : fanDefinitions) { - fans.emplace_back(std::make_unique(bus, eventPtr, fanDef)); + fans.emplace_back(bus, eventPtr, fanDef); } r = sd_event_loop(eventPtr.get()); diff --git a/monitor/tach_sensor.cpp b/monitor/tach_sensor.cpp index 806eed7..80eb974 100644 --- a/monitor/tach_sensor.cpp +++ b/monitor/tach_sensor.cpp @@ -85,7 +85,7 @@ TachSensor::TachSensor(sdbusplus::bus::bus& bus, const std::string& id, bool hasTarget, size_t timeout, - std::shared_ptr& events) : + phosphor::fan::event::EventPtr& events) : _bus(bus), _fan(fan), _name(FAN_SENSOR_PATH + id), diff --git a/monitor/tach_sensor.hpp b/monitor/tach_sensor.hpp index 7a65b64..84e02fe 100644 --- a/monitor/tach_sensor.hpp +++ b/monitor/tach_sensor.hpp @@ -3,6 +3,7 @@ #include #include #include +#include "event.hpp" #include "timer.hpp" namespace phosphor @@ -54,7 +55,7 @@ class TachSensor const std::string& id, bool hasTarget, size_t timeout, - std::shared_ptr& events); + phosphor::fan::event::EventPtr& events); /** * @brief Returns the target speed value diff --git a/test/timertest.cpp b/test/timertest.cpp index 40b2ddf..9d05e55 100644 --- a/test/timertest.cpp +++ b/test/timertest.cpp @@ -16,6 +16,7 @@ #include #include #include +#include "event.hpp" #include "timer.hpp" /** @@ -25,12 +26,6 @@ using namespace phosphor::fan::util; using namespace std::chrono; -void EventDeleter(sd_event* events) -{ - sd_event_unref(events); -} - -using EventPtr = std::shared_ptr; /** * Class to ensure sd_events are correctly @@ -40,7 +35,7 @@ class TimerTest : public ::testing::Test { public: // systemd event handler - EventPtr events; + phosphor::fan::event::EventPtr events; // Need this so that events can be initialized. int rc; @@ -52,7 +47,7 @@ class TimerTest : public ::testing::Test auto rc = sd_event_default(&event); EXPECT_GE(rc, 0); - events.reset(event, EventDeleter); + events.reset(event); } }; @@ -96,7 +91,7 @@ class CallbackTester class CallbackTesterWithTimer : public CallbackTester { public: - CallbackTesterWithTimer(EventPtr events) : + CallbackTesterWithTimer(phosphor::fan::event::EventPtr& events) : _timer(events, std::bind(&CallbackTesterWithTimer::callbackFunction, this)) diff --git a/timer.cpp b/timer.cpp index 41a8138..d371c98 100644 --- a/timer.cpp +++ b/timer.cpp @@ -27,7 +27,7 @@ namespace util using namespace phosphor::logging; -Timer::Timer(EventPtr& events, +Timer::Timer(phosphor::fan::event::EventPtr& events, std::function callbackFunc) : timeEvent(events), callback(callbackFunc), diff --git a/timer.hpp b/timer.hpp index ea28176..979a1cc 100644 --- a/timer.hpp +++ b/timer.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include "event.hpp" namespace phosphor { @@ -12,17 +12,6 @@ namespace fan namespace util { -struct EventSourceDeleter -{ - void operator()(sd_event_source* eventSource) const - { - sd_event_source_unref(eventSource); - } -}; - -using EventSourcePtr = std::unique_ptr; - -using EventPtr = std::shared_ptr; /** * @class Timer @@ -60,7 +49,7 @@ class Timer * @param[in] events - sd_event pointer, previously created * @param[in] callbackFunc - The function to call on timer expiration */ - Timer(EventPtr& events, + Timer(phosphor::fan::event::EventPtr& events, std::function callbackFunc); /** @@ -148,12 +137,12 @@ class Timer /** * @brief The sd_event structure */ - EventPtr timeEvent; + phosphor::fan::event::EventPtr& timeEvent; /** * @brief Source of events */ - EventSourcePtr eventSource; + phosphor::fan::event::EventSourcePtr eventSource; /** * @brief Either 'repeating' or 'oneshot' -- cgit v1.2.1