smgl 0.11.0
Structured Metadata Engine and Graph Objects Library
Loading...
Searching...
No Matches
NodeImpl.hpp
1#include "smgl/Utilities.hpp"
2
3namespace smgl
4{
5
6template <class PortType>
8 const std::string& name,
9 const Metadata& data,
12{
13 // Get old port info
14 auto* port = byName[name];
15 auto oldUuid = port->uuid();
16
17 // Load old port data
18 port->deserialize(data);
19
20 // Delete old port registration
21 auto nIt = byName.find(name);
22 assert(nIt != byName.end());
23 byName.erase(nIt);
24
25 auto uIt = byUuid.find(oldUuid);
26 assert(uIt != byUuid.end());
27 byUuid.erase(uIt);
28
29 // Reregister ports
30 byName[name] = port;
31 byUuid[port->uuid()] = port;
32}
33
34template <typename T>
36{
37 port.setParent(this);
38 inputs_by_uuid_[port.uuid()] = &port;
39 assert(inputs_by_name_.find(name) == inputs_by_name_.end());
40 inputs_by_name_[name] = &port;
41}
42
43template <typename T>
45{
46 registerInputPort(name, port);
47}
48
49template <typename T, typename... Args>
51 const std::string& name, OutputPort<T, Args...>& port)
52{
53 port.setParent(this);
54 outputs_by_uuid_[port.uuid()] = &port;
55 assert(outputs_by_name_.find(name) == outputs_by_name_.end());
56 outputs_by_name_[name] = &port;
57}
58
59template <typename T, typename... Args>
61{
62 registerOutputPort(name, port);
63}
64
65template <class... Ts>
66bool RegisterNodes(const NodeDesc<Ts>&... descs)
67{
68 // Reserve nodes
69 using NF = detail::NodeFactoryType;
70 NF::Instance().ReserveAdditional(sizeof...(Ts));
71 bool res{true};
72#if __cplusplus >= 201703L
73 ((res = res & RegisterNode<Ts>(descs.key)), ...);
74#elif __cplusplus > 201103L
75 detail::ExpandType{0, res &= RegisterNode<Ts>(descs.key)...};
76#endif
77 return res;
78}
79
80template <class T>
81bool RegisterNode(const std::string& name)
82{
83 return detail::NodeFactoryType::Instance().Register(
84 name, []() { return std::make_shared<T>(); }, typeid(T));
85}
86
87template <class T, class... Ts>
89{
90 // Lambda function for deregistering
91 bool res{true};
92 auto deregister = [&res](const std::type_info& i) {
93 auto name = detail::NodeFactoryType::Instance().GetTypeIdentifier(i);
94 res &= detail::NodeFactoryType::Instance().Deregister(name);
95 };
96 // Deregister the solo arg
97 deregister(typeid(T));
98 // Deregister the template argument pack
99#if __cplusplus >= 201703L
100 (deregister(typeid(Ts)), ...);
101#elif __cplusplus > 201103L
102 detail::ExpandType{0, (deregister(typeid(Ts)), 0)...};
103#endif
104 return res;
105}
106
107template <class... Ts>
108bool DeregisterNodes(const NodeDesc<Ts>&... descs)
109{
110 bool res{true};
111#if __cplusplus >= 201703L
112 ((res = res & DeregisterNode(descs.key)), ...);
113#elif __cplusplus > 201103L
114 detail::ExpandType{0, res &= DeregisterNode(descs.key)...};
115#endif
116 return res;
117}
118
119template <class T>
121{
122 return detail::NodeFactoryType::Instance().GetTypeIdentifier(typeid(T));
123}
124
125} // namespace smgl
Typed InputPort class.
Definition Ports.hpp:213
void registerOutputPort(const std::string &name, OutputPort< T, Args... > &port)
Register an OutputPort instance with this class.
Definition NodeImpl.hpp:50
std::map< std::string, Input * > inputs_by_name_
Definition Node.hpp:365
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
std::map< std::string, Output * > outputs_by_name_
Definition Node.hpp:369
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
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
void setParent(Node *p)
auto uuid() const -> Uuid
static T & Instance()
Access the singleton instance.
T end(T... args)
T erase(T... args)
T find(T... args)
T make_shared(T... args)
SingletonHolder< Factory< Node, std::string, Node::Pointer > > NodeFactoryType
Definition Node.hpp:384
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
nlohmann::ordered_json Metadata
Metadata storage class.
Definition Metadata.hpp:12
auto RegisterNodes(const NodeDesc< Ts > &... descs) -> bool
Register one or more Node types for serialization/deserialization.
Definition NodeImpl.hpp:66
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
Helper class for calling function iteratively on parameter pack.
Definition Utilities.hpp:37