Rune Caster 1.0.0
Modern C++ Text Processing Framework
Loading...
Searching...
No Matches
spell_base.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <type_traits>
5#include <concepts>
6#include <functional>
7#include <utility>
8#include <ranges>
9
10#include "rune_sequence.hpp"
11
12namespace rune_caster {
13
14// Forward declarations removed to avoid conflict; RuneSequence is defined via alias later.
15
26template<typename Input, typename Output = Input>
28public:
29 using input_type = Input;
30 using output_type = Output;
31
32 virtual ~spell_base() = default;
33
39 virtual output_type operator()(const input_type& input) const = 0;
40
45 virtual std::string name() const = 0;
46
51 virtual std::string description() const = 0;
52
57 template<typename T>
58 static constexpr bool can_process() noexcept {
59 return std::same_as<T, input_type> || std::convertible_to<T, input_type>;
60 }
61
65 template<typename T>
66 static constexpr bool produces() noexcept {
67 return std::same_as<T, output_type> || std::convertible_to<output_type, T>;
68 }
69
73 static constexpr const char* input_type_name() noexcept {
74 return typeid(input_type).name();
75 }
76
80 static constexpr const char* output_type_name() noexcept {
81 return typeid(output_type).name();
82 }
83};
84
92
93// C++20 Concepts - Modern Type Checking
94
98template<typename T>
99concept spell_concept = requires(T spell) {
100 typename T::input_type;
101 typename T::output_type;
102 { spell.name() } -> std::convertible_to<std::string>;
103 { spell.description() } -> std::convertible_to<std::string>;
104 requires std::invocable<T, typename T::input_type>;
105 requires std::same_as<
106 std::invoke_result_t<T, typename T::input_type>,
107 typename T::output_type
108 >;
109};
110
114template<typename Spell, typename Input>
116 std::same_as<typename Spell::input_type, Input>;
117
121template<typename T>
123 std::same_as<typename T::output_type, RuneSequence>;
124
128template<typename Spell, typename Input, typename Output>
131 std::same_as<typename Spell::output_type, Output>;
132
136template<typename Spell1, typename Spell2>
139 (std::same_as<typename Spell1::output_type, typename Spell2::input_type> ||
140 std::convertible_to<typename Spell1::output_type, typename Spell2::input_type>);
141
142// Backward compatibility - convert concepts to constexpr bool
143template<typename T>
145
146template<typename Spell, typename Input>
148
149template<typename Spell1, typename Spell2>
151
152template<typename T>
154
155} // namespace rune_caster
Base interface for all spell algorithms with C++20 enhancements.
virtual output_type operator()(const input_type &input) const =0
Apply the spell transformation.
virtual ~spell_base()=default
virtual std::string name() const =0
Get the spell's name.
static constexpr bool produces() noexcept
Check if this spell produces the expected output type (C++20 concepts)
virtual std::string description() const =0
Get the spell's description.
static constexpr const char * output_type_name() noexcept
Get output type name as compile-time string (C++20)
static constexpr const char * input_type_name() noexcept
Get input type name as compile-time string (C++20)
static constexpr bool can_process() noexcept
Check if this spell can be applied to the given input type (C++20 concepts)
Concept for spells that can be chained together.
Concept for sequence-based spells (most common case)
Concept defining what makes a valid spell.
Concept for spells that work with specific input types.
Concept for transformation spells that can change the output type.
constexpr bool can_spell_process_v
constexpr bool chainable_v
constexpr bool is_spell_v
spell_base< RuneSequence > sequence_spell
Most common spell type: RuneSequence -> RuneSequence.
constexpr bool is_sequence_spell_v