33
33
namespace gr {
34
34
35
35
namespace graph ::property {
36
- inline static const char * kEmplaceBlock = " EmplaceBlock" ;
37
- inline static const char * kRemoveBlock = " RemoveBlock" ;
38
- inline static const char * kReplaceBlock = " ReplaceBlock" ;
39
- inline static const char * kInspectBlock = " InspectBlock" ;
40
-
41
- inline static const char * kBlockEmplaced = " BlockEmplaced" ;
42
- inline static const char * kBlockRemoved = " BlockRemoved" ;
43
- inline static const char * kBlockReplaced = " BlockReplaced" ;
36
+ inline static const char * kInspectBlock = " InspectBlock" ;
44
37
inline static const char * kBlockInspected = " BlockInspected" ;
45
-
46
- inline static const char * kEmplaceEdge = " EmplaceEdge" ;
47
- inline static const char * kRemoveEdge = " RemoveEdge" ;
48
-
49
- inline static const char * kEdgeEmplaced = " EdgeEmplaced" ;
50
- inline static const char * kEdgeRemoved = " EdgeRemoved" ;
51
-
52
38
inline static const char * kGraphInspect = " GraphInspect" ;
53
39
inline static const char * kGraphInspected = " GraphInspected" ;
54
40
@@ -202,7 +188,6 @@ class Graph : public gr::Block<Graph> {
202
188
private:
203
189
std::shared_ptr<gr::Sequence> _progress = std::make_shared<gr::Sequence>();
204
190
std::shared_ptr<gr::thread_pool::BasicThreadPool> _ioThreadPool = std::make_shared<gr::thread_pool::BasicThreadPool>(" graph_thread_pool" , gr::thread_pool::TaskType::IO_BOUND, 2UZ, std::numeric_limits<uint32_t >::max());
205
- std::atomic_bool _topologyChanged{false };
206
191
std::vector<Edge> _edges;
207
192
std::vector<std::unique_ptr<BlockModel>> _blocks;
208
193
@@ -335,12 +320,8 @@ class Graph : public gr::Block<Graph> {
335
320
336
321
Graph (property_map settings = {}) : gr::Block<Graph>(std::move(settings)) {
337
322
_blocks.reserve (100 ); // TODO: remove
338
- propertyCallbacks[graph::property::kEmplaceBlock ] = std::mem_fn (&Graph::propertyCallbackEmplaceBlock);
339
- propertyCallbacks[graph::property::kRemoveBlock ] = std::mem_fn (&Graph::propertyCallbackRemoveBlock);
323
+
340
324
propertyCallbacks[graph::property::kInspectBlock ] = std::mem_fn (&Graph::propertyCallbackInspectBlock);
341
- propertyCallbacks[graph::property::kReplaceBlock ] = std::mem_fn (&Graph::propertyCallbackReplaceBlock);
342
- propertyCallbacks[graph::property::kEmplaceEdge ] = std::mem_fn (&Graph::propertyCallbackEmplaceEdge);
343
- propertyCallbacks[graph::property::kRemoveEdge ] = std::mem_fn (&Graph::propertyCallbackRemoveEdge);
344
325
propertyCallbacks[graph::property::kGraphInspect ] = std::mem_fn (&Graph::propertyCallbackGraphInspect);
345
326
propertyCallbacks[graph::property::kRegistryBlockTypes ] = std::mem_fn (&Graph::propertyCallbackRegistryBlockTypes);
346
327
}
@@ -356,33 +337,29 @@ class Graph : public gr::Block<Graph> {
356
337
}
357
338
_progress = std::move (other._progress );
358
339
_ioThreadPool = std::move (other._ioThreadPool );
359
- _topologyChanged.store (other._topologyChanged .load (std::memory_order_acquire), std::memory_order_release);
360
- _edges = std::move (other._edges );
361
- _blocks = std::move (other._blocks );
340
+ _edges = std::move (other._edges );
341
+ _blocks = std::move (other._blocks );
362
342
363
343
return *this ;
364
344
}
365
345
366
- void setTopologyChanged () noexcept { _topologyChanged.store (true , std::memory_order_release); }
367
- [[nodiscard]] bool hasTopologyChanged () const noexcept { return _topologyChanged; }
368
- void ackTopologyChange () noexcept { _topologyChanged.store (false , std::memory_order_release); }
369
-
370
346
[[nodiscard]] std::span<std::unique_ptr<BlockModel>> blocks () noexcept { return {_blocks}; }
371
347
[[nodiscard]] std::span<Edge> edges () noexcept { return {_edges}; }
372
348
349
+ void clear () {
350
+ _blocks.clear ();
351
+ _edges.clear ();
352
+ }
353
+
373
354
/* *
374
355
* @return atomic sequence counter that indicates if any block could process some data or messages
375
356
*/
376
357
[[nodiscard]] const Sequence& progress () noexcept { return *_progress.get (); }
377
358
378
- BlockModel& addBlock (std::unique_ptr<BlockModel> block, bool doEmitMessage = true ) {
359
+ BlockModel& addBlock (std::unique_ptr<BlockModel> block) {
379
360
auto & newBlock = _blocks.emplace_back (std::move (block));
380
361
newBlock->init (_progress, _ioThreadPool);
381
362
// TODO: Should we connectChildMessagePorts for these blocks as well?
382
- setTopologyChanged ();
383
- if (doEmitMessage) {
384
- this ->emitMessage (graph::property::kBlockEmplaced , serializeBlock (newBlock.get ()));
385
- }
386
363
return *newBlock.get ();
387
364
}
388
365
@@ -393,18 +370,12 @@ class Graph : public gr::Block<Graph> {
393
370
auto & newBlock = _blocks.emplace_back (std::make_unique<BlockWrapper<TBlock>>(std::move (initialSettings)));
394
371
auto * rawBlockRef = static_cast <TBlock*>(newBlock->raw ());
395
372
rawBlockRef->init (_progress, _ioThreadPool);
396
- setTopologyChanged ();
397
- this ->emitMessage (graph::property::kBlockEmplaced , serializeBlock (newBlock.get ()));
398
373
return *rawBlockRef;
399
374
}
400
375
401
376
[[maybe_unused]] auto & emplaceBlock (std::string_view type, property_map initialSettings) {
402
377
if (auto block_load = _pluginLoader->instantiate (type, std::move (initialSettings)); block_load) {
403
- setTopologyChanged ();
404
- auto & newBlock = addBlock (std::move (block_load), false ); // false == do not emit message
405
-
406
- this ->emitMessage (graph::property::kBlockEmplaced , serializeBlock (std::addressof (newBlock)));
407
-
378
+ auto & newBlock = addBlock (std::move (block_load));
408
379
return newBlock;
409
380
}
410
381
throw gr::exception (std::format (" Can not create block {}" , type));
@@ -503,25 +474,6 @@ class Graph : public gr::Block<Graph> {
503
474
return result;
504
475
}
505
476
506
- std::optional<Message> propertyCallbackEmplaceBlock ([[maybe_unused]] std::string_view propertyName, Message message) {
507
- assert (propertyName == graph::property::kEmplaceBlock );
508
- using namespace std ::string_literals;
509
- const auto & data = message.data .value ();
510
- const std::string& type = std::get<std::string>(data.at (" type" s));
511
- const property_map& properties = [&] {
512
- if (auto it = data.find (" properties" s); it != data.end ()) {
513
- return std::get<property_map>(it->second );
514
- } else {
515
- return property_map{};
516
- }
517
- }();
518
-
519
- emplaceBlock (type, properties);
520
-
521
- // Message is sent as a reaction to emplaceBlock, no need for a separate one
522
- return {};
523
- }
524
-
525
477
std::optional<Message> propertyCallbackInspectBlock ([[maybe_unused]] std::string_view propertyName, Message message) {
526
478
assert (propertyName == graph::property::kInspectBlock );
527
479
using namespace std ::string_literals;
@@ -540,12 +492,8 @@ class Graph : public gr::Block<Graph> {
540
492
return {reply};
541
493
}
542
494
543
- std::optional<Message> propertyCallbackRemoveBlock ([[maybe_unused]] std::string_view propertyName, Message message) {
544
- assert (propertyName == graph::property::kRemoveBlock );
545
- using namespace std ::string_literals;
546
- const auto & data = message.data .value ();
547
- const std::string& uniqueName = std::get<std::string>(data.at (" uniqueName" s));
548
- auto it = std::ranges::find_if (_blocks, [&uniqueName](const auto & block) { return block->uniqueName () == uniqueName; });
495
+ std::unique_ptr<BlockModel> removeBlockByName (std::string_view uniqueName) {
496
+ auto it = std::ranges::find_if (_blocks, [&uniqueName](const auto & block) { return block->uniqueName () == uniqueName; });
549
497
550
498
if (it == _blocks.end ()) {
551
499
throw gr::exception (std::format (" Block {} was not found in {}" , uniqueName, this ->unique_name ));
@@ -554,26 +502,14 @@ class Graph : public gr::Block<Graph> {
554
502
std::erase_if (_edges, [&it](const Edge& edge) { //
555
503
return std::addressof (edge.sourceBlock ()) == it->get () || std::addressof (edge.destinationBlock ()) == it->get ();
556
504
});
505
+
506
+ std::unique_ptr<BlockModel> removedBlock = std::move (*it);
557
507
_blocks.erase (it);
558
- message.endpoint = graph::property::kBlockRemoved ;
559
508
560
- return {message} ;
509
+ return removedBlock ;
561
510
}
562
511
563
- std::optional<Message> propertyCallbackReplaceBlock ([[maybe_unused]] std::string_view propertyName, Message message) {
564
- assert (propertyName == graph::property::kReplaceBlock );
565
- using namespace std ::string_literals;
566
- const auto & data = message.data .value ();
567
- const std::string& uniqueName = std::get<std::string>(data.at (" uniqueName" s));
568
- const std::string& type = std::get<std::string>(data.at (" type" s));
569
- const property_map& properties = [&] {
570
- if (auto it = data.find (" properties" s); it != data.end ()) {
571
- return std::get<property_map>(it->second );
572
- } else {
573
- return property_map{};
574
- }
575
- }();
576
-
512
+ std::pair<std::unique_ptr<BlockModel>, BlockModel*> replaceBlock (const std::string& uniqueName, const std::string& type, const property_map& properties) {
577
513
auto it = std::ranges::find_if (_blocks, [&uniqueName](const auto & block) { return block->uniqueName () == uniqueName; });
578
514
if (it == _blocks.end ()) {
579
515
throw gr::exception (std::format (" Block {} was not found in {}" , uniqueName, this ->unique_name ));
@@ -585,41 +521,26 @@ class Graph : public gr::Block<Graph> {
585
521
throw gr::exception (std::format (" Can not create block {}" , type));
586
522
}
587
523
588
- addBlock (std::move (newBlock), false ); // false == do not emit message
524
+ addBlock (std::move (newBlock));
589
525
590
- BlockModel* oldBlock = it->get ();
591
526
for (auto & edge : _edges) {
592
- if (edge._sourceBlock == oldBlock ) {
527
+ if (edge._sourceBlock == it-> get () ) {
593
528
edge._sourceBlock = newBlockRaw;
594
529
}
595
530
596
- if (edge._destinationBlock == oldBlock ) {
531
+ if (edge._destinationBlock == it-> get () ) {
597
532
edge._destinationBlock = newBlockRaw;
598
533
}
599
534
}
600
- _blocks.erase (it);
601
-
602
- std::optional<Message> result = gr::Message{};
603
- result->endpoint = graph::property::kBlockReplaced ;
604
- result->data = serializeBlock (newBlockRaw);
605
535
606
- (*result->data )[" replacedBlockUniqueName" s] = uniqueName;
536
+ std::unique_ptr<BlockModel> oldBlock = std::move (*it);
537
+ _blocks.erase (it);
607
538
608
- return result ;
539
+ return { std::move (oldBlock), newBlockRaw} ;
609
540
}
610
541
611
- std::optional<Message> propertyCallbackEmplaceEdge ([[maybe_unused]] std::string_view propertyName, Message message) {
612
- assert (propertyName == graph::property::kEmplaceEdge );
613
- using namespace std ::string_literals;
614
- const auto & data = message.data .value ();
615
- const std::string& sourceBlock = std::get<std::string>(data.at (" sourceBlock" s));
616
- const std::string& sourcePort = std::get<std::string>(data.at (" sourcePort" s));
617
- const std::string& destinationBlock = std::get<std::string>(data.at (" destinationBlock" s));
618
- const std::string& destinationPort = std::get<std::string>(data.at (" destinationPort" s));
619
- [[maybe_unused]] const std::size_t minBufferSize = std::get<gr::Size_t>(data.at (" minBufferSize" s));
620
- [[maybe_unused]] const std::int32_t weight = std::get<std::int32_t >(data.at (" weight" s));
621
- const std::string edgeName = std::get<std::string>(data.at (" edgeName" s));
622
-
542
+ void emplaceEdge (std::string_view sourceBlock, std::string sourcePort, std::string_view destinationBlock, //
543
+ std::string destinationPort, [[maybe_unused]] const std::size_t minBufferSize, [[maybe_unused]] const std::int32_t weight, std::string_view edgeName) {
623
544
auto sourceBlockIt = std::ranges::find_if (_blocks, [&sourceBlock](const auto & block) { return block->uniqueName () == sourceBlock; });
624
545
if (sourceBlockIt == _blocks.end ()) {
625
546
throw gr::exception (std::format (" Block {} was not found in {}" , sourceBlock, this ->unique_name ));
@@ -645,31 +566,20 @@ class Graph : public gr::Block<Graph> {
645
566
646
567
const bool isArithmeticLike = sourcePortRef.portInfo ().isValueTypeArithmeticLike ;
647
568
const std::size_t sanitizedMinBufferSize = minBufferSize == undefined_size ? graph::defaultMinBufferSize (isArithmeticLike) : minBufferSize;
648
- _edges.emplace_back (sourceBlockIt->get (), sourcePort, destinationBlockIt->get (), destinationPort, sanitizedMinBufferSize, weight, edgeName);
649
-
650
- message.endpoint = graph::property::kEdgeEmplaced ;
651
- return message;
569
+ _edges.emplace_back (sourceBlockIt->get (), sourcePort, destinationBlockIt->get (), destinationPort, sanitizedMinBufferSize, weight, std::string (edgeName));
652
570
}
653
571
654
- std::optional<Message> propertyCallbackRemoveEdge ([[maybe_unused]] std::string_view propertyName, Message message) {
655
- assert (propertyName == graph::property::kRemoveEdge );
656
- using namespace std ::string_literals;
657
- const auto & data = message.data .value ();
658
- const std::string& sourceBlock = std::get<std::string>(data.at (" sourceBlock" s));
659
- const std::string& sourcePort = std::get<std::string>(data.at (" sourcePort" s));
660
-
572
+ void removeEdgeBySourcePort (std::string_view sourceBlock, std::string_view sourcePort) {
661
573
auto sourceBlockIt = std::ranges::find_if (_blocks, [&sourceBlock](const auto & block) { return block->uniqueName () == sourceBlock; });
662
574
if (sourceBlockIt == _blocks.end ()) {
663
575
throw gr::exception (std::format (" Block {} was not found in {}" , sourceBlock, this ->unique_name ));
664
576
}
665
577
666
- auto & sourcePortRef = (*sourceBlockIt)->dynamicOutputPort (sourcePort);
578
+ auto & sourcePortRef = (*sourceBlockIt)->dynamicOutputPort (std::string ( sourcePort) );
667
579
668
580
if (sourcePortRef.disconnect () == ConnectionResult::FAILED) {
669
581
throw gr::exception (std::format (" Block {} sourcePortRef could not be disconnected {}" , sourceBlock, this ->unique_name ));
670
582
}
671
- message.endpoint = graph::property::kEdgeRemoved ;
672
- return message;
673
583
}
674
584
675
585
std::optional<Message> propertyCallbackGraphInspect ([[maybe_unused]] std::string_view propertyName, Message message) {
0 commit comments