R-Type  2
Doom but in better
Loading...
Searching...
No Matches
TOMLLoader.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** TOMLLoader.hpp
6*/
7
13#pragma once
14
15#include <mutex>
16#include <string>
17#include <vector>
18#include <iostream>
19#include <unordered_map>
20#include <toml++/toml.hpp>
21
22#include "Log.hpp"
23#include "LogMacros.hpp"
24#include "Utilities.hpp"
25#include "CustomExceptions.hpp"
26
32 public:
36 TOMLLoader();
37
42 TOMLLoader(const std::string &tomlPath);
43
48 TOMLLoader(const TOMLLoader &tomlInstance);
49
55 TOMLLoader(const toml::table &tomlTable, const std::string &tomlPath);
56
62 TOMLLoader(const toml::array &tomlArray, const std::string &tomlPath);
63
70 TOMLLoader(const toml::array &tomlArray, const std::string &key, const std::string &tomlPath);
71
75 ~TOMLLoader() = default;
76
81 void setTOMLPath(const std::string &tomlPath);
82
87 const bool isTOMLLoaded() const;
88
93 const std::string getTOMLPath() const;
94
99 const std::string getTOMLString() const;
100
105 const toml::table getRawTOML() const;
106
115 template <typename T>
116 T getValue(const std::string &key) const
117 {
118 _ensureLoaded();
119 if (auto value = _toml[key].value<T>()) {
120 return *value;
121 }
122 throw CustomExceptions::NoTOMLKey(_tomlPath, key);
123 };
124
130 const toml::node_type getValueType(const std::string &key) const;
131
137 const std::string getValueTypeAsString(const std::string &key) const;
138
144 const std::string getTypeAsString(const std::string &key) const;
145
151 const std::string getTypeAsString(const toml::node_type &type) const;
152
158 const bool hasKey(const std::string &key) const;
159
164 std::vector<std::string> getKeys() const;
165
172 toml::table getTable(const std::string &key) const;
173
180 toml::array getArray(const std::string &key) const;
181
186 void update(const TOMLLoader &copy);
187
192 void update(const toml::table &copy);
193
198 void update(const toml::array &copy);
199
205 void update(const toml::array &copy, const std::string &key);
206
210 void printTOML() const;
211
217 TOMLLoader &operator=(const TOMLLoader &copy);
218
224 TOMLLoader &operator=(const toml::table &copy);
225
231 TOMLLoader &operator=(const toml::array &copy);
232
233 private:
237 void _loadTOML();
238
243 void _ensureLoaded() const;
244
248 void _loadNodeTypeEquivalence();
249
250 toml::table _toml;
251 bool _tomlLoaded = false;
252 std::string _tomlPath = "";
253 std::string _tomlString = "";
254 std::unordered_map<toml::node_type, std::string> _nodeTypeEquivalence;
255};
256
263std::ostream &operator<<(std::ostream &os, const TOMLLoader &node);
File in charge of containing the custom errors that are going to be used for custom description error...
Macro definitions for logging messages with varying levels of detail and formatting.
This is the file in charge of containing the Log class (the one in charge of outputing info only when...
std::ostream & operator<<(std::ostream &os, const TOMLLoader &node)
Overloads the stream insertion operator for TOMLLoader.
This is the class in charge of informing the user that they tried to access a non-existant toml key i...
Definition No.hpp:772
A utility class for parsing, navigating, and managing TOML files and data.
~TOMLLoader()=default
Default destructor.
const toml::node_type getValueType(const std::string &key) const
Retrieves the type of a value for a specific key as a TOML node type.
const std::string getTOMLString() const
Retrieves the TOML data as a string.
void update(const TOMLLoader &copy)
Updates the current loader with another loader's data.
const toml::table getRawTOML() const
Retrieves the raw TOML table.
TOMLLoader & operator=(const TOMLLoader &copy)
Copy assignment operator.
TOMLLoader()
Default constructor.
const bool isTOMLLoaded() const
Checks if the TOML file is successfully loaded.
const std::string getTOMLPath() const
Retrieves the path of the loaded TOML file.
toml::table getTable(const std::string &key) const
Retrieves a TOML table for a specific key.
const bool hasKey(const std::string &key) const
Checks if a specific key exists in the TOML data.
void setTOMLPath(const std::string &tomlPath)
Sets the path of the TOML file to load.
void printTOML() const
Prints the TOML data to the debug stream.
std::vector< std::string > getKeys() const
Retrieves all keys from the TOML table.
const std::string getValueTypeAsString(const std::string &key) const
Retrieves the type of a value for a specific key as a string.
const std::string getTypeAsString(const std::string &key) const
Retrieves the type of a value for a specific key as a string (alias).
toml::array getArray(const std::string &key) const
Retrieves a TOML array for a specific key.
T getValue(const std::string &key) const
Retrieves a value of type T from the TOML table.