Magic_Game/src/kiwano/core/ObjectBase.h

125 lines
3.2 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2019-12-25 18:07:57 +08:00
#include <kiwano/core/RefCounter.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/SmartPtr.hpp>
2020-01-21 10:09:55 +08:00
#include <kiwano/macros.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(ObjectBase);
2020-01-21 10:09:55 +08:00
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief
2020-01-21 10:09:55 +08:00
*/
class KGE_API ObjectBase : public RefCounter
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 构造基础对象
2020-01-21 10:09:55 +08:00
ObjectBase();
2020-01-21 10:09:55 +08:00
virtual ~ObjectBase();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置对象名
2020-02-19 12:09:50 +08:00
void SetName(const String& name);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取对象名
2020-01-21 10:09:55 +08:00
String GetName() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 判断对象的名称是否相同
/// @param name 需要判断的名称
2020-02-19 12:09:50 +08:00
bool IsName(const String& name) const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取用户数据
2020-01-21 10:09:55 +08:00
const Any& GetUserData() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置用户数据
2020-02-19 12:09:50 +08:00
void SetUserData(const Any& data);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取对象ID
2020-01-21 10:09:55 +08:00
uint32_t GetObjectID() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 序列化对象
2020-01-21 10:09:55 +08:00
String DumpObject();
2020-01-21 10:09:55 +08:00
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否启用了内存泄漏追踪
2020-01-21 10:09:55 +08:00
static bool IsTracingLeaks();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 开始追踪内存泄漏
2020-01-21 10:09:55 +08:00
static void StartTracingLeaks();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 停止追踪内存泄漏
2020-01-21 10:09:55 +08:00
static void StopTracingLeaks();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 打印所有追踪中的对象信息
2020-01-21 10:09:55 +08:00
static void DumpTracingObjects();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取所有追踪中的对象
2020-01-21 10:09:55 +08:00
static Vector<ObjectBase*>& GetTracingObjects();
2020-01-21 10:09:55 +08:00
private:
static void AddObjectToTracingList(ObjectBase*);
2020-01-21 10:09:55 +08:00
static void RemoveObjectFromTracingList(ObjectBase*);
2020-01-21 10:09:55 +08:00
private:
const uint32_t id_;
2020-02-20 22:27:09 +08:00
bool tracing_leak_;
Any user_data_;
std::unique_ptr<String> name_;
2020-01-21 10:09:55 +08:00
};
2019-12-25 18:07:57 +08:00
2020-01-21 10:09:55 +08:00
inline String ObjectBase::GetName() const
{
if (name_)
return *name_;
return String();
}
2019-12-25 18:07:57 +08:00
2020-02-19 12:09:50 +08:00
inline bool ObjectBase::IsName(const String& name) const
2020-01-21 10:09:55 +08:00
{
return name_ ? (*name_ == name) : name.empty();
}
2019-12-25 18:07:57 +08:00
2020-01-21 10:09:55 +08:00
inline uint32_t ObjectBase::GetObjectID() const
{
return id_;
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano