λ61
Loading...
Searching...
No Matches
Object.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Tetex7
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18//
19// Created by tete on 05/13/2025.
20//
21#pragma once
22
23#ifndef L61_OBJECT_HPP
24#define L61_OBJECT_HPP
25
26#include <string>
27#include <type_traits>
28#include <utility>
29#include <typeinfo>
30#include <format>
31#include "json.hpp"
32
33namespace l61
34{
35 class Object;
36
37 namespace meta
38 {
39 template<class T>
40 concept l61Obj = std::derived_from<std::remove_cvref_t<T>, Object>;
41
42 template<class T>
43 concept l61ObjPtr = std::derived_from<Object, std::remove_pointer_t<T>>;
44 }
45
46 class Object
47 {
48 public:
49 virtual ~Object() = default;
50
60 virtual std::string toString() const;
61
72 virtual std::size_t hashCode() const;
73
83 std::string typeName() const;
84
94 const std::type_info& typeInfo() const;
95
105 virtual nlohmann::json toJsonValue() const;
106
120 explicit operator std::string() const;
121
129 explicit operator nlohmann::json() const;
130 };
131
132 namespace meta
133 {
148 template<l61Obj T, l61Obj Ty>
149 bool instanceof(const Ty& value)
150 {
151 return dynamic_cast<const T*>(&value) != nullptr;
152 }
153
167 template<l61Obj T, l61Obj Ty>
168 bool exact_instanceof(const Ty& value)
169 {
170 return typeid(T) == value.typeInfo();
171 }
172
173
187 template<l61Obj T, l61Obj Ty>
188 constexpr bool static_instanceof(const Ty&)
189 {
190 return std::derived_from<Ty, std::type_identity_t<T>>;
191 }
192 }
193}
194
195template <l61::meta::l61Obj T>
196struct nlohmann::adl_serializer<T>
197{
198 static void to_json(json& j, const T& obj) {
199 if (obj)
200 {
201 j = obj.toJsonValue();
202 }
203 else
204 {
205 j = nullptr;
206 }
207 }
208};
209
210template <l61::meta::l61Obj T>
211struct std::formatter<T> : formatter<std::string> {
212 auto format(const T& obj, std::format_context& ctx) const {
213 return std::formatter<std::string>::format(
214 static_cast<const l61::Object&>(obj).toString(),
215 ctx
216 );
217 }
218};
219
220template <l61::meta::l61Obj T>
221struct std::hash<T>
222{
223 std::size_t operator()(const T& obj) const noexcept
224 {
225 return obj.hashCode();
226 }
227};
228
229#endif //L61_OBJECT_HPP
Definition Object.hpp:47
virtual std::size_t hashCode() const
Returns a hash code for the object.
Definition Object.cpp:55
std::string typeName() const
Returns the demangled name of the type.
Definition Object.cpp:60
virtual nlohmann::json toJsonValue() const
Serializes the object into JSON form.
Definition Object.cpp:70
virtual std::string toString() const
Returns a human-readable string representation of the object.
Definition Object.cpp:50
const std::type_info & typeInfo() const
Returns RTTI type information object.
Definition Object.cpp:65
virtual ~Object()=default
Definition Object.hpp:43
Definition Object.hpp:40
Definition Object.hpp:38
bool instanceof(const Ty &value)
Checks if a value is an instance of type T (or derived from T).
Definition Object.hpp:149
constexpr bool static_instanceof(const Ty &)
Checks if a value is an instance of type T (or derived from T) at compile-time.
Definition Object.hpp:188
bool exact_instanceof(const Ty &value)
Checks if a value is exactly of type T.
Definition Object.hpp:168
Definition Object.hpp:34
static void to_json(json &j, const T &obj)
Definition Object.hpp:198
auto format(const T &obj, std::format_context &ctx) const
Definition Object.hpp:212
std::size_t operator()(const T &obj) const noexcept
Definition Object.hpp:223