NRP Core  1.4.1
input_port.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef INPUT_PORT_H
23 #define INPUT_PORT_H
24 
25 #include <functional>
26 #include <sstream>
27 
29 
31 
35 
41 template <class T_IN, class T_OUT>
42 class InputPort : public Port {
43 public:
44 
48  InputPort(const std::string& id, ComputationalNode* parent, std::function<void(const T_OUT*)> callback, std::size_t maxSubs = 0) :
49  Port(id, parent),
50  _callback(callback),
51  _maxSubs(maxSubs)
52  {}
53 
58  {
59  if(_maxSubs && _nSubs >= _maxSubs) {
60  std::stringstream s;
61  s << "Port \"" << this->id() << "\" of node \"" << this->parent()->id() << "\" can only have " << _maxSubs << " subscriber(s)";
62  throw NRPException::logCreate(s.str());
63  }
64 
65  using std::placeholders::_1;
66  std::function<void(const T_IN*)> receive_f = std::bind(&InputPort<T_IN,T_OUT>::receive, this, _1);
67  port->add_subscriber(receive_f);
68 
69  _nSubs++;
70  }
71 
75  size_t subscriptionsSize() override
76  { return _nSubs; }
77 
82  { return _maxSubs == 0 ? SIZE_MAX : _maxSubs; }
83 
84 private:
85 
91  void receive(const T_IN* msg)
92  {
93  if ( msg == nullptr ) {
94  _callback(nullptr);
95  return;
96  }
97 
98  if constexpr ( std::is_same_v<T_IN, T_OUT> )
99  _callback(msg);
100  else {
102  _callback(&_data);
103  }
104  }
105 
107  T_OUT _data;
109  std::function<void(const T_OUT*)> _callback;
111  std::size_t _maxSubs;
113  std::size_t _nSubs = 0;
114 };
115 
116 
117 #endif //INPUT_PORT_H
port.h
Port
Base class implementing a port in the computational graph.
Definition: port.h:30
data_conversion.h
Port::parent
ComputationalNode * parent() const
Returns the port parent node.
Definition: port.h:53
Port::id
const std::string & id()
Returns the port 'id'.
Definition: port.h:47
nrp_exceptions.h
ComputationalNode::id
const std::string & id() const
Returns the node 'id'.
Definition: computational_node.h:57
dataConverter::convert
static void convert(const T_IN *, T_OUT &)
Definition: data_conversion.h:37
computational_node.h
InputPort::subscriptionsMax
size_t subscriptionsMax()
Return the number ports this port is subscribed to.
Definition: input_port.h:81
output_port.h
NRPException::logCreate
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
OutputPort
Implementation of an output port in the computation graph.
Definition: output_port.h:36
InputPort
Implementation of an input port in the computation graph.
Definition: input_port.h:42
InputPort::subscriptionsSize
size_t subscriptionsSize() override
Return the number ports this port is subscribed to.
Definition: input_port.h:75
python_json_engine.port
port
Definition: python_json_engine.py:197
InputPort::InputPort
InputPort(const std::string &id, ComputationalNode *parent, std::function< void(const T_OUT *)> callback, std::size_t maxSubs=0)
Constructor.
Definition: input_port.h:48
InputPort::subscribeTo
void subscribeTo(OutputPort< T_IN > *port)
Subscribes this port to an OutputPort 'port'.
Definition: input_port.h:57
ComputationalNode
Base class implementing a node in the computational graph.
Definition: computational_node.h:31