lipsum-cpp 0.5.4
A basic library written in C++ for generating placeholder Lorem Ipsum text.
Loading...
Searching...
No Matches
internal.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include "../misc.hpp"
16#include "core.hpp"
17#include "stdincludes.hpp"
18
28namespace lipsum::internal
29{
30
36 enum class LogType : int
37 {
38 Trace = 0,
39 Info,
40 Warn,
41 Error,
42 Critical
43 };
44
45#ifndef LIPSUM_MIN_BUILD
46
52 template <typename T>
53 concept ToStringable = requires(const T& value) {
54 { std::to_string(value) } -> std::convertible_to<std::string>;
55 };
56
64 template <typename T>
65 concept Streamable = requires(std::ostream& outs, const T& value) {
66 { outs << value } -> std::same_as<std::ostream&>;
67 } && (!ToStringable<T>);
68
69#endif
77 template <typename T>
78 concept IsInt =
79 (std::integral<T> || std::unsigned_integral<T>) &&
80 !(std::same_as<T, char>) && !(std::same_as<T, signed char>) &&
81 !(std::same_as<T, unsigned char>) && !(std::same_as<T, bool>);
82
89 template <typename T>
90 concept UniformDistributionType = (IsInt<T> || std::floating_point<T>);
91
104 LIPSUM_API std::string HandleHTMLEntity(char letter,
105 Format format = Format::HTML);
106
125 template <typename... Args>
126 void LogWarn([[maybe_unused]] LogType type,
127 [[maybe_unused]] const Args&... args)
128 {
129#ifndef LIPSUM_QUIET
130 std::ostringstream oss;
131 oss << "lipsum-cpp ";
132 switch (type)
133 {
134 case LogType::Trace:
135 {
136 oss << "TRACE";
137 break;
138 }
139 case LogType::Info:
140 {
141 oss << "INFO";
142 break;
143 }
144 case LogType::Warn:
145 {
146 oss << "WARNING";
147 break;
148 }
149 case LogType::Error:
150 {
151 oss << "ERROR";
152 break;
153 }
154 case LogType::Critical:
155 {
156 oss << "CRITICAL";
157 break;
158 }
159 }
160 oss << " -- ";
161 ((oss << args), ...);
162 oss << '\n';
163 std::string message = oss.str();
164# ifndef LIPSUM_MIN_BUILD
165# ifdef __EMSCRIPTEN__
166 auto funcCalling = emscripten_console_warn;
167 if (type == LogType::Trace || type == LogType::Info)
168 {
169 funcCalling = emscripten_console_log;
170 }
171 if (type == LogType::Error || type == LogType::Critical)
172 {
173 funcCalling = emscripten_console_error;
174 }
175 funcCalling(message.c_str());
176# elif defined(_WIN32)
177 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
178 int colorUsing = 6; // yellow
179 if (type == LogType::Trace)
180 {
181 colorUsing = 8; // gray
182 }
183 if (type == LogType::Info)
184 {
185 colorUsing = 2; // green
186 }
187 if (type == LogType::Error)
188 {
189 colorUsing = 12; // light red
190 }
191 if (type == LogType::Critical)
192 {
193 colorUsing = 4; // red
194 }
195 // yellow
196 SetConsoleTextAttribute(hConsole, colorUsing);
197 std::cerr << message;
198 // default
199 SetConsoleTextAttribute(hConsole, 7);
200# else
201 int colorUsing = 33; // yellow
202 if (type == LogType::Trace)
203 {
204 colorUsing = 90; // gray
205 }
206 if (type == LogType::Info)
207 {
208 colorUsing = 32; // green
209 }
210 if (type == LogType::Error)
211 {
212 colorUsing = 91; // light red
213 }
214 if (type == LogType::Critical)
215 {
216 colorUsing = 31; // red
217 }
218 std::cerr << "\033[" << colorUsing << "m" << message << "\033[0m";
219# endif
220# else
221 std::cerr << message;
222# endif
223#endif
224 }
225
226#ifndef LIPSUM_MIN_BUILD
242 template <Streamable T> std::string ToString(const T& param)
243 {
244 // std::cout << "Using streamable\n";
245 std::ostringstream oss;
246 oss << param;
247 return oss.str();
248 }
249
250 template <ToStringable T> std::string ToString(const T& param)
251 {
252 // std::cout << "Using to_string-able\n";
253 return std::to_string(param);
254 }
255
273 template <typename T> T ToType(const std::string& input)
274 {
275 T res{};
276
277 if constexpr (std::same_as<T, std::string>)
278 {
279 return input;
280 }
281 else if constexpr (std::same_as<T, bool>)
282 {
283 // std::cout << "bool ToType\n";
284 return (input == "true" || input == "1");
285 }
286 else if constexpr (std::same_as<T, char>)
287 {
288 // std::cout << "char ToType\n";
289 return input.empty() ? '\0' : input.at(0);
290 }
291 else if constexpr (IsInt<T>)
292 {
293 // std::cout << "integer ToType\n";
294 auto [ptr, ec] = std::from_chars(input.data(),
295 input.data() + input.size(),
296 res);
297 if (ec != std::errc{})
298 {
299 LogWarn(internal::LogType::Error,
300 "lpsm::internal::ToType: std::from_chars() failed; "
301 "invalidly formatted integer? Returning default T.");
302 return T{};
303 }
304 return res;
305 }
306 else
307 // assume streamable
308 {
309 // std::cout << "generic ToType\n";
310 std::istringstream iss(input);
311 iss >> res;
312 return res;
313 }
314 }
315
316#else
317 template <typename T> std::string ToString(const T& param)
318 {
319 std::ostringstream oss;
320 oss << param;
321 return oss.str();
322 }
323
324 template <typename T> T ToType(const std::string& param)
325 {
326 T res;
327 std::istringstream iss(param);
328 iss >> std::boolalpha >> res;
329 return res;
330 }
331#endif
332} // namespace lipsum::internal
Is T an int?
Definition internal.hpp:78
Can std::ostream take T?
Definition internal.hpp:65
Can std::to_string() be called with T?
Definition internal.hpp:53
Can std::uniform_int_distribution or std::uniform_real_distribution be constructed with T?
Definition internal.hpp:90
Core macros for lipsum-cpp.
#define LIPSUM_API
Macro for shared libraries.
Definition core.hpp:71
Miscellaneous functions of lipsum-cpp.
This namespace contains functions only used internally by other functions.
LogType
Log types for lipsum::internal::LogWarn().
Definition internal.hpp:37
void LogWarn(LogType type, const Args &... args)
Log a warning.
Definition internal.hpp:126
LIPSUM_API std::string HandleHTMLEntity(char letter, Format format=Format::HTML)
Handle HTML entities.
Definition argvec2.inl:56
T ToType(const std::string &input)
Convert a string to an object.
Definition internal.hpp:273
std::string ToString(const T &param)
Convert an object to a string.
Definition internal.hpp:242
Format
Types of formats.
Definition misc.hpp:33
Standard library includes of lipsum-cpp.