${PROJECT_SOURCE_DIR}/modules/core/include/dpl/preprocessor.h
${PROJECT_SOURCE_DIR}/modules/core/include/dpl/read_write_mutex.h
${PROJECT_SOURCE_DIR}/modules/core/include/dpl/recursive_mutex.h
+ ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/scope_guard.h
${PROJECT_SOURCE_DIR}/modules/core/include/dpl/scoped_resource.h
${PROJECT_SOURCE_DIR}/modules/core/include/dpl/scoped_array.h
${PROJECT_SOURCE_DIR}/modules/core/include/dpl/scoped_close.h
#define DPL_MACRO_CONCAT_IMPL(x, y) x##y
#define DPL_MACRO_CONCAT(x, y) DPL_MACRO_CONCAT_IMPL(x, y)
+#ifdef __COUNTER__
+#define DPL_ANONYMOUS_VARIABLE(name) DPL_MACRO_CONCAT(name, __COUNTER__)
+#else
+#define DPL_ANONYMOUS_VARIABLE(name) DPL_MACRO_CONCAT(name, __LINE__)
+#endif
+
#endif //DPL_PREPROCESSOR_H
--- /dev/null
+/*
+ * Copyright 2013 Facebook, Inc.
+ *
+ * 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.
+ */
+/*!
+ * @file scope_guard.h
+ * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
+ * @version 1.0
+ * @brief This file is the implementation file of scope guard RAII
+ */
+#ifndef DPL_SCOPE_GUARD_H_
+#define DPL_SCOPE_GUARD_H_
+
+#include <cstddef>
+#include <utility>
+#include <new>
+#include <type_traits>
+#include <dpl/preprocessor.h>
+
+namespace DPL {
+template<typename FunctionType>
+class ScopeGuard
+{
+ public:
+ explicit ScopeGuard(const FunctionType& function)
+ : m_function{function},
+ m_released{false}
+ {}
+
+ explicit ScopeGuard(FunctionType&& function)
+ : m_function{std::move(function)},
+ m_released{false}
+ {}
+
+ ScopeGuard(ScopeGuard&& other)
+ : m_function{std::move(other.m_function)},
+ m_released{other.m_released}
+ {
+ other.Release();
+ }
+
+ ScopeGuard(const ScopeGuard&) = delete;
+
+ ~ScopeGuard()
+ {
+ if (!m_released)
+ {
+ Execute();
+ }
+ }
+
+ ScopeGuard& operator=(const ScopeGuard&) = delete;
+
+ void Release()
+ {
+ m_released = true;
+ }
+
+ void* operator new(size_t) = delete;
+
+ private:
+ // FIXME change to noexcept when available
+ void Execute() throw()
+ {
+ m_function();
+ }
+
+ FunctionType m_function;
+ bool m_released;
+};
+
+template<typename FunctionType>
+ScopeGuard<typename std::decay<FunctionType>::type>
+MakeScopeGuard(FunctionType&& function)
+{
+ return ScopeGuard<typename std::decay<FunctionType>::type>(
+ std::forward<FunctionType>(function));
+}
+
+namespace detail {
+enum class ScopeGuardOnExit {};
+
+template <typename FunctionType>
+ScopeGuard<typename std::decay<FunctionType>::type>
+operator+(detail::ScopeGuardOnExit, FunctionType&& function)
+{
+ return ScopeGuard<typename std::decay<FunctionType>::type>(
+ std::forward<FunctionType>(function));
+}
+}
+}
+
+// FIXME provide support for compilers not supporting variadic macros
+#define DPL_SCOPE_EXIT(...) \
+ auto DPL_ANONYMOUS_VARIABLE(DPL_SCOPE_EXIT_STATE) \
+ = ::DPL::detail::ScopeGuardOnExit() + [__VA_ARGS__]()
+
+#endif // DPL_SCOPE_GUARD_H_
${TESTS_DIR}/core/test_thread.cpp
${TESTS_DIR}/core/test_type_list.cpp
${TESTS_DIR}/core/test_zip_input.cpp
+ ${TESTS_DIR}/core/test_scope_guard.cpp
)
WRT_TEST_ADD_INTERNAL_DEPENDENCIES(${TARGET_NAME} ${TARGET_DPL_EFL})
--- /dev/null
+/*
+ * Copyright (c) 2011 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.
+ */
+/*
+ * @file test_scope_guard.cpp
+ * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
+ * @version 1.0
+ * @brief This file is the implementation file of test scope guard
+ */
+#include <dpl/test/test_runner.h>
+#include <dpl/scope_guard.h>
+
+namespace {
+bool g_guardCalled = false;
+
+void regularFunction()
+{
+ g_guardCalled = true;
+}
+
+struct Functor
+{
+ explicit Functor(bool& guardCalled)
+ : m_guardCalled(guardCalled)
+ {}
+
+ void operator()()
+ {
+ m_guardCalled = true;
+ }
+
+ bool& m_guardCalled;
+};
+}
+
+RUNNER_TEST_GROUP_INIT(DPL)
+
+/*
+Name: ScopeGuard_MakeScopeGuard_RegularFunction
+Description: tests scope guard, created explicitly, with regular function
+Expected: guard should be called
+*/
+RUNNER_TEST(ScopeGuard_MakeScopeGuard_RegularFunction)
+{
+ g_guardCalled = false;
+ {
+ auto guard = DPL::MakeScopeGuard(regularFunction);
+ }
+ RUNNER_ASSERT(g_guardCalled);
+}
+
+/*
+Name: ScopeGuard_MakeScopeGuard_Functor
+Description: tests scope guard, created explicitly, with a functor
+Expected: guard should be called
+*/
+RUNNER_TEST(ScopeGuard_MakeScopeGuard_Functor)
+{
+ g_guardCalled = false;
+ {
+ auto guard = DPL::MakeScopeGuard(Functor(g_guardCalled));
+ }
+ RUNNER_ASSERT(g_guardCalled);
+}
+
+/*
+Name: ScopeGuard_MakeScopeGuard_Lambda
+Description: tests scope guard, created explicitly, with a lambda
+Expected: guard should be called
+*/
+RUNNER_TEST(ScopeGuard_MakeScopeGuard_Lambda)
+{
+ g_guardCalled = false;
+ {
+ auto guard = DPL::MakeScopeGuard(
+ [&g_guardCalled](){ g_guardCalled = true; });
+ }
+ RUNNER_ASSERT(g_guardCalled);
+}
+
+/*
+Name: ScopeGuard_Macro
+Description: tests scope guard, created implicitly, with a lambda
+Expected: guard should be called
+*/
+RUNNER_TEST(ScopeGuard_Macro)
+{
+ g_guardCalled = false;
+ {
+ DPL_SCOPE_EXIT(&g_guardCalled)
+ {
+ g_guardCalled = true;
+ };
+ }
+ RUNNER_ASSERT(g_guardCalled);
+}
+
+/*
+Name: ScopeGuard_Release
+Description: tests scope guard releasing API
+Expected: guard should not be called
+*/
+RUNNER_TEST(ScopeGuard_Release)
+{
+ g_guardCalled = false;
+ {
+ auto guard = DPL::MakeScopeGuard(
+ [&g_guardCalled](){ g_guardCalled = true; });
+ guard.Release();
+ }
+ RUNNER_ASSERT(!g_guardCalled);
+}