Skip to content

Commit b0c8d72

Browse files
committed
refactor(merge): node builder api
2 parents 3949131 + c2b03cb commit b0c8d72

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

components/acoustics/module/module_dag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace module {
44

5-
core::Status MDAGBuilderRegistry::registerDAGBuilder(std::string_view name, DAGBuilder builder) noexcept
5+
constexpr core::Status MDAGBuilderRegistry::registerDAGBuilder(std::string_view name, DAGBuilder builder) noexcept
66
{
77
if (name.empty()) [[unlikely]]
88
{

components/acoustics/module/module_dag.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MDAGBuilderRegistry final
4747
return _dags;
4848
}
4949

50-
static core::Status registerDAGBuilder(std::string_view name, DAGBuilder builder) noexcept;
50+
static constexpr core::Status registerDAGBuilder(std::string_view name, DAGBuilder builder) noexcept;
5151

5252
private:
5353
static DAGBuilderMap _dags;

components/acoustics/module/module_io.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@
88
#include <algorithm>
99
#include <memory>
1010
#include <string_view>
11+
#include <type_traits>
1112
#include <utility>
1213
#include <vector>
1314

1415
namespace module {
1516

16-
struct MIO final
17+
class MIO final
1718
{
19+
public:
1820
template<typename T,
1921
std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>, std::shared_ptr<core::Tensor>>, bool> = true>
20-
explicit MIO(T &&tensor, std::string_view attribute = "") noexcept
22+
constexpr explicit MIO(T &&tensor, std::string_view attribute = "") noexcept
2123
: tensor(std::forward<T>(tensor)), attribute(attribute)
2224
{
2325
}
2426

2527
~MIO() = default;
2628

27-
inline core::Tensor *operator()() const noexcept
29+
constexpr inline core::Tensor *operator()() const noexcept
2830
{
2931
return tensor.get();
3032
}
@@ -35,7 +37,7 @@ struct MIO final
3537

3638
using MIOS = std::vector<std::shared_ptr<MIO>>;
3739

38-
bool operator==(const MIOS &lhs, const MIOS &rhs) noexcept
40+
inline bool operator==(const MIOS &lhs, const MIOS &rhs) noexcept
3941
{
4042
if (lhs.size() != rhs.size()) [[unlikely]]
4143
{

components/acoustics/module/module_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace module {
44

5-
core::Status MNodeBuilderRegistry::registerNodeBuilder(std::string_view name, NodeBuilder builder,
5+
constexpr core::Status MNodeBuilderRegistry::registerNodeBuilder(std::string_view name, NodeBuilder builder,
66
bool replace = false) noexcept
77
{
88
if (name.empty() || !builder) [[unlikely]]

components/acoustics/module/module_node.hpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "core/tensor.hpp"
1111

1212
#include <memory>
13+
#include <string>
1314
#include <string_view>
1415
#include <unordered_map>
1516
#include <utility>
@@ -21,19 +22,19 @@ class MNode;
2122
class MNodeBuilderRegistry final
2223
{
2324
public:
24-
using NodeBuilder = std::shared_ptr<MNode> (*)(int, MIOS &, MIOS &, const core::ConfigMap &);
25+
using NodeBuilder = std::shared_ptr<MNode> (*)(const core::ConfigMap *, MIOS *, MIOS *, int);
2526
using NodeBuilderMap = std::unordered_map<std::string_view, NodeBuilder>;
2627

2728
MNodeBuilderRegistry() = default;
2829
~MNodeBuilderRegistry() = default;
2930

30-
inline static std::shared_ptr<MNode> getNode(std::string_view name, int priority, MIOS &inputs, MIOS &outputs,
31-
const core::ConfigMap &configs) noexcept
31+
inline static std::shared_ptr<MNode> getNode(std::string_view name, const core::ConfigMap *configs = nullptr,
32+
MIOS *inputs = nullptr, MIOS *outputs = nullptr, int priority = 0) noexcept
3233
{
3334
auto it = _nodes.find(name);
3435
if (it != _nodes.end()) [[likely]]
3536
{
36-
return it->second(priority, inputs, outputs, configs);
37+
return it->second(configs, inputs, outputs, priority);
3738
}
3839
return {};
3940
}
@@ -43,7 +44,8 @@ class MNodeBuilderRegistry final
4344
return _nodes;
4445
}
4546

46-
static core::Status registerNodeBuilder(std::string_view name, NodeBuilder builder, bool replace = false) noexcept;
47+
static constexpr core::Status registerNodeBuilder(std::string_view name, NodeBuilder builder,
48+
bool replace = false) noexcept;
4749

4850
private:
4951
static NodeBuilderMap _nodes;
@@ -126,7 +128,10 @@ class MNode
126128
return {};
127129
}
128130

129-
virtual core::Status config(const core::ConfigMap &configs) noexcept = 0;
131+
virtual core::Status config(const core::ConfigMap &configs) noexcept
132+
{
133+
return STATUS(ENOTSUP, "Configuration not supported for this node");
134+
}
130135

131136
inline core::Status operator()() noexcept
132137
{
@@ -135,18 +140,20 @@ class MNode
135140

136141
protected:
137142
template<typename IS, typename OS>
138-
explicit MNode(std::string_view name, int priority, IS &&inputs, OS &&outputs) noexcept
139-
: _name(name), _priority(priority), _inputs(std::forward<IS>(inputs)), _outputs(std::forward<OS>(outputs))
143+
constexpr explicit MNode(std::string name, IS &&inputs, OS &&outputs, int priority) noexcept
144+
: _name(std::move(name)), _inputs(std::forward<IS>(inputs)), _outputs(std::forward<OS>(outputs)),
145+
_priority(priority)
140146
{
147+
LOG(DEBUG, "Module node '%s' created with priority %d", _name.c_str(), _priority);
141148
}
142149

143150
virtual inline core::Status forward(const MIOS &inputs, MIOS &outputs) noexcept = 0;
144151

145152
private:
146-
std::string_view _name;
147-
int _priority;
153+
const std::string _name;
148154
MIOS _inputs;
149155
MIOS _outputs;
156+
const int _priority;
150157
};
151158

152159
} // namespace module

0 commit comments

Comments
 (0)