diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj
index 8883793a..a0ca2a9c 100644
--- a/projects/kiwano/kiwano.vcxproj
+++ b/projects/kiwano/kiwano.vcxproj
@@ -17,6 +17,7 @@
+
@@ -132,6 +133,7 @@
+
diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters
index 9d5619f2..94ab0a18 100644
--- a/projects/kiwano/kiwano.vcxproj.filters
+++ b/projects/kiwano/kiwano.vcxproj.filters
@@ -351,6 +351,9 @@
utils
+
+ base
+
@@ -575,6 +578,9 @@
utils
+
+ base
+
diff --git a/src/kiwano/base/ObjectBase.cpp b/src/kiwano/base/ObjectBase.cpp
index 588e0a6e..629b9ef5 100644
--- a/src/kiwano/base/ObjectBase.cpp
+++ b/src/kiwano/base/ObjectBase.cpp
@@ -19,6 +19,7 @@
// THE SOFTWARE.
#include
+#include
#include
#include
#include
@@ -56,6 +57,11 @@ ObjectBase::~ObjectBase()
#endif
}
+void ObjectBase::AutoRelease()
+{
+ ObjectPool::GetInstance().AddObject(this);
+}
+
const Any& ObjectBase::GetUserData() const
{
return user_data_;
diff --git a/src/kiwano/base/ObjectBase.h b/src/kiwano/base/ObjectBase.h
index 6761c77c..927e2549 100644
--- a/src/kiwano/base/ObjectBase.h
+++ b/src/kiwano/base/ObjectBase.h
@@ -44,6 +44,10 @@ public:
virtual ~ObjectBase();
+ /// \~chinese
+ /// @brief 自动释放
+ void AutoRelease();
+
/// \~chinese
/// @brief 设置对象名
void SetName(const String& name);
diff --git a/src/kiwano/base/ObjectPool.cpp b/src/kiwano/base/ObjectPool.cpp
new file mode 100644
index 00000000..66ba259c
--- /dev/null
+++ b/src/kiwano/base/ObjectPool.cpp
@@ -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
+
+namespace kiwano
+{
+
+List 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 lock(mutex_);
+ objects_.push_back(obj);
+ }
+ }
+}
+
+bool ObjectPool::Contains(ObjectBase* obj) const
+{
+ std::lock_guard lock(const_cast(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 copied;
+
+ {
+ std::lock_guard lock(mutex_);
+ copied = std::move(objects_);
+ }
+
+ for (auto obj : copied)
+ obj->Release();
+}
+
+} // namespace kiwano
diff --git a/src/kiwano/base/ObjectPool.h b/src/kiwano/base/ObjectPool.h
new file mode 100644
index 00000000..72ac3d5b
--- /dev/null
+++ b/src/kiwano/base/ObjectPool.h
@@ -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
+#include
+
+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 objects_;
+
+ static List pools_;
+};
+} // namespace kiwano