Doxygen with github
Loading...
Searching...
No Matches
ksRtti.h
1/*
2 * Original author: Paul Varcholik
3 * Additional authors: Shao Voon Wong, Krzysztof Strehlau
4 *
5 * This file is a part of the ksIotFramework library.
6 * All licensing information can be found inside LICENSE.md file.
7 *
8 * https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
9 */
10
11#pragma once
12
13#include <stddef.h>
14
15namespace ksf
16{
25 class ksRtti
26 {
27 public:
32 virtual const size_t getInstanceType() const = 0;
33
39 virtual bool isA(const size_t id) const
40 {
41 return false;
42 }
43
49 template <typename TType>
50 TType* as()
51 {
52 if (is(TType::getClassType()))
53 return (TType*)this;
54
55 return nullptr;
56 }
57
63 template <typename TType>
64 const TType* as() const
65 {
66 if (is(TType::getClassType()))
67 return (TType*)this;
68
69 return nullptr;
70 }
71 };
72
73 #define KSF_RTTI_DECLARATIONS(_Type, _ParentType) \
74 public: \
75 virtual const size_t getInstanceType() const \
76 { \
77 return _Type::getClassType(); \
78 } \
79 static const size_t getClassType() \
80 { \
81 static int d{0}; return (size_t)&d; \
82 } \
83 virtual bool isA(const size_t id) const \
84 { \
85 if (id == getClassType()) \
86 return true; \
87 else \
88 return _ParentType::isA(id); \
89 } \
90 private:
91}
Implements RTTI (run-time type information) for objects.
Definition ksRtti.h:26
const TType * as() const
Tries to cast object to the type provided as a template parameter (const version).
Definition ksRtti.h:64
TType * as()
Tries to cast object to the type provided as a template parameter.
Definition ksRtti.h:50
virtual bool isA(const size_t id) const
Checks whether object is of given type.
Definition ksRtti.h:39
virtual const size_t getInstanceType() const =0
Retrieves type ID of the object.