From e44b4ad4b55cd0a9d9f0af2c7c77cf8398633057 Mon Sep 17 00:00:00 2001 From: Krylov Yaroslav Date: Thu, 24 Aug 2023 23:55:49 +0400 Subject: [PATCH] #128 Extract context related definitions in context.inl --- include/ureact/context.hpp | 59 +++++++++++++------------ include/ureact/detail/context.inl | 64 ++++++++++++++++++++++++++++ include/ureact/detail/graph_impl.hpp | 22 ---------- 3 files changed, 96 insertions(+), 49 deletions(-) create mode 100644 include/ureact/detail/context.inl diff --git a/include/ureact/context.hpp b/include/ureact/context.hpp index 583fd3f..1b959b6 100644 --- a/include/ureact/context.hpp +++ b/include/ureact/context.hpp @@ -10,17 +10,43 @@ #ifndef UREACT_CONTEXT_HPP #define UREACT_CONTEXT_HPP +#include + #include #include UREACT_BEGIN_NAMESPACE +namespace detail +{ + +class context_internals +{ +public: + explicit context_internals( + std::shared_ptr graph = std::make_shared() ); + + UREACT_WARN_UNUSED_RESULT react_graph& get_graph(); + UREACT_WARN_UNUSED_RESULT const react_graph& get_graph() const; + +private: + std::shared_ptr 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. @@ -77,34 +103,13 @@ class context final : protected detail::context_internals /*! * @brief Construct @ref context from given react_graph */ - explicit context( std::shared_ptr graph ) - : detail::context_internals( std::move( graph ) ) - {} + explicit context( std::shared_ptr graph ); }; -namespace default_context -{ - -/** - * @brief Return default context - * Default contexts are thread_local - */ -inline context get() -{ - thread_local static std::weak_ptr s_instance; - - auto graphPtr = s_instance.lock(); - - if( !graphPtr ) - { - s_instance = graphPtr = std::make_shared(); - } - - return context{ std::move( graphPtr ) }; -} - -} // namespace default_context - UREACT_END_NAMESPACE +#if UREACT_HEADER_ONLY +# include +#endif + #endif //UREACT_CONTEXT_HPP diff --git a/include/ureact/detail/context.inl b/include/ureact/detail/context.inl new file mode 100644 index 0000000..2d96599 --- /dev/null +++ b/include/ureact/detail/context.inl @@ -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 + +#include +#include + +UREACT_BEGIN_NAMESPACE + +namespace detail +{ + +UREACT_FUNC context_internals::context_internals( std::shared_ptr 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 graph ) + : detail::context_internals( std::move( graph ) ) +{} + +namespace default_context +{ + +UREACT_FUNC context get() +{ + thread_local static std::weak_ptr s_instance; + + auto graphPtr = s_instance.lock(); + + if( !graphPtr ) + { + s_instance = graphPtr = std::make_shared(); + } + + return context{ std::move( graphPtr ) }; +} + +} // namespace default_context + +UREACT_END_NAMESPACE + +#endif //UREACT_DETAIL_CONTEXT_INL diff --git a/include/ureact/detail/graph_impl.hpp b/include/ureact/detail/graph_impl.hpp index f8d4892..71b16d3 100644 --- a/include/ureact/detail/graph_impl.hpp +++ b/include/ureact/detail/graph_impl.hpp @@ -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 graph = std::make_shared() ) - : 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 m_graph_ptr; -}; - } // namespace detail UREACT_END_NAMESPACE