Implemented utility for convenient binding of paired callbacks (e.g. start/finish). 45/90245/1
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Thu, 29 Sep 2016 06:59:55 +0000 (09:59 +0300)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Thu, 29 Sep 2016 07:02:47 +0000 (10:02 +0300)
Implemented possibility to pass data between paired callbacks.

Change-Id: Iac0eb112f296ade8671cb32d7c61740e8cc02143
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
lib-apps-common/inc/Utils/CallbackPair.h [new file with mode: 0644]

diff --git a/lib-apps-common/inc/Utils/CallbackPair.h b/lib-apps-common/inc/Utils/CallbackPair.h
new file mode 100644 (file)
index 0000000..8df2753
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef UTILS_CALLBACK_PAIR_H
+#define UTILS_CALLBACK_PAIR_H
+
+#include <functional>
+
+namespace Utils
+{
+       /**
+        * @brief Represents two callbacks to be called subsequently.
+        * @details Next callback is called each time when operator() is used.
+        */
+       class CallbackPair
+       {
+       public:
+               typedef std::function<void()> Callback;
+
+               /**
+                * @brief Create callback pair.
+                * @param[in]   first   First callback
+                * @param[in]   second  Second callback
+                */
+               CallbackPair(Callback first, Callback second)
+                       : m_Callbacks{ std::move(first), std::move(second) }, m_IsFirst(true)
+               {
+               }
+
+               /**
+                * @brief Call next callback.
+                */
+               void operator()()
+               {
+                       if (m_IsFirst) {
+                               m_Callbacks.first();
+                       } else {
+                               m_Callbacks.second();
+                       }
+                       m_IsFirst = !m_IsFirst;
+               }
+
+       private:
+               std::pair<Callback, Callback> m_Callbacks;
+               bool m_IsFirst;
+       };
+
+       /**
+        * @brief Provides possibility to pass a result from first callback to the second.
+        * @see CallbackPair.
+        */
+       template <typename ResultType>
+       class CallbackPairWithResult
+       {
+       public:
+               /**
+                * @brief First callback.
+                * @return Result to pass to the second callback.
+                */
+               typedef std::function<ResultType()> FirstCallback;
+
+               /**
+                * @brief Second callback.
+                * @param[in]   Result received from the first callback.
+                */
+               typedef std::function<void(ResultType)> SecondCallback;
+
+               /**
+                * @brief Create callback pair.
+                * @param[in]   first   First callback
+                * @param[in]   second  Second callback
+                */
+               CallbackPairWithResult(FirstCallback first, SecondCallback second)
+                       : m_Callbacks{ std::move(first), std::move(second) }, m_IsFirst(true)
+               {
+               }
+
+               /**
+                * @brief Call next callback.
+                */
+               void operator()()
+               {
+                       if (m_IsFirst) {
+                               m_Result = m_Callbacks.first();
+                       } else {
+                               m_Callbacks.second(std::move(m_Result));
+                       }
+                       m_IsFirst = !m_IsFirst;
+               }
+
+       private:
+               std::pair<FirstCallback, SecondCallback> m_Callbacks;
+               ResultType m_Result;
+               bool m_IsFirst;
+       };
+}
+
+#endif /* UTILS_CALLBACK_PAIR_H */