0.4.1
Loading...
Searching...
No Matches
NodeManager.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9/// @file NodeManager.hpp
10/// @brief Manages all Nodes
11/// @author T. Topp (topp@ins.uni-stuttgart.de)
12/// @date 2020-12-14
13
14#pragma once
15
16#include <imgui_node_editor.h>
17
19#include "internal/Node/Pin.hpp"
20#include "util/Assert.h"
21
22#include "NodeData/NodeData.hpp"
23
24#include <vector>
25#include <functional>
26
27namespace NAV::NodeManager
28{
29/// Flag if invokeCallbacks triggers a GUI Flow event
31
32/// Flag if notifyOutputValueChanged & notifyInputValueChanged triggers a GUI Flow event
34
35/// @brief List of all registered Nodes
36const std::vector<Node*>& m_Nodes();
37
38/// @brief Add the provided node object to the list of nodes
39/// @param[in] node Node object to add to the list
40void AddNode(Node* node);
41
42/// @brief Update the provided node object
43/// @param[in] node Node object to add to the list
44void UpdateNode(Node* node);
45
46/// @brief Delete the node provided by id
47/// @param[in] nodeId Unique Id of the Node to delete
48/// @return True if delete was successful, false if NodeId does not exist
49bool DeleteNode(ax::NodeEditor::NodeId nodeId);
50
51/// @brief Delete all nodes
52void DeleteAllNodes();
53
54/// @brief Adds the link
55/// @param[in] linkId Unique Id of the link
56void AddLink(ax::NodeEditor::LinkId linkId);
57
58/// @brief Create an Input Pin object
59/// @param[in] node Node to register the Pin for
60/// @param[in] name Display name of the Pin
61/// @param[in] pinType Type of the pin
62/// @param[in] dataIdentifier Identifier of the data which is represented by the pin
63/// @param[in] callback Callback to register with the pin
64/// @param[in] firable Function to check whether the callback is firable
65/// @param[in] priority Priority when checking firable condition related to other pins (higher priority gets triggered first)
66/// @param[in] idx Index where to put the new pin (-1 means at the end)
67/// @return Pointer to the created pin
68InputPin* CreateInputPin(Node* node, const char* name, Pin::Type pinType, const std::vector<std::string>& dataIdentifier = {},
69 InputPin::Callback callback = static_cast<InputPin::FlowFirableCallbackFunc>(nullptr),
70 InputPin::FlowFirableCheckFunc firable = nullptr,
71 int priority = 0, int idx = -1);
72
73/// @brief Create an Input Pin object
74/// @tparam T Node Class where the function is member of
75/// @param[in] node Node to register the Pin for
76/// @param[in] name Display name of the Pin
77/// @param[in] pinType Type of the pin
78/// @param[in] dataIdentifier Identifier of the data which is represented by the pin
79/// @param[in] callback Flow firable callback function to register with the pin
80/// @param[in] firable Function to check whether the callback is firable
81/// @param[in] priority Priority when checking firable condition related to other pins (higher priority gets triggered first)
82/// @param[in] idx Index where to put the new pin (-1 means at the end)
83/// @return Pointer to the created pin
84template<std::derived_from<Node> T>
85InputPin* CreateInputPin(Node* node, const char* name, Pin::Type pinType, const std::vector<std::string>& dataIdentifier = {},
86 void (T::*callback)(InputPin::NodeDataQueue&, size_t) = nullptr,
87 InputPin::FlowFirableCheckFunc firable = nullptr,
88 int priority = 0, int idx = -1)
89{
90 assert(pinType == Pin::Type::Flow);
91
92 return CreateInputPin(node, name, pinType, dataIdentifier, InputPin::Callback(static_cast<InputPin::FlowFirableCallbackFunc>(callback)), firable, priority, idx);
93}
94
95/// @brief Create an Input Pin object for data pins. The additional notify function gets called when the connected data was changed and the connected node calls NAV::Node::notifyOutputValueChanged
96/// @tparam T Node Class where the function is member of
97/// @param[in] node Node to register the Pin for
98/// @param[in] name Display name of the Pin
99/// @param[in] pinType Type of the pin
100/// @param[in] dataIdentifier Identifier of the data which is represented by the pin
101/// @param[in] notifyFunc Function to call when the data is updated
102/// @param[in] idx Index where to put the new pin (-1 means at the end)
103/// @return Pointer to the created pin
104template<std::derived_from<Node> T>
105InputPin* CreateInputPin(Node* node, const char* name, Pin::Type pinType, const std::vector<std::string>& dataIdentifier,
106 void (T::*notifyFunc)(const InsTime&, size_t), int idx = -1)
107{
108 assert(pinType != Pin::Type::Flow);
109
110 return CreateInputPin(node, name, pinType, dataIdentifier, InputPin::Callback(static_cast<InputPin::DataChangedNotifyFunc>(notifyFunc)), nullptr, 0, idx);
111}
112
113/// @brief Create an Output Pin object
114/// @param[in] node Node to register the Pin for
115/// @param[in] name Display name of the Pin
116/// @param[in] pinType Type of the pin
117/// @param[in] dataIdentifier Identifier of the data which is represented by the pin
118/// @param[in] data Pointer to data which is represented by the pin
119/// @param[in] idx Index where to put the new pin (-1 means at the end)
120/// @return Pointer to the created pin
121OutputPin* CreateOutputPin(Node* node, const char* name, Pin::Type pinType, const std::vector<std::string>& dataIdentifier, OutputPin::PinData data = static_cast<void*>(nullptr), int idx = -1);
122
123/// @brief Create an Output Pin object for Flow Pins
124/// @tparam T Class where the function is member of
125/// @param[in] node Node to register the Pin for
126/// @param[in] name Display name of the Pin
127/// @param[in] pinType Type of the pin
128/// @param[in] dataIdentifier Identifier of the data which is represented by the pin
129/// @param[in] peekPollDataFunc Function to poll for data on this pin
130/// @param[in] idx Index where to put the new pin (-1 means at the end)
131/// @return Pointer to the created pin
132template<std::derived_from<Node> T>
133OutputPin* CreateOutputPin(Node* node, const char* name, Pin::Type pinType, const std::vector<std::string>& dataIdentifier,
134 std::shared_ptr<const NAV::NodeData> (T::*peekPollDataFunc)(size_t, bool) = nullptr, int idx = -1)
135{
136 assert(pinType == Pin::Type::Flow);
137 INS_ASSERT_USER_ERROR(std::none_of(node->outputPins.begin(), node->outputPins.end(),
138 [](const OutputPin& outputPin) { return std::holds_alternative<OutputPin::PollDataFunc>(outputPin.data); }),
139 "You cannot mix PollDataFunc and PeekPollDataFunc output pins. Use only PeekPollDataFunc pins if multiple pins are needed.");
140
141 return CreateOutputPin(node, name, pinType, dataIdentifier, OutputPin::PinData(static_cast<OutputPin::PeekPollDataFunc>(peekPollDataFunc)), idx);
142}
143
144/// @brief Create an Output Pin object for Flow Pins
145/// @tparam T Class where the function is member of
146/// @param[in] node Node to register the Pin for
147/// @param[in] name Display name of the Pin
148/// @param[in] pinType Type of the pin
149/// @param[in] dataIdentifier Identifier of the data which is represented by the pin
150/// @param[in] pollDataFunc Function to poll for data on this pin
151/// @param[in] idx Index where to put the new pin (-1 means at the end)
152/// @return Pointer to the created pin
153template<std::derived_from<Node> T>
154OutputPin* CreateOutputPin(Node* node, const char* name, Pin::Type pinType, const std::vector<std::string>& dataIdentifier,
155 std::shared_ptr<const NAV::NodeData> (T::*pollDataFunc)() = nullptr, int idx = -1)
156{
157 assert(pinType == Pin::Type::Flow);
158 INS_ASSERT_USER_ERROR(std::none_of(node->outputPins.begin(), node->outputPins.end(),
159 [](const OutputPin& outputPin) { return std::holds_alternative<OutputPin::PeekPollDataFunc>(outputPin.data)
160 || std::holds_alternative<OutputPin::PollDataFunc>(outputPin.data); }),
161 "There can only be one poll pin if the poll only data function is chosen. If multiple are needed, create PeekPollDataFunc pins.");
162
163 return CreateOutputPin(node, name, pinType, dataIdentifier, OutputPin::PinData(static_cast<OutputPin::PollDataFunc>(pollDataFunc)), idx);
164}
165
166/// @brief Deletes the output pin. Invalidates the pin reference given.
167/// @param[in, out] pin Output Pin to delete
168/// @return True if the pin was delete
169bool DeleteOutputPin(OutputPin& pin);
170
171/// @brief Deletes the input pin. Invalidates the pin reference given.
172/// @param[in, out] pin Input Pin to delete
173/// @return True if the pin was delete
174bool DeleteInputPin(InputPin& pin);
175
176/// @brief Finds the Node for the NodeId
177/// @param[in] id Unique Id of the Node to search for
178/// @return Pointer to the node or nullptr if the NodeId does not exist
179Node* FindNode(ax::NodeEditor::NodeId id);
180
181/// @brief Finds the Pin for the PinId
182/// @param[in] id Unique Id of the Pin to search for
183/// @return Pointer to the pin or nullptr if the PinId does not exist
184OutputPin* FindOutputPin(ax::NodeEditor::PinId id);
185
186/// @brief Finds the Pin for the PinId
187/// @param[in] id Unique Id of the Pin to search for
188/// @return Pointer to the pin or nullptr if the PinId does not exist
189InputPin* FindInputPin(ax::NodeEditor::PinId id);
190
191/// @brief Enables all Node callbacks
192void EnableAllCallbacks();
193
194/// @brief Disables all Node callbacks
196
197/// @brief Clears all nodes queues
198void ClearAllNodeQueues();
199
200/// @brief Initializes all nodes.
201/// @return Returns false if one of the nodes could not initialize
202bool InitializeAllNodes();
203
204/// @brief Initializes all nodes in a separate thread
206
207/// @brief Generates a new node id
208ax::NodeEditor::NodeId GetNextNodeId();
209
210/// @brief Generates a new link id
211ax::NodeEditor::LinkId GetNextLinkId();
212
213/// @brief Generates a new pin id
214ax::NodeEditor::PinId GetNextPinId();
215
216#ifdef TESTING
217
218/// @brief Registers the callback function to the watcher list
219/// @param[in] id Output pin id to add the callback to
220/// @param[in] callback Callback function
221/// @attention ApplyWatcherCallbacks() needs to be called after loading the flow to apply the list to the pins.
222void RegisterWatcherCallbackToInputPin(ax::NodeEditor::PinId id, const InputPin::WatcherCallback& callback);
223
224/// @brief Registers the callback function to the watcher list
225/// @param[in] id Link id to add the callback to
226/// @param[in] callback Callback function
227/// @attention ApplyWatcherCallbacks() needs to be called after loading the flow to apply the list to the pins.
228void RegisterWatcherCallbackToLink(ax::NodeEditor::LinkId id, const InputPin::WatcherCallback& callback);
229
230/// @brief Applies the watcher lists to the node pins
231void ApplyWatcherCallbacks();
232
233/// @brief Registers a callback function which gets called before the nodes are initialized. Used to change node settings.
234/// @param[in] callback Callback function
235void RegisterPreInitCallback(std::function<void()> callback);
236
237/// @brief Calls the pre-init callback
238void CallPreInitCallback();
239
240/// @brief Registers a callback which gets called after flow execution before cleanup
241/// @param[in] callback Callback function
242void RegisterCleanupCallback(std::function<void()> callback);
243
244/// @brief Calls the cleanup callback
245void CallCleanupCallback();
246
247/// @brief Clears the watcher list
248void ClearRegisteredCallbacks();
249
250#endif
251
252} // namespace NAV::NodeManager
Assertion helpers.
#define INS_ASSERT_USER_ERROR(_EXP, _MSG)
Assert function with message.
Definition Assert.h:21
Abstract NodeData Class.
Node Class.
Pin class.
Input pins of nodes.
Definition Pin.hpp:491
TsDeque< std::shared_ptr< const NAV::NodeData > > NodeDataQueue
Node data queue type.
Definition Pin.hpp:707
void(Node::*)(NodeDataQueue &, size_t) FlowFirableCallbackFunc
Definition Pin.hpp:712
std::variant< FlowFirableCallbackFunc, DataChangedNotifyFunc > Callback
Callback function types.
Definition Pin.hpp:718
bool(*)(const Node *, const InputPin &) FlowFirableCheckFunc
Function type to call when checking if a pin is firable.
Definition Pin.hpp:725
void(Node::*)(const InsTime &, size_t) DataChangedNotifyFunc
Definition Pin.hpp:716
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
Abstract parent class for all nodes.
Definition Node.hpp:92
std::vector< OutputPin > outputPins
List of output pins.
Definition Node.hpp:399
Output pins of nodes.
Definition Pin.hpp:338
std::shared_ptr< const NAV::NodeData >(Node::*)(size_t, bool) PeekPollDataFunc
FileReader/Simulator peekPollData function type for nodes with more than one polling pin.
Definition Pin.hpp:442
std::variant< const void *, const bool *, const int *, const float *, const double *, const std::string *, PeekPollDataFunc, PollDataFunc > PinData
Possible Types represented by an output pin.
Definition Pin.hpp:448
std::shared_ptr< const NAV::NodeData >(Node::*)() PollDataFunc
FileReader/Simulator pollData function type for nodes with a single poll pin.
Definition Pin.hpp:445
void ClearAllNodeQueues()
Clears all nodes queues.
OutputPin * CreateOutputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier, OutputPin::PinData data=static_cast< void * >(nullptr), int idx=-1)
Create an Output Pin object.
ax::NodeEditor::LinkId GetNextLinkId()
Generates a new link id.
bool showFlowWhenNotifyingValueChange
Flag if notifyOutputValueChanged & notifyInputValueChanged triggers a GUI Flow event.
bool DeleteOutputPin(OutputPin &pin)
Deletes the output pin. Invalidates the pin reference given.
bool DeleteNode(ax::NodeEditor::NodeId nodeId)
Delete the node provided by id.
InputPin * FindInputPin(ax::NodeEditor::PinId id)
Finds the Pin for the PinId.
void InitializeAllNodesAsync()
Initializes all nodes in a separate thread.
void UpdateNode(Node *node)
Update the provided node object.
void AddLink(ax::NodeEditor::LinkId linkId)
Adds the link.
OutputPin * FindOutputPin(ax::NodeEditor::PinId id)
Finds the Pin for the PinId.
void EnableAllCallbacks()
Enables all Node callbacks.
void DisableAllCallbacks()
Disables all Node callbacks.
ax::NodeEditor::NodeId GetNextNodeId()
Generates a new node id.
bool InitializeAllNodes()
Initializes all nodes.
bool DeleteInputPin(InputPin &pin)
Deletes the input pin. Invalidates the pin reference given.
void DeleteAllNodes()
Delete all nodes.
const std::vector< Node * > & m_Nodes()
List of all registered Nodes.
void AddNode(Node *node)
Add the provided node object to the list of nodes.
ax::NodeEditor::PinId GetNextPinId()
Generates a new pin id.
Node * FindNode(ax::NodeEditor::NodeId id)
Finds the Node for the NodeId.
bool showFlowWhenInvokingCallbacks
Flag if invokeCallbacks triggers a GUI Flow event.
InputPin * CreateInputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier={}, InputPin::Callback callback=static_cast< InputPin::FlowFirableCallbackFunc >(nullptr), InputPin::FlowFirableCheckFunc firable=nullptr, int priority=0, int idx=-1)
Create an Input Pin object.
Type of the data on the Pin.
Definition Pin.hpp:47
@ Flow
NodeData Trigger.
Definition Pin.hpp:52