Uniot Core
0.8.1
Loading...
Searching...
No Matches
TypeId.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2025 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
29
30
31#pragma once
32
33namespace uniot {
34
43typedef const void* type_id;
44
52class IWithType {
53 public:
58 virtual type_id getTypeId() const = 0;
59};
60
61
70class Type {
71 public:
81 template <class T>
82 static inline type_id getTypeId() {
83 static T* TypeUniqueMarker = nullptr;
84 return &TypeUniqueMarker;
85 }
86
98 template <typename T>
99 static inline T* safeStaticCast(IWithType* obj) {
100 if (obj->getTypeId() == Type::getTypeId<T>()) {
101 return static_cast<T*>(obj);
102 }
103 UNIOT_LOG_DEBUG("cast failed from [%lu] to [%lu]", obj->getTypeId(), Type::getTypeId<T>());
104 return nullptr;
105 }
106};
107
108}
Interface for objects that expose their runtime type information.
Definition TypeId.h:52
virtual type_id getTypeId() const =0
Get the runtime type identifier for this object.
Utility class providing static methods for runtime type identification and safe casting.
Definition TypeId.h:70
static type_id getTypeId()
Get the unique type identifier for a given type T.
Definition TypeId.h:82
static T * safeStaticCast(IWithType *obj)
Safely cast a pointer to type T if the runtime type matches.
Definition TypeId.h:99
#define UNIOT_LOG_DEBUG(...)
Log an DEBUG level message Used for general information about system operation. Only compiled if UNIO...
Definition Logger.h:293
const void * type_id
Type alias for a unique type identifier.
Definition TypeId.h:43
Contains all classes and functions related to the Uniot Core.