少特化一个函数 基本类型与类类型的区别方法

来源:互联网 发布:淘宝威露士官方旗舰店 编辑:程序博客网 时间:2024/04/29 02:22

#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;
}