From 035449a932690214b6df77a2c2e5d118dacb6c87 Mon Sep 17 00:00:00 2001 From: Lin Zhihao <59785146+LinZhihao-723@users.noreply.github.com> Date: Fri, 4 Oct 2024 17:45:42 -0400 Subject: [PATCH] core: Extract `TransactionManager` from `clp::ffi::ir_stream::Deserializer`; Improve class' docs and require nothrow for its handlers. (#548) Co-authored-by: kirkrodrigues <2454684+kirkrodrigues@users.noreply.github.com> --- components/core/CMakeLists.txt | 1 + .../core/src/clp/TransactionManager.hpp | 51 +++++++++++++++++++ .../src/clp/ffi/ir_stream/Deserializer.cpp | 49 ++---------------- 3 files changed, 55 insertions(+), 46 deletions(-) create mode 100644 components/core/src/clp/TransactionManager.hpp diff --git a/components/core/CMakeLists.txt b/components/core/CMakeLists.txt index 67eb80040..178f3e529 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 diff --git a/components/core/src/clp/ffi/ir_stream/Deserializer.cpp b/components/core/src/clp/ffi/ir_stream/Deserializer.cpp index 11034c4a6..5402f9b98 100644 --- a/components/core/src/clp/ffi/ir_stream/Deserializer.cpp +++ b/components/core/src/clp/ffi/ir_stream/Deserializer.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -14,6 +13,7 @@ #include "../../ReaderInterface.hpp" #include "../../time_types.hpp" +#include "../../TransactionManager.hpp" #include "../KeyValuePairLogEvent.hpp" #include "decoding_methods.hpp" #include "ir_unit_deserialization_methods.hpp" @@ -22,49 +22,6 @@ namespace clp::ffi::ir_stream { namespace { -/** - * Class to perform 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_invocable_v && std::is_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}; -}; - /** * @param tag * @return Whether the tag represents a schema tree node. @@ -122,8 +79,8 @@ auto Deserializer::deserialize_to_next_log_event(clp::ReaderInterface& reader auto const utc_offset_snapshot{m_utc_offset}; m_schema_tree->take_snapshot(); TransactionManager revert_manager{ - []() -> void {}, - [&]() -> void { + []() noexcept -> void {}, + [&]() noexcept -> void { m_utc_offset = utc_offset_snapshot; m_schema_tree->revert(); }