Rune Caster 1.0.0
Modern C++ Text Processing Framework
Loading...
Searching...
No Matches
spell_unified.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <string>
5#include <string_view>
6#include <memory>
7#include <type_traits>
8
9#include "rune_sequence.hpp"
10#include "caster.hpp"
11
12namespace rune_caster {
13
29class Spell {
30public:
31 // === Type definitions ===
32 using transform_function = std::function<RuneSequence(const RuneSequence&)>;
33 using validation_function = std::function<bool(const RuneSequence&)>;
34
35 // === Constructors ===
36
40 Spell() noexcept;
41
48 explicit Spell(transform_function transform,
49 std::string name = "CustomSpell",
50 std::string description = "User-defined transformation");
51
58 template<typename Func>
59 requires std::invocable<Func, const RuneSequence&> &&
60 std::convertible_to<std::invoke_result_t<Func, const RuneSequence&>, RuneSequence>
61 explicit Spell(Func&& func,
62 std::string name = "LambdaSpell",
63 std::string description = "Lambda transformation")
64 : transform_(std::forward<Func>(func))
65 , name_(std::move(name))
66 , description_(std::move(description))
67 , id_(generate_id()) {}
68
69 // === Copy/Move semantics ===
70 Spell(const Spell&) = default;
71 Spell(Spell&&) noexcept = default;
72 Spell& operator=(const Spell&) = default;
73 Spell& operator=(Spell&&) noexcept = default;
74 ~Spell() = default;
75
76 // === Core transformation ===
77
83 RuneSequence operator()(const RuneSequence& input) const;
84
90 std::string operator()(std::string_view utf8_input) const;
91
92 // === Pipeline composition ===
93
99 Spell operator|(const Spell& next) const;
100
101 // Note: Only one operator| definition to avoid ambiguity
102
103 // === Accessors ===
104
109 [[nodiscard]] const std::string& name() const noexcept { return name_; }
110
115 [[nodiscard]] const std::string& description() const noexcept { return description_; }
116
121 [[nodiscard]] uint64_t id() const noexcept { return id_; }
122
123 // === Validation and properties ===
124
129 [[nodiscard]] bool is_identity() const noexcept;
130
135 [[nodiscard]] bool is_composition() const noexcept { return is_composition_; }
136
141 [[nodiscard]] size_t composition_depth() const noexcept { return composition_depth_; }
142
143 // === Debugging and introspection ===
144
149 [[nodiscard]] std::string to_string() const;
150
156 RuneSequence test(const RuneSequence& test_input) const;
157
158 // === Static factory methods ===
159
164 static Spell identity();
165
171 static Spell constant(RuneSequence constant_result);
172
180 static Spell conditional(validation_function condition,
181 const Spell& if_true,
182 const Spell& if_false);
183
184private:
185 transform_function transform_;
186 std::string name_;
187 std::string description_;
188 uint64_t id_;
189 bool is_composition_;
190 size_t composition_depth_;
191
196 static uint64_t generate_id();
197};
198
199// === Factory functions for common spells ===
200
201namespace spell_factory {
202
210template<typename Lambda>
211auto make_spell(Lambda&& lambda,
212 std::string name = "CustomSpell",
213 std::string description = "Custom transformation") {
214 return Spell{std::forward<Lambda>(lambda), std::move(name), std::move(description)};
215}
216
224template<typename RuneTransform>
225auto per_rune_spell(RuneTransform&& rune_transform,
226 std::string name = "PerRuneSpell",
227 std::string description = "Per-rune transformation") {
228 return make_spell([transform = std::forward<RuneTransform>(rune_transform)]
229 (const RuneSequence& input) -> RuneSequence {
230 RuneSequence result;
231 for (const auto& rune : input) {
232 result.append(transform(rune));
233 }
234 return result;
235 }, std::move(name), std::move(description));
236}
237
245template<typename Predicate>
246auto filter_spell(Predicate&& predicate,
247 std::string name = "FilterSpell",
248 std::string description = "Rune filtering") {
249 return make_spell([pred = std::forward<Predicate>(predicate)]
250 (const RuneSequence& input) -> RuneSequence {
251 RuneSequence result;
252 for (const auto& rune : input) {
253 if (pred(rune)) {
254 result.append(rune);
255 }
256 }
257 return result;
258 }, std::move(name), std::move(description));
259}
260
261} // namespace spell_factory
262
263// === Integration with Caster ===
264// Note: Caster integration will be handled in caster.hpp
265
266} // namespace rune_caster
267
268// === User-defined literals ===
269
271
276Spell operator""_spell(const char* replacement_rule, size_t len);
277
278} // namespace rune_caster::literals
RuneString & append(const RuneString &other)
Append another RuneString.
Unified spell object for text transformation.
Spell(const Spell &)=default
Spell(Spell &&) noexcept=default
RuneSequence test(const RuneSequence &test_input) const
Test the spell with sample input.
uint64_t id() const noexcept
Get the spell unique ID.
std::function< RuneSequence(const RuneSequence &)> transform_function
bool is_identity() const noexcept
Check if this spell is the identity transformation.
std::string to_string() const
Get a detailed string representation.
static Spell constant(RuneSequence constant_result)
Create a spell that always returns the same result.
const std::string & description() const noexcept
Get the spell description.
std::function< bool(const RuneSequence &)> validation_function
bool is_composition() const noexcept
Check if this spell is a composition of multiple spells.
static Spell identity()
Create an identity spell (no transformation)
const std::string & name() const noexcept
Get the spell name.
Spell() noexcept
Default constructor (identity spell)
static Spell conditional(validation_function condition, const Spell &if_true, const Spell &if_false)
Create a conditional spell.
size_t composition_depth() const noexcept
Get the number of composed spells (1 for simple spells)
auto filter_spell(Predicate &&predicate, std::string name="FilterSpell", std::string description="Rune filtering")
Create a spell that filters runes based on a predicate.
auto per_rune_spell(RuneTransform &&rune_transform, std::string name="PerRuneSpell", std::string description="Per-rune transformation")
Create a spell that applies a transformation to each rune.
auto make_spell(Lambda &&lambda, std::string name="CustomSpell", std::string description="Custom transformation")
Create a spell from a lambda.
RuneString RuneSequence
Backward compatibility alias for RuneString.