smgl 0.11.0
Structured Metadata Engine and Graph Objects Library
Loading...
Searching...
No Matches
Node.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <functional>
6#include <map>
7#include <memory>
8#include <typeinfo>
9#include <unordered_map>
10#include <vector>
11
12#include "smgl/Factory.hpp"
13#include "smgl/Metadata.hpp"
14#include "smgl/Ports.hpp"
15#include "smgl/Singleton.hpp"
16#include "smgl/Uuid.hpp"
17#include "smgl/filesystem.hpp"
18
19namespace smgl
20{
21
70{
71public:
73 enum class State {
75 Idle,
77 Waiting,
79 Ready,
83 Error
84 };
85
88
90 struct Info {
92 Info(std::string n, const Uuid& u) : name{std::move(n)}, uuid{u} {}
97 };
98
106 void update();
107
118 auto serialize(bool useCache, const filesystem::path& cacheRoot)
119 -> Metadata;
120
131 void deserialize(const Metadata& meta, const filesystem::path& cacheRoot);
132
134 auto getInputPortsInfo() const -> std::vector<Info>;
135
137 auto getOutputPortsInfo() const -> std::vector<Info>;
138
143 auto getInputPort(const Uuid& uuid) -> Input&;
148 auto getInputPort(const std::string& name) -> Input&;
149
154 auto getOutputPort(const Uuid& uuid) -> Output&;
159 auto getOutputPort(const std::string& name) -> Output&;
160
162 auto getInputConnections() const -> std::vector<Connection>;
163
165 auto getNumberOfInputConnections() const -> size_t;
166
168 auto getOutputConnections() const -> std::vector<Connection>;
169
171 auto getNumberOfOutputConnections() const -> size_t;
172
174 auto state() -> State;
175
176protected:
180 explicit Node(bool usesCacheDir);
182 virtual ~Node() = default;
183
190 template <typename T>
191 void registerInputPort(const std::string& name, InputPort<T>& port);
192
194 template <typename T>
195 void registerPort(const std::string& name, InputPort<T>& port);
196
203 template <typename T, typename... Args>
205 const std::string& name, OutputPort<T, Args...>& port);
206
208 template <typename T, typename... Args>
209 void registerPort(const std::string& name, OutputPort<T, Args...>& port);
210
234 std::function<void()> compute;
235
251 std::function<bool()> usesCacheDir;
252
253private:
294 virtual auto serialize_(bool useCache, const filesystem::path& cacheDir)
295 -> Metadata;
296
297 // clang-format off
335 // clang-format on
336 virtual void deserialize_(
337 const Metadata& data, const filesystem::path& cacheDir);
338
343 auto update_input_ports_() -> bool;
344
347
349 auto update_output_ports_() -> bool;
350
355 template <class PortType>
356 static void LoadAndRegisterPort(
357 const std::string& name,
358 const Metadata& data,
359 std::unordered_map<Uuid, PortType*>& byUuid,
360 std::map<std::string, PortType*>& byName);
361
363 std::unordered_map<Uuid, Input*> inputs_by_uuid_;
365 std::map<std::string, Input*> inputs_by_name_;
367 std::unordered_map<Uuid, Output*> outputs_by_uuid_;
372};
373
374namespace detail
375{
385} // namespace detail
386
389 detail::NodeFactoryType::InstanceType::unknown_identifier;
390
401template <class T>
402struct NodeDesc {
404 explicit NodeDesc(std::string k) : key{std::move(k)} {}
407};
408
428template <class... Ts>
429auto RegisterNodes(const NodeDesc<Ts>&... descs) -> bool;
430
442template <class T>
443auto RegisterNode(const std::string& name) -> bool;
444
455template <class T, class... Ts>
456auto DeregisterNode() -> bool;
457
470template <class... Ts>
471auto DeregisterNodes(const NodeDesc<Ts>&... descs) -> bool;
472
479auto DeregisterNode(const std::string& name) -> bool;
480
485
492template <class T>
493auto NodeName() -> std::string;
494
496auto NodeName(const Node::Pointer& node) -> std::string;
497
499auto NodeName(const Node* node) -> std::string;
500
502auto IsRegistered(const std::string& name) -> bool;
503
505auto IsRegistered(const Node::Pointer& node) -> bool;
506
508auto IsRegistered(const Node* node) -> bool;
509
510} // namespace smgl
511
531#define SMGL_NODE(...) ::smgl::NodeDesc<__VA_ARGS__>(#__VA_ARGS__)
532
533#include "smgl/NodeImpl.hpp"
Typed InputPort class.
Definition Ports.hpp:213
Generic input port interface.
Definition Ports.hpp:132
Generic Node class.
Definition Node.hpp:70
void notify_output_ports_(Port::State s)
auto serialize(bool useCache, const filesystem::path &cacheRoot) -> Metadata
Serialize the Node to Metadata, optionally caching to disk.
void registerOutputPort(const std::string &name, OutputPort< T, Args... > &port)
Register an OutputPort instance with this class.
Definition NodeImpl.hpp:50
virtual auto serialize_(bool useCache, const filesystem::path &cacheDir) -> Metadata
Serialize the Node's state to Metadata.
std::map< std::string, Input * > inputs_by_name_
Definition Node.hpp:365
std::shared_ptr< Node > Pointer
Definition Node.hpp:87
static void LoadAndRegisterPort(const std::string &name, const Metadata &data, std::unordered_map< Uuid, PortType * > &byUuid, std::map< std::string, PortType * > &byName)
Definition NodeImpl.hpp:7
auto getOutputConnections() const -> std::vector< Connection >
Get a list of active output connections.
auto getOutputPort(const Uuid &uuid) -> Output &
Get a registered OutputPort by Uuid.
auto update_input_ports_() -> bool
auto getNumberOfOutputConnections() const -> size_t
Get the number of active output connections.
std::function< bool()> usesCacheDir
Definition Node.hpp:251
auto state() -> State
Get the current update state.
auto getInputPort(const Uuid &uuid) -> Input &
Get a registered InputPort by Uuid.
void update()
Update the Node.
std::map< std::string, Output * > outputs_by_name_
Definition Node.hpp:369
State state_
Definition Node.hpp:371
auto getOutputPortsInfo() const -> std::vector< Info >
Get registered OutputPort info.
virtual void deserialize_(const Metadata &data, const filesystem::path &cacheDir)
Deserialize the Node's state from Metadata.
auto getInputPortsInfo() const -> std::vector< Info >
Get registered InputPort info.
auto update_output_ports_() -> bool
std::function< void()> compute
Function executed when Node's InputPorts have queued updates and update() is called.
Definition Node.hpp:234
auto getInputConnections() const -> std::vector< Connection >
Get a list of active input connections.
void deserialize(const Metadata &meta, const filesystem::path &cacheRoot)
Deserialize the Node to Metadata.
std::unordered_map< Uuid, Input * > inputs_by_uuid_
Definition Node.hpp:363
void registerInputPort(const std::string &name, InputPort< T > &port)
Register an InputPort instance with this class.
Definition NodeImpl.hpp:35
auto getNumberOfInputConnections() const -> size_t
Get the number of active input connections.
void registerPort(const std::string &name, InputPort< T > &port)
Register an InputPort instance with this class.
Definition NodeImpl.hpp:44
std::unordered_map< Uuid, Output * > outputs_by_uuid_
Definition Node.hpp:367
Typed OutputPort class.
Definition Ports.hpp:295
Generic output port interface.
Definition Ports.hpp:175
Generic port interface.
Definition Ports.hpp:91
Base class for objects which are uniquely identifiable.
Definition Uuid.hpp:77
auto uuid() const -> Uuid
Universally unique identifier class.
Definition Uuid.hpp:18
Class for constructing and managing the lifetime of a singleton object.
Project top-level namespace.
auto RegisterNode(const std::string &name) -> bool
Register a Node type for serialization/deserialization using a custom name.
Definition NodeImpl.hpp:81
auto DeregisterNodes(const NodeDesc< Ts > &... descs) -> bool
Deregister one or more Node types using source-token descriptors.
Definition NodeImpl.hpp:108
auto NodeName() -> std::string
Definition NodeImpl.hpp:120
auto DeregisterNode() -> bool
Deregister a Node type.
Definition NodeImpl.hpp:88
detail::NodeFactoryType::InstanceType::unknown_identifier unknown_identifier
Definition Node.hpp:389
nlohmann::ordered_json Metadata
Metadata storage class.
Definition Metadata.hpp:12
auto CreateNode(const std::string &name) -> Node::Pointer
auto RegisterNodes(const NodeDesc< Ts > &... descs) -> bool
Register one or more Node types for serialization/deserialization.
Definition NodeImpl.hpp:66
auto IsRegistered(const std::string &name) -> bool
ISO C++ top-level namespace.
Describes untyped connections from an output port.
Definition Ports.hpp:71
Source-token descriptor pairing a Node type with a serialization key.
Definition Node.hpp:402
std::string key
Registered serialization key (the source spelling of T)
Definition Node.hpp:406
NodeDesc(std::string k)
Construct a descriptor with an explicit serialization key.
Definition Node.hpp:404
Port registration information.
Definition Node.hpp:90
Info(std::string n, const Uuid &u)
Definition Node.hpp:92
std::string name
Definition Node.hpp:94