-
Notifications
You must be signed in to change notification settings - Fork 6
/
Object.h
67 lines (57 loc) · 2.44 KB
/
Object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
class Context;
class Subsystem;
#define FLUX_OBJECT(typeName, baseTypeName) \
public: \
using ClassName = typeName; \
using BaseClass = baseTypeName; \
virtual StringHash GetType() const override { return GetTypeInfoStatic()->GetType(); } \
virtual const std::string& GetTypeName() const override { return GetTypeInfoStatic()->GetTypeName(); } \
virtual const TypeInfo* GetTypeInfo() const override { return GetTypeInfoStatic(); } \
static StringHash GetTypeStatic() { return GetTypeInfoStatic()->GetType(); } \
static const std::string& GetTypeNameStatic() { return GetTypeInfoStatic()->GetTypeName(); } \
static const TypeInfo* GetTypeInfoStatic() { static const TypeInfo typeInfoStatic(#typeName, baseTypeName::GetTypeInfoStatic()); return &typeInfoStatic; } \
private: \
virtual void NOTYPEINFO() override {} \
class Object
{
public:
Object(Context* pContext) : m_pContext(pContext) {}
virtual ~Object() {}
virtual StringHash GetType() const { return GetTypeInfoStatic()->GetType(); }
virtual const std::string& GetTypeName() const { return GetTypeInfoStatic()->GetTypeName(); }
virtual const TypeInfo* GetTypeInfo() const { return GetTypeInfoStatic(); }
static StringHash GetTypeStatic() { return GetTypeInfoStatic()->GetType(); }
static const std::string& GetTypeNameStatic() { return GetTypeInfoStatic()->GetTypeName(); }
static const TypeInfo* GetTypeInfoStatic() { static const TypeInfo typeInfoStatic("Object", nullptr); return &typeInfoStatic; }
bool IsTypeOf(const TypeInfo* pTypeInfo) const;
bool IsTypeOf(StringHash type) const;
template<typename T>
inline bool IsTypeOf() const { return IsTypeOf(T::GetTypeInfoStatic()); }
Subsystem* GetSubsystem(StringHash type) const;
template<typename T>
T* GetSubsystem(bool required = true) const { return m_pContext->GetSubsystem<T>(required); }
Context* GetContext() const { return m_pContext; }
protected:
//This enforces the FLUX_OBJECT on all classes that inherit from Object
virtual void NOTYPEINFO() = 0;
Context* m_pContext = nullptr;
};
template<typename T>
T* DynamicCast(Object* pObject)
{
if (pObject->GetTypeInfo()->IsTypeOf<T>())
{
return static_cast<T*>(pObject);
}
return nullptr;
}
template<typename T>
std::shared_ptr<T> DynamicCastPtr(const std::shared_ptr<Object>& pObject)
{
if (pObject->GetTypeInfo()->IsTypeOf<T>())
{
return std::static_pointer_cast<T>(pObject);
}
return std::shared_ptr<T>();
}