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 <cstddef>
14
15namespace ksf
16{
25 class ksRtti
26 {
27 public:
31 virtual ~ksRtti() = default;
32
37 virtual const std::size_t getInstanceType() const = 0;
38
44 virtual bool isA(const std::size_t id) const
45 {
46 return false;
47 }
48
54 template <typename TType>
55 TType* as()
56 {
57 if (isA(TType::getClassType()))
58 return static_cast<TType*>(this);
59
60 return nullptr;
61 }
62
68 template <typename TType>
69 const TType* as() const
70 {
71 if (isA(TType::getClassType()))
72 return static_cast<const TType*>(this);
73
74 return nullptr;
75 }
76 };
77
78 #define KSF_RTTI_DECLARATIONS(_Type, _ParentType) \
79 public: \
80 virtual const std::size_t getInstanceType() const \
81 { \
82 return _Type::getClassType(); \
83 } \
84 static const std::size_t getClassType() \
85 { \
86 static int d{0}; return (std::size_t)&d; \
87 } \
88 virtual bool isA(const std::size_t id) const \
89 { \
90 if (id == getClassType()) \
91 return true; \
92 else \
93 return _ParentType::isA(id); \
94 } \
95 private:
96}
Implements RTTI (run-time type information) for objects.
Definition ksRtti.h:26
virtual const std::size_t getInstanceType() const =0
Retrieves type ID of the object.
const TType * as() const
Tries to cast object to the type provided as a template parameter (const version).
Definition ksRtti.h:69
TType * as()
Tries to cast object to the type provided as a template parameter.
Definition ksRtti.h:55
virtual bool isA(const std::size_t id) const
Checks whether object is of given type.
Definition ksRtti.h:44
virtual ~ksRtti()=default
Destructor.