Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
functional.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common.hpp>
4
5
24#define GRAPHLE_MAKE_NIEBLOID(Name, Function, ...) \
25__VA_OPT__(template <) __VA_ARGS__ __VA_OPT__(>) \
26struct Name##_t { \
27 constexpr decltype(auto) operator()(auto&&... args) const { \
28 return Function(GRAPHLE_FWD(args)...); \
29 } \
30}; \
31 \
32__VA_OPT__(template <typename... Args>) \
33constexpr inline Name##_t __VA_OPT__(<Args...>) Name = {}
34
35
36// Doxygen seems to struggle with VA_OPT so provide a simplified version of the above macro when running it.
37#ifdef GRAPHLE_DOXYGEN
42 #define GRAPHLE_MAKE_NIEBLOID_IMPL(Name, Function, ...) \
43 struct Name##_t { constexpr static decltype(auto) operator()(auto&&) const {} }; \
44 constexpr inline Name##_t Name = {};
45#else
46 #define GRAPHLE_MAKE_NIEBLOID_IMPL(Name, Function, ...) GRAPHLE_MAKE_NIEBLOID(Name, Function, __VA_ARGS__)
47#endif
48
49
50namespace graphle::util {
55 GRAPHLE_MAKE_NIEBLOID_IMPL(no_op, [] (auto&&...) {});
56
61 GRAPHLE_MAKE_NIEBLOID_IMPL(addressof, [](auto&& obj) { return std::addressof(obj); });
62
67 GRAPHLE_MAKE_NIEBLOID_IMPL(indirect, [] (auto&& pointer) -> decltype(auto) { return *pointer; });
68
73 GRAPHLE_MAKE_NIEBLOID_IMPL(as_const, [](auto&& obj) -> decltype(auto) { return std::as_const(obj); });
74
80 GRAPHLE_MAKE_NIEBLOID_IMPL(pointed_to_as_const, [] <typename T> (T* pointer) -> const T* { return pointer; });
81
82
87 GRAPHLE_MAKE_NIEBLOID_IMPL(transpose_edge, [] (auto edge) { std::swap(edge.first, edge.second); return edge; });
88
89
95 template <typename F> constexpr inline auto transform_edge(F&& fn) {
96 // Edge is just a pair of pointers, no need to forward.
97 return [f = GRAPHLE_FWD(fn)] (auto&& edge) {
98 return std::pair { f(GRAPHLE_FWD(edge.first)), f(GRAPHLE_FWD(edge.second)) };
99 };
100 }
101}
#define GRAPHLE_FWD(...)
Definition: common.hpp:14
#define GRAPHLE_MAKE_NIEBLOID_IMPL(Name, Function,...)
Definition: functional.hpp:42
constexpr auto transform_edge(F &&fn)
Definition: functional.hpp:95
constexpr no_op_t no_op
Definition: functional.hpp:55
constexpr pointed_to_as_const_t pointed_to_as_const
Definition: functional.hpp:80
constexpr transpose_edge_t transpose_edge
Definition: functional.hpp:87
constexpr addressof_t addressof
Definition: functional.hpp:61
constexpr as_const_t as_const
Definition: functional.hpp:73
constexpr indirect_t indirect
Definition: functional.hpp:67