add ObjectPool
This commit is contained in:
parent
091a05e8fd
commit
a794894452
|
|
@ -17,6 +17,7 @@
|
|||
<ClInclude Include="..\..\src\kiwano\base\Director.h" />
|
||||
<ClInclude Include="..\..\src\kiwano\base\Module.h" />
|
||||
<ClInclude Include="..\..\src\kiwano\base\ObjectBase.h" />
|
||||
<ClInclude Include="..\..\src\kiwano\base\ObjectPool.h" />
|
||||
<ClInclude Include="..\..\src\kiwano\base\RefCounter.h" />
|
||||
<ClInclude Include="..\..\src\kiwano\core\Allocator.h" />
|
||||
<ClInclude Include="..\..\src\kiwano\core\Any.h" />
|
||||
|
|
@ -132,6 +133,7 @@
|
|||
<ClCompile Include="..\..\src\kiwano\base\Director.cpp" />
|
||||
<ClCompile Include="..\..\src\kiwano\base\Module.cpp" />
|
||||
<ClCompile Include="..\..\src\kiwano\base\ObjectBase.cpp" />
|
||||
<ClCompile Include="..\..\src\kiwano\base\ObjectPool.cpp" />
|
||||
<ClCompile Include="..\..\src\kiwano\base\RefCounter.cpp" />
|
||||
<ClCompile Include="..\..\src\kiwano\core\Allocator.cpp" />
|
||||
<ClCompile Include="..\..\src\kiwano\core\Exception.cpp" />
|
||||
|
|
|
|||
|
|
@ -351,6 +351,9 @@
|
|||
<ClInclude Include="..\..\src\kiwano\utils\ConfigIni.h">
|
||||
<Filter>utils</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\kiwano\base\ObjectPool.h">
|
||||
<Filter>base</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
|
||||
|
|
@ -575,6 +578,9 @@
|
|||
<ClCompile Include="..\..\src\kiwano\utils\ConfigIni.cpp">
|
||||
<Filter>utils</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\kiwano\base\ObjectPool.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="suppress_warning.ruleset" />
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
// THE SOFTWARE.
|
||||
|
||||
#include <kiwano/base/ObjectBase.h>
|
||||
#include <kiwano/base/ObjectPool.h>
|
||||
#include <kiwano/utils/Logger.h>
|
||||
#include <kiwano/utils/Json.h>
|
||||
#include <typeinfo>
|
||||
|
|
@ -56,6 +57,11 @@ ObjectBase::~ObjectBase()
|
|||
#endif
|
||||
}
|
||||
|
||||
void ObjectBase::AutoRelease()
|
||||
{
|
||||
ObjectPool::GetInstance().AddObject(this);
|
||||
}
|
||||
|
||||
const Any& ObjectBase::GetUserData() const
|
||||
{
|
||||
return user_data_;
|
||||
|
|
|
|||
|
|
@ -44,6 +44,10 @@ public:
|
|||
|
||||
virtual ~ObjectBase();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 自动释放
|
||||
void AutoRelease();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 设置对象名
|
||||
void SetName(const String& name);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,86 @@
|
|||
// Copyright (c) 2016-2018 Kiwano - Nomango
|
||||
//
|
||||
// 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:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <kiwano/base/ObjectPool.h>
|
||||
|
||||
namespace kiwano
|
||||
{
|
||||
|
||||
List<ObjectPool*> ObjectPool::pools_;
|
||||
|
||||
ObjectPool& ObjectPool::GetInstance()
|
||||
{
|
||||
static ObjectPool instance;
|
||||
return *pools_.back();
|
||||
}
|
||||
|
||||
ObjectPool::ObjectPool()
|
||||
{
|
||||
pools_.push_back(this);
|
||||
}
|
||||
|
||||
ObjectPool::~ObjectPool()
|
||||
{
|
||||
Clear();
|
||||
|
||||
auto iter = std::find(pools_.begin(), pools_.end(), this);
|
||||
if (iter != pools_.end())
|
||||
pools_.erase(iter);
|
||||
}
|
||||
|
||||
void ObjectPool::AddObject(ObjectBase* obj)
|
||||
{
|
||||
if (obj)
|
||||
{
|
||||
if (!Contains(obj))
|
||||
{
|
||||
obj->Retain();
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
objects_.push_back(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ObjectPool::Contains(ObjectBase* obj) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(const_cast<std::mutex&>(mutex_));
|
||||
|
||||
for (auto iter = pools_.rbegin(); iter != pools_.rend(); iter++)
|
||||
for (const auto o : (*iter)->objects_)
|
||||
if (obj == o)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void ObjectPool::Clear()
|
||||
{
|
||||
Vector<ObjectBase*> copied;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
copied = std::move(objects_);
|
||||
}
|
||||
|
||||
for (auto obj : copied)
|
||||
obj->Release();
|
||||
}
|
||||
|
||||
} // namespace kiwano
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
// Copyright (c) 2016-2018 Kiwano - Nomango
|
||||
//
|
||||
// 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:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// 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
|
||||
#include <kiwano/base/ObjectBase.h>
|
||||
#include <mutex>
|
||||
|
||||
namespace kiwano
|
||||
{
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 对象池
|
||||
*/
|
||||
class KGE_API ObjectPool
|
||||
: public Noncopyable
|
||||
{
|
||||
public:
|
||||
static ObjectPool& GetInstance();
|
||||
|
||||
ObjectPool();
|
||||
|
||||
virtual ~ObjectPool();
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 添加对象到内存池
|
||||
* @param[in] obj 基础对象
|
||||
*/
|
||||
void AddObject(ObjectBase* obj);
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 判断对象是否在对象池中
|
||||
* @param[in] obj 基础对象
|
||||
*/
|
||||
bool Contains(ObjectBase* obj) const;
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 清空所有对象
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
ObjectPool(const ObjectPool&) = delete;
|
||||
|
||||
ObjectPool& operator=(const ObjectPool&) = delete;
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
Vector<ObjectBase*> objects_;
|
||||
|
||||
static List<ObjectPool*> pools_;
|
||||
};
|
||||
} // namespace kiwano
|
||||
Loading…
Reference in New Issue