Skip to content

Commit

Permalink
#128 Extract context related definitions in context.inl
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikTH committed Aug 24, 2023
1 parent daa924b commit e44b4ad
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 49 deletions.
59 changes: 32 additions & 27 deletions include/ureact/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,43 @@
#ifndef UREACT_CONTEXT_HPP
#define UREACT_CONTEXT_HPP

#include <memory>

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

UREACT_BEGIN_NAMESPACE

namespace detail
{

class context_internals
{
public:
explicit context_internals(
std::shared_ptr<react_graph> graph = std::make_shared<react_graph>() );

UREACT_WARN_UNUSED_RESULT react_graph& get_graph();
UREACT_WARN_UNUSED_RESULT const react_graph& get_graph() const;

private:
std::shared_ptr<react_graph> m_graph_ptr;
};

} // namespace detail

class context;

namespace default_context
{

/**
* @brief Return default context
* Default contexts are thread_local
*/
context get();
}

} // namespace default_context

/*!
* @brief Core class that connects all reactive nodes together.
Expand Down Expand Up @@ -77,34 +103,13 @@ class context final : protected detail::context_internals
/*!
* @brief Construct @ref context from given react_graph
*/
explicit context( std::shared_ptr<detail::react_graph> graph )
: detail::context_internals( std::move( graph ) )
{}
explicit context( std::shared_ptr<detail::react_graph> graph );
};

namespace default_context
{

/**
* @brief Return default context
* Default contexts are thread_local
*/
inline context get()
{
thread_local static std::weak_ptr<detail::react_graph> s_instance;

auto graphPtr = s_instance.lock();

if( !graphPtr )
{
s_instance = graphPtr = std::make_shared<detail::react_graph>();
}

return context{ std::move( graphPtr ) };
}

} // namespace default_context

UREACT_END_NAMESPACE

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

#endif //UREACT_CONTEXT_HPP
64 changes: 64 additions & 0 deletions include/ureact/detail/context.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// 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_CONTEXT_INL
#define UREACT_DETAIL_CONTEXT_INL

#include <memory>

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

UREACT_BEGIN_NAMESPACE

namespace detail
{

UREACT_FUNC context_internals::context_internals( std::shared_ptr<react_graph> graph )
: m_graph_ptr( std::move( graph ) )
{}

UREACT_FUNC react_graph& context_internals::get_graph()
{
return *m_graph_ptr;
}

UREACT_FUNC const react_graph& context_internals::get_graph() const
{
return *m_graph_ptr;
}

} // namespace detail

UREACT_FUNC context::context( std::shared_ptr<detail::react_graph> graph )
: detail::context_internals( std::move( graph ) )
{}

namespace default_context
{

UREACT_FUNC context get()
{
thread_local static std::weak_ptr<detail::react_graph> s_instance;

auto graphPtr = s_instance.lock();

if( !graphPtr )
{
s_instance = graphPtr = std::make_shared<detail::react_graph>();
}

return context{ std::move( graphPtr ) };
}

} // namespace default_context

UREACT_END_NAMESPACE

#endif //UREACT_DETAIL_CONTEXT_INL
22 changes: 0 additions & 22 deletions include/ureact/detail/graph_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,28 +430,6 @@ UREACT_WARN_UNUSED_RESULT inline bool react_graph::topological_queue::fetch_next
return !m_next_data.empty();
}

class context_internals
{
public:
explicit context_internals(
std::shared_ptr<detail::react_graph> graph = std::make_shared<react_graph>() )
: m_graph_ptr( std::move( graph ) )
{}

UREACT_WARN_UNUSED_RESULT react_graph& get_graph()
{
return *m_graph_ptr;
}

UREACT_WARN_UNUSED_RESULT const react_graph& get_graph() const
{
return *m_graph_ptr;
}

private:
std::shared_ptr<react_graph> m_graph_ptr;
};

} // namespace detail

UREACT_END_NAMESPACE
Expand Down

0 comments on commit e44b4ad

Please sign in to comment.