Magic_Game/src/kiwano/base/ObjectBase.cpp

262 lines
5.8 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.
2020-07-26 00:22:32 +08:00
#include <typeinfo>
#include <atomic>
2020-05-24 11:26:21 +08:00
#include <kiwano/base/ObjectBase.h>
#include <kiwano/utils/Logger.h>
2020-05-20 23:48:56 +08:00
#include <kiwano/utils/Json.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace
{
2020-07-26 00:22:32 +08:00
bool tracing_leaks = false;
Vector<ObjectBase*> tracing_objects;
std::atomic<uint64_t> last_object_id = 0;
ObjectPolicyFunc object_policy_ = ObjectPolicy::ErrorLog();
2020-01-21 10:09:55 +08:00
} // namespace
2020-07-26 00:22:32 +08:00
ObjectFailException::ObjectFailException(ObjectBase* obj, const ObjectStatus& status)
: obj_(obj)
, status_(status)
{
}
char const* ObjectFailException::what() const
{
return status_.msg.empty() ? "Object operation failed" : status_.msg.c_str();
}
2020-07-26 10:38:41 +08:00
ObjectPolicyFunc ObjectPolicy::WarnLog(int threshold)
2020-07-26 00:22:32 +08:00
{
2020-07-26 10:38:41 +08:00
return [=](ObjectBase* obj, const ObjectStatus& status)
2020-07-26 00:22:32 +08:00
{
2020-07-26 10:38:41 +08:00
if (!obj->IsValid() || status.code <= threshold)
2020-07-26 00:22:32 +08:00
{
KGE_WARNF("Object operation failed: obj(%p), code(%d), msg(%s)", obj, status.code, status.msg.c_str());
}
};
}
2020-07-26 10:38:41 +08:00
ObjectPolicyFunc ObjectPolicy::ErrorLog(int threshold)
2020-07-26 00:22:32 +08:00
{
2020-07-26 10:38:41 +08:00
return [=](ObjectBase* obj, const ObjectStatus& status)
2020-07-26 00:22:32 +08:00
{
2020-07-26 10:38:41 +08:00
if (!obj->IsValid() || status.code <= threshold)
2020-07-26 00:22:32 +08:00
{
KGE_ERRORF("Object operation failed: obj(%p), code(%d), msg(%s)", obj, status.code, status.msg.c_str());
}
};
}
2020-07-26 10:38:41 +08:00
ObjectPolicyFunc ObjectPolicy::Exception(int threshold)
2020-07-26 00:22:32 +08:00
{
2020-07-26 10:38:41 +08:00
return [=](ObjectBase* obj, const ObjectStatus& status)
2020-07-26 00:22:32 +08:00
{
2020-07-26 10:38:41 +08:00
if (!obj->IsValid() || status.code <= threshold)
2020-07-26 00:22:32 +08:00
{
throw ObjectFailException(obj, status);
}
};
}
2020-01-21 10:09:55 +08:00
ObjectBase::ObjectBase()
: tracing_leak_(false)
2020-02-22 19:44:50 +08:00
, name_(nullptr)
2020-07-26 00:22:32 +08:00
, user_data_(nullptr)
2020-07-26 10:38:41 +08:00
, status_(nullptr)
2020-01-21 10:09:55 +08:00
, id_(++last_object_id)
{
2019-04-11 14:40:54 +08:00
#ifdef KGE_DEBUG
2020-01-21 10:09:55 +08:00
ObjectBase::AddObjectToTracingList(this);
#endif
2020-01-21 10:09:55 +08:00
}
2020-01-21 10:09:55 +08:00
ObjectBase::~ObjectBase()
{
2020-02-22 19:44:50 +08:00
if (name_)
{
delete name_;
name_ = nullptr;
}
2020-07-26 10:38:41 +08:00
ClearStatus();
2019-04-11 14:40:54 +08:00
#ifdef KGE_DEBUG
2020-01-21 10:09:55 +08:00
ObjectBase::RemoveObjectFromTracingList(this);
#endif
2020-01-21 10:09:55 +08:00
}
2020-07-26 00:22:32 +08:00
void* ObjectBase::GetUserData() const
2020-01-21 10:09:55 +08:00
{
return user_data_;
}
2020-07-26 00:22:32 +08:00
void ObjectBase::SetUserData(void* data)
2020-01-21 10:09:55 +08:00
{
user_data_ = data;
}
2020-02-19 12:09:50 +08:00
void ObjectBase::SetName(const String& name)
2020-01-21 10:09:55 +08:00
{
if (IsName(name))
return;
if (name.empty())
{
if (name_)
name_->clear();
return;
}
if (!name_)
{
2020-02-22 19:44:50 +08:00
name_ = new String(name);
2020-01-21 10:09:55 +08:00
return;
}
*name_ = name;
}
2020-04-15 16:37:58 +08:00
void ObjectBase::DoSerialize(Serializer* serializer) const
2020-01-21 10:09:55 +08:00
{
2020-04-15 16:37:58 +08:00
(*serializer) << GetName();
}
void ObjectBase::DoDeserialize(Deserializer* deserializer)
{
String name;
(*deserializer) >> name;
SetName(name);
2020-01-21 10:09:55 +08:00
}
2020-07-26 00:22:32 +08:00
bool ObjectBase::IsValid() const
{
2020-07-26 10:38:41 +08:00
return status_ ? status_->Success() : true;
2020-07-26 00:22:32 +08:00
}
2020-07-26 10:38:41 +08:00
ObjectStatus* ObjectBase::GetStatus() const
2020-07-26 00:22:32 +08:00
{
return status_;
}
void ObjectBase::SetStatus(const ObjectStatus& status)
{
2020-07-26 10:38:41 +08:00
if (!status_)
{
status_ = new ObjectStatus;
}
status_->msg = status.msg;
if (status_->code != status.code)
2020-07-26 00:22:32 +08:00
{
2020-07-26 10:38:41 +08:00
status_->code = status.code;
2020-07-26 00:22:32 +08:00
if (object_policy_)
{
2020-07-26 10:38:41 +08:00
object_policy_(this, *status_);
2020-07-26 00:22:32 +08:00
}
}
}
void ObjectBase::Fail(const String& msg, int code)
{
2020-07-26 11:51:27 +08:00
SetStatus(ObjectStatus(code, msg));
2020-07-26 00:22:32 +08:00
}
void ObjectBase::ClearStatus()
{
2020-07-26 10:38:41 +08:00
if (status_)
{
delete status_;
status_ = nullptr;
}
2020-07-26 00:22:32 +08:00
}
void ObjectBase::SetObjectPolicy(const ObjectPolicyFunc& policy)
{
object_policy_ = policy;
}
2020-01-21 10:09:55 +08:00
bool ObjectBase::IsTracingLeaks()
{
return tracing_leaks;
}
void ObjectBase::StartTracingLeaks()
{
tracing_leaks = true;
}
void ObjectBase::StopTracingLeaks()
{
tracing_leaks = false;
}
void ObjectBase::DumpTracingObjects()
{
KGE_DEBUG_LOGF("-------------------------- All Objects --------------------------");
2020-01-21 10:09:55 +08:00
for (const auto object : tracing_objects)
{
KGE_DEBUG_LOGF("{ class=\"%s\" id=%d refcount=%d name=\"%s\" }", typeid(*object).name(), object->GetObjectID(),
2020-04-15 16:37:58 +08:00
object->GetRefCount(), object->GetName().c_str());
2020-01-21 10:09:55 +08:00
}
KGE_DEBUG_LOGF("------------------------- Total size: %d -------------------------", tracing_objects.size());
2020-01-21 10:09:55 +08:00
}
Vector<ObjectBase*>& ObjectBase::GetTracingObjects()
{
return tracing_objects;
}
void ObjectBase::AddObjectToTracingList(ObjectBase* obj)
{
2019-04-11 14:40:54 +08:00
#ifdef KGE_DEBUG
2020-01-21 10:09:55 +08:00
if (tracing_leaks && !obj->tracing_leak_)
{
obj->tracing_leak_ = true;
tracing_objects.push_back(obj);
}
#endif
2020-01-21 10:09:55 +08:00
}
2020-01-21 10:09:55 +08:00
void ObjectBase::RemoveObjectFromTracingList(ObjectBase* obj)
{
2019-04-11 14:40:54 +08:00
#ifdef KGE_DEBUG
2020-01-21 10:09:55 +08:00
if (tracing_leaks && obj->tracing_leak_)
{
obj->tracing_leak_ = false;
auto iter = std::find(tracing_objects.begin(), tracing_objects.end(), obj);
if (iter != tracing_objects.end())
{
tracing_objects.erase(iter);
}
}
#endif
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano