Saving current context in DeferrableCallback added.

DeferrableCallback operator() saves the context in
self instance. When the timer expires, it calls
operator() for the ConditionalCallback with the
context saved in DeferrableCallback instance.

Resolves openbmc/phosphor-dbus-monitor#1

Change-Id: I8d235b0747005c403829f8262d0290548b0a9910
Signed-off-by: Alexander Soldatov <a.soldatov@yadro.com>
diff --git a/src/callback.hpp b/src/callback.hpp
index 0be7dc0..b1c8d0b 100644
--- a/src/callback.hpp
+++ b/src/callback.hpp
@@ -227,6 +227,12 @@
     {
     }
 
+    /** @brief Start internal timer if the condition is satisfied.
+     *
+     * When the timer expires, it calls operator() for the
+     * ConditionalCallback with the context saved in
+     * DeferrableCallback instance.
+     */
     void operator()(Context ctx) override
     {
         if (!timer)
@@ -234,8 +240,10 @@
             timer = std::make_unique<TimerType>(
                 sdeventplus::Event::get_default(),
                 // **INDENT-OFF**
-                [ctx, this](auto& source) {
-                    this->ConditionalCallback<CallbackAccess>::operator()(ctx);
+                [this](auto& source) {
+                    // The timer uses the context saved on timer enable
+                    this->ConditionalCallback<CallbackAccess>::operator()(
+                        this->ctx);
                 });
             // **INDENT-ON**
         }
@@ -245,6 +253,8 @@
             if (!timer->isEnabled())
             {
                 // This is the first time the condition evaluated.
+                // Save current context for timer use.
+                this->ctx = ctx;
                 // Start the countdown.
                 timer->restartOnce(delayInterval);
             }
@@ -262,6 +272,9 @@
 
     /** @brief Delegated timer functions. */
     std::unique_ptr<TimerType> timer;
+
+    /** @brief Current context for timer. */
+    Context ctx;
 };
 
 } // namespace monitoring