SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送
メインページ   名前空間一覧   クラス階層   構成   ファイル一覧   名前空間メンバ   構成メンバ   ファイルメンバ  

dkutilTL.h

解説を見る。
00001 
00002 #include "AllLoad.h"
00003 //#include "dKingyoMemoryManager.h"
00004 
00005 
00006 #ifndef __dKingyoTemplateLibrary__
00007 #define __dKingyoTemplateLibrary__
00008 
00009 namespace dkutil{
00010     namespace DTL{
00011 
00012 
00013 //************************
00014 //必殺!テンプレート集
00015 //**************************
00016 
00017 
00018 template<class T,class OBJ>
00019 void GeneralPurposeFree(T func,OBJ obj)
00020 {
00021     if(obj){func(obj);obj=NULL;}
00022 }
00023 
00024 template<class Result,class T>
00025 Result GeneralPurposeMessage(T function,char *str,...){
00026     Result r;
00027     int c;
00028     static char s[c=1024+512];
00029     memset(s,'\0',sizeof(s));
00030     va_list VaList ;
00031     va_start( VaList , str) ;
00032     _vsnprintf( s ,c, str , VaList ) ;
00033     va_end( VaList ) ;
00034     r=(Result)function(s);
00035     return r;
00036 }
00037 
00038 
00039 //**********************************************************
00040 //シングルトン用テンプレートクラス
00041 //**********************************************************
00042 template <class T>
00043 class dk_singleton {
00047 public:
00048     dk_singleton() {}
00049     ~dk_singleton() { Release(); }
00051     T& operator*() { return *get(); }
00052     T* operator->() { return get(); }
00053     T* get() { CheckInstance(); return m_lpObj; }
00054     bool    isNull() const { return m_lpObj == NULL; }
00055     void Release();
00056     void CheckInstance();
00057     void InsertThreadLockUnlockFunction(VOIDFUNC lock,VOIDFUNC unlock){
00058         m_lockfunc=lock;m_unlockfunc=unlock;
00059     }
00060 protected:
00061     static T*   m_lpObj;
00062     static VOIDFUNC m_lockfunc,m_unlockfunc;
00063 };
00064 
00065 
00066 //  static なオブジェクト
00067 template <class T> T* dk_singleton<T>::m_lpObj = 0;
00068 template<class T> VOIDFUNC dk_singleton<T>::m_lockfunc = 0;
00069 template<class T> VOIDFUNC dk_singleton<T>::m_unlockfunc = 0;
00070 
00071 template <class T> void dk_singleton<T>::CheckInstance() {
00072     if (m_lpObj==NULL) {
00073         if(m_lockfunc !=NULL && m_unlockfunc != NULL){m_lockfunc();}
00074         if(m_lpObj==NULL){
00075             m_lpObj = new T;
00076         }
00077         if(m_lockfunc !=NULL && m_unlockfunc != NULL){m_unlockfunc();}
00078     }
00079 }
00080 
00081 template <class T> void dk_singleton<T>::Release() {
00082     if (m_lpObj!=NULL) {
00083         if(m_lockfunc !=NULL && m_unlockfunc != NULL){m_lockfunc();}
00084         if(m_lpObj!=NULL){      
00085             delete m_lpObj;
00086             m_lpObj = NULL;
00087         }
00088         if(m_lockfunc !=NULL && m_unlockfunc != NULL){m_unlockfunc();}
00089     }
00090 }
00091 
00092 
00093 
00094 template<DWORD n>
00095 struct GetKB{
00096     public:
00097         enum{value=1024*n,};
00098         //int GetValue(){return value;}
00099 };
00100 
00101 
00102 
00103 template<class T>
00104 inline T *GetSingleton(){
00105     static dk_singleton<T> g;
00106     return g.get();
00107 }
00108 
00109 
00110 
00111 
00112 //**********************************************************
00113 //allocator
00114 //**********************************************************
00115 //address - アドレスを返す。 
00116 //allocate - 領域を確保する。 
00117 //deallocate - 領域を解放する。 
00118 //construct - 領域にオブジェクトをコピーする。 
00119 //destroy - オブジェクトにデストラクタを呼出す。 
00120 
00121 template <class T> 
00122 class dkutil_allocator {
00123 protected:
00124 public:
00125   typedef size_t    size_type;
00126   typedef ptrdiff_t difference_type;
00127   typedef T*        pointer;
00128   typedef const T*  const_pointer;
00129   typedef T&        reference;
00130   typedef const T&  const_reference;
00131   typedef T         value_type;
00132   
00133     pointer       address(reference x) const
00134     { return &x; }
00135   const_pointer address(const_reference x) const
00136     { return &x; }
00137   pointer       allocate(size_type n, const void* hint = 0) {
00138     //cout << "allocate " << n << " of " << typeid(T).name() << endl;
00139     return new value_type[n];//new(n * sizeof(value_type));
00140     }
00141   void      deallocate(pointer p, size_type n = 0) {
00142     //cout << "deallocate " << n << " of " << typeid(T).name() << endl;
00143     //if( (p == 0) == (n == 0) ) MB("WHAT!!! deallocate error");
00144         if(p==NULL) MB("WHAT!!! deallocate error");
00145 
00146     delete [] p;
00147   }
00148   size_type max_size() const
00149     { return (size_t)-1 / sizeof(T); }
00150   void      construct(pointer p, const T& val) {
00151     //cout << "construct " << typeid(T).name()<< '(' << val << ')' << endl;
00152     new ((void*)p) T(val);
00153   }
00154   void      destroy(pointer p) {
00155    // cout << "destroy " << typeid(T).name() << endl;
00156   }
00157   char* _Charalloc(size_type n) {
00158     return allocate(n);
00159   }
00160 };
00161 
00162 //なんか、d金魚があほなことはじめましたなぁ普通にstd::vector使えよ!!?
00163 template<class T,class A=dkutil_allocator<T> >
00164 class basic_vector{
00165 protected:
00166     T *t;//メモリ
00167     A a;//あろけぇた
00168     DWORD count;//今入れることの出来る領域の添え字
00169     DWORD count_max;//今確保している最大値
00170     enum{
00171         enuEXTEND_ARRAY_NUM = 10,
00172     };
00173 public:
00174     basic_vector(){
00175         t=NULL;
00176         t=a.allocate(count_max=100);//とりあえず100個ほど^^;
00177         count=0;
00178     }
00179     virtual ~basic_vector(){
00180         a.deallocate(t,count_max);
00181     }
00182     void set_allocator(A *get){a=get;}
00183     T *data(){return t;}
00184     bool resize(DWORD num){
00185         if(t==NULL){
00186             t=a.allocate(count_max=100);
00187             if(t==NULL) return  false;
00188         }
00189 
00190         if(count_max >= num || num==0)//馬鹿値を入れた奴は排除
00191             return false;
00192         
00193         T *temp=a.allocate(num);
00194         if(!temp)
00195             return false;
00196         
00197         //こっちの方が速い。
00198         //しかし、classの=オペレータは呼ばれないので却下
00199         //memcpy((void *)temp,(const void *)t,sizeof(T)*count);
00200 
00201         DWORD i;
00202         for(i=0;i<count;i++){
00203             temp[i]=t[i];
00204         }
00205         a.deallocate(t,count_max);
00206         t=temp;
00207         count_max=num;
00208         return true;
00209     }
00210 
00211     int push_back(T set){
00212         if(count >= count_max)//大きくなってきたら
00213         {
00214             if(false==resize(count + enuEXTEND_ARRAY_NUM))
00215                 return -1;
00216         }
00217         t[count++]=set;//入れます。
00218         return count;
00219     }
00220     int pop_back(){count--;}
00221     bool empty(){return (count==0);}
00222     DWORD now_count(){return count;}
00223     DWORD max_count(){return count_max;}
00224     bool isOK(){return (t!=NULL);}
00225     void clear(){
00226         memset((void *)t,NULL,sizeof(T)*count_max);
00227         count=0;
00228     }
00229     virtual int sort(){
00230         return -100;//実装されてません。
00231     }
00232 };
00233 
00234 #ifdef USE_DKINGYO_OBJECT_TEST
00235 extern void basic_vector_test();
00236 #endif
00237 
00238 
00239 }//end of DTL
00240 }//end of dkutil
00241 
00242 #endif // end of __dKingyoTemplateLibrary__ defined

dKingyoUtilClass (dkutil)に対してMon Jun 9 01:32:41 2003に生成されました。 doxygen1.3