Kiwano Engine  v1.2.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 
29 namespace kiwano
30 {
31 KGE_DECLARE_SMART_PTR(ObjectBase);
32 
38 {
39  int code = 0;
40  String msg;
41 
42  ObjectStatus() = default;
43 
44  ObjectStatus(int code, const String& msg)
45  : code(code)
46  , msg(msg)
47  {
48  }
49 
52  inline bool Success() const
53  {
54  return this->code == 0;
55  }
56 
59  static const int fail = -1;
60 };
61 
66 class ObjectFailException : public Exception
67 {
68 public:
69  ObjectFailException(ObjectBase* obj, const ObjectStatus& status);
70 
73  inline ObjectBase* GetObj() const
74  {
75  return obj_;
76  }
77 
80  inline ObjectStatus GetStatus() const
81  {
82  return status_;
83  }
84 
85  virtual char const* what() const override;
86 
87 private:
88  ObjectBase* obj_;
89  ObjectStatus status_;
90 };
91 
97 
103 {
106  static inline ObjectPolicyFunc Ignore()
107  {
108  return nullptr;
109  }
110 
115  static ObjectPolicyFunc WarnLog(int threshold = ObjectStatus::fail);
116 
121  static ObjectPolicyFunc ErrorLog(int threshold = ObjectStatus::fail);
122 
127  static ObjectPolicyFunc Exception(int threshold = ObjectStatus::fail);
128 };
129 
134 class KGE_API ObjectBase
135  : public RefObject
136  , public Serializable
137 {
138 public:
141  ObjectBase();
142 
143  virtual ~ObjectBase();
144 
147  void SetName(const String& name);
148 
151  String GetName() const;
152 
156  bool IsName(const String& name) const;
157 
160  void* GetUserData() const;
161 
164  void SetUserData(void* data);
165 
168  uint64_t GetObjectID() const;
169 
172  void DoSerialize(Serializer* serializer) const override;
173 
176  void DoDeserialize(Deserializer* deserializer) override;
177 
180  virtual bool IsValid() const;
181 
184  ObjectStatus* GetStatus() const;
185 
188  void SetStatus(const ObjectStatus& status);
189 
192  void Fail(const String& msg, int code = ObjectStatus::fail);
193 
196  void ClearStatus();
197 
200  static void SetObjectPolicy(const ObjectPolicyFunc& policy);
201 
202 public:
205  static bool IsTracingLeaks();
206 
209  static void StartTracingLeaks();
210 
213  static void StopTracingLeaks();
214 
217  static void DumpTracingObjects();
218 
221  static Vector<ObjectBase*>& GetTracingObjects();
222 
223 private:
224  static void AddObjectToTracingList(ObjectBase*);
225 
226  static void RemoveObjectFromTracingList(ObjectBase*);
227 
228 private:
229  const uint64_t id_;
230 
231  bool tracing_leak_;
232  String* name_;
233  void* user_data_;
234 
235  ObjectStatus* status_;
236 };
237 
238 inline String ObjectBase::GetName() const
239 {
240  if (name_)
241  return *name_;
242  return String();
243 }
244 
245 inline bool ObjectBase::IsName(const String& name) const
246 {
247  return name_ ? (*name_ == name) : name.empty();
248 }
249 
250 inline uint64_t ObjectBase::GetObjectID() const
251 {
252  return id_;
253 }
254 } // namespace kiwano
String GetName() const
获取对象名
Definition: ObjectBase.h:238
bool IsName(const String &name) const
判断对象的名称是否相同
Definition: ObjectBase.h:245
int code
状态码,等于 0 时为成功状态,否则为失败状态
Definition: ObjectBase.h:39
可序列化对象
Definition: Serializable.h:238
序列化器
Definition: Serializable.h:39
引用计数器
Definition: RefObject.h:32
static const int fail
对象失败状态
Definition: ObjectBase.h:59
Definition: Function.h:228
Definition: Actor.cpp:26
对象状态
Definition: ObjectBase.h:37
String msg
状态信息
Definition: ObjectBase.h:40
bool Success() const
对象状态是否成功
Definition: ObjectBase.h:52
uint64_t GetObjectID() const
获取对象ID
Definition: ObjectBase.h:250
ObjectBase * GetObj() const
获取失败的对象指针
Definition: ObjectBase.h:73
ObjectStatus GetStatus() const
获取对象状态
Definition: ObjectBase.h:80
对象失败状态异常
Definition: ObjectBase.h:66
static ObjectPolicyFunc Ignore()
忽略对象失败状态
Definition: ObjectBase.h:106
反序列化器
Definition: Serializable.h:146
对象处理策略
Definition: ObjectBase.h:102
基础对象
Definition: ObjectBase.h:134