/**
* @file CNewBuffMngr.h
* @brief 用new[]操作符创建数组并管理对应的内存
*
* Developer can include this file in your project to build applications.
* For more information, please read the developer guide.
* Use of this software is subject to certain restrictions and limitations set
* forth in a license agreement entered into between iFLYTEK, Co,LTD.
* and the licensee of this software. Please refer to the license
* agreement for license use rights and restrictions.
*
* Copyright (C) 1999 - 2008 by ANHUI USTC iFLYTEK, Co,LTD.
* All rights reserved.
*
* @author minglu2
* @version 1.0
* @date 2017/06/09
*
* @see
*
* History:
* index version date author notes
* 0 1.0 2017/06/09 minglu2 Create this file
*/
#ifndef _CNEWBUFFMNGR_H_
#define _CNEWBUFFMNGR_H_
#ifdef __cplusplus
extern "C" {
#include <stdio.h>
#endif
#ifdef __cplusplus
}
#endif
#include <stdio.h>
/************************************************************************/
/* 创建并管理一个数组,此数组不可跨函数使用 */
/************************************************************************/
template <typename Element_Type>
class CNewBuffMngr
{
public:
//创建一个内存并返回首地址
CNewBuffMngr(Element_Type*& pElement, size_t Size_Of_Array)
{
pElement = new Element_Type[Size_Of_Array];
memset(pElement, 0, Size_Of_Array);
m_pElement = pElement;
}
~CNewBuffMngr()
{
if (m_pElement)
{
delete[] m_pElement;
m_pElement = NULL;
}
}
protected:
private:
CNewBuffMngr& operator=(const CNewBuffMngr&);
CNewBuffMngr(const CNewBuffMngr&);
Element_Type* m_pElement;
};
/************************************************************************/
/* 创建并管理一个文件句柄,此数组不可跨函数使用 */
/************************************************************************/
class CFileMng
{
public:
/**
* @ func: CFileMng
* @ brief: 按照指定模式打开指定文件,并返回对应的文件指针,如果出错则返回NULL
*
* @author iFLYTEK
* Access: public
* @return: 出错则返回NULL;否则返回文件指针
* @param: FILE * & pFile [out] 可能为NULL
* @param: const char * file_patch 文件路径
* @param: const char * mode 打开模式
* @see
*/
CFileMng(FILE* &pFile ,const char* file_patch, const char* mode)
{
if (file_patch && mode)
{
m_pFile = fopen(file_patch, mode);
}
else
{//参数不合法
m_pFile = NULL;
}
pFile = m_pFile;
}
~CFileMng()
{
if (m_pFile)
{
fclose(m_pFile);
m_pFile = NULL;
}
}
protected:
private:
CFileMng& operator=(const CFileMng&);
CFileMng(const CFileMng&);
FILE* m_pFile;
};
/************************************************************************/
/* 普通的资源管理模块,在对象析构时调用注册进来的回调函数。 */
/************************************************************************/
typedef void* (*CallBack_Func_Type)(void*);
class CCommonResMng
{
public:
/**
* @ func: CCommonResMng
* @ brief: 根据外部资源极其释放函数来生成一个资源管理对象
* @author iFLYTEK
* Access: public
* @return:
* @param: void * pRes 资源
* @param: CallBack_Func_Type pRelease_Source_Callback_Fun 指向用于释放资源的函数
* @see
*/
CCommonResMng(void *pRes ,CallBack_Func_Type pRelease_Source_Callback_Fun)
{
m_pRes = pRes;
m_pRelease_Source_Callback_Fun = pRelease_Source_Callback_Fun;
}
virtual ~CCommonResMng(void)
{
if (m_pRes && m_pRelease_Source_Callback_Fun)
{
m_pRelease_Source_Callback_Fun(m_pRes);
m_pRelease_Source_Callback_Fun = NULL;
}
}
protected:
private:
CallBack_Func_Type m_pRelease_Source_Callback_Fun; //函数指针,指向用于释放资源的回调函数
void* m_pRes; //指向外部的资源实体对象
};
#endif //_CNEWBUFFMNGR_H_
将资源管理类和boost/shared_ptr结合使用,则可以解决跨域情况下的资源管理问题。即保证资源管理类所生成的对象有且仅被析构一次。