vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
Parser.hpp
Go to the documentation of this file.
1///
2/// @file Parser.hpp
3/// @brief This file contains the Parser class
4/// @namespace ven
5///
6
7#pragma once
8
9#include <algorithm>
10#include <functional>
11#include <unordered_map>
12#include <vector>
13
16
17namespace ven {
18
19 ///
20 /// @class ParserException
21 /// @brief Custom exception class for parsing errors.
22 /// @namespace ven
23 ///
24 class ParserException final : public std::exception {
25
26 public:
27
28 explicit ParserException(std::string msg) : m_msg{std::move(msg)} {};
29 ~ParserException() override = default;
30
35
36 [[nodiscard]] const char *what() const noexcept override { return m_msg.c_str(); };
37
38 private:
39
40 std::string m_msg{0};
41
42 }; // class ParserException
43
44 inline bool isNumeric(const std::string_view str) { return !str.empty() && std::ranges::all_of(str, ::isdigit); }
45
46 static const std::unordered_map<std::string_view, std::function<void(Config& conf, std::string_view arg)>> FUNCTION_MAP_OPT_LONG = {
47 { "help", [](Config& conf, std::string_view arg) { std::cout << HELP_MESSAGE; throw ParserException(""); } },
48 { "version", [](Config& conf, std::string_view arg) { std::cout << VERSION_MESSAGE; throw ParserException(""); } },
49 { "width", [](Config& conf, const std::string_view arg)
50 {
51 if (!isNumeric(arg)) {
52 throw std::invalid_argument("Invalid value for width: " + std::string(arg));
53 }
54 conf.window.width = std::stoi(std::string(arg));
55 } },
56 { "height", [](Config& conf, const std::string_view arg)
57 {
58 if (!isNumeric(arg)) {
59 throw std::invalid_argument("Invalid value for height: " + std::string(arg));
60 }
61 conf.window.height = std::stoi(std::string(arg));
62 } },
63 { "fullscreen", [](Config& conf, std::string_view arg) { conf.window.fullscreen = true; } },
64 { "vsync", [](Config& conf, std::string_view arg) { conf.vsync = true; } },
65 { "fov", [](Config& conf, const std::string_view arg)
66 {
67 if (!isNumeric(arg)) {
68 throw std::invalid_argument("Invalid value for fov: " + std::string(arg));
69 }
70 const float value = std::stof(std::string(arg));
71 if (value < 1.0F || value > 300.0F) {
72 throw std::out_of_range("Field of view must be between 1 and 300");
73 }
74 conf.camera.fov = value;
75 } },
76 { "mspeed", [](Config& conf, const std::string_view arg)
77 {
78 if (!isNumeric(arg)) {
79 throw std::invalid_argument("Invalid value for move speed: " + std::string(arg));
80 }
81 const float value = std::stof(std::string(arg));
82 if (value < 0.1F || value > 100.0F) {
83 throw std::out_of_range("Move speed must be between 0.1 and 100.0");
84 }
85 conf.camera.move_speed = value;
86 } },
87 { "lspeed", [](Config& conf, const std::string_view arg)
88 {
89 if (!isNumeric(arg)) {
90 throw std::invalid_argument("Invalid value for look speed: " + std::string(arg));
91 }
92 const float value = std::stof(std::string(arg));
93 if (value < 0.1F || value > 100.0F) {
94 throw std::out_of_range("Look speed must be between 0.1 and 100.0");
95 }
96 conf.camera.look_speed = value;
97 } },
98 { "far", [](Config& conf, const std::string_view arg)
99 {
100 if (!isNumeric(arg)) {
101 throw std::invalid_argument("Invalid value for far: " + std::string(arg));
102 }
103 const float value = std::stof(std::string(arg));
104 if (value < 0.1F || value > 100.0F) {
105 throw std::out_of_range("Far plane must be between 0.1 and 100.0");
106 }
107 conf.camera.far = value;
108 } },
109 { "near", [](Config& conf, const std::string_view arg)
110 {
111 if (!isNumeric(arg)) {
112 throw std::invalid_argument("Invalid value for near: " + std::string(arg));
113 }
114 const float value = std::stof(std::string(arg));
115 if (value < 0.1F || value > 100.0F) {
116 throw std::out_of_range("Near plane must be between 0.1 and 100.0");
117 }
118 conf.camera.near = value;
119 } }
120 };
121
122 static const std::unordered_map<std::string_view, std::function<void(Config& conf)>> FUNCTION_MAP_OPT_SHORT = {
123 { "h", [](Config& conf) { std::cout << HELP_MESSAGE; throw ParserException(""); } },
124 { "v", [](Config& conf) { std::cout << VERSION_MESSAGE; throw ParserException(""); } },
125 { "f", [](Config& conf) { conf.window.fullscreen = true; } },
126 { "V", [](Config& conf) { conf.vsync = true; } }
127 };
128
129 ///
130 /// @class Parser
131 /// @brief Class for Parser
132 /// @namespace ven
133 ///
134 class Parser {
135
136 public:
137
138 Parser(int argc, char* argv[], char* envp[]);
139 ~Parser() = default;
140
141 Parser(const Parser&) = delete;
142 Parser& operator=(const Parser&) = delete;
143 Parser(Parser&&) = delete;
144 Parser& operator=(Parser&&) = delete;
145
146 void printArguments() const;
147
148 [[nodiscard]] Config getConfig() const { return m_config; }
149
150 private:
151
153 bool m_state = true;
154
155 void parseArgs(const std::vector<std::string_view>& argv);
156 void parseEnv(const std::unordered_map<std::string, std::string>& envp);
157
158 void handleLongOption(const std::string_view& arg, const std::vector<std::string_view>& argv, size_t& index);
159 void handleShortOptions(const std::string_view& arg);
160
161 [[nodiscard]] static bool isValidOption(const std::string_view& option) { return FUNCTION_MAP_OPT_LONG.contains(option) || FUNCTION_MAP_OPT_SHORT.contains(option); }
162
163 }; // class Parser
164
165} // namespace ven
Custom exception class for parsing errors.
Definition Parser.hpp:24
ParserException & operator=(const ParserException &)=delete
ParserException(const ParserException &)=delete
ParserException & operator=(ParserException &&)=delete
std::string m_msg
Definition Parser.hpp:40
ParserException(ParserException &&)=delete
const char * what() const noexcept override
Definition Parser.hpp:36
ParserException(std::string msg)
Definition Parser.hpp:28
~ParserException() override=default
Class for Parser.
Definition Parser.hpp:134
~Parser()=default
Config getConfig() const
Definition Parser.hpp:148
Parser(int argc, char *argv[], char *envp[])
Definition parser.cpp:54
Parser(const Parser &)=delete
static bool isValidOption(const std::string_view &option)
Definition Parser.hpp:161
bool m_state
Definition Parser.hpp:153
Config m_config
Definition Parser.hpp:152
void printArguments() const
Parser(Parser &&)=delete
Parser & operator=(const Parser &)=delete
Parser & operator=(Parser &&)=delete
void parseEnv(const std::unordered_map< std::string, std::string > &envp)
Definition parser.cpp:27
void handleShortOptions(const std::string_view &arg)
Definition parser.cpp:15
void parseArgs(const std::vector< std::string_view > &argv)
Definition parser.cpp:38
void handleLongOption(const std::string_view &arg, const std::vector< std::string_view > &argv, size_t &index)
Definition parser.cpp:6
static const std::unordered_map< std::string_view, std::function< void(Config &conf, std::string_view arg)> > FUNCTION_MAP_OPT_LONG
Definition Parser.hpp:46
constexpr auto VERSION_MESSAGE
Definition Message.hpp:13
bool isNumeric(const std::string_view str)
Definition Parser.hpp:44
constexpr auto HELP_MESSAGE
Definition Message.hpp:21
static const std::unordered_map< std::string_view, std::function< void(Config &conf)> > FUNCTION_MAP_OPT_SHORT
Definition Parser.hpp:122
float move_speed
Definition Config.hpp:29
float look_speed
Definition Config.hpp:30
bool vsync
Definition Config.hpp:39
WindowConf window
Definition Config.hpp:37
CameraConf camera
Definition Config.hpp:38
uint16_t height
Definition Config.hpp:22
uint16_t width
Definition Config.hpp:21