码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式

时间:2015-12-29 06:28:02      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:单例模式

//#pragma once
//#include<iostream>
//#include<string>
//#include<list>
//#include<assert.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 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;
//		}
//	}
//private:
//	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 Test1()
//{
//	int *p1 =NEW(int);
//	int *p2 =NEW(int);
//	int *p3 =NEW(int);
//	*p1 = 1;
//	DELETE(p1);
//	DELETE(p2);
//	DELETE(p3);
//}
////自定义类型
//void Test2()
//{
//	string *s1 =NEW(string);
//	*s1 = "abcd";
//	cout << s1->c_str() << endl;
//	string *s2 = NEW(string);
//	*s2= "erty";
//	cout << s2->c_str() << endl;
//	DELETE(s1);
//	DELETE(s2);
//}
////基本类型数组
//void Test3()
//{
//	int *p1 = NEW_ARRAY(int, 10);
//	*p1 = 2;
//	p1[1] = 3;
//	p1[9] = 10;
//	int *p2 = NEW_ARRAY(int, 10);
//	DELETE_ARRAY( p1);
//	DELETE_ARRAY( p2);
//}
////自定义类型数组
//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)
//	atexit(Singleton::Print);
//	system("pause");
//	return 0;
//}


本文出自 “小止” 博客,谢绝转载!

单例模式

标签:单例模式

原文地址:http://10541556.blog.51cto.com/10531556/1729331

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!