Skip to content

Commit 4e15633

Browse files
authored
Merge pull request #793 from elbeno/clean-up-flow
🎨 Refactor flow builder
2 parents c4d632f + 494624d commit 4e15633

25 files changed

+181
-181
lines changed

CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,17 @@ target_sources(
241241
include
242242
FILES
243243
include/flow/builder.hpp
244-
include/flow/common.hpp
245-
include/flow/detail/par.hpp
246-
include/flow/detail/seq.hpp
247-
include/flow/detail/walk.hpp
244+
include/flow/dsl/par.hpp
245+
include/flow/dsl/seq.hpp
246+
include/flow/dsl/subgraph_identity.hpp
247+
include/flow/dsl/walk.hpp
248248
include/flow/flow.hpp
249+
include/flow/func_list.hpp
249250
include/flow/graph_builder.hpp
250251
include/flow/graphviz_builder.hpp
251-
include/flow/impl.hpp
252+
include/flow/log.hpp
252253
include/flow/run.hpp
254+
include/flow/service.hpp
253255
include/flow/step.hpp)
254256

255257
add_library(cib_seq INTERFACE)

include/flow/builder.hpp

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
#pragma once
22

3-
#include <flow/common.hpp>
3+
#include <flow/dsl/walk.hpp>
44
#include <flow/graph_builder.hpp>
5-
#include <flow/impl.hpp>
65
#include <flow/log.hpp>
76

8-
#include <stdx/compiler.hpp>
97
#include <stdx/ct_string.hpp>
10-
#include <stdx/panic.hpp>
8+
#include <stdx/tuple.hpp>
9+
#include <stdx/tuple_algorithms.hpp>
10+
#include <stdx/type_traits.hpp>
11+
12+
#include <utility>
1113

