Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,17 @@ target_sources(
include
FILES
include/flow/builder.hpp
include/flow/common.hpp
include/flow/detail/par.hpp
include/flow/detail/seq.hpp
include/flow/detail/walk.hpp
include/flow/dsl/par.hpp
include/flow/dsl/seq.hpp
include/flow/dsl/subgraph_identity.hpp
include/flow/dsl/walk.hpp
include/flow/flow.hpp
include/flow/func_list.hpp
include/flow/graph_builder.hpp
include/flow/graphviz_builder.hpp
include/flow/impl.hpp
include/flow/log.hpp
include/flow/run.hpp
include/flow/service.hpp
include/flow/step.hpp)

add_library(cib_seq INTERFACE)
Expand Down
53 changes: 35 additions & 18 deletions include/flow/builder.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
#pragma once

#include <flow/common.hpp>
#include <flow/dsl/walk.hpp>
#include <flow/graph_builder.hpp>
#include <flow/impl.hpp>
#include <flow/log.hpp>

#include <stdx/compiler.hpp>
#include <stdx/ct_string.hpp>
#include <stdx/panic.hpp>
#include <stdx/tuple.hpp>
#include <stdx/tuple_algorithms.hpp>
#include <stdx/type_traits.hpp>

#include <utility>

namespace flow {
template <stdx::ct_string Name = "",
typename LogPolicy = flow::log_policy_t<Name>>
using builder = graph<Name, LogPolicy>;
template <typename Renderer, flow::dsl::subgraph... Fragments>
class builder_for {
template <typename Tag>
friend constexpr auto tag_invoke(Tag, builder_for const &b) {
return b.fragments.apply([](auto const &...frags) {
return stdx::tuple_cat(Tag{}(frags)...);
});
}

template <stdx::ct_string Name = "", typename LogPolicy = log_policy_t<Name>>
struct service {
using builder_t = builder<Name, LogPolicy>;
using interface_t = FunctionPtr;

CONSTEVAL static auto uninitialized() -> interface_t {
return [] {
using namespace stdx::literals;
stdx::panic<"Attempting to run flow ("_cts + Name +
") before it is initialized"_cts>();
};
public:
using interface_t = typename Renderer::interface_t;
constexpr static auto name = Renderer::name;

template <flow::dsl::subgraph... Ns>
[[nodiscard]] constexpr auto add(Ns &&...ns) {
return fragments.apply([&](auto &...frags) {
return builder_for<Renderer, Fragments...,
stdx::remove_cvref_t<Ns>...>{
{frags..., std::forward<Ns>(ns)...}};
});
}

template <typename BuilderValue>
[[nodiscard]] constexpr static auto build() {
return Renderer::template render<BuilderValue>();
}

stdx::tuple<Fragments...> fragments;
};

template <stdx::ct_string Name = "", typename LogPolicy = log_policy_t<Name>>
using builder = builder_for<graph_builder<Name, LogPolicy>>;
} // namespace flow
5 changes: 0 additions & 5 deletions include/flow/common.hpp

This file was deleted.

6 changes: 3 additions & 3 deletions include/flow/detail/par.hpp → include/flow/dsl/par.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <flow/detail/walk.hpp>
#include <flow/subgraph_identity.hpp>
#include <flow/dsl/subgraph_identity.hpp>
#include <flow/dsl/walk.hpp>

#include <stdx/tuple_algorithms.hpp>

Expand Down Expand Up @@ -59,7 +59,7 @@ template <flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs>
}

template <typename Cond, flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs,
flow::subgraph_identity Identity>
flow::dsl::subgraph_identity Identity>
constexpr auto make_runtime_conditional(Cond,
flow::dsl::par<Lhs, Rhs, Identity>) {
auto lhs = make_runtime_conditional(Cond{}, Lhs{});
Expand Down
6 changes: 3 additions & 3 deletions include/flow/detail/seq.hpp → include/flow/dsl/seq.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <cib/detail/runtime_conditional.hpp>
#include <flow/detail/walk.hpp>
#include <flow/subgraph_identity.hpp>
#include <flow/dsl/subgraph_identity.hpp>
#include <flow/dsl/walk.hpp>

#include <stdx/tuple_algorithms.hpp>

Expand Down Expand Up @@ -72,7 +72,7 @@ template <flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs>
}

template <typename Cond, flow::dsl::subgraph Lhs, flow::dsl::subgraph Rhs,
flow::subgraph_identity Identity, typename EdgeCond>
flow::dsl::subgraph_identity Identity, typename EdgeCond>
constexpr auto
make_runtime_conditional(Cond, flow::dsl::seq<Lhs, Rhs, Identity, EdgeCond>) {
auto lhs = make_runtime_conditional(Cond{}, Lhs{});
Expand Down
5 changes: 5 additions & 0 deletions include/flow/dsl/subgraph_identity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace flow::dsl {
enum struct subgraph_identity : bool { VALUE, REFERENCE };
}
2 changes: 1 addition & 1 deletion include/flow/detail/walk.hpp → include/flow/dsl/walk.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <flow/subgraph_identity.hpp>
#include <flow/dsl/subgraph_identity.hpp>

#include <stdx/concepts.hpp>
#include <stdx/tuple.hpp>
Expand Down
8 changes: 3 additions & 5 deletions include/flow/flow.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#include <flow/builder.hpp>
#include <flow/common.hpp>
#include <flow/detail/par.hpp>
#include <flow/detail/seq.hpp>
#include <flow/impl.hpp>
#include <flow/dsl/par.hpp>
#include <flow/dsl/seq.hpp>
#include <flow/run.hpp>
#include <flow/service.hpp>
#include <flow/step.hpp>
50 changes: 20 additions & 30 deletions include/flow/impl.hpp → include/flow/func_list.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <flow/common.hpp>
#include <flow/log.hpp>
#include <log/log.hpp>

Expand All @@ -11,18 +10,10 @@

#include <algorithm>
#include <array>
#include <concepts>
#include <cstddef>
#include <iterator>

namespace flow {
template <typename T>
concept log_policy = requires {
{
T::template log<default_log_env>(stdx::ct_format<"">())
} -> std::same_as<void>;
};

namespace detail {
template <stdx::ct_string FlowName, log_policy LogPolicy, typename CTNode>
constexpr static auto run_func = []() -> void {
Expand All @@ -35,28 +26,7 @@ constexpr static auto run_func = []() -> void {
typename CTNode::func_t{}();
}
};
} // namespace detail

template <stdx::ct_string Name, log_policy LogPolicy, std::size_t NumSteps>
struct impl {
using node_t = FunctionPtr;
std::array<FunctionPtr, NumSteps> functionPtrs{};

constexpr static auto name = Name;

template <typename CTNode>
constexpr static auto create_node(CTNode) -> node_t {
constexpr auto fp = detail::run_func<Name, LogPolicy, CTNode>;
return fp;
}

constexpr explicit(true) impl(stdx::span<node_t const, NumSteps> steps) {
std::copy(std::cbegin(steps), std::cend(steps),
std::begin(functionPtrs));
}
};

namespace detail {
template <stdx::ct_string Name, log_policy LogPolicy, auto... FuncPtrs>
struct inlined_func_list {
constexpr static auto active = sizeof...(FuncPtrs) > 0;
Expand All @@ -75,4 +45,24 @@ struct inlined_func_list {
}
};
} // namespace detail

template <stdx::ct_string Name, log_policy LogPolicy, std::size_t NumSteps>
struct func_list {
using node_t = auto (*)() -> void;
std::array<node_t, NumSteps> nodes{};

template <typename CTNode>
constexpr static auto create_node(CTNode) -> node_t {
constexpr auto fp = detail::run_func<Name, LogPolicy, CTNode>;
return fp;
}

constexpr explicit(true)
func_list(stdx::span<node_t const, NumSteps> steps) {
std::copy(std::cbegin(steps), std::cend(steps), std::begin(nodes));
}

template <node_t... Fs>
using finalized_t = detail::inlined_func_list<Name, LogPolicy, Fs...>;
};
} // namespace flow
59 changes: 14 additions & 45 deletions include/flow/graph_builder.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include <flow/common.hpp>
#include <flow/detail/walk.hpp>
#include <flow/impl.hpp>
#include <flow/dsl/walk.hpp>
#include <flow/func_list.hpp>

#include <stdx/ct_string.hpp>
#include <stdx/cx_multimap.hpp>
Expand Down Expand Up @@ -58,9 +57,9 @@ template <typename T> using name_for = typename T::name_t;
});
}

