Skip to content

Commit

Permalink
#128 Extract transaction related definitions in transaction.inl
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikTH committed Aug 24, 2023
1 parent 80b381d commit daa924b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 31 deletions.
60 changes: 60 additions & 0 deletions include/ureact/detail/transaction.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Copyright (C) 2014-2017 Sebastian Jeckel.
// Copyright (C) 2020-2023 Krylov Yaroslav.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef UREACT_DETAIL_TRANSACTION_INL
#define UREACT_DETAIL_TRANSACTION_INL

#include <cassert>

#include <ureact/context.hpp>
#include <ureact/detail/defines.hpp>
#include <ureact/detail/graph_impl.hpp>

UREACT_BEGIN_NAMESPACE

UREACT_FUNC transaction::transaction( context ctx )
: m_context( std::move( ctx ) )
{
auto& graph = get_internals( m_context ).get_graph();
assert( !graph.is_propagation_in_progress()
&& "Can't start transaction in the middle of the change propagation process" );
graph.start_transaction();
}

UREACT_FUNC transaction::~transaction()
{
finish_impl();
}

UREACT_FUNC void transaction::finish()
{
finish_impl();
}

UREACT_FUNC void transaction::finish_impl()
{
if( !m_finished )
{
get_internals( m_context ).get_graph().finish_transaction();
m_finished = true;
}
}

namespace default_context
{

UREACT_FUNC default_transaction::default_transaction()
: transaction( default_context::get() )
{}

} // namespace default_context

UREACT_END_NAMESPACE

#endif //UREACT_DETAIL_TRANSACTION_INL
41 changes: 10 additions & 31 deletions include/ureact/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,25 @@

#include <ureact/context.hpp>
#include <ureact/detail/defines.hpp>
#include <ureact/detail/graph_impl.hpp>

UREACT_BEGIN_NAMESPACE

/*!
* @brief Guard class to perform several changes atomically
*
*/
class UREACT_WARN_UNUSED_RESULT transaction
{
public:
explicit transaction( context ctx )
: m_context( std::move( ctx ) )
{
auto& graph = get_internals( m_context ).get_graph();
assert( !graph.is_propagation_in_progress()
&& "Can't start transaction in the middle of the change propagation process" );
graph.start_transaction();
}

~transaction()
{
finish_impl();
}
explicit transaction( context ctx );
~transaction();

/*!
* @brief Finish transaction before code scope is ended
*/
void finish()
{
finish_impl();
}
void finish();

private:
void finish_impl()
{
if( !m_finished )
{
get_internals( m_context ).get_graph().finish_transaction();
m_finished = true;
}
}
void finish_impl();

UREACT_MAKE_NONCOPYABLE( transaction );
UREACT_MAKE_NONMOVABLE( transaction );
Expand All @@ -71,15 +48,17 @@ namespace default_context
* Named differently from "transaction", so it is possible to use them both,
* where both ureact and ureact::default_context namespaces are used
*/
struct UREACT_WARN_UNUSED_RESULT default_transaction : ureact::transaction
struct UREACT_WARN_UNUSED_RESULT default_transaction : public transaction
{
default_transaction()
: ureact::transaction( default_context::get() )
{}
default_transaction();
};

} // namespace default_context

UREACT_END_NAMESPACE

#if UREACT_HEADER_ONLY
# include <ureact/detail/transaction.inl>
#endif

#endif // UREACT_TRANSACTION_HPP

0 comments on commit daa924b

Please sign in to comment.