Files
cocos4/native/cocos/core/utils/Pool.h
T
pandamicro 12072ffc62 Merge v3.5.1 zqb branch to v3.6 (#11637)
* support asset property preview (#11017)

(cherry picked from commit 85769cf8f81459b5ad98ccab4138420009572a32)

* show mesh asset thumbnail (#10981)

* V3.5.1 noise particle (#11003)

* add cpu & gpu noise for particle

* add some params & change particle noise shader

* change shader

* add cpu noise particle

* add noise preview

* run generation when preview

* revert shaders

* remove unused statements

* add particle noise module

* change noise preview interface

* add invisible to remap

* remove get deltaTime

* set noiseModule private

* fix eslint

* feat: add noise preview (#11260)

* [Optimize] asset editing undo (#10779)

* edit render-pipeline

* support renderpipeline undo

* optimize material editing undo

* reset last record data

* temporary not support animation-mask undo

* support animation-mask edit undo

* animation mask remove _jointMasks

* Animation clip embedded players (#11320)

* Animation clip subregions

* Log

* Test

* Eval

* Update

* Update

* No longer use animation state to implement clip motion

* Clear subregions

* Fix

* Mark subregion as editor extendable

* Animation clip subregion player path

* fix isnan(0.0) with webgl1.0 (#11262)

* fix isinf(0.0)==true with webgl1.0

* code style

* fix specular params with surface toon (#11265)

* code format and add sliders for parameters (#11266)

* Broadcasting of animation state\'s speed

* Fix

* Rename

* Update

* Fix

* Revert "No longer use animation state to implement clip motion"

This reverts commit 20755f9ec525b27b5b9a515c8dd687349aa4c637.

* Revert some exotic changes

* Update docs

Co-authored-by: jk20012001 <jkregister@163.com>
Co-authored-by: 徐兵 <49358166+xubing0906@users.noreply.github.com>

* fix skinning model split (#11378)

* fix skinning model split

* rename function _resizeMaxJointForDS

Co-authored-by: zhakesi <zhakesi@foxmail.com>

* [ci skip][AUTO]: Automated genbindings update: d823cbba1f7ce8284da28458c26534da2e033961  (#11378) (#11383)

Co-authored-by: cocos-robot <cocos-robot@cocos.com>

* use stop emitting instead (#11381)

* add stop emitting api

* use stop emitting instead

* Update cocos/particle/particle-system.ts

Co-authored-by: Leslie Leigh (李的序) <leslieleigh@outlook.com>

Co-authored-by: Leslie Leigh (李的序) <leslieleigh@outlook.com>

* fix: imageData fault tolerance (#11374)

* fix: imageData fault tolerance

* chore: when data is an empty array,stop the function

* material  undo updating effects list (#11409)

* modify noise module ui (#11433)

* use visible decorator to control the visibility of property (#11438)

* fix joints data with value nan (#11489)

* V3.5.2 CI Android NDK  set version to 21 (#11448) (#11492)

* Minor improvement

* Fix format

* Fix particle culler

* Improve particle culler

* Manually migrate joint data upload with texture in skinning model

* Update shaders for skinning model based on joint texture

* fis native skinning model with realtime-texture mode (#45)

Co-authored-by: zhakesi <zhakesi@foxmail.com>

* fix clangtidy (#46)

* Small tweaks

* modify code struct for skinning model (#47)

* modify code struct for skinning model

* update skinning chunk

Co-authored-by: 黄森斌 <arsen2010@126.com>
Co-authored-by: zxx43 <zxx43@hotmail.com>
Co-authored-by: 袁炜海 <35713518+dogodo-cc@users.noreply.github.com>
Co-authored-by: Leslie Leigh (李的序) <leslieleigh@outlook.com>
Co-authored-by: jk20012001 <jkregister@163.com>
Co-authored-by: 徐兵 <49358166+xubing0906@users.noreply.github.com>
Co-authored-by: zhakesi <liqiao@cocos.com>
Co-authored-by: zhakesi <zhakesi@foxmail.com>
Co-authored-by: Cocos Robot <48829427+cocos-robot@users.noreply.github.com>
Co-authored-by: cocos-robot <cocos-robot@cocos.com>
Co-authored-by: Santy-Wang <wh19930829@qq.com>
Co-authored-by: zhangxin <59186205+chichinohaha@users.noreply.github.com>
Co-authored-by: 江战 <40414978+PatriceJiang@users.noreply.github.com>
2022-06-24 15:10:45 +08:00

186 lines
5.8 KiB
C++

/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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
/**
* @en
* A fixed-length object pool designed for general type.<br>
* The implementation of this object pool is very simple,
* it can helps you to improve your game performance for objects which need frequent release and recreate operations<br/>
* @zh
* 长度固定的对象缓存池,可以用来缓存各种对象类型。<br/>
* 这个对象池的实现非常精简,它可以帮助您提高游戏性能,适用于优化对象的反复创建和销毁。
* @class js.Pool
* @example
* ```
*
* Example 1:
*
* function Details () {
* uuidList = [];
* };
* Details.prototype.reset = function () {
* uuidList.length = 0;
* };
* Details.pool = new js.Pool(function (obj) {
* obj.reset();
* }, 5);
* Details.pool.get = function () {
* return _get() || new Details();
* };
*
* var detail = Details.pool.get();
* ...
* Details.pool.put(detail);
*
* Example 2:
*
* function Details (buffer) {
* uuidList = buffer;
* };
* ...
* Details.pool.get = function (buffer) {
* var cached = _get();
* if (cached) {
* cached.uuidList = buffer;
* return cached;
* }
* else {
* return new Details(buffer);
* }
* };
*
* var detail = Details.pool.get( [] );
* ...
* ```
*/
#include <functional>
#include <memory>
#include "base/Macros.h"
template <typename T>
class Pool {
public:
using CleanUpFunction = std::function<bool(T &)>;
/**
* @en
* Get and initialize an object from pool. This method defaults to null and requires the user to implement it.
* @zh
* 获取并初始化对象池中的对象。这个方法默认为空,需要用户自己实现。
* @param args - parameters to used to initialize the object
*/
std::function<T()> get{};
/**
* 使用构造函数来创建一个指定对象类型的对象池,您可以传递一个回调函数,用于处理对象回收时的清理逻辑。
* @method constructor
* @param {Function} [cleanupFunc] - the callback method used to process the cleanup logic when the object is recycled.
* @param {Object} cleanupFunc.obj
* @param {Number} size - initializes the length of the array
*/
Pool(const CleanUpFunction &cleanup, int32_t size) {
_count = 0;
_pool.resize(size);
_cleanup = cleanup;
}
/**
* 使用构造函数来创建一个指定对象类型的对象池,您可以传递一个回调函数,用于处理对象回收时的清理逻辑。
* @method constructor
* @param {Function} [cleanupFunc] - the callback method used to process the cleanup logic when the object is recycled.
* @param {Object} cleanupFunc.obj
* @param {Number} size - initializes the length of the array
*/
explicit Pool(int32_t size)
: Pool{nullptr, size} {}
~Pool() = default;
/**
* @en
* Get an object from pool, if no available object in the pool, null will be returned.
* @zh
* 获取对象池中的对象,如果对象池没有可用对象,则返回空。
*/
T _get() { //NOLINT(readability-identifier-naming)
if (_count > 0) {
--_count;
T cache = _pool[_count];
_pool[_count] = nullptr;
return cache;
}
return nullptr;
}
/**
* @en Put an object into the pool.
* @zh 向对象池返还一个不再需要的对象。
*/
void put(T &obj) {
if (_count < static_cast<int32_t>(_pool.size())) {
if (_cleanup != nullptr && !_cleanup(obj)) {
return;
}
_pool[_count] = obj;
++_count;
}
}
/**
* @en Resize the pool.
* @zh 设置对象池容量。
*/
void resize(int32_t length) {
if (length >= 0) {
_pool.resize(length);
if (_count > length) {
_count = length;
}
}
}
int32_t getCount() const { return _count; }
private:
ccstd::vector<T> _pool;
CleanUpFunction _cleanup{nullptr};
/**
* @en
* The current number of available objects, the default is 0, it will gradually increase with the recycle of the object,
* the maximum will not exceed the size specified when the constructor is called.
* @zh
* 当前可用对象数量,一开始默认是 0,随着对象的回收会逐渐增大,最大不会超过调用构造函数时指定的 size。
* @default 0
*/
int32_t _count{0};
CC_DISALLOW_COPY_MOVE_ASSIGN(Pool);
};