OASIS
Open Algebra Software
Loading...
Searching...
No Matches
MatchCast.hpp
Go to the documentation of this file.
1//
2// Created by Matthew McCall on 10/22/24.
3//
4
5// Hear ye, valiant coder! Within this hallowed script of Oasis' MatchCast,
6// lies an unfathomable confluence of templated magic, scarcely understood only by even the
7// most exalted compilers and the divine overseers. This arcane construct,
8// when tampered with, may envelop thee in utter confusion, leaving thee solitary in thy pursuits.
9// Of course, this sagely advice surely does not emanate from a mere language model. Rather,
10// it is the timeless counsel of battle-hardened developers who have faced such arcane complexity.
11// Should regret cloud thy mind and thou desires to undo thine alterations, perform the following
12// sacred rite to restore the code to its pristine state:
13//
14// git checkout -- <path_to_this_file>
15//
16
17#ifndef OASIS_MATCHCAST_HPP
18#define OASIS_MATCHCAST_HPP
19
20#include <boost/mpl/for_each.hpp>
21#include <boost/mpl/push_back.hpp>
22#include <boost/mpl/vector.hpp>
23
24#include <functional>
25
26namespace Oasis {
27
28template <typename T>
30
31template <typename Ret, typename ClassType, typename Arg>
32struct lambda_traits<Ret (ClassType::*)(Arg) const> {
34};
35
36template <typename Lambda>
37using lambda_argument_type = typename lambda_traits<decltype(&Lambda::operator())>::argument_type;
38
39template <typename ArgumentT, typename Cases>
41public:
42 template <typename Lambda>
48
49 std::unique_ptr<ArgumentT> Execute(const ArgumentT& arg, std::unique_ptr<ArgumentT>&& fallback) const
50 {
51 std::unique_ptr<ArgumentT> result = nullptr;
52 boost::mpl::for_each<Cases>([&]<typename CaseTrueCallbackT>(CaseTrueCallbackT caseTrueCallback) {
54 if (result)
55 return;
56 if (std::unique_ptr<CaseType> castResult = RecursiveCast<CaseType>(arg))
57 result = caseTrueCallback(*castResult);
58 });
59 return result ? std::move(result) : std::move(fallback);
60 }
61};
62
63template <typename ArgumentT>
65
66}
67
68#endif // OASIS_MATCHCAST_HPP
Definition MatchCast.hpp:40
std::unique_ptr< ArgumentT > Execute(const ArgumentT &arg, std::unique_ptr< ArgumentT > &&fallback) const
Definition MatchCast.hpp:49
MatchCastImpl< ArgumentT, typename boost::mpl::push_back< Cases, Lambda >::type > Case(Lambda) const
Definition MatchCast.hpp:44
Definition Add.hpp:11
typename lambda_traits< decltype(&Lambda::operator())>::argument_type lambda_argument_type
Definition MatchCast.hpp:37
Definition MatchCast.hpp:29