Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e8bf664

Browse files
committedAug 26, 2023
#128 Extract react_graph related definitions in graph_impl.inl
1 parent 1569d54 commit e8bf664

File tree

2 files changed

+140
-86
lines changed

2 files changed

+140
-86
lines changed
 

‎include/ureact/detail/graph_impl.hpp

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// Copyright (C) 2014-2017 Sebastian Jeckel.
3+
// Copyright (C) 2020-2023 Krylov Yaroslav.
4+
//
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
//
9+
10+
#ifndef UREACT_DETAIL_GRAPH_IMPL_HPP
11+
#define UREACT_DETAIL_GRAPH_IMPL_HPP
12+
13+
#include <memory>
14+
15+
#include <ureact/detail/defines.hpp>
16+
#include <ureact/detail/graph_interface.hpp>
17+
18+
UREACT_BEGIN_NAMESPACE
19+
20+
namespace detail
21+
{
22+
23+
#if !defined( NDEBUG )
24+
struct callback_sanitizer
25+
{
26+
virtual ~callback_sanitizer() = default;
27+
28+
/// Return if external callback is in progress
29+
UREACT_WARN_UNUSED_RESULT virtual bool is_locked() const = 0;
30+
31+
/// Marks begin of an external callback
32+
virtual void begin_external_callback() = 0;
33+
34+
/// Marks end of an external callback
35+
virtual void end_external_callback() = 0;
36+
37+
/// Marks a place where an external callback is called
38+
struct guard
39+
{
40+
callback_sanitizer& self;
41+
42+
explicit guard( callback_sanitizer& self )
43+
: self( self )
44+
{
45+
self.begin_external_callback();
46+
}
47+
48+
~guard()
49+
{
50+
self.end_external_callback();
51+
}
52+
53+
UREACT_MAKE_NONCOPYABLE( guard );
54+
UREACT_MAKE_NONMOVABLE( guard );
55+
};
56+
};
57+
# define UREACT_CALLBACK_GUARD( _SELF_ ) callback_sanitizer::guard _( _SELF_ )
58+
#else
59+
# define UREACT_CALLBACK_GUARD( _SELF_ ) \
60+
do \
61+
{ \
62+
} while( false )
63+
#endif
64+
65+
struct react_graph
66+
#if !defined( NDEBUG )
67+
: public callback_sanitizer
68+
#endif
69+
{
70+
virtual ~react_graph() = default;
71+
72+
virtual node_id register_node() = 0;
73+
virtual void register_node_ptr(
74+
node_id nodeId, const std::weak_ptr<reactive_node_interface>& nodePtr )
75+
= 0;
76+
virtual void unregister_node( node_id nodeId ) = 0;
77+
78+
virtual void attach_node( node_id nodeId, node_id parentId ) = 0;
79+
virtual void detach_node( node_id nodeId, node_id parentId ) = 0;
80+
81+
virtual void push_input( node_id nodeId ) = 0;
82+
83+
virtual void start_transaction() = 0;
84+
virtual void finish_transaction() = 0;
85+
86+
[[nodiscard]] virtual bool is_propagation_in_progress() const = 0;
87+
};
88+
89+
std::shared_ptr<react_graph> make_react_graph();
90+
91+
} // namespace detail
92+
93+
UREACT_END_NAMESPACE
94+
95+
#if UREACT_HEADER_ONLY
96+
# include <ureact/detail/graph_impl.inl>
97+
#endif
98+
99+
#endif // UREACT_DETAIL_GRAPH_IMPL_HPP

‎include/ureact/detail/graph_impl.inl

+41-86
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111
#define UREACT_DETAIL_GRAPH_IMPL_INL
1212

1313
#include <cassert>
14-
#include <cstddef>
15-
#include <cstdint>
1614
#include <limits>
1715
#include <memory>
18-
#include <tuple>
19-
#include <type_traits>
20-
#include <utility>
2116
#include <vector>
2217

