您的位置:首页 > 其它

内存泄漏+适配器+多态

2015-12-29 15:46 260 查看
//#pragma once
//#include<iostream>
//#include<string>
//Singleton.hpp
//#include<list>
//#include<assert.h>
//#include<stdarg.h>
//using namespace std;
//struct BlockInfo
//{
//	void* _ptr;
//	string _file;
//	int _line;
//	BlockInfo(void *ptr, const char*file, int line)
//		:_ptr(ptr)
//		, _file(file)
//		, _line(line)
//	{}
//};
//class SaveAdapter//适配器
//{
//public:
//	//可变参数列表
//	//纯虚函数,抽象类,不能实例化出对象,继承的类应该重写此虚函数
//	virtual void Save(const char* fmt, ...) = 0;
//};
//class ConsoleSaveAdapter :public SaveAdapter
//{
//public:
//	//显示在控制台,同理还可记入数据库中
//	void Save(const char* fmt, ...)
//	{
//		va_list args;
//		va_start(args, fmt);
//		vfprintf(stdout, fmt, args);
//		va_end(args);
//	}
//};
//class  FileSaveAdapter :public SaveAdapter
//{
//public:
//	FileSaveAdapter(const char* filename = "Memory.txt")
//	{
//		_fOut = fopen(filename, "w");
//	}
//	//记录在文件
//	void Save(const char* fmt, ...)
//	{
//		va_list args;
//		va_start(args, fmt);
//		vfprintf(_fOut, fmt, args);
//		va_end(args);
//	}
//	~FileSaveAdapter()
//	{
//		fclose(_fOut);
//	}
//protected:
//	FILE* _fOut;
//};
////单例模式
//class Singleton
//{
//public:
//	static Singleton* GetInstance()
//	{
//		if (sInstance == NULL)
//		{
//			sInstance = new Singleton;
//		}
//		return sInstance;
//	}
//	void *Alloc(size_t size, const char* file, int line)
//	{
//		void* ptr = malloc(size);
//		if (ptr)
//		{
//			BlockInfo  b(ptr, file, line);
//			GetInstance()->BlockList.push_back(b);
//		}
//		return ptr;
//	}
//	void Dalloc(void *ptr)
//	{
//		free(ptr);
//		//[)
//		list<BlockInfo>::iterator it = GetInstance()->BlockList.begin();
//		while (it != GetInstance()->BlockList.end())
//		{
//			if (it->_ptr == ptr)
//			{
//				GetInstance()->BlockList.erase(it);
//				return;
//			}
//			++it;
//		}
//		assert(false);
//	}
//	//静态成员函数没有this指针
//	//静态函数里面不能调用非静态函数,没有this指针传过去
//	static void Print()
//	{
//		cout << "内存泄漏的内存块" << endl;
//		list<BlockInfo>::iterator it = GetInstance()->BlockList.begin();
//		while (it != GetInstance()->BlockList.end())
//		{
//			printf("ptr:%p file:%s line:%d\n", it->_ptr, it->_file.c_str(), it->_line);
//			++it;
//		}
//	}
//	void Persistent()
//	{
//		ConsoleSaveAdapter  csa;
//		Singleton::GetInstance()->_Persistent(&csa);
//		FileSaveAdapter  fsa;
//		Singleton::GetInstance()->_Persistent(&fsa);
//	}
//private:
//	void  _Persistent(SaveAdapter* sa)//单例模式此时应传父类指针或引用
//	{
//		std::list<BlockInfo>::iterator it = GetInstance()->BlockList.begin();
//		while (it != GetInstance()->BlockList.end())
//		{
//			sa->Save("ptr:%p, file:%s, line:%d\n", it->_ptr, it->_file.c_str(), it->_line);//运用的是多态
//			++it;
//		}
//	}
//	Singleton()
//	{}
//	Singleton(const Singleton& s);
//	Singleton& operator=(const Singleton& s);
//protected:
//	static Singleton* sInstance;
//	list<BlockInfo> BlockList;
//};
//template<class T>
//T *__NEW(size_t num, const char* file, int line)
//{
//	T *ptr = (T*)(Singleton::GetInstance()->Alloc(sizeof(T)*num, file, line));
//	if (TypeTraits <T>::__IsPODType().Get())
//		return ptr;
//	else
//		return new(ptr)T;
//}
//template<class T>
//void __DELETE(T *ptr)
//{
//	if (!TypeTraits <T>::__IsPODType().Get())
//		ptr->~T();
//	Singleton::GetInstance()->Dalloc(ptr);
//}
//template<class T>
//T *__NEW_ARRAY(size_t num, const char* file, int line)
//{
//	size_t size = sizeof(T)*num;
//	T* ptr = NULL;
//	if (TypeTraits <T>::__IsPODType().Get())
//	{
//		ptr = (T*)(Singleton::GetInstance()->Alloc(size, file, line));
//		return ptr;
//	}
//	else
//	{
//		size += 4;
//		ptr = (T*)(Singleton::GetInstance()->Alloc(size, file, line));
//		*((int*)ptr) = num;
//		T* cur = (T*)((int*)ptr + 1);
//		for (size_t i = 0; i < num; i++)
//		{
//			new(&cur[i])T;//如果是基本类型则不执行,直接跳过
//		}
//		return cur;
//	}
//
//}
//template<class T>
//void __DELETE_ARRAY(T* ptr)
//{
//	if (TypeTraits <T>::__IsPODType().Get())
//	{
//		Singleton::GetInstance()->Dalloc(ptr);
//	}
//	else
//	{
//		int num = *((int*)ptr - 1);
//		for (int i = 0; i < num; i++)
//		{
//			ptr[i].~T();
//		}
//		Singleton::GetInstance()->Dalloc((void*)((int*)ptr - 1));
//	}
//
//}
//struct __TrueType
//{
//	bool Get()
//	{
//		return true;
//	}
//};
//
//struct __FalseType
//{
//	bool Get()
//	{
//		return false;
//	}
//};
//
//template <class _Tp>
//struct TypeTraits
//{
//	typedef __FalseType   __IsPODType;
//};
//
//template <>
//struct TypeTraits< bool>
//{
//	typedef __TrueType     __IsPODType;
//};
//
//template <>
//struct TypeTraits< char>
//{
//	typedef __TrueType     __IsPODType;
//};
//template <>
//struct TypeTraits< int>
//{
//	typedef __TrueType     __IsPODType;
//};
//
//#define NEW(type) __NEW<type>(1,__FILE__,__LINE__)
//#define DELETE(ptr) __DELETE(ptr)
//#define NEW_ARRAY(type,num)  __NEW_ARRAY<type>(num,__FILE__,__LINE__)
//#define DELETE_ARRAY(ptr)  __DELETE_ARRAY(ptr)//模板类型根据传过去参数自己推演类型
//Singleton* Singleton::sInstance = NULL;

////测试用例
//#include<iostream>
//using namespace std;
//#include"Singleton.hpp"
////自定义类型数组
//void Test4()
//{
//	string*  p1 =NEW_ARRAY(string, 5);
//	p1[0] = "abc";
//	p1[1] = "def";
//	p1[2] = "xxx";
//	p1[3] = "lll";
//	p1[4] = "www";
//	cout << p1->c_str() << endl;
//	DELETE_ARRAY(p1);
//}
//int main()
//{
//	Test4();
//	//登记一个函数在main函数执行结束以后执行,函数限制void Func(void)
//	Singleton::GetInstance()->Persistent();
//	atexit(Singleton::Print);
//	system("pause");
//	return 0;
//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多态应用