Kiwano Engine v1.3.x
ObjectBase.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/macros.h>
23#include <kiwano/core/Common.h>
24#include <kiwano/core/Exception.h>
25#include <kiwano/core/Serializable.h>
26#include <kiwano/base/RefObject.h>
27#include <kiwano/base/RefPtr.h>
28
29namespace kiwano
30{
31
32class ObjectBase;
33
39{
40 int code = 0;
41 String msg;
42
43 ObjectStatus() = default;
44
46 : code(code)
47 , msg(msg)
48 {
49 }
50
53 inline bool Success() const
54 {
55 return this->code == 0;
56 }
57
60 static const int fail = -1;
61};
62
67class ObjectFailException : public Exception
68{
69public:
70 ObjectFailException(ObjectBase* obj, const ObjectStatus& status);
71
74 inline ObjectBase* GetObj() const
75 {
76 return obj_;
77 }
78
81 inline ObjectStatus GetStatus() const
82 {
83 return status_;
84 }
85
86 virtual char const* what() const override;
87
88private:
89 ObjectBase* obj_;
90 ObjectStatus status_;
91};
92
97typedef Function<void(ObjectBase*, const ObjectStatus&)> ObjectPolicyFunc;
98
104{
107 static inline ObjectPolicyFunc Ignore()
108 {
109 return nullptr;
110 }
111
116 static ObjectPolicyFunc WarnLog(int threshold = ObjectStatus::fail);
117
122 static ObjectPolicyFunc ErrorLog(int threshold = ObjectStatus::fail);
123
128 static ObjectPolicyFunc Exception(int threshold = ObjectStatus::fail);
129};
130
135class KGE_API ObjectBase
136 : public RefObject
137 , public Serializable
138{
139public:
142 ObjectBase();
143
144 virtual ~ObjectBase();
145
148 void SetName(StringView name);
149
152 StringView GetName() const;
153
157 bool IsName(StringView name) const;
158
161 void* GetUserData() const;
162
166 void SetUserData(void* data);
167
171 void Hold(RefPtr<ObjectBase> other);
172
176 void Unhold(RefPtr<ObjectBase> other);
177
180 uint64_t GetObjectID() const;
181
184 void DoSerialize(Serializer* serializer) const override;
185
188 void DoDeserialize(Deserializer* deserializer) override;
189
192 virtual bool IsValid() const;
193
196 ObjectStatus* GetStatus() const;
197
200 void SetStatus(const ObjectStatus& status);
201
204 void Fail(StringView msg, int code = ObjectStatus::fail);
205
208 void ClearStatus();
209
212 static void SetObjectPolicy(const ObjectPolicyFunc& policy);
213
214public:
217 static bool IsTracingLeaks();
218
221 static void StartTracingLeaks();
222
225 static void StopTracingLeaks();
226
229 static void DumpTracingObjects();
230
233 static Vector<ObjectBase*>& GetTracingObjects();
234
235private:
236 static void AddObjectToTracingList(ObjectBase*);
237
238 static void RemoveObjectFromTracingList(ObjectBase*);
239
240private:
241 const uint64_t id_;
242
243 bool tracing_leak_;
244 String* name_;
245 void* user_data_;
246
247 ObjectStatus* status_;
248 Set<RefPtr<ObjectBase>>* holdings_;
249};
250
252{
253 if (name_)
254 return StringView(*name_);
255 return StringView();
256}
257
258inline bool ObjectBase::IsName(StringView name) const
259{
260 return name_ ? (*name_ == name) : name.empty();
261}
262
263inline uint64_t ObjectBase::GetObjectID() const
264{
265 return id_;
266}
267} // namespace kiwano
Definition: Function.h:228
基础对象
Definition: ObjectBase.h:138
StringView GetName() const
获取对象名
Definition: ObjectBase.h:251
uint64_t GetObjectID() const
获取对象ID
Definition: ObjectBase.h:263
bool IsName(StringView name) const
判断对象的名称是否相同
Definition: ObjectBase.h:258
对象失败状态异常
Definition: ObjectBase.h:68
ObjectBase * GetObj() const
获取失败的对象指针
Definition: ObjectBase.h:74
ObjectStatus GetStatus() const
获取对象状态
Definition: ObjectBase.h:81
引用计数智能指针
Definition: RefBasePtr.hpp:35
引用计数器
Definition: RefObject.h:33
可序列化对象
Definition: Serializable.h:239
反序列化器
Definition: Serializable.h:147
对象处理策略
Definition: ObjectBase.h:104
static ObjectPolicyFunc WarnLog(int threshold=ObjectStatus::fail)
在对象状态变为失败时打印警告日志
Definition: ObjectBase.cpp:50
static ObjectPolicyFunc Ignore()
忽略对象失败状态
Definition: ObjectBase.h:107
static ObjectPolicyFunc Exception(int threshold=ObjectStatus::fail)
在对象状态变为失败时抛出 ObjectFailException
Definition: ObjectBase.cpp:72
static ObjectPolicyFunc ErrorLog(int threshold=ObjectStatus::fail)
在对象状态变为失败时打印错误日志(默认策略)
Definition: ObjectBase.cpp:61
对象状态
Definition: ObjectBase.h:39
String msg
状态信息
Definition: ObjectBase.h:41
static const int fail
对象失败状态
Definition: ObjectBase.h:60
bool Success() const
对象状态是否成功
Definition: ObjectBase.h:53
int code
状态码,等于 0 时为成功状态,否则为失败状态
Definition: ObjectBase.h:40
序列化器
Definition: Serializable.h:40