1214
namespace flow {
13-
template <stdx::ct_string Name = "",
14-
typename LogPolicy = flow::log_policy_t<Name>>
15-
using builder = graph<Name, LogPolicy>;
15+
template <typename Renderer, flow::dsl::subgraph... Fragments>
16+
class builder_for {
17+
template <typename Tag>
18+
friend constexpr auto tag_invoke(Tag, builder_for const &b) {
19+
return b.fragments.apply([](auto const &...frags) {
20+
return stdx::tuple_cat(Tag{}(frags)...);
21+
});
22+
}
1623

17-
template <stdx::ct_string Name = "", typename LogPolicy = log_policy_t<Name>>
18-
struct service {
19-
using builder_t = builder<Name, LogPolicy>;
20-
using interface_t = FunctionPtr;
21-
22-
CONSTEVAL static auto uninitialized() -> interface_t {
23-
return [] {
24-
using namespace stdx::literals;
25-
stdx::panic<"Attempting to run flow ("_cts + Name +
26-
") before it is initialized"_cts>();
27-
};
24+
public:
25+
using interface_t = typename Renderer::interface_t;
26+
constexpr static auto name = Renderer::name;
27+
28+
template <flow::dsl::subgraph... Ns>
29+
[[nodiscard]] constexpr auto add(Ns &&...ns) {
30+
return fragments.apply([&](auto &...frags) {
31+
return builder_for<Renderer, Fragments...,
32+
stdx::remove_cvref_t<Ns>...>{
33+
{frags..., std::forward<Ns>(ns)...}};
34+
});
2835
}
36+
37+
template <typename BuilderValue>
38+
[[nodiscard]] constexpr static auto build() {
39+
return Renderer::template render<BuilderValue>();
40+
}
41+
42+
stdx::tuple<Fragments...> fragments;
2943
};
44+
45+
template <stdx::ct_string Name = "", typename LogPolicy = log_policy_t<Name>>
46+
using builder = builder_for<graph_builder<Name, LogPolicy>>;
3047
} // namespace flow

include/flow/common.hpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

include/flow/detail/par.hpp renamed to include/flow/dsl/par.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <flow/detail/walk.hpp>
4-
#include <flow/subgraph_identity.hpp>
3+
#include <flow/dsl/subgraph_identity.hpp>
4+
#include <flow/dsl/walk.hpp>
55

66
#include <stdx/tuple_algorithms.hpp>
77

@@ -59,7 +59,7 @@ template <flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs>
5959
}
6060

6161
template <typename Cond, flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs,
62-
flow::subgraph_identity Identity>
62+
flow::dsl::subgraph_identity Identity>
6363
constexpr auto make_runtime_conditional(Cond,
6464
flow::dsl::par<Lhs, Rhs, Identity>) {
6565
auto lhs = make_runtime_conditional(Cond{}, Lhs{});

include/flow/detail/seq.hpp renamed to include/flow/dsl/seq.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

33
#include <cib/detail/runtime_conditional.hpp>
4-
#include <flow/detail/walk.hpp>
5-
#include <flow/subgraph_identity.hpp>
4+
#include <flow/dsl/subgraph_identity.hpp>
5+
#include <flow/dsl/walk.hpp>
66

77
#include <stdx/tuple_algorithms.hpp>
88

@@ -72,7 +72,7 @@ template <flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs>
7272
}
7373

7474
template <typename Cond, flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs,
75-
flow::subgraph_identity Identity, typename EdgeCond>
75+
flow::dsl::subgraph_identity Identity, typename EdgeCond>
7676
constexpr auto
7777
make_runtime_conditional(Cond, flow::dsl::seq<Lhs, Rhs, Identity, EdgeCond>) {
7878
auto lhs = make_runtime_conditional(Cond{}, Lhs{});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
namespace flow::dsl {
4+
enum struct subgraph_identity : bool { VALUE, REFERENCE };
5+
}

include/flow/detail/walk.hpp renamed to include/flow/dsl/walk.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <flow/subgraph_identity.hpp>
3+
#include <flow/dsl/subgraph_identity.hpp>
44

55
#include <stdx/concepts.hpp>
66
#include <stdx/tuple.hpp>

include/flow/flow.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#pragma once
22

3-
#include <flow/builder.hpp>
4-
#include <flow/common.hpp>
5-
#include <flow/detail/par.hpp>
6-
#include <flow/detail/seq.hpp>
7-
#include <flow/impl.hpp>
3+
#include <flow/dsl/par.hpp>
4+
#include <flow/dsl/seq.hpp>
85
#include <flow/run.hpp>
6+
#include <flow/service.hpp>
97
#include <flow/step.hpp>
Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22

3-
#include <flow/common.hpp>
43
#include <flow/log.hpp>
54
#include <log/log.hpp>
65

@@ -11,18 +10,10 @@
1110

1211
#include <algorithm>
1312
#include <array>
14-
#include <concepts>
1513
#include <cstddef>
1614
#include <iterator>
1715

1816
namespace flow {
19-
template <typename T>
20-
concept log_policy = requires {
21-
{
22-
T::template log<default_log_env>(stdx::ct_format<"">())
23-
} -> std::same_as<void>;
24-
};
25-
2617
namespace detail {
2718
template <stdx::ct_string FlowName, log_policy LogPolicy, typename CTNode>
2819
constexpr static auto run_func = []() -> void {
@@ -35,28 +26,7 @@ constexpr static auto run_func = []() -> void {
3526
typename CTNode::func_t{}();
3627
}
3728
};
38-
} // namespace detail
39-
40-
template <stdx::ct_string Name, log_policy LogPolicy, std::size_t NumSteps>
41-
struct impl {
42-
using node_t = FunctionPtr;
43-
std::array<FunctionPtr, NumSteps> functionPtrs{};
44-
45-
constexpr static auto name = Name;
46-
47-
template <typename CTNode>
48-
constexpr static auto create_node(CTNode) -> node_t {
49-
constexpr auto fp = detail::run_func<Name, LogPolicy, CTNode>;
50-
return fp;
51-
}
52-
53-
constexpr explicit(true) impl(stdx::span<node_t const, NumSteps> steps) {
54-
std::copy(std::cbegin(steps), std::cend(steps),
55-
std::begin(functionPtrs));
56-
}
57-
};
5829

59-
namespace detail {
6030
template <stdx::ct_string Name, log_policy LogPolicy, auto... FuncPtrs>
6131
struct inlined_func_list {
6232
constexpr static auto active = sizeof...(FuncPtrs) > 0;
@@ -75,4 +45,24 @@ struct inlined_func_list {
7545
}
7646
};
7747
} // namespace detail
48+
49+
template <stdx::ct_string Name, log_policy LogPolicy, std::size_t NumSteps>
50+
struct func_list {
51+
using node_t = auto (*)() -> void;
52+
std::array<node_t, NumSteps> nodes{};
53+
54+
template <typename CTNode>
55+
constexpr static auto create_node(CTNode) -> node_t {
56+
constexpr auto fp = detail::run_func<Name, LogPolicy, CTNode>;
57+
return fp;
58+
}
59+
60+
constexpr explicit(true)
61+
func_list(stdx::span<node_t const, NumSteps> steps) {
62+
std::copy(std::cbegin(steps), std::cend(steps), std::begin(nodes));
63+
}
64+
65+
template <node_t... Fs>
66+
using finalized_t = detail::inlined_func_list<Name, LogPolicy, Fs...>;
67+
};
7868
} // namespace flow

include/flow/graph_builder.hpp

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

3-
#include <flow/common.hpp>
4-
#include <flow/detail/walk.hpp>
5-
#include <flow/impl.hpp>
3+
#include <flow/dsl/walk.hpp>
4+
#include <flow/func_list.hpp>
65

76
#include <stdx/ct_string.hpp>
87
#include <stdx/cx_multimap.hpp>
@@ -58,9 +57,9 @@ template <typename T> using name_for = typename T::name_t;
5857
});
5958
}
6059

