Polyphase Game Engine
Loading...
Searching...
No Matches
Object.h
Go to the documentation of this file.
1#pragma once
2
3#include "PolyphaseAPI.h"
4#include "Utilities.h"
5#include "EngineTypes.h"
6#include "ScriptAutoReg.h"
7#include <string>
8#include <stdint.h>
9
10POLYPHASE_API int CreateClassMetatable(const char* className, const char* classFlag, const char* parentClassName);
11
13{
14public:
15 virtual ~Object() = default;
16
17 virtual const char* RuntimeName() const = 0;
18 virtual const char* RuntimeParentName() const = 0;
19 virtual RuntimeId InstanceRuntimeId() const = 0;
20
21 static const char* ClassRuntimeName() { return "Object"; }
22
23 virtual Object* QueryInterface(RuntimeId id) const
24 {
25 OCT_UNUSED(id);
26 return nullptr;
27 }
28
29 virtual bool Is(RuntimeId id) const
30 {
31 OCT_UNUSED(id);
32 return false;
33 }
34
35 virtual bool Is(const char* name) const
36 {
37 OCT_UNUSED(name);
38 return false;
39 }
40
41 virtual void GatherProperties(std::vector<Property>& props)
42 {
43
44 }
45
46 virtual bool DrawCustomProperty(Property& prop)
47 {
48 return false;
49 }
50
51 template <typename T>
52 T* As() const
53 {
54 if (Is(T::ClassRuntimeId()))
55 {
56 return (T*)this;
57 }
58
59 return nullptr;
60 }
61
62 virtual bool Equals(const Object* rhs) const
63 {
64 return this == rhs;
65 }
66};
67
68template <typename T>
69T* Cast(Object* object)
70{
71 if (object && object->Is(T::ClassRuntimeId()))
72 {
73 return (T*)object;
74 }
75
76 return nullptr;
77}
78
79template <typename T, typename U>
81{
82 if (object && object->Is(T::ClassRuntimeId()))
83 {
84 return PtrStaticCast<T>(object);
85 }
86
87 return nullptr;
88}
89
90template <typename T, typename U>
92{
93 if (object && object->Is(T::ClassRuntimeId()))
94 {
95 return PtrStaticCast<T>(object);
96 }
97
98 return nullptr;
99}
100
101#define DECLARE_OBJECT(Type, ParentType) \
102 public: \
103 static const char* ClassRuntimeName() { return #Type; } \
104 virtual const char* RuntimeName() const override { return Type::ClassRuntimeName(); } \
105 virtual const char* RuntimeParentName() const override { return ParentType::ClassRuntimeName(); } \
106 static RuntimeId ClassRuntimeId() { return sRuntimeId; } \
107 virtual RuntimeId InstanceRuntimeId() const override { return Type::ClassRuntimeId(); } \
108 virtual Object* QueryInterface(RuntimeId id) const override \
109 { \
110 if (id == sRuntimeId) \
111 { return (Object*)this; } \
112 else \
113 { return ParentType::QueryInterface(id); } \
114 } \
115 virtual bool Is(RuntimeId id) const override \
116 { \
117 if (id == sRuntimeId) \
118 { return true; } \
119 else \
120 { return ParentType::Is(id); } \
121 } \
122 virtual bool Is(const char* name) const override \
123 { \
124 if (strncmp(name, ClassRuntimeName(), 256) == 0) \
125 { return true; } \
126 else \
127 { return ParentType::Is(name); } \
128 } \
129 private: \
130 static RuntimeId sRuntimeId; \
131 public:
132
133#define DEFINE_OBJECT(Type) RuntimeId Type::sRuntimeId = reinterpret_cast<RuntimeId>(&Type::sRuntimeId);
uint64_t RuntimeId
Definition EngineTypes.h:67
POLYPHASE_API int CreateClassMetatable(const char *className, const char *classFlag, const char *parentClassName)
T * Cast(Object *object)
Definition Object.h:69
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
#define OCT_UNUSED(var)
Definition Utilities.h:17
Definition Object.h:13
virtual bool DrawCustomProperty(Property &prop)
Definition Object.h:46
virtual const char * RuntimeParentName() const =0
virtual ~Object()=default
virtual const char * RuntimeName() const =0
virtual void GatherProperties(std::vector< Property > &props)
Definition Object.h:41
virtual RuntimeId InstanceRuntimeId() const =0
static const char * ClassRuntimeName()
Definition Object.h:21
virtual bool Equals(const Object *rhs) const
Definition Object.h:62
virtual Object * QueryInterface(RuntimeId id) const
Definition Object.h:23
virtual bool Is(RuntimeId id) const
Definition Object.h:29
virtual bool Is(const char *name) const
Definition Object.h:35
T * As() const
Definition Object.h:52
Definition Property.h:14
Definition SmartPointer.h:33
Definition SmartPointer.h:312