积累-destroy

#pragma once

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

#define SAFE_DELETE(point) do {delete (point); (point) = nullptr;} while(false);

template<class T>
struct auto_delete
{
	inline static void safe_delete( const T & )
	{

	}
};

template<class T>
struct auto_delete<T *>
{
	inline static void safe_delete( T* p )
	{
		SAFE_DELETE( p );
	}
};

template<class T>
struct auto_delete<const T *>
{
	inline static void safe_delete( const T* p )
	{
		SAFE_DELETE( p );
	}
};

template<class T>
struct destroyer
{
	inline static void destroy( const T & )
	{

	}
};

template<class T>
struct destroyer<T *>
{
	inline static void destroy( T* p )
	{
		auto_delete<T *>::safe_delete( p );
	}
};

template<class T>
struct destroyer<const T *>
{
	inline static void destroy( const T* p )
	{
		auto_delete<const T *>::safe_delete( p );
	}
};

template<class V, class A>
struct destroyer<typename std::vector<V, A> >
{
	inline static void destroy( const std::vector<V, A>& v )
	{
		for( typename std::vector<V, A>::const_iterator iter = v.begin(); iter != v.end(); ++iter )
		{
			destroyer<V>::destroy( *iter );
		}
	}
};

template<class V, class A>
struct destroyer<typename std::list<V, A> >
{
	inline static void destroy( const std::list<V, A>& l )
	{
		for( typename std::list<V, A>::const_iterator iter = l.begin(); iter != l.end(); ++iter )
		{
			destroyer<V>::destroy( *iter );
		}
	}
};

template<class K, class P, class A>
struct destroyer<typename std::set<K, P, A> >
{
	inline static void destroy( const std::set<K, P, A>& s )
	{
		for( typename std::set<K, P, A>::const_iterator iter = s.begin(); iter != s.end(); ++iter )
		{
			destroyer<K>::destroy( *iter );
		}
	}
};

template<class K, class V, class P, class A>
struct destroyer<typename std::map<K, V, P, A> >
{
	inline static void destroy( const std::map<K, V, P, A>& m )
	{
		for( typename std::map<K, V, P, A>::const_iterator iter = m.begin(); iter != m.end(); ++iter )
		{
			destroyer<K>::destroy( iter->first );
			destroyer<V>::destroy( iter->second );
		}
	}
};

template<class K, class V, class P, class A>
struct destroyer<typename std::multimap<K, V, P, A> >
{
	inline static void destroy( const std::multimap<K, V, P, A>& mm )
	{
		for( typename std::multimap<K, V, P, A>::const_iterator iter = mm.begin(); iter != mm.end(); ++iter )
		{
			destroyer<K>::destroy( iter->first );
			destroyer<V>::destroy( iter->second );
		}
	}
};

template< class K, class H, class E, class A >
struct destroyer< typename std::unordered_set< K, H, E, A > >
{
	inline static void destroy( const std::unordered_set< K, H, E, A >& us )
	{
		for( typename std::unordered_set<K, H, E, A>::const_iterator iter = us.begin(); iter != us.end(); ++iter )
		{
			destroyer<K>::destroy( *iter );
		}
	}
};

template< class K, class V, class H, class E, class A >
struct destroyer< typename std::unordered_map< K, V, H, E, A > >
{
	inline static void destroy( const std::unordered_map< K, V, H, E, A >& um )
	{
		for( typename std::unordered_map< K, V, H, E, A >::const_iterator iter = um.begin(); iter != um.end(); ++iter )
		{
			destroyer<K>::destroy( iter->first );
			destroyer<V>::destroy( iter->second );
		}
	}
};

template<class T>
void destroy( const T& v )
{
	destroyer<T>::destroy( v );
}

自行加入其它需要的类型即可,如果是容器调用过destroy后需要自行处理clear


template<class _Ty>
vector<_Ty> split(const char* src, const char* tokens)
{
	std::vector<_Ty> ret;
	if (!src)
		return ret;

	const char* str = src + strspn(src, tokens);
	while(*str)
	{
		const char* brk = strpbrk(str, tokens);
		if (NULL == brk)
		{
			std::istringstream oss(str);
			std::copy(std::istream_iterator<_Ty>(oss), std::istream_iterator<_Ty>(), std::back_inserter(ret));
			break;
		}

		std::istringstream oss(string(str, brk));
		std::copy(std::istream_iterator<_Ty>(oss), std::istream_iterator<_Ty>(), std::back_inserter(ret));
		str = brk + strspn(brk, tokens);
	}
	return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值