ORIGINAL
Loading...
Searching...
No Matches
printable.h
Go to the documentation of this file.
1#ifndef PRINTABLE_H
2#define PRINTABLE_H
3#pragma once
4
5#include <memory>
6#include "format"
7#include "types.h"
8#include "sstream"
9
10
19
20namespace original {
21
29class printable {
30 mutable std::string cache_string_;
31
32public:
33 virtual ~printable() = 0;
34
46 [[nodiscard]] virtual std::string className() const;
47
61 [[nodiscard]] virtual std::string toString(bool enter) const;
62
67 explicit operator std::string() const;
68
73 explicit operator const char*() const;
74
80 [[nodiscard]] const char* toCString(bool enter) const;
81
82 // ----------------- Static Format Utilities -----------------
83
102 template<typename TYPE>
103 static std::string formatString(const TYPE& t);
104
116 template <typename TYPE>
117 static std::string formatString(TYPE* const& ptr);
118
127 template<typename TYPE>
128 static const char* formatCString(const TYPE& t);
129
141 template<typename TYPE>
142 static std::string formatEnum(const TYPE& t);
143
144 friend std::ostream& operator<<(std::ostream& os, const printable& p);
145};
146
160std::ostream& operator<<(std::ostream& os, const printable& p);
161
162} // namespace original
163
164
176 template<typename T>
178 struct std::formatter<T> { // NOLINT
179
185 static constexpr auto parse(format_parse_context& ctx);
186
193 static auto format(const T& p, format_context& ctx);
194 };
195
196// ----------------- Definitions of printable.h -----------------
197
198inline original::printable::~printable() = default;
199
200inline auto original::printable::className() const -> std::string
201{
202 return "printable";
203}
204
205inline auto original::printable::toString(const bool enter) const -> std::string
206{
207 std::stringstream ss;
208 ss << this->className() << "(" << formatString(this) << ")";
209 if (enter) ss << "\n";
210 return ss.str();
211}
212
213inline original::printable::operator std::string() const {
214 return this->toString(false);
215}
216
217inline original::printable::operator const char*() const {
218 return this->toCString(false);
219}
220
221inline auto original::printable::toCString(const bool enter) const -> const char*
222{
223 this->cache_string_ = this->toString(enter);
224 return this->cache_string_.c_str();
225}
226
227template<typename TYPE>
228auto original::printable::formatString(const TYPE& t) -> std::string
229{
230 if constexpr (std::is_enum_v<TYPE>) {
231 return formatEnum(t);
232 } else {
233 std::stringstream ss;
234 ss << t;
235 return ss.str();
236 }
237}
238
239template <typename TYPE>
240auto original::printable::formatCString(const TYPE& t) -> const char*
241{
242 static auto result =
243 std::make_unique<std::string>(formatString<TYPE>(t));
244 return result->c_str();
245}
246
247template <typename TYPE>
248auto original::printable::formatEnum(const TYPE& t) -> std::string
249{
250 const std::string enumName = typeid(t).name();
251 const int enumValue = static_cast<std::underlying_type_t<TYPE>>(t);
252 return enumName + "(" + std::to_string(enumValue) + ")";
253}
254
255template<>
256inline auto original::printable::formatString<std::nullptr_t>(const std::nullptr_t&) -> std::string
257{
258 return "nullptr";
259}
260
261template<>
262inline auto original::printable::formatString<std::string>(const std::string& t) -> std::string
263{
264 return "\"" + t + "\"";
265}
266
267template<>
268inline auto original::printable::formatString<char>(const char& t) -> std::string
269{
270 return "'" + std::string(1, t) + "'";
271}
272
273template<>
274inline auto original::printable::formatString<bool>(const bool& t) -> std::string
275{
276 return t != 0 ? "true" : "false";
277}
278
279template <typename TYPE>
280auto original::printable::formatString(TYPE* const& ptr) -> std::string
281{
282 if (!ptr)
283 return "nullptr";
284
285 std::stringstream ss;
286 ss << "@" << ptr;
287 return ss.str();
288}
289
290template<>
291inline auto original::printable::formatString<const char>(const char* const &ptr) -> std::string {
292 return formatString<std::string>(ptr);
293}
294
295inline std::ostream& original::operator<<(std::ostream& os, const printable& p){
296 os << p.toString(false);
297 return os;
298}
299
300template<typename T>
302constexpr auto std::formatter<T>::parse(std::format_parse_context &ctx) {
303 return ctx.begin();
304}
305
306template<typename T>
308auto std::formatter<T>::format(const T &p, std::format_context &ctx) {
309 return formatter<std::string>().format(static_cast<std::string>(p), ctx);
310}
311
312#endif // PRINTABLE_H
Base class providing polymorphic string conversion capabilities.
Definition printable.h:29
const char * toCString(bool enter) const
Direct C-string access with formatting control.
Definition printable.h:221
static std::string formatString(TYPE *const &ptr)
Pointer-specific formatting.
friend std::ostream & operator<<(std::ostream &os, const printable &p)
Stream insertion operator for printable objects.
virtual std::string toString(bool enter) const
Generates formatted string representation.
Definition printable.h:205
virtual std::string className() const
Gets the class name for type identification.
Definition printable.h:200
static const char * formatCString(const TYPE &t)
C-string cache for temporary usage.
static std::string formatEnum(const TYPE &t)
Enum formatting utility.
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Checks derivation or type identity.
Definition types.h:173
Main namespace for the project Original.
Definition algorithms.h:21
std::ostream & operator<<(std::ostream &os, const printable &p)
Stream insertion operator for printable objects.
Definition printable.h:295
static auto format(const T &p, format_context &ctx)
Formats the printable object.
Definition printable.h:308
static constexpr auto parse(format_parse_context &ctx)
Parses the format specification.
Definition printable.h:302
Core type system foundations and concept definitions.