61-
template <stdx::ct_string Name,
62-
template <stdx::ct_string, typename, std::size_t> typename Impl,
63-
typename LogPolicy = log_policy_t<Name>>
60+
template <stdx::ct_string Name, typename LogPolicy = log_policy_t<Name>,
61+
template <stdx::ct_string, typename, std::size_t> typename Impl =
62+
func_list>
6463
struct graph_builder {
6564
// NOLINTBEGIN(readability-function-cognitive-complexity)
6665
template <typename Output, std::size_t N, std::size_t E>
@@ -234,29 +233,29 @@ struct graph_builder {
234233
return topo_sort<output_t>(g);
235234
}
236235

236+
constexpr static auto name = Name;
237+
using interface_t = auto (*)() -> void;
238+
237239
template <typename Initialized> class built_flow {
238240
constexpr static auto built() {
239241
constexpr auto v = Initialized::value;
240242
constexpr auto built = build(v);
241243
static_assert(built.has_value(),
242244
"Topological sort failed: cycle in flow");
243245

244-
constexpr auto functionPtrs = built->functionPtrs;
245-
constexpr auto size = std::size(functionPtrs);
246-
constexpr auto name = built->name;
247-
246+
using impl_t = typename decltype(built)::value_type;
247+
constexpr auto nodes = built->nodes;
248248
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
249-
return detail::inlined_func_list<name, LogPolicy,
250-
functionPtrs[Is]...>{};
251-
}(std::make_index_sequence<size>{});
249+
return typename impl_t::template finalized_t<nodes[Is]...>{};
250+
}(std::make_index_sequence<std::size(nodes)>{});
252251
}
253252

254253
constexpr static auto run() { built()(); }
255254

256255
public:
257256
// NOLINTNEXTLINE(google-explicit-constructor)
258-
constexpr explicit(false) operator FunctionPtr() const { return run; }
259-
constexpr auto operator()() const -> void { run(); }
257+
constexpr explicit(false) operator interface_t() const { return run; }
258+
constexpr auto operator()() const { return run(); }
260259
constexpr static bool active = decltype(built())::active;
261260
};
262261

@@ -265,34 +264,4 @@ struct graph_builder {
265264
return {};
266265
}
267266
};
268-
269-
template <stdx::ct_string Name = "", typename LogPolicy = log_policy_t<Name>,
270-
typename Renderer = graph_builder<Name, impl, LogPolicy>,
271-
flow::dsl::subgraph... Fragments>
272-
class graph {
273-
template <typename Tag>
274-
friend constexpr auto tag_invoke(Tag, graph const &g) {
275-
return g.fragments.apply([](auto const &...frags) {
276-
return stdx::tuple_cat(Tag{}(frags)...);
277-
});
278-
}
279-
280-
public:
281-
template <flow::dsl::subgraph... Ns>
282-
[[nodiscard]] constexpr auto add(Ns &&...ns) {
283-
return fragments.apply([&](auto &...frags) {
284-
return graph<Name, LogPolicy, Renderer, Fragments...,
285-
stdx::remove_cvref_t<Ns>...>{
286-
{frags..., std::forward<Ns>(ns)...}};
287-
});
288-
}
289-
290-
template <typename BuilderValue>
291-
[[nodiscard]] constexpr static auto build() {
292-
return Renderer::template render<BuilderValue>();
293-
}
294-
295-
constexpr static auto name = Name;
296-
stdx::tuple<Fragments...> fragments;
297-
};
298267
} // namespace flow

0 commit comments

Comments
 (0)