2318
#include <ureact/detail/algorithm.hpp>
@@ -31,86 +26,46 @@ UREACT_BEGIN_NAMESPACE
3126
namespace detail
3227
{
3328

34-
#if !defined( NDEBUG )
35-
/// Utility class to check if callbacks passed in lift(), process() etc
36-
/// are used properly
37-
class callback_sanitizer
29+
class react_graph_impl : public react_graph
3830
{
39-
public:
40-
/// Return if external callback is in progress
41-
UREACT_WARN_UNUSED_RESULT bool is_locked() const
31+
#if !defined( NDEBUG )
32+
bool m_is_locked = false;
33+
34+
bool is_locked() const override
4235
{
4336
return m_is_locked;
4437
}
4538

46-
/// Marks begin of an external callback
47-
void begin_external_callback()
39+
void begin_external_callback() override
4840
{
4941
assert( !m_is_locked );
5042
m_is_locked = true;
5143
}
5244

53-
/// Marks end of an external callback
54-
void end_external_callback()
45+
void end_external_callback() override
5546
{
5647
assert( m_is_locked );
5748
m_is_locked = false;
5849
}
59-
60-
/// Marks a place where an external callback is called
61-
struct guard
62-
{
63-
callback_sanitizer& self;
64-
65-
explicit guard( callback_sanitizer& self )
66-
: self( self )
67-
{
68-
self.begin_external_callback();
69-
}
70-
71-
~guard()
72-
{
73-
self.end_external_callback();
74-
}
75-
76-
UREACT_MAKE_NONCOPYABLE( guard );
77-
UREACT_MAKE_NONMOVABLE( guard );
78-
};
79-
80-
private:
81-
bool m_is_locked = false;
82-
};
83-
84-
# define UREACT_CALLBACK_GUARD( _SELF_ ) callback_sanitizer::guard _( _SELF_ )
85-
#else
86-
# define UREACT_CALLBACK_GUARD( _SELF_ ) \
87-
do \
88-
{ \
89-
} while( false )
90-
#endif
91-
92-
class react_graph
93-
#if !defined( NDEBUG )
94-
: public callback_sanitizer
9550
#endif
96-
{
9751
public:
98-
react_graph() = default;
99-
~react_graph();
52+
react_graph_impl() = default;
53+
~react_graph_impl() override;
10054

101-
node_id register_node();
102-
void register_node_ptr( node_id nodeId, const std::weak_ptr<reactive_node_interface>& nodePtr );
103-
void unregister_node( node_id nodeId );
55+
node_id register_node() override;
56+
void register_node_ptr(
57+
node_id nodeId, const std::weak_ptr<reactive_node_interface>& nodePtr ) override;
58+
void unregister_node( node_id nodeId ) override;
10459

105-
void attach_node( node_id nodeId, node_id parentId );
106-
void detach_node( node_id nodeId, node_id parentId );
60+
void attach_node( node_id nodeId, node_id parentId ) override;
61+
void detach_node( node_id nodeId, node_id parentId ) override;
10762

108-
void push_input( node_id nodeId );
63+
void push_input( node_id nodeId ) override;
10964

110-
void start_transaction();
111-
void finish_transaction();
65+
void start_transaction() override;
66+
void finish_transaction() override;
11267

113-
[[nodiscard]] bool is_propagation_in_progress() const
68+
[[nodiscard]] bool is_propagation_in_progress() const override
11469
{
11570
return m_propagation_is_in_progress;
11671
}
@@ -199,7 +154,7 @@ private:
199154
node_id_vector m_nodes_queued_for_unregister;
200155
};
201156

202-
inline react_graph::~react_graph()
157+
UREACT_FUNC react_graph_impl::~react_graph_impl()
203158
{
204159
assert( m_node_data.empty() );
205160
assert( m_scheduled_nodes.empty() );
@@ -209,12 +164,12 @@ inline react_graph::~react_graph()
209164
assert( m_nodes_queued_for_unregister.empty() );
210165
}
211166

212-
inline node_id react_graph::register_node()
167+
UREACT_FUNC node_id react_graph_impl::register_node()
213168
{
214169
return node_id{ m_id, m_node_data.emplace() };
215170
}
216171

217-
inline void react_graph::register_node_ptr(
172+
UREACT_FUNC void react_graph_impl::register_node_ptr(
218173
const node_id nodeId, const std::weak_ptr<reactive_node_interface>& nodePtr )
219174
{
220175
assert( nodeId.context_id() == m_id );
@@ -224,7 +179,7 @@ inline void react_graph::register_node_ptr(
224179
node.node_ptr = nodePtr;
225180
}
226181

227-
inline void react_graph::unregister_node( const node_id nodeId )
182+
UREACT_FUNC void react_graph_impl::unregister_node( const node_id nodeId )
228183
{
229184
assert( nodeId.context_id() == m_id );
230185
assert( m_node_data[nodeId].successors.empty() );
@@ -235,7 +190,7 @@ inline void react_graph::unregister_node( const node_id nodeId )
235190
m_nodes_queued_for_unregister.add( nodeId );
236191
}
237192

238-
inline void react_graph::attach_node( const node_id nodeId, const node_id parentId )
193+
UREACT_FUNC void react_graph_impl::attach_node( const node_id nodeId, const node_id parentId )
239194
{
240195
assert( nodeId.context_id() == m_id );
241196
assert( parentId.context_id() == m_id );
@@ -248,7 +203,7 @@ inline void react_graph::attach_node( const node_id nodeId, const node_id parent
248203
node.level = std::max( node.level, parent.level + 1 );
249204
}
250205

251-
inline void react_graph::detach_node( const node_id nodeId, const node_id parentId )
206+
UREACT_FUNC void react_graph_impl::detach_node( const node_id nodeId, const node_id parentId )
252207
{
253208
assert( nodeId.context_id() == m_id );
254209
assert( parentId.context_id() == m_id );
@@ -259,7 +214,7 @@ inline void react_graph::detach_node( const node_id nodeId, const node_id parent
259214
successors.remove( nodeId );
260215
}
261216

262-
inline void react_graph::push_input( const node_id nodeId )
217+
UREACT_FUNC void react_graph_impl::push_input( const node_id nodeId )
263218
{
264219
assert( !m_propagation_is_in_progress );
265220

@@ -269,14 +224,14 @@ inline void react_graph::push_input( const node_id nodeId )
269224
propagate();
270225
}
271226

272-
inline void react_graph::start_transaction()
227+
UREACT_FUNC void react_graph_impl::start_transaction()
273228
{
274229
assert( !m_propagation_is_in_progress );
275230

276231
++m_transaction_level;
277232
}
278233

279-
inline void react_graph::finish_transaction()
234+
UREACT_FUNC void react_graph_impl::finish_transaction()
280235
{
281236
assert( !m_propagation_is_in_progress );
282237
assert( m_transaction_level > 0 );
@@ -287,18 +242,18 @@ inline void react_graph::finish_transaction()
287242
propagate();
288243
}
289244

290-
inline node_id::context_id_type react_graph::create_context_id()
245+
UREACT_FUNC node_id::context_id_type react_graph_impl::create_context_id()
291246
{
292247
static node_id::context_id_type s_next_id = 1u;
293248
return s_next_id++;
294249
}
295250

296-
UREACT_WARN_UNUSED_RESULT inline bool react_graph::can_unregister_node() const
251+
UREACT_FUNC bool react_graph_impl::can_unregister_node() const
297252
{
298253
return m_transaction_level == 0 && !m_propagation_is_in_progress;
299254
}
300255

301-
inline void react_graph::propagate()
256+
UREACT_FUNC void react_graph_impl::propagate()
302257
{
303258
m_propagation_is_in_progress = true;
304259

@@ -313,7 +268,7 @@ inline void react_graph::propagate()
313268
unregister_queued_nodes();
314269
}
315270

316-
inline void react_graph::recalculate_successor_levels( node_data& parentNode )
271+
UREACT_FUNC void react_graph_impl::recalculate_successor_levels( node_data& parentNode )
317272
{
318273
for( const node_id successorId : parentNode.successors )
319274
{
@@ -322,7 +277,7 @@ inline void react_graph::recalculate_successor_levels( node_data& parentNode )
322277
}
323278
}
324279

325-
inline void react_graph::schedule_node( const node_id nodeId )
280+
UREACT_FUNC void react_graph_impl::schedule_node( const node_id nodeId )
326281
{
327282
node_data& node = m_node_data[nodeId];
328283

@@ -333,20 +288,20 @@ inline void react_graph::schedule_node( const node_id nodeId )
333288
}
334289
}
335290

336-
inline void react_graph::re_schedule_node( const node_id nodeId )
291+
UREACT_FUNC void react_graph_impl::re_schedule_node( const node_id nodeId )
337292
{
338293
node_data& node = m_node_data[nodeId];
339294
recalculate_successor_levels( node );
340295
m_scheduled_nodes.push( nodeId, node.level );
341296
}
342297

343-
inline void react_graph::schedule_successors( node_data& parentNode )
298+
UREACT_FUNC void react_graph_impl::schedule_successors( node_data& parentNode )
344299
{
345300
for( const node_id successorId : parentNode.successors )
346301
schedule_node( successorId );
347302
}
348303

349-
inline void react_graph::propagate_node_change( const node_id nodeId )
304+
UREACT_FUNC void react_graph_impl::propagate_node_change( const node_id nodeId )
350305
{
351306
node_data& node = m_node_data[nodeId];
352307
if( std::shared_ptr<reactive_node_interface> nodePtr = node.node_ptr.lock() )
@@ -378,7 +333,7 @@ inline void react_graph::propagate_node_change( const node_id nodeId )
378333
node.queued = false;
379334
}
380335

381-
inline void react_graph::finalize_changed_nodes()
336+
UREACT_FUNC void react_graph_impl::finalize_changed_nodes()
382337
{
383338
// Cleanup buffers in changed nodes etc
384339
for( const node_id nodeId : m_changed_nodes )
@@ -392,7 +347,7 @@ inline void react_graph::finalize_changed_nodes()
392347
m_changed_nodes.clear();
393348
}
394349

395-
inline void react_graph::unregister_queued_nodes()
350+
UREACT_FUNC void react_graph_impl::unregister_queued_nodes()
396351
{
397352
assert( !m_propagation_is_in_progress );
398353

@@ -401,7 +356,7 @@ inline void react_graph::unregister_queued_nodes()
401356
m_nodes_queued_for_unregister.clear();
402357
}
403358

404-
UREACT_WARN_UNUSED_RESULT inline bool react_graph::topological_queue::fetch_next()
359+
UREACT_FUNC bool react_graph_impl::topological_queue::fetch_next()
405360
{
406361
// Throw away previous values
407362
m_next_data.clear();
@@ -431,9 +386,9 @@ UREACT_WARN_UNUSED_RESULT inline bool react_graph::topological_queue::fetch_next
431386
return !m_next_data.empty();
432387
}
433388

434-
inline std::shared_ptr<react_graph> make_react_graph()
389+
UREACT_FUNC std::shared_ptr<react_graph> make_react_graph()
435390
{
436-
return std::make_shared<react_graph>();
391+
return std::make_shared<react_graph_impl>();
437392
}
438393

439394
} // namespace detail

0 commit comments

Comments
 (0)
Please sign in to comment.