Rune Caster 1.0.0
Modern C++ Text Processing Framework
Loading...
Searching...
No Matches
caster.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "spell_base.hpp"
4#include "rune_sequence.hpp"
5#include <type_traits>
6#include <utility>
7#include <concepts>
8
9namespace rune_caster {
10
21template<typename T>
22class caster {
23private:
24 T data_;
25
26public:
27 using value_type = T;
28
32 template<typename U>
33 explicit constexpr caster(U&& data)
34 noexcept(std::is_nothrow_constructible_v<T, U>)
35 : data_(std::forward<U>(data)) {}
36
47 template<spell_for<T> Spell>
48 constexpr auto cast(Spell&& spell) const & -> caster<typename Spell::output_type> {
50 }
51
55 template<spell_for<T> Spell>
57 return caster<typename Spell::output_type>{spell(std::move(data_))};
58 }
59
63 constexpr const T& result() const & noexcept {
64 return data_;
65 }
66
70 constexpr T&& result() && noexcept {
71 return std::move(data_);
72 }
73
77 constexpr T result() const && {
78 return data_;
79 }
80
84 constexpr const T& operator*() const & noexcept {
85 return data_;
86 }
87
88 constexpr T&& operator*() && noexcept {
89 return std::move(data_);
90 }
91
95 constexpr explicit operator bool() const noexcept {
96 if constexpr (std::is_pointer_v<T>) {
97 return data_ != nullptr;
98 } else if constexpr (requires { data_.empty(); }) {
99 return !data_.empty();
100 } else {
101 return true; // For non-pointer, non-container types are always valid
102 }
103 }
104
108 static constexpr const char* type_name() noexcept {
109 return typeid(T).name();
110 }
111};
112
123template<typename T>
124constexpr auto make_caster(T&& data) noexcept -> caster<std::decay_t<T>> {
125 return caster<std::decay_t<T>>{std::forward<T>(data)};
126}
127
134template<typename T, spell_concept Spell>
135 requires spell_for<Spell, std::decay_t<T>> ||
136 (requires { RuneSequence::from_utf8(std::declval<T>()); } &&
138constexpr auto operator|(T&& data, Spell&& spell) {
139 if constexpr (spell_for<Spell, std::decay_t<T>>) {
140 return make_caster(std::forward<T>(data))
141 .cast(std::forward<Spell>(spell))
142 .result();
143 } else {
145 .cast(std::forward<Spell>(spell))
146 .result();
147 }
148}
149
153template<typename T, spell_for<T> Spell>
154constexpr auto operator|(caster<T>&& caster_obj, Spell&& spell) {
155 return std::move(caster_obj).cast(std::forward<Spell>(spell));
156}
157
158} // namespace rune_caster
static RuneString from_utf8(std::string_view utf8_text)
Create a RuneString from UTF-8 text.
Unified spell object for text transformation.
A powerful chaining pipeline for applying spells to data.
Definition caster.hpp:22
constexpr const T & result() const &noexcept
Get the final result (const lvalue reference)
Definition caster.hpp:63
constexpr const T & operator*() const &noexcept
Access the underlying data directly.
Definition caster.hpp:84
constexpr T && operator*() &&noexcept
Definition caster.hpp:88
constexpr T && result() &&noexcept
Get the final result (rvalue reference - move semantics)
Definition caster.hpp:70
constexpr T result() const &&
Get the final result (const rvalue reference)
Definition caster.hpp:77
constexpr auto cast(Spell &&spell) const &-> caster< typename Spell::output_type >
Apply a spell and return a new caster with the result (C++20 concepts)
Definition caster.hpp:48
constexpr caster(U &&data) noexcept(std::is_nothrow_constructible_v< T, U >)
Construct caster with data (C++20 perfect forwarding)
Definition caster.hpp:33
static constexpr const char * type_name() noexcept
Get type information at compile time.
Definition caster.hpp:108
constexpr auto cast(Spell &&spell) &&-> caster< typename Spell::output_type >
Apply a spell and return a new caster (rvalue reference optimization)
Definition caster.hpp:56
Concept for spells that work with specific input types.
constexpr auto make_caster(T &&data) noexcept -> caster< std::decay_t< T > >
Factory function to create a caster (C++20 template argument deduction)
Definition caster.hpp:124
constexpr auto operator|(T &&data, Spell &&spell)
Pipe operator for functional programming style (C++20 concepts)
Definition caster.hpp:138