Node.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <memory>
5 #include <set>
6 #include <string>
7 #include <tuple>
8 
9 // project
13 
14 // depthai-shared
17 
18 // libraries
19 #include "tl/optional.hpp"
20 
21 namespace dai {
22 // fwd declare Pipeline
23 class Pipeline;
24 class PipelineImpl;
25 
29 class Node {
30  friend class Pipeline;
31  friend class PipelineImpl;
32 
33  public:
35  using Id = std::int64_t;
36  struct Connection;
37  // fwd declare classes
38  class Input;
39  class Output;
40  class InputMap;
41  class OutputMap;
42 
43  protected:
44  std::unordered_map<std::string, Output*> outputRefs;
45  std::unordered_map<std::string, Input*> inputRefs;
46 
47  std::unordered_map<std::string, OutputMap*> outputMapRefs;
48  std::unordered_map<std::string, InputMap*> inputMapRefs;
49 
50  // helpers for setting refs
51  void setOutputRefs(std::initializer_list<Output*> l);
52  void setOutputRefs(Output* outRef);
53  void setInputRefs(std::initializer_list<Input*> l);
54  void setInputRefs(Input* inRef);
55  void setOutputMapRefs(std::initializer_list<OutputMap*> l);
56  void setOutputMapRefs(OutputMap* outMapRef);
57  void setInputMapRefs(std::initializer_list<InputMap*> l);
58  void setInputMapRefs(InputMap* inMapRef);
59 
60  public:
65  };
66 
67  class Output {
69 
70  public:
71  enum class Type { MSender, SSender };
72  std::string group = "";
73  std::string name;
75  // Which types and do descendants count as well?
76  std::vector<DatatypeHierarchy> possibleDatatypes;
77  Output(Node& par, std::string n, Type t, std::vector<DatatypeHierarchy> types)
78  : parent(par), name(std::move(n)), type(t), possibleDatatypes(std::move(types)) {}
79  Output(Node& par, std::string group, std::string n, Type t, std::vector<DatatypeHierarchy> types)
80  : parent(par), group(std::move(group)), name(std::move(n)), type(t), possibleDatatypes(std::move(types)) {}
81 
83  return parent;
84  }
85  const Node& getParent() const {
86  return parent;
87  }
88 
90  std::string toString() const;
91 
97  bool isSamePipeline(const Input& in);
98 
104  bool canConnect(const Input& in);
105 
110  std::vector<Connection> getConnections();
111 
120  void link(const Input& in);
121 
129  void unlink(const Input& in);
130  };
131 
136  class OutputMap : public std::unordered_map<std::string, Output> {
138 
139  public:
140  std::string name;
141  OutputMap(std::string name, Output defaultOutput);
144  Output& operator[](const std::string& key);
145  };
146 
147  class Input {
149 
150  public:
151  enum class Type { SReceiver, MReceiver };
152  std::string group = "";
153  std::string name;
155  bool defaultBlocking{true};
159  // Options - more information about the input
162  friend class Output;
163  std::vector<DatatypeHierarchy> possibleDatatypes;
164 
166  Input(Node& par, std::string n, Type t, std::vector<DatatypeHierarchy> types)
167  : parent(par), name(std::move(n)), type(t), possibleDatatypes(std::move(types)) {}
168 
170  Input(Node& par, std::string n, Type t, bool blocking, int queueSize, std::vector<DatatypeHierarchy> types)
171  : parent(par), name(std::move(n)), type(t), defaultBlocking(blocking), defaultQueueSize(queueSize), possibleDatatypes(std::move(types)) {}
172 
174  Input(Node& par, std::string n, Type t, bool blocking, int queueSize, bool waitForMessage, std::vector<DatatypeHierarchy> types)
175  : parent(par),
176  name(std::move(n)),
177  type(t),
181  possibleDatatypes(std::move(types)) {}
182 
184  Input(Node& par, std::string group, std::string n, Type t, bool blocking, int queueSize, bool waitForMessage, std::vector<DatatypeHierarchy> types)
185  : parent(par),
186  group(std::move(group)),
187  name(std::move(n)),
188  type(t),
192  possibleDatatypes(std::move(types)) {}
193 
195  return parent;
196  }
197  const Node& getParent() const {
198  return parent;
199  }
200 
202  std::string toString() const;
203 
208  void setBlocking(bool blocking);
209 
214  bool getBlocking() const;
215 
221  void setQueueSize(int size);
222 
227  int getQueueSize() const;
228 
236 
241  bool getWaitForMessage() const;
242 
246  void setReusePreviousMessage(bool reusePreviousMessage);
247 
251  bool getReusePreviousMessage() const;
252  };
253 
258  class InputMap : public std::unordered_map<std::string, Input> {
260 
261  public:
262  std::string name;
264  InputMap(std::string name, Input defaultInput);
266  Input& operator[](const std::string& key);
267  };
268 
270  struct Connection {
271  friend struct std::hash<Connection>;
272  Connection(Output out, Input in);
274  std::string outputName;
275  std::string outputGroup;
277  std::string inputName;
278  std::string inputGroup;
279  bool operator==(const Connection& rhs) const;
280  };
281 
282  protected:
283  // when Pipeline tries to serialize and construct on remote, it will check if all connected nodes are on same pipeline
284  std::weak_ptr<PipelineImpl> parent;
285 
286  public:
288  const Id id;
289 
290  protected:
292 
293  virtual Properties& getProperties();
296 
297  public:
298  // Underlying properties
300 
301  // access
303  const Pipeline getParentPipeline() const;
304 
306  virtual std::unique_ptr<Node> clone() const = 0;
307 
309  virtual const char* getName() const = 0;
310 
312  std::vector<Output> getOutputs();
313 
315  std::vector<Input> getInputs();
316 
318  std::vector<Output*> getOutputRefs();
319 
321  std::vector<const Output*> getOutputRefs() const;
322 
324  std::vector<Input*> getInputRefs();
325 
327  std::vector<const Input*> getInputRefs() const;
328 
330  Node(const std::shared_ptr<PipelineImpl>& p, Id nodeId, std::unique_ptr<Properties> props);
331  virtual ~Node() = default;
332 
334  const AssetManager& getAssetManager() const;
335 
338 };
339 
340 // Node CRTP class
341 template <typename Base, typename Derived, typename Props>
342 class NodeCRTP : public Base {
343  public:
344  using Properties = Props;
347  const char* getName() const override {
348  return Derived::NAME;
349  };
350  std::unique_ptr<Node> clone() const override {
351  return std::make_unique<Derived>(static_cast<const Derived&>(*this));
352  };
353 
354  private:
355  NodeCRTP(const std::shared_ptr<PipelineImpl>& par, int64_t nodeId, std::unique_ptr<Properties> props)
356  : Base(par, nodeId, std::move(props)), properties(static_cast<Properties&>(Node::properties)) {}
357  NodeCRTP(const std::shared_ptr<PipelineImpl>& par, int64_t nodeId) : NodeCRTP(par, nodeId, std::make_unique<Props>()) {}
358  friend Derived;
359  friend Base;
360  friend class PipelineImpl;
361 };
362 
363 } // namespace dai
364 
365 // Specialization of std::hash for Node::Connection
366 namespace std {
367 template <>
368 struct hash<dai::Node::Connection> {
369  size_t operator()(const dai::Node::Connection& obj) const {
370  size_t seed = 0;
371  std::hash<dai::Node::Id> hId;
372  std::hash<std::string> hStr;
373  seed ^= hId(obj.outputId) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
374  seed ^= hStr(obj.outputName) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
375  seed ^= hId(obj.inputId) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
376  seed ^= hStr(obj.outputName) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
377  return seed;
378  }
379 };
380 
381 } // namespace std
dai::NodeCRTP::properties
Properties & properties
Underlying properties.
Definition: Node.hpp:346
dai::Node::OutputMap::name
std::string name
Definition: Node.hpp:140
std::hash< dai::Node::Connection >::operator()
size_t operator()(const dai::Node::Connection &obj) const
Definition: Node.hpp:369
dai::Node::getParentPipeline
Pipeline getParentPipeline()
Definition: Node.cpp:15
dai::Node::Output::getParent
const Node & getParent() const
Definition: Node.hpp:85
dai::Node::id
const Id id
Id of node.
Definition: Node.hpp:288
dai::Node::Input::getBlocking
bool getBlocking() const
Definition: Node.cpp:98
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
dai::Node::clone
virtual std::unique_ptr< Node > clone() const =0
Deep copy the node.
dai::Node::Id
std::int64_t Id
Node identificator. Unique for every node on a single Pipeline.
Definition: Node.hpp:35
dai::Node::Input::Type::MReceiver
@ MReceiver
dai::Node::getProperties
virtual Properties & getProperties()
Definition: Node.cpp:25
dai::Node::Input::getReusePreviousMessage
bool getReusePreviousMessage() const
Definition: Node.cpp:128
dai::DatatypeEnum
DatatypeEnum
Definition: DatatypeEnum.hpp:7
dai::Node::InputMap::defaultInput
Input defaultInput
Definition: Node.hpp:259
optional.hpp
dai::Node::Output::possibleDatatypes
std::vector< DatatypeHierarchy > possibleDatatypes
Definition: Node.hpp:76
dai::Node::Output::isSamePipeline
bool isSamePipeline(const Input &in)
Definition: Node.cpp:70
dai::Node::setInputRefs
void setInputRefs(std::initializer_list< Input * > l)
dai::Node::Input::type
Type type
Definition: Node.hpp:154
dai::Node::Output::Type
Type
Definition: Node.hpp:71
dai::Node::Output::getParent
Node & getParent()
Definition: Node.hpp:82
dai::Node::DatatypeHierarchy
Definition: Node.hpp:61
dai::Node::Input::group
std::string group
Definition: Node.hpp:152
dai::Node::setOutputMapRefs
void setOutputMapRefs(std::initializer_list< OutputMap * > l)
dai::Node
Abstract Node.
Definition: Node.hpp:29
dai::Node::propertiesHolder
copyable_unique_ptr< Properties > propertiesHolder
Definition: Node.hpp:295
dai::Node::Input::getParent
const Node & getParent() const
Definition: Node.hpp:197
dai::Node::Connection::inputGroup
std::string inputGroup
Definition: Node.hpp:278
dai::Node::Input::setWaitForMessage
void setWaitForMessage(bool waitForMessage)
Definition: Node.cpp:116
dai::Node::Output::type
Type type
Definition: Node.hpp:74
dai::Node::assetManager
AssetManager assetManager
Definition: Node.hpp:291
dai::Node::Output
Definition: Node.hpp:67
dai::AssetManager
AssetManager can store assets and serialize.
Definition: AssetManager.hpp:33
dai::Node::InputMap::name
std::string name
Definition: Node.hpp:262
dai::NodeCRTP
Definition: Node.hpp:342
dai::Node::setInputMapRefs
void setInputMapRefs(std::initializer_list< InputMap * > l)
dai::Node::properties
Properties & properties
Definition: Node.hpp:299
dai::Node::InputMap::InputMap
InputMap(Input defaultInput)
Definition: Node.cpp:155
DatatypeEnum.hpp
dai::Node::Output::canConnect
bool canConnect(const Input &in)
Definition: Node.cpp:80
dai::Node::Output::unlink
void unlink(const Input &in)
Definition: Node.cpp:89
dai::Node::OutputMap::operator[]
Output & operator[](const std::string &key)
Create or modify an input.
Definition: Node.cpp:142
dai::Node::Input::possibleDatatypes
std::vector< DatatypeHierarchy > possibleDatatypes
Definition: Node.hpp:163
dai::Node::Output::name
std::string name
Definition: Node.hpp:73
dai::Node::Input::setReusePreviousMessage
void setReusePreviousMessage(bool reusePreviousMessage)
Definition: Node.cpp:124
dai::Node::Input::defaultWaitForMessage
bool defaultWaitForMessage
Definition: Node.hpp:161
dai::Node::getInputRefs
std::vector< Input * > getInputRefs()
Retrieves reference to node inputs.
Definition: Node.cpp:224
copyable_unique_ptr.hpp
dai::Node::Input::defaultQueueSize
int defaultQueueSize
Definition: Node.hpp:156
dai::Node::getAssetManager
const AssetManager & getAssetManager() const
Get node AssetManager as a const reference.
Definition: Node.cpp:132
dai::Node::Input::Input
Input(Node &par, std::string n, Type t, bool blocking, int queueSize, std::vector< DatatypeHierarchy > types)
Constructs Input with specified blocking and queueSize options.
Definition: Node.hpp:170
dai::PipelineImpl
Definition: Pipeline.hpp:23
dai::NodeCRTP::NodeCRTP
NodeCRTP(const std::shared_ptr< PipelineImpl > &par, int64_t nodeId, std::unique_ptr< Properties > props)
Definition: Node.hpp:355
DAI_SPAN_NAMESPACE_NAME::detail::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.hpp:167
dai::Node::getOutputs
std::vector< Output > getOutputs()
Retrieves all nodes outputs.
Definition: Node.cpp:169
dai::Node::Input::getParent
Node & getParent()
Definition: Node.hpp:194
dai::Node::OutputMap::OutputMap
OutputMap(std::string name, Output defaultOutput)
Definition: Node.cpp:140
dai::Node::Output::group
std::string group
Definition: Node.hpp:72
dai::Node::Input
Definition: Node.hpp:147
dai::Node::Connection
Connection between an Input and Output.
Definition: Node.hpp:270
dai::Node::Input::Input
Input(Node &par, std::string group, std::string n, Type t, bool blocking, int queueSize, bool waitForMessage, std::vector< DatatypeHierarchy > types)
Constructs Input with specified blocking and queueSize as well as additional options.
Definition: Node.hpp:184
dai::Node::getName
virtual const char * getName() const =0
Retrieves nodes name.
dai::Node::DatatypeHierarchy::DatatypeHierarchy
DatatypeHierarchy(DatatypeEnum d, bool c)
Definition: Node.hpp:62
dai::NodeCRTP::clone
std::unique_ptr< Node > clone() const override
Definition: Node.hpp:350
dai::Node::Output::Type::SSender
@ SSender
dai::Node::Output::getConnections
std::vector< Connection > getConnections()
Definition: Node.cpp:59
dai::Node::Output::toString
std::string toString() const
Output to string representation.
Definition: Node.cpp:43
dai::Node::InputMap::operator[]
Input & operator[](const std::string &key)
Create or modify an input.
Definition: Node.cpp:156
dai::Node::Node
Node(const std::shared_ptr< PipelineImpl > &p, Id nodeId, std::unique_ptr< Properties > props)
Constructs Node.
Definition: Node.cpp:8
AssetManager.hpp
dai::Node::Connection::outputName
std::string outputName
Definition: Node.hpp:274
dai::Node::Input::queueSize
tl::optional< int > queueSize
Definition: Node.hpp:158
dai::Node::getOutputRefs
std::vector< Output * > getOutputRefs()
Retrieves reference to node outputs.
Definition: Node.cpp:187
dai::Node::Input::parent
Node & parent
Definition: Node.hpp:148
dai::Node::Output::parent
Node & parent
Definition: Node.hpp:68
dai::Properties
Base Properties structure.
Definition: Properties.hpp:8
dai::Node::outputRefs
std::unordered_map< std::string, Output * > outputRefs
Definition: Node.hpp:41
dai::Node::Input::toString
std::string toString() const
Input to string representation.
Definition: Node.cpp:51
dai::Node::Connection::outputId
Id outputId
Definition: Node.hpp:273
dai::Node::Input::setBlocking
void setBlocking(bool blocking)
Definition: Node.cpp:94
dai::Node::setOutputRefs
void setOutputRefs(std::initializer_list< Output * > l)
dai::Node::getRequiredOpenVINOVersion
virtual tl::optional< OpenVINO::Version > getRequiredOpenVINOVersion()
Definition: Node.cpp:11
dai::Node::Output::Output
Output(Node &par, std::string n, Type t, std::vector< DatatypeHierarchy > types)
Definition: Node.hpp:77
dai::Node::Input::Type
Type
Definition: Node.hpp:151
dai::Node::Connection::operator==
bool operator==(const Connection &rhs) const
Definition: Node.cpp:38
dai::Node::outputMapRefs
std::unordered_map< std::string, OutputMap * > outputMapRefs
Definition: Node.hpp:47
dai::NodeCRTP::NodeCRTP
NodeCRTP(const std::shared_ptr< PipelineImpl > &par, int64_t nodeId)
Definition: Node.hpp:357
OpenVINO.hpp
tl::optional< bool >
std
Definition: Node.hpp:366
dai::Node::inputMapRefs
std::unordered_map< std::string, InputMap * > inputMapRefs
Definition: Node.hpp:48
Properties.hpp
dai::Node::parent
std::weak_ptr< PipelineImpl > parent
Definition: Node.hpp:284
dai::Node::Input::blocking
tl::optional< bool > blocking
Definition: Node.hpp:157
dai::Node::Input::defaultBlocking
bool defaultBlocking
Definition: Node.hpp:155
dai::Node::Input::Input
Input(Node &par, std::string n, Type t, bool blocking, int queueSize, bool waitForMessage, std::vector< DatatypeHierarchy > types)
Constructs Input with specified blocking and queueSize as well as additional options.
Definition: Node.hpp:174
dai::Node::OutputMap::defaultOutput
Output defaultOutput
Definition: Node.hpp:137
dai::Node::Connection::outputGroup
std::string outputGroup
Definition: Node.hpp:275
dai::NodeCRTP::getName
const char * getName() const override
Definition: Node.hpp:347
dai::Node::OutputMap
Definition: Node.hpp:136
dai::Node::Connection::Connection
Connection(Output out, Input in)
Definition: Node.cpp:29
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
dai::MessageDemuxProperties
Definition: MessageDemuxProperties.hpp:10
dai::Node::Connection::inputId
Id inputId
Definition: Node.hpp:276
dai::Node::InputMap
Definition: Node.hpp:258
dai::NodeCRTP::Base
friend Base
Definition: Node.hpp:359
dai::Node::Input::Type::SReceiver
@ SReceiver
dai::Node::Output::Type::MSender
@ MSender
dai::Node::inputRefs
std::unordered_map< std::string, Input * > inputRefs
Definition: Node.hpp:45
dai::NodeCRTP::Derived
friend Derived
Definition: Node.hpp:358
dai::Node::Input::setQueueSize
void setQueueSize(int size)
Definition: Node.cpp:105
dai
Definition: CameraExposureOffset.hpp:6
dai::Node::getInputs
std::vector< Input > getInputs()
Retrieves all nodes inputs.
Definition: Node.cpp:178
dai::Node::~Node
virtual ~Node()=default
dai::Node::Input::getQueueSize
int getQueueSize() const
Definition: Node.cpp:109
dai::Node::Output::Output
Output(Node &par, std::string group, std::string n, Type t, std::vector< DatatypeHierarchy > types)
Definition: Node.hpp:79
dai::Node::DatatypeHierarchy::descendants
bool descendants
Definition: Node.hpp:64
dai::Node::Input::getWaitForMessage
bool getWaitForMessage() const
Definition: Node.cpp:120
dai::copyable_unique_ptr
Definition: copyable_unique_ptr.hpp:104
dai::Node::Input::waitForMessage
tl::optional< bool > waitForMessage
Definition: Node.hpp:160
dai::Node::Connection::inputName
std::string inputName
Definition: Node.hpp:277
dai::Node::Input::Input
Input(Node &par, std::string n, Type t, std::vector< DatatypeHierarchy > types)
Constructs Input with default blocking and queueSize options.
Definition: Node.hpp:166
dai::Node::Input::name
std::string name
Definition: Node.hpp:153
dai::Node::DatatypeHierarchy::datatype
DatatypeEnum datatype
Definition: Node.hpp:63


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19