[deploy] support holding objects

This commit is contained in:
Nomango 2022-06-07 15:46:14 +08:00
parent 0118220874
commit b8b1d78032
2 changed files with 37 additions and 1 deletions

View File

@ -87,6 +87,7 @@ ObjectBase::ObjectBase()
, name_(nullptr)
, user_data_(nullptr)
, status_(nullptr)
, holdings_(nullptr)
, id_(++last_object_id)
{
#ifdef KGE_DEBUG
@ -104,6 +105,12 @@ ObjectBase::~ObjectBase()
ClearStatus();
if (holdings_)
{
delete holdings_;
holdings_ = nullptr;
}
#ifdef KGE_DEBUG
ObjectBase::RemoveObjectFromTracingList(this);
#endif
@ -119,6 +126,23 @@ void ObjectBase::SetUserData(void* data)
user_data_ = data;
}
void ObjectBase::Hold(ObjectBasePtr other)
{
if (!holdings_)
{
holdings_ = new Set<ObjectBasePtr>;
}
holdings_->insert(other);
}
void ObjectBase::Unhold(ObjectBasePtr other)
{
if (holdings_)
{
holdings_->erase(other);
}
}
void ObjectBase::SetName(const String& name)
{
if (IsName(name))

View File

@ -161,8 +161,19 @@ public:
/// \~chinese
/// @brief 设置用户数据
/// @param data 数据指针
void SetUserData(void* data);
/// \~chinese
/// @brief 持有一个对象并管理其生命周期
/// @param other 对象指针
void Hold(ObjectBasePtr other);
/// \~chinese
/// @brief 放弃持有的对象
/// @param other 对象指针
void Unhold(ObjectBasePtr other);
/// \~chinese
/// @brief 获取对象ID
uint64_t GetObjectID() const;
@ -233,6 +244,7 @@ private:
void* user_data_;
ObjectStatus* status_;
Set<ObjectBasePtr>* holdings_;
};
inline String ObjectBase::GetName() const