1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

for #515, merge from srs2, use SrsAutoFreeA.

This commit is contained in:
winlin 2015-11-02 11:29:20 +08:00
parent 604486b0a3
commit f57e537686
14 changed files with 54 additions and 37 deletions

View file

@ -31,28 +31,41 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_core.hpp>
/**
* auto free the instance in the current scope, for instance, MyClass* ptr,
* which is a ptr and this class will:
* 1. free the ptr.
* 2. set ptr to NULL.
* Usage:
* MyClass* po = new MyClass();
* // ...... use po
* SrsAutoFree(MyClass, po);
*/
* auto free the instance in the current scope, for instance, MyClass* ptr,
* which is a ptr and this class will:
* 1. free the ptr.
* 2. set ptr to NULL.
*
* Usage:
* MyClass* po = new MyClass();
* // ...... use po
* SrsAutoFree(MyClass, po);
*
* Usage for array:
* MyClass** pa = new MyClass*[size];
* // ....... use pa
* SrsAutoFreeA(MyClass*, pa);
*
* @remark the MyClass can be basic type, for instance, SrsAutoFreeA(char, pstr),
* where the char* pstr = new char[size].
*/
#define SrsAutoFree(className, instance) \
impl__SrsAutoFree<className> _auto_free_##instance(&instance)
impl__SrsAutoFree<className> _auto_free_##instance(&instance, false)
#define SrsAutoFreeA(className, instance) \
impl__SrsAutoFree<className> _auto_free_array_##instance(&instance, true)
template<class T>
class impl__SrsAutoFree
{
private:
T** ptr;
bool is_array;
public:
/**
* auto delete the ptr.
*/
impl__SrsAutoFree(T** p) {
* auto delete the ptr.
*/
impl__SrsAutoFree(T** p, bool array) {
ptr = p;
is_array = array;
}
virtual ~impl__SrsAutoFree() {
@ -60,7 +73,11 @@ public:
return;
}
delete *ptr;
if (is_array) {
delete[] *ptr;
} else {
delete *ptr;
}
*ptr = NULL;
}