le2d v0.4.7
2D game engine framework
 
Loading...
Searching...
No Matches
parser.hpp
Go to the documentation of this file.
1#pragma once
2#include <glm/vec2.hpp>
3#include <charconv>
4#include <format>
5#include <string>
6
7namespace le::tweak {
8template <typename Type>
9concept IntegerT = std::same_as<Type, std::int64_t> || std::same_as<Type, std::int32_t>;
10
11template <typename Type>
12concept UnsignedT = std::same_as<Type, std::uint64_t> || std::same_as<Type, std::uint32_t>;
13
14template <typename Type>
15concept FloatT = std::same_as<Type, float> || std::same_as<Type, double>;
16
17template <typename Type>
19
21template <typename Type>
22struct Parser;
23
24template <>
25struct Parser<bool> {
26 [[nodiscard]] static constexpr auto type_name() -> std::string_view { return "bool"; }
27 [[nodiscard]] static auto parse(std::string_view in, bool& out) -> bool;
28 [[nodiscard]] static auto to_string(bool const value) -> std::string { return value ? "true" : "false"; }
29};
30
31template <>
32struct Parser<std::string> {
33 [[nodiscard]] static constexpr auto type_name() -> std::string_view { return "string"; }
34 [[nodiscard]] static auto parse(std::string_view in, std::string& out) -> bool;
35 [[nodiscard]] static auto to_string(std::string_view const value) -> std::string { return std::string{value}; }
36};
37
38template <NumberT Type>
39struct Parser<Type> {
40 [[nodiscard]] static constexpr auto type_name() -> std::string_view {
41 if constexpr (FloatT<Type>) {
42 return "float";
43 } else if constexpr (UnsignedT<Type>) {
44 return "unsigned";
45 } else {
46 return "integer";
47 }
48 }
49
50 [[nodiscard]] static auto parse(std::string_view const in, Type& out) -> bool {
51 char const* end = in.data() + in.size();
52 auto const [ptr, ec] = std::from_chars(in.data(), end, out);
53 return ec == std::errc{} && ptr == end;
54 }
55
56 [[nodiscard]] static auto to_string(Type const value) -> std::string { return std::format("{}", value); }
57};
58
59template <NumberT Type>
60struct Parser<glm::tvec2<Type>> {
61 [[nodiscard]] static constexpr auto type_name() -> std::string_view {
62 if constexpr (UnsignedT<Type>) {
63 return "uvec2";
64 } else if constexpr (IntegerT<Type>) {
65 return "ivec2";
66 } else {
67 return "vec2";
68 }
69 }
70
71 [[nodiscard]] static auto parse(std::string_view const in, glm::tvec2<Type>& out) -> bool {
72 auto const i = in.find('x');
73 if (i == std::string_view::npos) { return false; }
74 if (!Parser<Type>::parse(in.substr(0, i), out.x)) { return false; }
75 if (!Parser<Type>::parse(in.substr(i + 1), out.y)) { return false; }
76 return true;
77 }
78
79 [[nodiscard]] static auto to_string(glm::tvec2<Type> const value) -> std::string { return std::format("{}x{}", value.x, value.y); }
80};
81} // namespace le::tweak
Definition parser.hpp:15
Definition parser.hpp:9
Definition parser.hpp:18
Definition parser.hpp:12
Definition parser.hpp:7
static auto to_string(Type const value) -> std::string
Definition parser.hpp:56
static constexpr auto type_name() -> std::string_view
Definition parser.hpp:40
static auto parse(std::string_view const in, Type &out) -> bool
Definition parser.hpp:50
static constexpr auto type_name() -> std::string_view
Definition parser.hpp:26
static auto to_string(bool const value) -> std::string
Definition parser.hpp:28
static auto parse(std::string_view in, bool &out) -> bool
static auto to_string(glm::tvec2< Type > const value) -> std::string
Definition parser.hpp:79
static auto parse(std::string_view const in, glm::tvec2< Type > &out) -> bool
Definition parser.hpp:71
static constexpr auto type_name() -> std::string_view
Definition parser.hpp:61
static auto parse(std::string_view in, std::string &out) -> bool
static auto to_string(std::string_view const value) -> std::string
Definition parser.hpp:35
static constexpr auto type_name() -> std::string_view
Definition parser.hpp:33
Customization point. Specialize this type for custom Tweakable template types.
Definition parser.hpp:22