[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) , name_(nullptr)
, user_data_(nullptr) , user_data_(nullptr)
, status_(nullptr) , status_(nullptr)
, holdings_(nullptr)
, id_(++last_object_id) , id_(++last_object_id)
{ {
#ifdef KGE_DEBUG #ifdef KGE_DEBUG
@ -104,6 +105,12 @@ ObjectBase::~ObjectBase()
ClearStatus(); ClearStatus();
if (holdings_)
{
delete holdings_;
holdings_ = nullptr;
}
#ifdef KGE_DEBUG #ifdef KGE_DEBUG
ObjectBase::RemoveObjectFromTracingList(this); ObjectBase::RemoveObjectFromTracingList(this);
#endif #endif
@ -119,6 +126,23 @@ void ObjectBase::SetUserData(void* data)
user_data_ = 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) void ObjectBase::SetName(const String& name)
{ {
if (IsName(name)) if (IsName(name))

View File

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