载入中...
搜索中...
未找到
Object.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Assert.h"
4
5#include <type_traits>
6
7namespace eve
8{
9
10class Object;
11template <typename T>
12concept DerivedFromObject = std::is_base_of_v<eve::Object, T>;
13
18class Object {
19public:
20 Object() { update = this; }
21
28 EV_PARAM_CHECK(update != nullptr, "replacement object must not be null");
29 this->update = update;
30 }
31
32protected:
33 virtual ~Object() {}
34
35 template <typename T>
36 friend class ref;
37
38 bool isDirty() const { return this != update; }
39 Object *getUpdate() const { return update; }
40
41 void ref() { ++ref_count; }
42 void unref() { if (--ref_count == 0) delete this; }
43
45 unsigned ref_count = 0;
46};
47
48
53template <typename T>
54class ref {
55public:
56 static_assert( DerivedFromObject<T> );
57
58 // Default constructor creates a null pointer.
59 ref() : ptr(nullptr) {}
60
61 // Constructor from a raw pointer.
62 ref(T* ptr) : ptr(ptr) { ptr->ref(); }
63
64 // Copy constructor.
65 ref(const ref& other) : ptr(other.ptr) { ptr->ref(); }
66
67 // Destructor. will call unref() on the object.
68 ~ref() { ptr->unref(); }
69
70 // Assignment operator. The old object will be unref() and the new one will be ref().
71 ref& operator=(const ref& other) {
72 if (ptr != other.ptr) {
73 ptr->unref();
74 ptr = other.ptr;
75 ptr->ref();
76 }
77 return *this;
78 }
79
80 // Assignment operator from a raw pointer.
81 ref& operator=(T* other) {
82 if (ptr != other) {
83 ptr->unref();
84 ptr = other;
85 ptr->ref();
86 }
87 return *this;
88 }
89
90 operator T*() { checkDirty(); return ptr; }
91 T* operator->() { checkDirty(); return ptr; }
92 T& operator*() { checkDirty(); return *ptr; }
93
94 T* get() { checkDirty(); return ptr; }
95protected:
96 // getUpdate() returns the base Object*; the update contract guarantees
97 // the replacement is actually a T*, so a downcast here is safe.
98 void checkDirty() { if (ptr->isDirty()) ptr = static_cast<T*>(ptr->getUpdate()); }
99
100 T* ptr;
101};
102
103
104} // namespace eve
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
Object is the base class for all game objects. It provides reference counting and dirty flag for upda...
Definition Object.h:18
Object * update
Definition Object.h:44
virtual ~Object()
Definition Object.h:33
unsigned ref_count
Definition Object.h:45
void setUpdate(Object *update)
Update the current object with a replacement. You don't need to care about the reference to the old o...
Definition Object.h:27
bool isDirty() const
Definition Object.h:38
void unref()
Definition Object.h:42
void ref()
Definition Object.h:41
Object * getUpdate() const
Definition Object.h:39
ref is a smart pointer that automatically calls ref() and unref() on the object. Type T must be a sub...
Definition Object.h:54
ref()
Definition Object.h:59
T * ptr
Definition Object.h:100
ref & operator=(T *other)
Definition Object.h:81
ref(T *ptr)
Definition Object.h:62
~ref()
Definition Object.h:68
ref & operator=(const ref &other)
Definition Object.h:71
T * operator->()
Definition Object.h:91
T * get()
Definition Object.h:94
ref(const ref &other)
Definition Object.h:65
T & operator*()
Definition Object.h:92
void checkDirty()
Definition Object.h:98
Definition Build.cpp:11