webrtc中scope_ptr的使用




//  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
//  Copyright (c) 2001, 2002 Peter Dimov
//
//  Permission to copy, use, modify, sell and distribute this software
//  is granted provided this copyright notice appears in all copies.
//  This software is provided "as is" without express or implied
//  warranty, and with no claim as to its suitability for any purpose.
//
//  See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
//

//  scoped_ptr mimics a built-in pointer except that it guarantees deletion
//  of the object pointed to, either on destruction of the scoped_ptr or via
//  an explicit reset(). scoped_ptr is a simple solution for simple needs;
//  use shared_ptr or std::auto_ptr if your needs are more complex.

//  scoped_ptr_malloc added in by Google.  When one of
//  these goes out of scope, instead of doing a delete or delete[], it
//  calls free().  scoped_ptr_malloc<char> is likely to see much more
//  use than any other specializations.

//  release() added in by Google. Use this to conditionally
//  transfer ownership of a heap-allocated object to the caller, usually on
//  method success.
#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_PTR_H_
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_PTR_H_

#include <assert.h>            // for assert
#include <stdlib.h>            // for free() decl

#include <cstddef>             // for std::ptrdiff_t

#ifdef _WIN32
namespace std { using ::ptrdiff_t; };
#endif // _WIN32

namespace webrtc {

	template <typename T>
	class scoped_ptr {
	private:

		T* ptr;

		scoped_ptr(scoped_ptr const &);
		scoped_ptr & operator=(scoped_ptr const &);

	public:

		typedef T element_type;

		explicit scoped_ptr(T* p = NULL) : ptr(p) 
		{

		}

		~scoped_ptr() {
			typedef char type_must_be_complete[sizeof(T)];
			delete ptr;
		}

		void reset(T* p = NULL) {
			typedef char type_must_be_complete[sizeof(T)];

			if (ptr != p) {
				T* obj = ptr;
				ptr = p;
				// Delete last, in case obj destructor indirectly results in ~scoped_ptr
				delete obj;
			}
		}

		T& operator*() const {
			assert(ptr != NULL);
			return *ptr;
		}

		T* operator->() const  {
			assert(ptr != NULL);
			return ptr;
		}

		T* get() const  {
			return ptr;
		}

		void swap(scoped_ptr & b) {
			T* tmp = b.ptr;
			b.ptr = ptr;
			ptr = tmp;
		}

		T* release() {
			T* tmp = ptr;
			ptr = NULL;
			return tmp;
		}

		T** accept() {
			if (ptr) {
				delete ptr;
				ptr = NULL;
			}
			return &ptr;
		}

		T** use() {
			return &ptr;
		}
	};

	template<typename T> inline
		void swap(scoped_ptr<T>& a, scoped_ptr<T>& b) {
		a.swap(b);
	}




	//  scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
	//  is guaranteed, either on destruction of the scoped_array or via an explicit
	//  reset(). Use shared_array or std::vector if your needs are more complex.

	template<typename T>
	class scoped_array {
	private:

		T* ptr;

		scoped_array(scoped_array const &);
		scoped_array & operator=(scoped_array const &);

	public:

		typedef T element_type;

		explicit scoped_array(T* p = NULL) : ptr(p) {}

		~scoped_array() {
			typedef char type_must_be_complete[sizeof(T)];
			delete[] ptr;
		}

		void reset(T* p = NULL) {
			typedef char type_must_be_complete[sizeof(T)];

			if (ptr != p) {
				T* arr = ptr;
				ptr = p;
				// Delete last, in case arr destructor indirectly results in ~scoped_array
				delete[] arr;
			}
		}

		T& operator[](std::ptrdiff_t i) const {
			assert(ptr != NULL);
			assert(i >= 0);
			return ptr[i];
		}

		T* get() const {
			return ptr;
		}

		void swap(scoped_array & b) {
			T* tmp = b.ptr;
			b.ptr = ptr;
			ptr = tmp;
		}

		T* release() {
			T* tmp = ptr;
			ptr = NULL;
			return tmp;
		}

		T** accept() {
			if (ptr) {
				delete[] ptr;
				ptr = NULL;
			}
			return &ptr;
		}
	};

	template<class T> inline
		void swap(scoped_array<T>& a, scoped_array<T>& b) {
		a.swap(b);
	}

	// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
	// second template argument, the function used to free the object.

	template<typename T, void(*FF)(void*) = free> class scoped_ptr_malloc {
	private:

		T* ptr;

		scoped_ptr_malloc(scoped_ptr_malloc const &);
		scoped_ptr_malloc & operator=(scoped_ptr_malloc const &);

	public:

		typedef T element_type;

		explicit scoped_ptr_malloc(T* p = 0) : ptr(p) {}

		~scoped_ptr_malloc() {
			FF(static_cast<void*>(ptr));
		}

		void reset(T* p = 0) {
			if (ptr != p) {
				FF(static_cast<void*>(ptr));
				ptr = p;
			}
		}

		T& operator*() const {
			assert(ptr != 0);
			return *ptr;
		}

		T* operator->() const {
			assert(ptr != 0);
			return ptr;
		}

		T* get() const {
			return ptr;
		}

		void swap(scoped_ptr_malloc & b) {
			T* tmp = b.ptr;
			b.ptr = ptr;
			ptr = tmp;
		}

		T* release() {
			T* tmp = ptr;
			ptr = 0;
			return tmp;
		}

		T** accept() {
			if (ptr) {
				FF(static_cast<void*>(ptr));
				ptr = 0;
			}
			return &ptr;
		}
	};

	template<typename T, void(*FF)(void*)> inline
		void swap(scoped_ptr_malloc<T, FF>& a, scoped_ptr_malloc<T, FF>& b) {
		a.swap(b);
	}

} // namespace webrtc

#endif  // #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_PTR_H_


#include "scoped_ptr.h"
#include<iostream>
#include<string>
using namespace std;
using namespace webrtc;


class VoiceChannelTransport
{
public:
	VoiceChannelTransport()
	{
		name = "";
	}
	VoiceChannelTransport(string s)
	{
		name = s;
	}
	~VoiceChannelTransport()
	{
		cout << "Deconstruct VoiceChannelTransport" << endl;
	}
	void Initialize()
	{
		cout << "Initialize" << endl;
	}
private:
	string name;

};

int main()
{

	{
		scoped_ptr<VoiceChannelTransport> voice_channel_transport(new VoiceChannelTransport("abc"));
		scoped_ptr<VoiceChannelTransport> voice_channel_transport1(new VoiceChannelTransport("test copy"));
		voice_channel_transport.reset(new VoiceChannelTransport("cde"));
		//voice_channel_transport(voice_channel_transport1);  不能复制
		//voice_channel_transport=voice_channel_transport1;  不能复制

		voice_channel_transport.get()->Initialize();
	}

	
	{
		scoped_array<int> si(new int[10]);//数组
		scoped_array<VoiceChannelTransport> ScopedArray(new VoiceChannelTransport[10]);
	}
	
	cout << "Test" << endl;
	//error 
	//VoiceChannelTransport * p = voice_channel_transport.get();
	//delete p;

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值