ChimeraTK-ApplicationCore  04.01.00
FanOut.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
5 #include "VariableNetworkNode.h"
6 
7 #include <ChimeraTK/NDRegisterAccessor.h>
8 
9 #include <list>
10 #include <utility>
11 
12 namespace ChimeraTK {
13 
14  /********************************************************************************************************************/
15 
16  template<typename UserType>
18  std::list<std::pair<boost::shared_ptr<ChimeraTK::NDRegisterAccessor<UserType>>, VariableNetworkNode>>;
19 
20  /********************************************************************************************************************/
21 
23  class FanOutBase {
24  public:
25  virtual ~FanOutBase() = default;
26  virtual void removeSlave(const boost::shared_ptr<ChimeraTK::TransferElement>& slave) = 0;
27 
31  void disable() { _disabled = true; }
32 
33  protected:
34  bool _disabled{false};
35  };
36 
37  /********************************************************************************************************************/
38 
41  template<typename UserType>
42  class FanOut : public FanOutBase {
43  public:
44  explicit FanOut(boost::shared_ptr<ChimeraTK::NDRegisterAccessor<UserType>> feedingImpl)
45  : _impl(std::move(feedingImpl)) {}
46 
49  virtual void addSlave(
50  boost::shared_ptr<ChimeraTK::NDRegisterAccessor<UserType>> slave, VariableNetworkNode& /*consumer*/);
51 
52  // remove a slave identified by its consuming node from the FanOut
53  void removeSlave(const boost::shared_ptr<ChimeraTK::TransferElement>& slave) override;
54 
55  // interrupt the input and all slaves
56  virtual void interrupt();
57 
58  protected:
59  boost::shared_ptr<ChimeraTK::NDRegisterAccessor<UserType>> _impl;
60 
61  std::list<boost::shared_ptr<ChimeraTK::NDRegisterAccessor<UserType>>> _slaves;
62  };
63 
64  /********************************************************************************************************************/
65  /********************************************************************************************************************/
66 
67  template<typename UserType>
69  boost::shared_ptr<ChimeraTK::NDRegisterAccessor<UserType>> slave, VariableNetworkNode& /*consumer*/) {
70  if(!slave->isWriteable()) {
71  throw ChimeraTK::logic_error("FanOut::addSlave() has been called with a "
72  "receiving implementation!");
73  }
74  // check if array shape is compatible, unless the receiver is a trigger
75  // node, so no data is expected
76  if(slave->getNumberOfSamples() != 0 &&
77  (slave->getNumberOfChannels() != _impl->getNumberOfChannels() ||
78  slave->getNumberOfSamples() != _impl->getNumberOfSamples())) {
79  std::string what = "FanOut::addSlave(): Trying to add a slave '";
80  what += slave->getName();
81  what += "' with incompatible array shape! Name of master: ";
82  what += _impl->getName();
83  what += " Length of master: " + std::to_string(_impl->getNumberOfChannels()) + " x " +
84  std::to_string(_impl->getNumberOfSamples());
85  what += " Length of slave: " + std::to_string(slave->getNumberOfChannels()) + " x " +
86  std::to_string(slave->getNumberOfSamples());
87  throw ChimeraTK::logic_error(what);
88  }
89  _slaves.push_back(slave);
90  }
91 
92  /********************************************************************************************************************/
93 
94  template<typename UserType>
95  void FanOut<UserType>::removeSlave(const boost::shared_ptr<ChimeraTK::TransferElement>& slave) {
96  // make sure the slave is actually currently in the list, and get it by the right typ
97  boost::shared_ptr<ChimeraTK::NDRegisterAccessor<UserType>> slave_typed;
98  for(auto& s : _slaves) {
99  if(s == slave) {
100  slave_typed = s;
101  break;
102  }
103  }
104  assert(slave_typed != nullptr);
105 
106  [[maybe_unused]] size_t nOld = _slaves.size();
107  _slaves.remove(slave_typed);
108  assert(_slaves.size() == nOld - 1);
109  }
110 
111  /********************************************************************************************************************/
112 
113  template<typename UserType>
115  if(_impl) {
116  _impl->interrupt();
117  }
118  for(auto& slave : _slaves) {
119  slave->interrupt();
120  }
121  }
122 
123  /********************************************************************************************************************/
124 
125 } /* namespace ChimeraTK */
ChimeraTK::VariableNetworkNode
Class describing a node of a variable network.
Definition: VariableNetworkNode.h:37
ChimeraTK::FanOut::_impl
boost::shared_ptr< ChimeraTK::NDRegisterAccessor< UserType > > _impl
Definition: FanOut.h:59
ChimeraTK::FanOutBase::removeSlave
virtual void removeSlave(const boost::shared_ptr< ChimeraTK::TransferElement > &slave)=0
ChimeraTK::FanOut::addSlave
virtual void addSlave(boost::shared_ptr< ChimeraTK::NDRegisterAccessor< UserType >> slave, VariableNetworkNode &)
Add a slave to the FanOut.
Definition: FanOut.h:68
ChimeraTK::FanOutBase::disable
void disable()
Disable the FanOut so it does nothing.
Definition: FanOut.h:31
ChimeraTK::FanOut::removeSlave
void removeSlave(const boost::shared_ptr< ChimeraTK::TransferElement > &slave) override
Definition: FanOut.h:95
ChimeraTK::FanOutBase
Type independent base.
Definition: FanOut.h:23
ChimeraTK::FanOutBase::_disabled
bool _disabled
Definition: FanOut.h:34
VariableNetworkNode.h
ChimeraTK::FanOutBase::~FanOutBase
virtual ~FanOutBase()=default
ChimeraTK::FanOut::_slaves
std::list< boost::shared_ptr< ChimeraTK::NDRegisterAccessor< UserType > > > _slaves
Definition: FanOut.h:61
ChimeraTK::FanOut::FanOut
FanOut(boost::shared_ptr< ChimeraTK::NDRegisterAccessor< UserType >> feedingImpl)
Definition: FanOut.h:44
ChimeraTK::FanOut::interrupt
virtual void interrupt()
Definition: FanOut.h:114
ChimeraTK
InvalidityTracer application module.
Definition: spec_dataValidityPropagation.dox:2
ChimeraTK::ConsumerImplementationPairs
std::list< std::pair< boost::shared_ptr< ChimeraTK::NDRegisterAccessor< UserType > >, VariableNetworkNode > > ConsumerImplementationPairs
Definition: FanOut.h:18