Maze Builder Docs 8.10.23
Loading...
Searching...
No Matches
processed_text.h
Go to the documentation of this file.
1#ifndef PROCESSED_TEXT_H
2#define PROCESSED_TEXT_H
3
4#include <string>
5#include <string_view>
6#include <type_traits>
7#include <variant>
8
11namespace mazes
12{
14 class processed_text final
15 {
17 using artifacts = std::variant<std::monostate, std::string, std::string_view, char*>;
18
19 public:
22 [[nodiscard]] artifacts get() const noexcept { return buffer; }
23
26 void set_processed(artifacts anything) noexcept
27 {
28 if (const auto *sv = std::get_if<std::string_view>(&anything); sv != nullptr)
29 {
30 buffer = std::string{*sv};
31 return;
32 }
33
34 if (const auto *cstr = std::get_if<char *>(&anything); cstr != nullptr)
35 {
36 buffer = *cstr ? std::string{*cstr} : std::string{};
37 return;
38 }
39
40 buffer = std::move(anything);
41 }
42
46 bool set(const std::string_view sv) noexcept
47 {
48 set_processed(std::string{sv});
49 return true;
50 }
51
55 [[nodiscard]] bool is_processed() const noexcept
56 {
57 return std::visit([](const auto& value) -> bool
58 {
59 using T = std::decay_t<decltype(value)>;
60 if constexpr (std::is_same_v<T, std::monostate>)
61 {
62 return false;
63 }
64 else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>)
65 {
66 return !value.empty();
67 }
68 else if constexpr (std::is_same_v<T, char*>)
69 {
70 return value != nullptr && *value != '\0';
71 }
72 else
73 {
74 return false;
75 }
76 }, get());
77 }
78
79 static std::string to_string(const artifacts& artifact) noexcept
80 {
81 return std::visit([](const auto& value) -> std::string
82 {
83 using T = std::decay_t<decltype(value)>;
84 if constexpr (std::is_same_v<T, std::monostate>)
85 {
86 return {};
87 }
88 else if constexpr (std::is_same_v<T, std::string>)
89 {
90 return value;
91 }
92 else if constexpr (std::is_same_v<T, std::string_view>)
93 {
94 return std::string{value};
95 }
96 else if constexpr (std::is_same_v<T, char*>)
97 {
98 return value ? std::string{value} : std::string{};
99 }
100 else
101 {
102 return {};
103 }
104 }, artifact);
105 }
106
107 private:
108 artifacts buffer;
109 };
110}
111
112#endif // PROCESSED_TEXT_H
Class for managing processed text resources.
Definition processed_text.h:15
artifacts get() const noexcept
Gets the processed artifact.
Definition processed_text.h:22
bool set(const std::string_view sv) noexcept
Sets the processed artifact from a string view.
Definition processed_text.h:46
void set_processed(artifacts anything) noexcept
Sets the processed artifact.
Definition processed_text.h:26
bool is_processed() const noexcept
Checks if the processed artifact is finalized - empty or null.
Definition processed_text.h:55