-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |