TailorMade 2.0
Tailor Made is a high-performance C++20 ECS library with on-the-fly entity creation and JSON-based definitions for entities, components, and their relationships.
 
Loading...
Searching...
No Matches
TM_Tools.h
Go to the documentation of this file.
1
7
8#ifndef _TM_TOOLS_H
9#define _TM_TOOLS_H
10
11#include <variant>
12#include <json.hpp>
13#include <vector>
14#include <unordered_map>
15#include <mutex>
16#include <fstream>
17#include <iostream>
18#include <string>
19#include <filesystem>
20#include <sstream>
21
22/*
23 * Types definitions and tools to use them inside TailorMade
24 * These functions have to be update for each new type.
25 */
26#include <cctype>
27
28// Types and operators
30typedef struct Vector2 { float x; float y;
31
33 Vector2 operator+(const Vector2 v2) const {
34 return { x + v2.x, y + v2.y };
35 }
36
38 Vector2 operator-(const Vector2 v2) const {
39 return { x - v2.x, y - v2.y };
40 }
41
43 float operator*(const Vector2 v2) const {
44 return x * v2.x + y * v2.y;
45 }
46
48 Vector2 operator*=(const float s) const {
49 return { x * s, y * s };
50 }
51
53 Vector2 operator/=(const float s) const {
54 return { x / s, y / s };
55 }
56
58 float operator!() const {
59 return sqrt(x * x + y * y);
60 }
61
64 float norm = !(*this);
65 return { x / norm, y / norm };
66 }
67
69 float operator%(const Vector2 v2) const {
70 float norm1 = !(*this);
71 float norm2 = !v2;
72 return acos(((*this) * v2) / (norm1 * norm2));
73 }
74
76 Vector2 operator>>(const Vector2 v2) const {
77 float norm2 = !v2;
78 norm2 *= norm2; // Squared the norm
79 float dotProd = (*this) * v2;
80 return (*this) *= (dotProd / norm2);
81 }
83
85typedef struct Vector3 { float x; float y; float z;
86
88 Vector3 operator+(const Vector3 v2) const {
89 return { x + v2.x, y + v2.y, z + v2.z };
90 }
91
93 Vector3 operator-(const Vector3 v2) const {
94 return { x - v2.x, y - v2.y, z - v2.z };
95 }
96
98 float operator*(const Vector3 v2) const {
99 return x * v2.x + y * v2.y + z * v2.z;
100 }
101
103 Vector3 operator*=(const float s) const {
104 return { x * s, y * s, z * s };
105 }
106
108 Vector3 operator/=(const float s) const {
109 return { x / s, y / s, z / s };
110 }
111
113 Vector3 operator^(const Vector3 v2) const {
114 return { y * v2.z - z * v2.y, z * v2.x - x * v2.z, x * v2.y - y * v2.x };
115 }
116
118 float operator!() const {
119 return sqrt(x * x + y * y + z * z);
120 }
121
124 float norm = !(*this);
125 return { x / norm, y / norm, z / norm };
126 }
127
129 float operator%(const Vector3 v2) const {
130 float norm1 = !(*this);
131 float norm2 = !v2;
132 return acos(((*this) * v2) / (norm1 * norm2));
133 }
134
136 Vector3 operator>>(const Vector3 v2) const {
137 float norm2 = !v2;
138 norm2 *= norm2; // Squared the norm
139 float dotProd = (*this) * v2;
140 return (*this) *= (dotProd / norm2);
141 }
143
145inline std::ostream& operator<<(std::ostream& os, const Vector2& v) {
146 os << "{ x: " << v.x << ", y: " << v.y << "}";
147 return os;
148}
149
151inline std::ostream& operator<<(std::ostream& os, const Vector3& v) {
152 os << "{ x: " << v.x << ", y: " << v.y << ", z: " << v.z << "}";
153 return os;
154}
155
157#define ECS_Types int, float, std::string, bool, Vector2, Vector3
158
160using dataUnMap = std::unordered_map<std::string, std::pair<std::string, std::variant<ECS_Types>>>;
161
163using dataVector = std::vector<std::pair<std::string, std::variant<ECS_Types>>>;
164
171std::variant<ECS_Types> strToType(std::string type);
172
180std::variant<ECS_Types> valueToType(const nlohmann::json& value, std::string type);
181
187nlohmann::ordered_json serializeType(const dataUnMap& data);
188
194void valueToStream(std::ostream& stream, std::variant<ECS_Types> value);
195
200std::vector<std::string> getAllFilesFromDirectory(const std::string& directory);
201
202inline std::variant<ECS_Types> strToType(std::string type) {
203 // This function return the default value for the given type.
204 // We authorize the first letter to be an upper or lower case, the others must be lower case exclusively.
205 // There is the common variations for most of the types.
206
207 type[0] = tolower(static_cast<unsigned char>(type[0]));
208
209 if (type == "integer" || type == "int") {
210 return 0;
211 }
212 else if (type == "float") {
213 return 0.0f;
214 }
215 else if (type == "string" || type == "str") {
216 return "";
217 }
218 else if (type == "boolean" || type == "bool") {
219 return false;
220 }
221 else if (type == "vector2") {
222 return Vector2({ 0.0f, 0.0f });
223 }
224 else if (type == "vector3") {
225 return Vector3({ 0.0f, 0.0f, 0.0f });
226 }
227
228 throw std::runtime_error("Error : invalid type \"" + type + "\".");
229}
230
231inline std::variant<ECS_Types> valueToType(const nlohmann::json& value, std::string type) {
232 // This function return the variant from the given type with the given value.
233 type[0] = tolower(static_cast<unsigned char>(type[0]));
234
235 if (type == "integer" || type == "int") {
236 return value.get<int>();
237 }
238 else if (type == "float") {
239 return value.get<float>();
240 }
241 else if (type == "string" || type == "str") {
242 return value.get<std::string>();
243 }
244 else if (type == "boolean" || type == "bool") {
245 return value.get<bool>();
246 }
247 else if (type == "vector2") {
248 return Vector2{ value[0].get<float>(), value[1].get<float>() };
249 }
250 else if (type == "vector3") {
251 return Vector3{ value[0].get<float>(), value[1].get<float>(), value[2].get<float>() };
252 }
253
254 throw std::runtime_error("Error : invalid type \"" + type + "\".");
255}
256
257inline nlohmann::ordered_json serializeType(const dataUnMap& dataMap) {
258 nlohmann::ordered_json dict = nlohmann::ordered_json::object();
259
260 for (const auto& [key, valuePair] : dataMap) {
261 const auto& data = valuePair.second;
262
263 // Fast way to visit all the possibility.
264 std::visit([&](auto&& val) {
265 using T = std::decay_t<decltype(val)>;
266
267 if constexpr (std::is_same_v<T, int> ||
268 std::is_same_v<T, float> ||
269 std::is_same_v<T, std::string> ||
270 std::is_same_v<T, bool>) {
271 dict[key] = val; // int, float, string, bool
272 }
273 else if constexpr (std::is_same_v<T, Vector2>) {
274 dict[key] = { val.x, val.y };
275 }
276 else if constexpr (std::is_same_v<T, Vector3>) {
277 dict[key] = { val.x, val.y, val.z };
278 }
279 }, data);
280 }
281 return dict;
282}
283
284inline void valueToStream(std::ostream& stream, std::variant<ECS_Types> value) {
285 // Fast way to add to stream.
286 std::visit([&](auto&& val) {
287 stream << val;
288 }, value);
289}
290
291inline std::vector<std::string> getAllFilesFromDirectory(const std::string& directory) {
292 std::vector<std::string> result;
293
294 try {
295 for (const auto& file : std::filesystem::recursive_directory_iterator(directory)) {
296 if (std::filesystem::is_regular_file(file.path())) { // Only the files
297 result.push_back(file.path().string());
298 }
299 }
300 }
301 catch (const std::filesystem::filesystem_error& e) {
302 std::cerr << e.what() << std::endl;
303 }
304
305 return result;
306}
307
308#endif //_TM_TOOLS_H
std::variant< ECS_Types > valueToType(const nlohmann::json &value, std::string type)
Return a variant<ECS_Types> with the given value of the given type.
Definition TM_Tools.h:231
std::ostream & operator<<(std::ostream &os, const Vector2 &v)
Print a Vector2.
Definition TM_Tools.h:145
struct Vector3 Vector3
Vector3 structure and operators.
std::unordered_map< std::string, std::pair< std::string, std::variant< ECS_Types > > > dataUnMap
Map of dataName -> {dataType, value}.
Definition TM_Tools.h:160
std::vector< std::pair< std::string, std::variant< ECS_Types > > > dataVector
Vector of {dataType, value}.
Definition TM_Tools.h:163
struct Vector2 Vector2
Vector2 structure and operators.
std::variant< ECS_Types > strToType(std::string type)
Return a variant<ECS_Types> with the default value of the given type.
Definition TM_Tools.h:202
nlohmann::ordered_json serializeType(const dataUnMap &data)
Serialize the data in an ordered_json.
Definition TM_Tools.h:257
void valueToStream(std::ostream &stream, std::variant< ECS_Types > value)
Will append in the given stream, the serialized version of the given variant value.
Definition TM_Tools.h:284
std::vector< std::string > getAllFilesFromDirectory(const std::string &directory)
Return every files from the given directory, includes all subfolders.
Definition TM_Tools.h:291
Vector2 structure and operators.
Definition TM_Tools.h:30
float x
Definition TM_Tools.h:30
float operator%(const Vector2 v2) const
Angle.
Definition TM_Tools.h:69
float y
Definition TM_Tools.h:30
Vector2 operator>>(const Vector2 v2) const
Projection.
Definition TM_Tools.h:76
Vector2 operator+(const Vector2 v2) const
Addition.
Definition TM_Tools.h:33
float operator!() const
Norm.
Definition TM_Tools.h:58
float operator*(const Vector2 v2) const
Dot product.
Definition TM_Tools.h:43
Vector2 operator-(const Vector2 v2) const
Soustraction.
Definition TM_Tools.h:38
Vector2 operator*=(const float s) const
Scalar product.
Definition TM_Tools.h:48
Vector2 operator/=(const float s) const
Scalar division.
Definition TM_Tools.h:53
Vector2 operator~() const
Normalization.
Definition TM_Tools.h:63
Vector3 structure and operators.
Definition TM_Tools.h:85
float operator*(const Vector3 v2) const
Dot product.
Definition TM_Tools.h:98
Vector3 operator/=(const float s) const
Scalar division.
Definition TM_Tools.h:108
Vector3 operator+(const Vector3 v2) const
Addition.
Definition TM_Tools.h:88
Vector3 operator^(const Vector3 v2) const
Cross product.
Definition TM_Tools.h:113
float operator%(const Vector3 v2) const
Angle.
Definition TM_Tools.h:129
float x
Definition TM_Tools.h:85
float y
Definition TM_Tools.h:85
Vector3 operator>>(const Vector3 v2) const
Projection.
Definition TM_Tools.h:136
Vector3 operator-(const Vector3 v2) const
Soustraction.
Definition TM_Tools.h:93
float z
Definition TM_Tools.h:85
float operator!() const
Norm.
Definition TM_Tools.h:118
Vector3 operator*=(const float s) const
Scalar product.
Definition TM_Tools.h:103
Vector3 operator~() const
Normalization.
Definition TM_Tools.h:123