diff --git a/.github/workflows/clp-core-build-macos.yaml b/.github/workflows/clp-core-build-macos.yaml index 2398b14a2..dfcc46d31 100644 --- a/.github/workflows/clp-core-build-macos.yaml +++ b/.github/workflows/clp-core-build-macos.yaml @@ -49,6 +49,7 @@ jobs: - name: "Remove preinstalled binaries which conflict with brew's installs" run: | rm -f /usr/local/bin/2to3* + rm -f /usr/local/bin/go* rm -f /usr/local/bin/idle3* rm -f /usr/local/bin/pydoc3* rm -f /usr/local/bin/python3* diff --git a/components/core/CMakeLists.txt b/components/core/CMakeLists.txt index 7c2ff25f6..09577fa31 100644 --- a/components/core/CMakeLists.txt +++ b/components/core/CMakeLists.txt @@ -480,6 +480,7 @@ set(SOURCE_FILES_unitTest src/clp/TimestampPattern.cpp src/clp/TimestampPattern.hpp src/clp/TraceableException.hpp + src/clp/TransactionManager.hpp src/clp/type_utils.hpp src/clp/utf8_utils.cpp src/clp/utf8_utils.hpp diff --git a/components/core/src/clp/TransactionManager.hpp b/components/core/src/clp/TransactionManager.hpp new file mode 100644 index 000000000..fe70f5547 --- /dev/null +++ b/components/core/src/clp/TransactionManager.hpp @@ -0,0 +1,51 @@ +#ifndef CLP_TRANSACTIONMANAGER_HPP +#define CLP_TRANSACTIONMANAGER_HPP + +#include + +namespace clp { +/** + * A class that on destruction, performs different actions depending on whether a transaction + * succeeds or fails. The default state assumes the transaction fails. + * @tparam SuccessHandler A cleanup lambda to call on success. + * @tparam FailureHandler A cleanup lambda to call on failure. + */ +template +requires(std::is_nothrow_invocable_v && std::is_nothrow_invocable_v) +class TransactionManager { +public: + // Constructor + TransactionManager(SuccessHandler success_handler, FailureHandler failure_handler) + : m_success_handler{success_handler}, + m_failure_handler{failure_handler} {} + + // Delete copy/move constructor and assignment + TransactionManager(TransactionManager const&) = delete; + TransactionManager(TransactionManager&&) = delete; + auto operator=(TransactionManager const&) -> TransactionManager& = delete; + auto operator=(TransactionManager&&) -> TransactionManager& = delete; + + // Destructor + ~TransactionManager() { + if (m_success) { + m_success_handler(); + } else { + m_failure_handler(); + } + } + + // Methods + /** + * Marks the transaction as successful. + */ + auto mark_success() -> void { m_success = true; } + +private: + // Variables + SuccessHandler m_success_handler; + FailureHandler m_failure_handler; + bool m_success{false}; +}; +} // namespace clp + +#endif // CLP_TRANSACTIONMANAGER_HPP