draconisplusplus/include/rfl/visit.hpp

34 lines
1.1 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_VISIT_HPP_
#define RFL_VISIT_HPP_
#include <variant>
#include "Literal.hpp"
#include "TaggedUnion.hpp"
#include "internal/StringLiteral.hpp"
#include "internal/VisitTree.hpp"
#include "internal/VisitorWrapper.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
/// Implements the visitor pattern for Literals.
template <class Visitor, internal::StringLiteral... _fields, class... Args>
2024-06-16 00:13:15 -04:00
inline auto
visit(const Visitor& _visitor, const Literal<_fields...> _literal, const Args&... _args) {
2024-05-31 22:59:00 -04:00
constexpr int size = sizeof...(_fields);
2024-06-08 14:10:59 -04:00
using WrapperType = internal::VisitorWrapper<Visitor, _fields...>;
2024-05-31 22:59:00 -04:00
const auto wrapper = WrapperType(&_visitor);
2024-06-16 00:13:15 -04:00
return internal::VisitTree::visit<0, size, WrapperType>(wrapper, _literal.value(), _args...);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Implements the visitor pattern for TaggedUnions.
2024-06-16 00:13:15 -04:00
template <class Visitor, internal::StringLiteral _discriminator, class... Args>
inline auto
visit(const Visitor& _visitor, const TaggedUnion<_discriminator, Args...>& _tagged_union) {
2024-05-31 22:59:00 -04:00
return std::visit(_visitor, _tagged_union.variant());
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 rfl
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
#endif // RFL_VISIT_HPP_