template <stdx::ct_string Name,
template <stdx::ct_string, typename, std::size_t> typename Impl,
typename LogPolicy = log_policy_t<Name>>
template <stdx::ct_string Name, typename LogPolicy = log_policy_t<Name>,
template <stdx::ct_string, typename, std::size_t> typename Impl =
func_list>
struct graph_builder {
// NOLINTBEGIN(readability-function-cognitive-complexity)
template <typename Output, std::size_t N, std::size_t E>
Expand Down Expand Up @@ -234,29 +233,29 @@ struct graph_builder {
return topo_sort<output_t>(g);
}

constexpr static auto name = Name;
using interface_t = auto (*)() -> void;

template <typename Initialized> class built_flow {
constexpr static auto built() {
constexpr auto v = Initialized::value;
constexpr auto built = build(v);
static_assert(built.has_value(),
"Topological sort failed: cycle in flow");

constexpr auto functionPtrs = built->functionPtrs;
constexpr auto size = std::size(functionPtrs);
constexpr auto name = built->name;

using impl_t = typename decltype(built)::value_type;
constexpr auto nodes = built->nodes;
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return detail::inlined_func_list<name, LogPolicy,
functionPtrs[Is]...>{};
}(std::make_index_sequence<size>{});
return typename impl_t::template finalized_t<nodes[Is]...>{};
}(std::make_index_sequence<std::size(nodes)>{});
}

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

