Skip to content

Commit

Permalink
Merge from main
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Oct 4, 2024
2 parents 8d81f37 + 035449a commit 7fd57f5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/clp-core-build-macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
1 change: 1 addition & 0 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions components/core/src/clp/TransactionManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef CLP_TRANSACTIONMANAGER_HPP
#define CLP_TRANSACTIONMANAGER_HPP

#include <type_traits>

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 <typename SuccessHandler, typename FailureHandler>
requires(std::is_nothrow_invocable_v<SuccessHandler> && std::is_nothrow_invocable_v<FailureHandler>)
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

0 comments on commit 7fd57f5

Please sign in to comment.