Skip to content

Commit

Permalink
#128 Extract node_base definition in node_base.inl
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikTH committed Aug 26, 2023
1 parent 2e2fba3 commit 462d19b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 38 deletions.
50 changes: 12 additions & 38 deletions include/ureact/detail/node_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define UREACT_DETAIL_NODE_BASE_HPP

#include <memory>
#include <tuple>
#include <utility>

#include <ureact/context.hpp>
Expand Down Expand Up @@ -41,18 +42,9 @@ Ret create_wrapped_node( Args&&... args )
class node_base : public reactive_node_interface
{
public:
explicit node_base( context context )
: m_context( std::move( context ) )
{
assert( !get_graph().is_locked() && "Can't create node from callback" );
m_id = get_graph().register_node();
}
explicit node_base( context context );

~node_base() override
{
detach_from_all();
get_graph().unregister_node( m_id );
}
~node_base() override;

UREACT_WARN_UNUSED_RESULT node_id get_node_id() const
{
Expand All @@ -70,36 +62,14 @@ class node_base : public reactive_node_interface
}

protected:
UREACT_WARN_UNUSED_RESULT react_graph& get_graph()
{
return get_internals( m_context ).get_graph();
}

UREACT_WARN_UNUSED_RESULT const react_graph& get_graph() const
{
return get_internals( m_context ).get_graph();
}
UREACT_WARN_UNUSED_RESULT react_graph& get_graph();
UREACT_WARN_UNUSED_RESULT const react_graph& get_graph() const;

void attach_to( node_id parentId )
{
m_parents.add( parentId );
get_graph().attach_node( m_id, parentId );
}
void attach_to( node_id parentId );

void detach_from( node_id parentId )
{
get_graph().detach_node( m_id, parentId );
m_parents.remove( parentId );
}
void detach_from( node_id parentId );

void detach_from_all()
{
for( node_id parentId : m_parents )
{
get_graph().detach_node( m_id, parentId );
}
m_parents.clear();
}
void detach_from_all();

template <class... Deps>
void attach_to( const Deps&... deps )
Expand Down Expand Up @@ -137,4 +107,8 @@ class node_base : public reactive_node_interface

UREACT_END_NAMESPACE

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

#endif // UREACT_DETAIL_NODE_BASE_HPP
69 changes: 69 additions & 0 deletions include/ureact/detail/node_base.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// 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_NODE_BASE_INL
#define UREACT_DETAIL_NODE_BASE_INL

#include <cassert>

#include <ureact/detail/defines.hpp>
#include <ureact/detail/node_base.hpp>

UREACT_BEGIN_NAMESPACE

namespace detail
{

UREACT_FUNC node_base::node_base( context context )
: m_context( std::move( context ) )
{
assert( !get_graph().is_locked() && "Can't create node from callback" );
m_id = get_graph().register_node();
}

UREACT_FUNC node_base::~node_base()
{
detach_from_all();
get_graph().unregister_node( m_id );
}

UREACT_FUNC react_graph& node_base::get_graph()
{
return get_internals( m_context ).get_graph();
}

UREACT_FUNC const react_graph& node_base::get_graph() const
{
return get_internals( m_context ).get_graph();
}

UREACT_FUNC void node_base::attach_to( node_id parentId )
{
m_parents.add( parentId );
get_graph().attach_node( m_id, parentId );
}

UREACT_FUNC void node_base::detach_from( node_id parentId )
{
get_graph().detach_node( m_id, parentId );
m_parents.remove( parentId );
}

UREACT_FUNC void node_base::detach_from_all()
{
for( node_id parentId : m_parents )
get_graph().detach_node( m_id, parentId );
m_parents.clear();
}

} // namespace detail

UREACT_END_NAMESPACE

#endif //UREACT_DETAIL_NODE_BASE_INL

0 comments on commit 462d19b

Please sign in to comment.