public:
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr explicit(false) operator FunctionPtr() const { return run; }
constexpr auto operator()() const -> void { run(); }
constexpr explicit(false) operator interface_t() const { return run; }
constexpr auto operator()() const { return run(); }
constexpr static bool active = decltype(built())::active;
};

Expand All @@ -265,34 +264,4 @@ struct graph_builder {
return {};
}
};

template <stdx::ct_string Name = "", typename LogPolicy = log_policy_t<Name>,
typename Renderer = graph_builder<Name, impl, LogPolicy>,
flow::dsl::subgraph... Fragments>
class graph {
template <typename Tag>
friend constexpr auto tag_invoke(Tag, graph const &g) {
return g.fragments.apply([](auto const &...frags) {
return stdx::tuple_cat(Tag{}(frags)...);
});
}

public:
template <flow::dsl::subgraph... Ns>
[[nodiscard]] constexpr auto add(Ns &&...ns) {
return fragments.apply([&](auto &...frags) {
return graph<Name, LogPolicy, Renderer, Fragments...,
stdx::remove_cvref_t<Ns>...>{
{frags..., std::forward<Ns>(ns)...}};
});
}

template <typename BuilderValue>
[[nodiscard]] constexpr static auto build() {
return Renderer::template render<BuilderValue>();
}

constexpr static auto name = Name;
stdx::tuple<Fragments...> fragments;
};
} // namespace flow
16 changes: 8 additions & 8 deletions include/flow/graphviz_builder.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#pragma once

#include <flow/detail/walk.hpp>
#include <flow/dsl/walk.hpp>

#include <stdx/ct_string.hpp>
#include <stdx/tuple_algorithms.hpp>

#include <set>
#include <string>
#include <string_view>

namespace flow {
using VizFunctionPtr = auto (*)() -> std::string;

struct graphviz_builder {
template <stdx::ct_string Name> struct graphviz_builder {
template <typename Graph>
[[nodiscard]] static auto build(Graph const &input) {
auto const nodes = flow::dsl::get_nodes(input);
Expand Down Expand Up @@ -54,6 +53,9 @@ struct graphviz_builder {
return output;
}

constexpr static auto name = Name;
using interface_t = auto (*)() -> std::string;

template <typename Initialized> class built_flow {
static auto built() {
auto const v = Initialized::value;
Expand All @@ -64,10 +66,8 @@ struct graphviz_builder {

public:
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr explicit(false) operator VizFunctionPtr() const {
return run;
}
auto operator()() const -> std::string { return run(); }
constexpr explicit(false) operator interface_t() const { return run; }
auto operator()() const { return run(); }
constexpr static bool active = true;
};

Expand Down
Loading
Loading