Skip to content

Commit f055ea5

Browse files
committed
🎨 Collapse flow step run/log functions
Problem: - Now that built flow steps have been refactored to have access to the right compile-time information, there's no need to have separate `run` and `log` functions. Solution: - Collapse built flow steps so that one function logs the step and runs it.
1 parent 1dd5170 commit f055ea5

File tree

2 files changed

+17
-80
lines changed

2 files changed

+17
-80
lines changed

include/flow/graph_builder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ struct graph_builder {
241241
"Topological sort failed: cycle in flow");
242242

243243
constexpr auto functionPtrs = built->functionPtrs;
244-
constexpr auto size = functionPtrs.size();
244+
constexpr auto size = std::size(functionPtrs);
245245
constexpr auto name = built->name;
246246

247247
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {

include/flow/impl.hpp

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,47 @@
22

33
#include <flow/common.hpp>
44
#include <flow/log.hpp>
5-
#include <flow/step.hpp>
65
#include <log/log.hpp>
76

87
#include <stdx/ct_string.hpp>
9-
#include <stdx/cx_vector.hpp>
108
#include <stdx/span.hpp>
119

1210
#include <algorithm>
11+
#include <array>
1312
#include <cstddef>
1413
#include <iterator>
15-
#include <type_traits>
1614

1715
namespace flow {
1816
namespace detail {
19-
template <typename CTNode> constexpr auto run_func() -> void {
17+
template <stdx::ct_string FlowName, typename CTNode>
18+
constexpr auto run_func() -> void {
2019
if (CTNode::condition) {
20+
if constexpr (not FlowName.empty()) {
21+
using log_spec_t =
22+
decltype(get_log_spec<CTNode, log_spec_id_t<FlowName>>());
23+
CIB_LOG(typename log_spec_t::flavor, log_spec_t::level,
24+
"flow.{}({})", typename CTNode::type_t{},
25+
typename CTNode::name_t{});
26+
}
2127
typename CTNode::func_t{}();
2228
}
2329
}
24-
25-
template <typename Flow, typename CTNode> constexpr auto log_func() -> void {
26-
if (CTNode::condition) {
27-
using log_spec_t = decltype(get_log_spec<CTNode, Flow>());
28-
CIB_LOG(typename log_spec_t::flavor, log_spec_t::level, "flow.{}({})",
29-
typename CTNode::type_t{}, typename CTNode::name_t{});
30-
}
31-
}
3230
} // namespace detail
3331

34-
struct rt_node {
35-
FunctionPtr run{};
36-
FunctionPtr log_name{};
37-
38-
private:
39-
friend constexpr auto operator==(rt_node const &,
40-
rt_node const &) -> bool = default;
41-
};
42-
43-
/**
44-
* flow::impl is a constant representation of a series of Milestones and actions
45-
* to be executed in a specific order.
46-
*
47-
* flow::builder allows multiple independent components to collaboratively
48-
* specify a flow::impl. Use flow::builder to create Flows. Independent
49-
* components can then add their own actions and milestones to a flow::impl
50-
* relative to other actions and milestones.
51-
*
52-
* @tparam Name
53-
* Name of flow as a compile-time string.
54-
*
55-
* @tparam NumSteps
56-
* The number of Milestones this flow::impl represents.
57-
*
58-
* @see flow::builder
59-
*/
60-
template <stdx::ct_string Name, std::size_t NumSteps> class impl {
61-
private:
62-
constexpr static bool loggingEnabled = not Name.empty();
32+
template <stdx::ct_string Name, std::size_t NumSteps> struct impl {
33+
using node_t = FunctionPtr;
34+
std::array<node_t, NumSteps> functionPtrs{};
6335

64-
constexpr static auto capacity = [] {
65-
if constexpr (loggingEnabled) {
66-
return NumSteps * 2;
67-
} else {
68-
return NumSteps;
69-
}
70-
}();
71-
72-
public:
73-
stdx::cx_vector<FunctionPtr, capacity> functionPtrs{};
74-
75-
constexpr static bool active = capacity > 0;
7636
constexpr static auto name = Name;
7737

78-
using node_t = rt_node;
79-
8038
template <typename CTNode>
8139
constexpr static auto create_node(CTNode) -> node_t {
82-
constexpr auto rf = detail::run_func<CTNode>;
83-
constexpr auto lf = detail::log_func<log_spec_id_t<Name>, CTNode>;
84-
return node_t{rf, lf};
40+
return detail::run_func<Name, CTNode>;
8541
}
8642

87-
/**
88-
* Create a new flow::impl of Milestones.
89-
*
90-
* Do not call this constructor directly, use flow::builder instead.
91-
*
92-
* @param newMilestones
93-
* Array of Milestones to execute in the flow.
94-
*
95-
* @see flow::builder
96-
*/
9743
constexpr explicit(true) impl(stdx::span<node_t const, NumSteps> steps) {
98-
if constexpr (loggingEnabled) {
99-
for (auto const &step : steps) {
100-
functionPtrs.push_back(step.log_name);
101-
functionPtrs.push_back(step.run);
102-
}
103-
} else {
104-
std::transform(std::cbegin(steps), std::cend(steps),
105-
std::back_inserter(functionPtrs),
106-
[](auto const &step) { return step.run; });
107-
}
44+
std::copy(std::cbegin(steps), std::cend(steps),
45+
std::begin(functionPtrs));
10846
}
10947
};
11048

@@ -135,5 +73,4 @@ template <stdx::ct_string Name, auto... FuncPtrs> struct inlined_func_list {
13573
}
13674
};
13775
} // namespace detail
138-
13976
} // namespace flow

0 commit comments

Comments
 (0)