Skip to content

Commit

Permalink
add transformation in enum_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut committed Jul 8, 2024
1 parent 25c2994 commit 4756013
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ using list_variant = [:form::util::create_variant(^list):];
## Enum to string
Transform enum directly to std::string
``` c++
enum class Color { red, green, blue };
Expand All @@ -34,6 +36,27 @@ void EnumToString() {
```

If you want to apply some pattern to the formatting of enum value name

``` c++
void EnumToString() {
auto transform = [](std::string data) {
std::string out;
out += std::tolower(data[0]);
for (auto const c : data.substr(1, data.size())) {
if (std::isupper(c))
out += "-";
out += std::tolower(c);
}
return out;
};
form::enum_to_string(Decorator::Underline, transform) // underline
form::enum_to_string(Decorator::DottedUnderline, transform) // dotted-underline
}
```

`

## Variant type to string

``` c++
Expand Down
6 changes: 6 additions & 0 deletions include/form/form.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ constexpr std::string enum_to_string(E value) {
return result;
}

template <typename E>
requires std::is_enum_v<E>
constexpr std::string enum_to_string(E value, auto transformation) {
return transformation(enum_to_string(value));
}

template <typename T> bool compare(T const &lhs, T const &rhs) {
bool result = true;
if constexpr (std::is_arithmetic_v<T>)
Expand Down
8 changes: 0 additions & 8 deletions src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include "test.h"

template <typename T>
requires form::util::is_one_of<T, Z>
struct std::formatter<T> : std::formatter<std::string> {
auto format(const T &val, std::format_context &ctx) const {
return std::format_to(ctx.out(), "{}", form::format_univ(val));
}
};

int main() {
form::run_tests<^form::tests>();
form::run_tests<^form::round_trip_tests>();
Expand Down
31 changes: 31 additions & 0 deletions src/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ using list_variant = [:form::util::create_variant(^list):];

enum class Color { red, green, blue };

enum class Decorator : uint8_t {
Underline,
DoubleUnderline,
CurlyUnderline,
DottedUnderline,
DashedUnderline,
Overline,
CrossedOut,
Framed,
Encircle,
};

namespace form::examples {
bool VariantCreate() {
list_variant v{list::CancelSelection{}};
Expand All @@ -335,6 +347,25 @@ bool VariantToString() {
}

bool EnumToString() { return form::enum_to_string(Color::red) == "red"; }

bool EnumToStringWithTransform() {
bool res = true;
auto transform = [](std::string data) {
std::string out;
out += std::tolower(data[0]);
for (auto const c : data.substr(1, data.size())) {
if (std::isupper(c))
out += "-";
out += std::tolower(c);
}
return out;
};
res &= (form::enum_to_string(Decorator::Underline, transform) == "underline");
res &= (form::enum_to_string(Decorator::DottedUnderline, transform) ==
"dotted-underline");
return res;
}

} // namespace form::examples

void ExampleConfig() {
Expand Down

0 comments on commit 4756013

Please sign in to comment.