少特化一个函数 基本类型与类类型的区别方法
2010-12-07 17:52
337 查看
#ifndef FACTORYNODE_H
#define FACTORYNODE_H
#include<queue>
using namespace std;
// 可以自增长的自定义节点缓冲工厂
template<typename T>
class FactoryNode
{
protected:
queue<T*> m_queue;
enum {m_MAX = 100};//缓冲区节点最大数目
public:
// class constructor
FactoryNode();
// class destructor
virtual ~FactoryNode();
//获取一个T节点
virtual T* GetNode();
//归还一个T节点
virtual void GiveBackNode(T* pNode);
};
#endif // FACTORYNODE_H
// Class automatically generated by Dev-C++ New Class wizard
#include "factorynode.h" // class's header file
#include<iostream>
// class constructor
template<typename T>
FactoryNode<T>::FactoryNode<T>()
{
// insert your code here
}
// class destructor
template<typename T>
FactoryNode<T>::~FactoryNode()
{
T* pOne = NULL;
while(m_queue.size() != 0){
pOne = m_queue.front();
m_queue.pop();
delete pOne;
pOne = NULL;
}
}
//获取一个T节点
template<typename T>
T* FactoryNode<T>::GetNode()
{
T* pOne = NULL;
if(m_queue.size() > 0){
pOne = m_queue.front();
m_queue.pop();
}else{
pOne = new T;
}
return pOne;
}
//归还一个T节点,缓冲区内节点数目不能超过 m_MAX个
template<typename T>
void FactoryNode<T>::GiveBackNode(T* pNode)
{
if(m_queue.size() > m_MAX){delete pNode;}
else{m_queue.push(pNode);}
}
#include <cstdlib>
#include <iostream>
using namespace std;
#include "factorynode.cpp"
#define MAX_C 5
class MyClass
{
public:
int m_n;
~MyClass(){cout<<m_n<<":析构"<<endl;}
};
struct MyStruct
{
int m_n;
~MyStruct(){cout<<m_n<<":析构"<<endl;}
};
////////////////////////////////////////
template<typename T>
class IsClassT
{
template<typename C>
static long double test(int C::*);
template<typename C>
static char test(...);
public:
enum { Yes = sizeof( test< T >(0) ) == 1};
enum { No = !Yes};
};
template <int v>
struct Int2Type
{
//enum { value = v };
};
/*template <typename T>
void check()
{
if(IsClassT<T>::Yes)
{
std::cout <<"IsClassT" <<std::endl;
}
else
{
std::cout <<"!IsClasst" <<std::endl;
}
}*/
template<typename T>
void DoImpl(Int2Type<0>)
{
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
n->m_n = 10 + i;
cout<<n->m_n<<endl;
myUseList.GiveBackNode(n);
}
}
template<typename T>
void DoImpl(Int2Type<1>)
{
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
*n = 10 + i;
cout<<*n<<endl;
myUseList.GiveBackNode(n);
}
}
template<typename T>
void Do()
{
DoImpl<T>(Int2Type<IsClassT<T>::Yes>());
}
int main(int argc, char *argv[])
{
Do<int>();
cout<<"---------int"<<endl<<endl;
Do<float>();
cout<<"---------float"<<endl<<endl;
Do<MyStruct>();
cout<<"---------MyStruct"<<endl<<endl;
Do<MyClass>();
cout<<"---------MyClass"<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------------------------------
以下是区分是否为基本类型的第二种方法,效果与上各有千秋
#include <cstdlib>
#include <iostream>
#include "111.cpp"
using namespace std;
#define MAX_C 5
class MyClass
{
public:
int m_n;
~MyClass(){cout<<m_n<<":析构"<<endl;}
};
struct MyStruct
{
int m_n;
~MyStruct(){cout<<m_n<<":析构"<<endl;}
};
template <int v>
struct Int2Type{};
class NullType;
namespace TL
{
template<class T,class U>
struct TypeList
{
typedef T head;
typedef U tail;
};
#define TYPELIST_1(T1) TypeList<T1,NullType>
#define TYPELIST_2(T1,T2) TypeList<T1,TYPELIST_1(T2)>
#define TYPELIST_3(T1,T2,T3) TypeList<T1,TYPELIST_2(T2,T3)>
#define TYPELIST_4(T1,T2,T3,T4) TypeList<T1,TYPELIST_3(T2,T3,T4)>
//-----------------------------------------------------------------
//Length
template<class Tlist>struct Length;
template<>struct Length<NullType>
{
enum{value = 0};
};
template<class T,class U>
struct Length<TypeList<T,U> >
{
enum{value = 1 + Length<U>::value};
};
//---------------------------------------------------------------
//利用索引查找对象
template<class T,int U>struct TypeAt;
template<class head,class tail>
struct TypeAt<TypeList<head,tail>,0>
{
typedef head Result;
};
template<class head,class tail,int i>
struct TypeAt<TypeList<head,tail>,i>
{
typedef typename TypeAt<tail,i - 1>::Result Result;
};
//---------------------------------------------------------------
//indexof
template<class TList,class T>struct IndexOf;
template<class T>
struct IndexOf<NullType,T>
{
enum{value = -1};
};
template<class Tail,class T>
struct IndexOf<TypeList<T,Tail>,T>
{
enum{value = 0};
};
template<class Head,class Tail,class T>
struct IndexOf<TypeList<Head,Tail>,T>
{
private:
enum{temp = IndexOf<Tail,T>::value};
public:
enum{value = temp == -1 ? -1 : 1 + temp};
};
//---------------------------------------------------------------
//Append
template<class TList,class T>struct Append;
template<>
struct Append<NullType,NullType>
{
typedef NullType Result;
};
template<class T>
struct Append<NullType,T>
{
typedef TYPELIST_1(T) Result;
};
template<class head,class Tail>
struct Append<NullType,TypeList<head,Tail> >
{
typedef TypeList<head,Tail> Result;
};
template<class head,class Tail,class T>
struct Append<TypeList<head,Tail>,T>
{
typedef TypeList<head,typename Append<Tail,T>::Result> Result;
};
//---------------------------------------------------------------
//Erase
template<class TList,class T>struct Erase;
template<class T>
struct Erase<NullType,T>
{
typedef NullType Result;
};
template<class T,class tail>
struct Erase<TypeList<T,tail>,T>
{
typedef tail Result;
};
template<class head,class tail,class T>
struct Erase<TypeList<head,tail>,T>
{
typedef TypeList<head,typename Erase<tail,T>::Result> Result;
};
//---------------------------------------------------------------
//NoDuplicate
template<class TList>struct NoDuplicate;
template<>struct NoDuplicate<NullType>
{
typedef NullType Result;
};
template<class head,class tail>
struct NoDuplicate<TypeList<head,tail> >
{
private:
typedef typename NoDuplicate<tail>::Result L1;
typedef typename Erase<L1,head>::Result L2;
public:
typedef TypeList<head,L2> Result;
};
}
using namespace TL;
typedef TYPELIST_3(int,float,double) BasicType;
//typedef TYPELIST_2(MyClass,MyStruct)PointerType;
template<class T> void Do2Imp(Int2Type<0>)
{//类类型
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
n->m_n = 10 + i;
cout<<n->m_n<<endl;
myUseList.GiveBackNode(n);
}
}
template<class T>void Do2Imp(Int2Type<1>)
{//基本类型
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
*n = 10 + i;
cout<<*n<<endl;
myUseList.GiveBackNode(n);
}
}
template<class T>class IsClass
{
public:
enum{Yes = IndexOf<BasicType,T>::value > -1};
enum{No = !Yes};
};
template<class T>void Do2()
{
Do2Imp<T>(Int2Type<IsClass<T>::Yes>());
}
int main(int argc, char *argv[])
{
Do2<int>();
cout<<"---------int"<<endl<<endl;
Do2<float>();
cout<<"---------float"<<endl<<endl;
Do2<MyStruct>();
cout<<"---------MyStruct"<<endl<<endl;
Do2<MyClass>();
cout<<"---------MyClass"<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#define FACTORYNODE_H
#include<queue>
using namespace std;
// 可以自增长的自定义节点缓冲工厂
template<typename T>
class FactoryNode
{
protected:
queue<T*> m_queue;
enum {m_MAX = 100};//缓冲区节点最大数目
public:
// class constructor
FactoryNode();
// class destructor
virtual ~FactoryNode();
//获取一个T节点
virtual T* GetNode();
//归还一个T节点
virtual void GiveBackNode(T* pNode);
};
#endif // FACTORYNODE_H
// Class automatically generated by Dev-C++ New Class wizard
#include "factorynode.h" // class's header file
#include<iostream>
// class constructor
template<typename T>
FactoryNode<T>::FactoryNode<T>()
{
// insert your code here
}
// class destructor
template<typename T>
FactoryNode<T>::~FactoryNode()
{
T* pOne = NULL;
while(m_queue.size() != 0){
pOne = m_queue.front();
m_queue.pop();
delete pOne;
pOne = NULL;
}
}
//获取一个T节点
template<typename T>
T* FactoryNode<T>::GetNode()
{
T* pOne = NULL;
if(m_queue.size() > 0){
pOne = m_queue.front();
m_queue.pop();
}else{
pOne = new T;
}
return pOne;
}
//归还一个T节点,缓冲区内节点数目不能超过 m_MAX个
template<typename T>
void FactoryNode<T>::GiveBackNode(T* pNode)
{
if(m_queue.size() > m_MAX){delete pNode;}
else{m_queue.push(pNode);}
}
#include <cstdlib>
#include <iostream>
using namespace std;
#include "factorynode.cpp"
#define MAX_C 5
class MyClass
{
public:
int m_n;
~MyClass(){cout<<m_n<<":析构"<<endl;}
};
struct MyStruct
{
int m_n;
~MyStruct(){cout<<m_n<<":析构"<<endl;}
};
////////////////////////////////////////
template<typename T>
class IsClassT
{
template<typename C>
static long double test(int C::*);
template<typename C>
static char test(...);
public:
enum { Yes = sizeof( test< T >(0) ) == 1};
enum { No = !Yes};
};
template <int v>
struct Int2Type
{
//enum { value = v };
};
/*template <typename T>
void check()
{
if(IsClassT<T>::Yes)
{
std::cout <<"IsClassT" <<std::endl;
}
else
{
std::cout <<"!IsClasst" <<std::endl;
}
}*/
template<typename T>
void DoImpl(Int2Type<0>)
{
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
n->m_n = 10 + i;
cout<<n->m_n<<endl;
myUseList.GiveBackNode(n);
}
}
template<typename T>
void DoImpl(Int2Type<1>)
{
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
*n = 10 + i;
cout<<*n<<endl;
myUseList.GiveBackNode(n);
}
}
template<typename T>
void Do()
{
DoImpl<T>(Int2Type<IsClassT<T>::Yes>());
}
int main(int argc, char *argv[])
{
Do<int>();
cout<<"---------int"<<endl<<endl;
Do<float>();
cout<<"---------float"<<endl<<endl;
Do<MyStruct>();
cout<<"---------MyStruct"<<endl<<endl;
Do<MyClass>();
cout<<"---------MyClass"<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------------------------------
以下是区分是否为基本类型的第二种方法,效果与上各有千秋
#include <cstdlib>
#include <iostream>
#include "111.cpp"
using namespace std;
#define MAX_C 5
class MyClass
{
public:
int m_n;
~MyClass(){cout<<m_n<<":析构"<<endl;}
};
struct MyStruct
{
int m_n;
~MyStruct(){cout<<m_n<<":析构"<<endl;}
};
template <int v>
struct Int2Type{};
class NullType;
namespace TL
{
template<class T,class U>
struct TypeList
{
typedef T head;
typedef U tail;
};
#define TYPELIST_1(T1) TypeList<T1,NullType>
#define TYPELIST_2(T1,T2) TypeList<T1,TYPELIST_1(T2)>
#define TYPELIST_3(T1,T2,T3) TypeList<T1,TYPELIST_2(T2,T3)>
#define TYPELIST_4(T1,T2,T3,T4) TypeList<T1,TYPELIST_3(T2,T3,T4)>
//-----------------------------------------------------------------
//Length
template<class Tlist>struct Length;
template<>struct Length<NullType>
{
enum{value = 0};
};
template<class T,class U>
struct Length<TypeList<T,U> >
{
enum{value = 1 + Length<U>::value};
};
//---------------------------------------------------------------
//利用索引查找对象
template<class T,int U>struct TypeAt;
template<class head,class tail>
struct TypeAt<TypeList<head,tail>,0>
{
typedef head Result;
};
template<class head,class tail,int i>
struct TypeAt<TypeList<head,tail>,i>
{
typedef typename TypeAt<tail,i - 1>::Result Result;
};
//---------------------------------------------------------------
//indexof
template<class TList,class T>struct IndexOf;
template<class T>
struct IndexOf<NullType,T>
{
enum{value = -1};
};
template<class Tail,class T>
struct IndexOf<TypeList<T,Tail>,T>
{
enum{value = 0};
};
template<class Head,class Tail,class T>
struct IndexOf<TypeList<Head,Tail>,T>
{
private:
enum{temp = IndexOf<Tail,T>::value};
public:
enum{value = temp == -1 ? -1 : 1 + temp};
};
//---------------------------------------------------------------
//Append
template<class TList,class T>struct Append;
template<>
struct Append<NullType,NullType>
{
typedef NullType Result;
};
template<class T>
struct Append<NullType,T>
{
typedef TYPELIST_1(T) Result;
};
template<class head,class Tail>
struct Append<NullType,TypeList<head,Tail> >
{
typedef TypeList<head,Tail> Result;
};
template<class head,class Tail,class T>
struct Append<TypeList<head,Tail>,T>
{
typedef TypeList<head,typename Append<Tail,T>::Result> Result;
};
//---------------------------------------------------------------
//Erase
template<class TList,class T>struct Erase;
template<class T>
struct Erase<NullType,T>
{
typedef NullType Result;
};
template<class T,class tail>
struct Erase<TypeList<T,tail>,T>
{
typedef tail Result;
};
template<class head,class tail,class T>
struct Erase<TypeList<head,tail>,T>
{
typedef TypeList<head,typename Erase<tail,T>::Result> Result;
};
//---------------------------------------------------------------
//NoDuplicate
template<class TList>struct NoDuplicate;
template<>struct NoDuplicate<NullType>
{
typedef NullType Result;
};
template<class head,class tail>
struct NoDuplicate<TypeList<head,tail> >
{
private:
typedef typename NoDuplicate<tail>::Result L1;
typedef typename Erase<L1,head>::Result L2;
public:
typedef TypeList<head,L2> Result;
};
}
using namespace TL;
typedef TYPELIST_3(int,float,double) BasicType;
//typedef TYPELIST_2(MyClass,MyStruct)PointerType;
template<class T> void Do2Imp(Int2Type<0>)
{//类类型
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
n->m_n = 10 + i;
cout<<n->m_n<<endl;
myUseList.GiveBackNode(n);
}
}
template<class T>void Do2Imp(Int2Type<1>)
{//基本类型
FactoryNode<T> myTempList;
FactoryNode<T> myUseList;
T* n = NULL;
for(int i = 0; i < MAX_C; i++){
n = myTempList.GetNode();
*n = 10 + i;
cout<<*n<<endl;
myUseList.GiveBackNode(n);
}
}
template<class T>class IsClass
{
public:
enum{Yes = IndexOf<BasicType,T>::value > -1};
enum{No = !Yes};
};
template<class T>void Do2()
{
Do2Imp<T>(Int2Type<IsClass<T>::Yes>());
}
int main(int argc, char *argv[])
{
Do2<int>();
cout<<"---------int"<<endl<<endl;
Do2<float>();
cout<<"---------float"<<endl<<endl;
Do2<MyStruct>();
cout<<"---------MyStruct"<<endl<<endl;
Do2<MyClass>();
cout<<"---------MyClass"<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
相关文章推荐
- java基本数据类型传递与引用传递区别详解(一个大神的解析,不是我。。)
- 类类型变量与基本类型变量在做函数参数时的区别
- lua 的基本数据类型和函数应用 . :符号的区别
- 第64+65讲 函数基本数据类型和数组方式 成员方法
- Java中int和Integer的区别:int 是基本类型,直接存数值 ;integer是对象,用一个引用指向这个对象
- JAVA基础知识回顾(基本数据类型,switch参数,equals与==的区别,Object有哪些公用方法)-1
- 【JavaScript】基本数据类型与引用数据类型区别(及为什么String、Boolean、Number基本数据类型会有属性和方法?)
- C#取得一个函数或方法的参数类型参数名称返回值类型
- 数组和指针、数组指针和指针数组、函数指针和指针函数、数组标识符的意义、静态和动态创建的数组的本质区别、标识符类型判断方法
- Python中list数据类型的基本函数方法
- C#File类常用的文件操作方法(创建、移动、删除、复制等) File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和
- JavaScript中有一个call和apply方法,其作用基本相同,但也有略微的区别
- ios 中调用函数的方法是消息传递,这个和普通的函数调用的区别是,你可以随时对一个对象传递任何消息,而不需要在编译的时候声明这些方法。所以Objective-C可以在runtime的时候
- 一个基本类型在方法中修改引起的思考
- switch,break和continue区别,函数调用方法,默认类型提升
- 例题4-7向一个方法的基本数据类型参数传值/4-8引用类型参数
- C Primer plus 10.2 初始化一个double类型的数组,然后把该数组的内容拷贝至3个其它的数组中,使用带数组的表示方法的函数进行第一份拷贝,使用带指针表示方法和指针递增的函..
- 深入解析js中基本数据类型与引用类型,函数参数传递的区别
- C# 001 --- 正确的删除一个控件的事件函数 --- 解决错误: “A”不包含“B”的定义,并且找不到可接受类型为“A”的第一个参数的扩展方法“B”(是否缺少 using 指令或程序集引用?)
- python和php的基本数据类型的定义赋值及常用方法函数