forked from y-scope/clp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Extract
TransactionManager
from `clp::ffi::ir_stream::Deseria…
…lizer`; Improve class' docs and require nothrow for its handlers. (y-scope#548) Co-authored-by: kirkrodrigues <[email protected]>
- Loading branch information
1 parent
9202761
commit 035449a
Showing
3 changed files
with
55 additions
and
46 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
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 |
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