.. _program_listing_file_include_depthai_pipeline_Node.hpp: Program Listing for File Node.hpp ================================= |exhale_lsh| :ref:`Return to documentation for file ` (``include/depthai/pipeline/Node.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include // project #include "depthai/openvino/OpenVINO.hpp" #include "depthai/pipeline/AssetManager.hpp" #include "depthai/utility/copyable_unique_ptr.hpp" // depthai-shared #include "depthai-shared/datatype/DatatypeEnum.hpp" #include "depthai-shared/properties/Properties.hpp" // libraries #include "tl/optional.hpp" namespace dai { // fwd declare Pipeline class Pipeline; class PipelineImpl; class Node { friend class Pipeline; friend class PipelineImpl; public: using Id = std::int64_t; struct Connection; // fwd declare classes class Input; class Output; class InputMap; class OutputMap; protected: std::unordered_map outputRefs; std::unordered_map inputRefs; std::unordered_map outputMapRefs; std::unordered_map inputMapRefs; // helpers for setting refs void setOutputRefs(std::initializer_list l); void setOutputRefs(Output* outRef); void setInputRefs(std::initializer_list l); void setInputRefs(Input* inRef); void setOutputMapRefs(std::initializer_list l); void setOutputMapRefs(OutputMap* outMapRef); void setInputMapRefs(std::initializer_list l); void setInputMapRefs(InputMap* inMapRef); public: struct DatatypeHierarchy { DatatypeHierarchy(DatatypeEnum d, bool c) : datatype(d), descendants(c) {} DatatypeEnum datatype; bool descendants; }; class Output { Node& parent; public: enum class Type { MSender, SSender }; std::string group = ""; std::string name; Type type; // Which types and do descendants count as well? std::vector possibleDatatypes; Output(Node& par, std::string n, Type t, std::vector types) : parent(par), name(std::move(n)), type(t), possibleDatatypes(std::move(types)) {} Output(Node& par, std::string group, std::string n, Type t, std::vector types) : parent(par), group(std::move(group)), name(std::move(n)), type(t), possibleDatatypes(std::move(types)) {} Node& getParent() { return parent; } const Node& getParent() const { return parent; } std::string toString() const; bool isSamePipeline(const Input& in); bool canConnect(const Input& in); std::vector getConnections(); void link(const Input& in); void unlink(const Input& in); }; class OutputMap : public std::unordered_map { Output defaultOutput; public: std::string name; OutputMap(std::string name, Output defaultOutput); OutputMap(Output defaultOutput); Output& operator[](const std::string& key); }; class Input { Node& parent; public: enum class Type { SReceiver, MReceiver }; std::string group = ""; std::string name; Type type; bool defaultBlocking{true}; int defaultQueueSize{8}; tl::optional blocking; tl::optional queueSize; // Options - more information about the input tl::optional waitForMessage; bool defaultWaitForMessage{false}; friend class Output; std::vector possibleDatatypes; Input(Node& par, std::string n, Type t, std::vector types) : parent(par), name(std::move(n)), type(t), possibleDatatypes(std::move(types)) {} Input(Node& par, std::string n, Type t, bool blocking, int queueSize, std::vector types) : parent(par), name(std::move(n)), type(t), defaultBlocking(blocking), defaultQueueSize(queueSize), possibleDatatypes(std::move(types)) {} Input(Node& par, std::string n, Type t, bool blocking, int queueSize, bool waitForMessage, std::vector types) : parent(par), name(std::move(n)), type(t), defaultBlocking(blocking), defaultQueueSize(queueSize), defaultWaitForMessage(waitForMessage), possibleDatatypes(std::move(types)) {} Input(Node& par, std::string group, std::string n, Type t, bool blocking, int queueSize, bool waitForMessage, std::vector types) : parent(par), group(std::move(group)), name(std::move(n)), type(t), defaultBlocking(blocking), defaultQueueSize(queueSize), defaultWaitForMessage(waitForMessage), possibleDatatypes(std::move(types)) {} Node& getParent() { return parent; } const Node& getParent() const { return parent; } std::string toString() const; void setBlocking(bool blocking); bool getBlocking() const; void setQueueSize(int size); int getQueueSize() const; void setWaitForMessage(bool waitForMessage); bool getWaitForMessage() const; void setReusePreviousMessage(bool reusePreviousMessage); bool getReusePreviousMessage() const; }; class InputMap : public std::unordered_map { Input defaultInput; public: std::string name; InputMap(Input defaultInput); InputMap(std::string name, Input defaultInput); Input& operator[](const std::string& key); }; struct Connection { friend struct std::hash; Connection(Output out, Input in); Id outputId; std::string outputName; std::string outputGroup; Id inputId; std::string inputName; std::string inputGroup; bool operator==(const Connection& rhs) const; }; protected: // when Pipeline tries to serialize and construct on remote, it will check if all connected nodes are on same pipeline std::weak_ptr parent; public: const Id id; protected: AssetManager assetManager; virtual Properties& getProperties(); virtual tl::optional getRequiredOpenVINOVersion(); copyable_unique_ptr propertiesHolder; public: // Underlying properties Properties& properties; // access Pipeline getParentPipeline(); const Pipeline getParentPipeline() const; virtual std::unique_ptr clone() const = 0; virtual const char* getName() const = 0; std::vector getOutputs(); std::vector getInputs(); std::vector getOutputRefs(); std::vector getOutputRefs() const; std::vector getInputRefs(); std::vector getInputRefs() const; Node(const std::shared_ptr& p, Id nodeId, std::unique_ptr props); virtual ~Node() = default; const AssetManager& getAssetManager() const; AssetManager& getAssetManager(); }; // Node CRTP class template class NodeCRTP : public Base { public: using Properties = Props; Properties& properties; const char* getName() const override { return Derived::NAME; }; std::unique_ptr clone() const override { return std::make_unique(static_cast(*this)); }; private: NodeCRTP(const std::shared_ptr& par, int64_t nodeId, std::unique_ptr props) : Base(par, nodeId, std::move(props)), properties(static_cast(Node::properties)) {} NodeCRTP(const std::shared_ptr& par, int64_t nodeId) : NodeCRTP(par, nodeId, std::make_unique()) {} friend Derived; friend Base; friend class PipelineImpl; }; } // namespace dai // Specialization of std::hash for Node::Connection namespace std { template <> struct hash { size_t operator()(const dai::Node::Connection& obj) const { size_t seed = 0; std::hash hId; std::hash hStr; seed ^= hId(obj.outputId) + 0x9e3779b9 + (seed << 6) + (seed >> 2); seed ^= hStr(obj.outputName) + 0x9e3779b9 + (seed << 6) + (seed >> 2); seed ^= hId(obj.inputId) + 0x9e3779b9 + (seed << 6) + (seed >> 2); seed ^= hStr(obj.outputName) + 0x9e3779b9 + (seed << 6) + (seed >> 2); return seed; } }; } // namespace std