draconisplusplus/include/rfl/internal/VisitTree.hpp

30 lines
885 B
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_INTERNAL_VISITTREE_HPP_
#define RFL_INTERNAL_VISITTREE_HPP_
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace internal {
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
struct VisitTree {
/// Evaluates a visitor pattern using a tree-like structure.
template <int _begin, int _end, class Visitor, class... Args>
2024-06-08 15:53:06 -04:00
static inline auto
visit(const auto& _v, const int _i, const Args&... _args) {
2024-05-31 22:59:00 -04:00
static_assert(_end > _begin, "_end needs to be greater than _begin.");
if constexpr (_end - _begin == 1) {
2024-06-08 14:10:59 -04:00
return _v.template visit<_begin>(_args...);
2024-05-31 22:59:00 -04:00
} else {
2024-06-08 14:10:59 -04:00
constexpr int middle = (_begin + _end) / 2;
if (_i < middle) {
return visit<_begin, middle, Visitor>(_v, _i, _args...);
} else {
return visit<middle, _end, Visitor>(_v, _i, _args...);
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
}
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace internal
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif