一、typeScript源码
namespace dragonBones {
/**
* - The BaseObject is the base class for all objects in the DragonBones framework.
* All BaseObject instances are cached to the object pool to reduce the performance consumption of frequent requests for memory or memory recovery.
* @version DragonBones 4.5
* @language en_US
*/
/**
* - 基础对象,通常 DragonBones 的对象都继承自该类。
* 所有基础对象的实例都会缓存到对象池,以减少频繁申请内存或内存回收的性能消耗。
* @version DragonBones 4.5
* @language zh_CN
*/
export abstract class BaseObject {
private static _hashCode: number = 0;
private static _defaultMaxCount: number = 3000;
private static readonly _maxCountMap: Map
private static readonly _poolsMap: Map
private static _returnObject(object: BaseObject): void {
const classType = String(object.constructor);
const maxCount = classType in BaseObject._maxCountMap ? BaseObject._maxCountMap[classType] : BaseObject._defaultMaxCount;
const pool = BaseObject._poolsMap[classType] = BaseObject._poolsMap[classType] || [];
if (pool.length < maxCount) {
if (!object._isInPool) {
object._isInPool = true;
pool.push(object);
}
else {
console.warn("The object is already in the pool.");
}
}
else {
}
}
public static toString(): string {
throw new Error();
}
/**
* - Set the maximum cache count of the specify object pool.
* @param objectConstructor - The specify class. (Set all object pools max cache count if not set)
* @param maxCount - Max count.
* @version DragonBones 4.5
* @language en_US
*/
/**
* - 设置特定对象池的最大缓存数量。
* @param objectConstructor - 特定的类。 (不设置则设置所有对象池的最大缓存数量)
* @param maxCount - 最大缓存数量。
* @version DragonBones 4.5
* @language zh_CN
*/
public static setMaxCount(objectConstructor: (typeof BaseObject) | null, maxCount: number): void {
if (maxCount < 0 || maxCount !== maxCount) { // isNaN
maxCount = 0;
}
if (objectConstructor !== null) {
const classType = String(objectConstructor);
const pool = classType in BaseObject._poolsMap ? BaseObject._poolsMap[classType] : null;
if (pool !== null && pool.length > maxCount) {
pool.length = maxCount;
}
BaseObject._maxCountMap[classType] = maxCount;
}
else {
BaseObject._defaultMaxCount = maxCount;
for (let classType in BaseObject._poolsMap) {
const pool = BaseObject._poolsMap[classType];
if (pool.length > maxCount) {
pool.length = maxCount;
}
if (classType in BaseObject._maxCountMap) {
BaseObject._maxCountMap[classType] = maxCount;
}
}
}
}
/**
* - Clear the cached instances of a specify object pool.
* @param objectConstructor - Specify class. (Clear all cached instances if not set)
* @version DragonBones 4.5
* @language en_US
*/
/**
* - 清除特定对象池的缓存实例。
* @param objectConstructor - 特定的类。 (不设置则清除所有缓存的实例)
* @version DragonBones 4.5
* @language zh_CN
*/
public static clearPool(objectConstructor: (typeof BaseObject) | null = null): void {
if (objectConstructor !== null) {
const classType = String(objectConstructor);
const pool = classType in BaseObject._poolsMap ? BaseObject._poolsMap[classType] : null;
if (pool !== null && pool.length > 0) {
pool.length = 0;
}
}
else {
for (let k in BaseObject._poolsMap) {
const pool = BaseObject._poolsMap[k];
pool.length = 0;
}
}
}
/**
* - Get an instance of the specify class from object pool.
* @param objectConstructor - The specify class.
* @version DragonBones 4.5
* @language en_US
*/
/**
* - 从对象池中获取特定类的实例。
* @param objectConstructor - 特定的类。
* @version DragonBones 4.5
* @language zh_CN
*/
public static borrowObject
const classType = String(objectConstructor);
const pool = classType in BaseObject._poolsMap ? BaseObject._poolsMap[classType] : null;
if (pool !== null && pool.length > 0) {
const object = pool.pop() as T;
object._isInPool = false;
return object;
}
const object = new objectConstructor();
object._onClear();
return object;
}
/**
* - A unique identification number assigned to the object.
* @version DragonBones 4.5
* @language en_US
*/
/**
* - 分配给此实例的唯一标识号。
* @version DragonBones 4.5
* @language zh_CN
*/
public readonly hashCode: number = BaseObject._hashCode++;
private _isInPool: boolean = false;
protected abstract _onClear(): void;
/**
* - Clear the object and return it back to object pool。
* @version DragonBones 4.5
* @language en_US
*/
/**
* - 清除该实例的所有数据并将其返还对象池。
* @version DragonBones 4.5
* @language zh_CN
*/
public returnToPool(): void {
this._onClear();
BaseObject._returnObject(this);
}
}
}
二、C#源码
using System.Collections.Generic;
namespace DragonBones
{
///
/// - The BaseObject is the base class for all objects in the DragonBones framework.
/// All BaseObject instances are cached to the object pool to reduce the performance consumption of frequent requests for memory or memory recovery.
///
///
///
///
/// - 基础对象,通常 DragonBones 的对象都继承自该类。
/// 所有基础对象的实例都会缓存到对象池,以减少频繁申请内存或内存回收的性能消耗。
///
///
///
public abstract class BaseObject
{
private static uint _hashCode = 0;
private static uint _defaultMaxCount = 3000;
private static readonly Dictionary
private static readonly Dictionary
private static void _ReturnObject(BaseObject obj)
{
var classType = obj.GetType();
var maxCount = _maxCountMap.ContainsKey(classType) ? _maxCountMap[classType] : _defaultMaxCount;
var pool = _poolsMap.ContainsKey(classType) ? _poolsMap[classType] : _poolsMap[classType] = new List
if (pool.Count < maxCount)
{
if (!pool.Contains(obj))
{
pool.Add(obj);
}
else
{
Helper.Assert(false, "The object is already in the pool.");
}
}
else
{
}
}
///
/// - Set the maximum cache count of the specify object pool.
///
/// - The specify class. (Set all object pools max cache count if not set)
/// - Max count.
///
///
///
/// - 设置特定对象池的最大缓存数量。
///
/// - 特定的类。 (不设置则设置所有对象池的最大缓存数量)
/// - 最大缓存数量。
///
///
public static void SetMaxCount(System.Type classType, uint maxCount)
{
if (classType != null)
{
if (_poolsMap.ContainsKey(classType))
{
var pool = _poolsMap[classType];
if (pool.Count > maxCount)
{
pool.ResizeList((int)maxCount, null);
}
}
_maxCountMap[classType] = maxCount;
}
else
{
_defaultMaxCount = maxCount;
foreach (var key in _poolsMap.Keys)
{
var pool = _poolsMap[key];
if (pool.Count > maxCount)
{
pool.ResizeList((int)maxCount, null);
}
if (_maxCountMap.ContainsKey(key))
{
_maxCountMap[key] = maxCount;
}
}
}
}
///
/// - Clear the cached instances of a specify object pool.
///
/// - Specify class. (Clear all cached instances if not set)
///
///
///
/// - 清除特定对象池的缓存实例。
///
/// - 特定的类。 (不设置则清除所有缓存的实例)
///
///
public static void ClearPool(System.Type classType)
{
if (classType != null)
{
if (_poolsMap.ContainsKey(classType))
{
var pool = _poolsMap[classType];
if (pool != null)
{
pool.Clear();
}
}
}
else
{
foreach (var pair in _poolsMap)
{
var pool = _poolsMap[pair.Key];
if (pool != null)
{
pool.Clear();
}
}
}
}
///
/// - Get an instance of the specify class from object pool.
///
/// - The specify class.
///
///
///
/// - 从对象池中获取特定类的实例。
///
/// - 特定的类。
///
///
public static T BorrowObject
{
var type = typeof(T);
var pool = _poolsMap.ContainsKey(type) ? _poolsMap[type] : null;
if (pool != null && pool.Count > 0)
{
var index = pool.Count - 1;
var obj = pool[index];
pool.RemoveAt(index);
return (T)obj;
}
else
{
var obj = new T();
obj._OnClear();
return obj;
}
}
///
/// - A unique identification number assigned to the object.
///
///
///
///
/// - 分配给此实例的唯一标识号。
///
///
///
public readonly uint hashCode = _hashCode++;
protected BaseObject()
{
}
///
protected abstract void _OnClear();
///
/// - Clear the object and return it back to object pool。
///
///
///
///
/// - 清除该实例的所有数据并将其返还对象池。
///
///
///
public void ReturnToPool()
{
_OnClear();
_ReturnObject(this);
}
// public static implicit operator bool(BaseObject exists)
// {
// return exists != null;
// }
}
}