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

dKingyoGameManager.h

解説を見る。
00001 #include "stdafx.h"
00002 
00003 #ifndef DONOT_USE_GAMEMANAGER__ //こいつが宣言されていたらコンパイルしない。
00004 
00005 #ifndef _DKINGYO_GAME_MANAGER__
00006 #define _DKINGYO_GAME_MANAGER__
00007 
00008 
00009 #include "dKingyoMemoryManager.h"
00010 #include "dKingyoFrameMemory.h"
00011 #include "dKingyoDataWrapper.h"
00012 #include "dkutilDebug.h"
00013 #include "dkutilTL.h"
00014 
00015 
00016 
00017 namespace dkutil{
00018     namespace dkingyoGM{
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 //以下のコメントアウトした奴はあるだけ邪魔。
00029 //dKingyoIsWarehouserクラスに任せよう^^ d金魚は倉庫業者〜〜^^;
00030 
00031 //タスクを参照するためのデータ構造
00032 //typedef struct tugTasClassManagement{
00033 //  std::string string_str;
00034 //  DWORD ID;
00035 //  TasClassTask *p;
00036 //}TasClassManagement;
00037 
00038 //すでに作ってあるタスクのデータ
00039 //typedef struct tugTasClassData{
00040 //  std::string string_str;
00041 //  DWORD ID;
00042 //  TasClassTask data;
00043 //}TasClassData;
00044 
00045 
00046 //タスクネームのマクロとか
00047 #ifdef _DEBUG
00048 # define _SetDebugTaskName(tasclass_p,s) strncpy(tasclass_p->name,s,TASKNAME_SIZE)
00049 # define _NULLDebugTaskName(tasclass_p) ZeroMemory(tasclass_p->name,TASKNAME_SIZE)
00050 
00051 #else
00052 # define _SetDebugTaskName(tasclass_p,s)
00053 # define _NULLDebugTaskName(tasclass_p)
00054 
00055 #endif 
00056 
00057 
00058 
00059 
00060 //global (あまり使いたくなかったけど・・・
00061 
00062 
00063 
00064 
00065 //名前のとおり、タスクメモリーのプールです^^
00066 //全部スタティックメンバーって邪道だなぁ
00067 class OnlyForTaskMemoryPool{
00068     enum{
00069         tcsnum=100,//string max
00070     };
00071     static dkutil::memory::dKingyoArrayOneByOneDynamic<TasClassTask> m;
00072     static char taskcountstr[tcsnum];//タスクのデフォルトデバック名
00073     static DWORD m_counter;
00074     static DWORD m_now_count;
00075 public:
00076     OnlyForTaskMemoryPool(){
00077         static int once =Initialize();
00078     }
00079     ~OnlyForTaskMemoryPool(){}
00080     static int Initialize(){
00081         m_counter=0;
00082         m_now_count=0;
00083         memset(taskcountstr,0,sizeof(taskcountstr));
00084         _snprintf(taskcountstr,tcsnum,"taskcount=%d",m_counter);
00085         return 0;
00086     }
00087         
00088     static inline TasClassTask *GetNewTaskDomain(DWORD *ID,
00089                                                         int state,int type,int prio,
00090                                                         DWORD counter,VOIDFUNC setfunc,VOIDFUNC setkill,
00091                                                         size_t setsize=0,char *debugname=taskcountstr
00092     ){
00093     TasClassTask *p=m.GetArray(ID);
00094 #ifdef _DEBUG
00095         if(!p){MB("Can't GetNewTaskDomain");throw -1;}
00096 #endif
00097         p->m_state = state;
00098         p->m_type = type;
00099         p->m_prio = prio;
00100         p->counter = counter;
00101         p->func = setfunc;
00102         p->kill = setkill;
00103         p->ID = *ID;
00104         p->worksize = setsize;
00105         p->next=p->prev=NULL;//next prev はNULL
00106         _SetDebugTaskName(p,debugname);
00107 #ifdef _DEBUG
00108         m_counter++;//カウンタインクリメント
00109         _snprintf(taskcountstr,tcsnum,"taskcount=%d",m_counter);//次の名前を入れる。
00110 #endif
00111         m_now_count++;
00112         return p;
00113     }
00114     static inline int FreeDomain(DWORD *ID){
00115         m_now_count--;
00116         return m.ReleaseArray(ID);
00117     }
00118     static inline TasClassTask *GetDomain(DWORD n){
00119         //TasClassTask *p = m.Data(
00120         return &m.Data()[n];
00121     }
00122     static inline DWORD GetCount(){return m_now_count;}
00123 };
00124 
00125 inline TasClassTask *CreateTask(int prio,DWORD counter,VOIDFUNC exe,VOIDFUNC kill)
00126 {
00127     DWORD ID;
00128     return OnlyForTaskMemoryPool::GetNewTaskDomain(
00129                                             &ID,enuRUN,enuDEFAULT,prio,
00130                                                         counter,exe,kill);
00131 }
00132 
00133 inline TasClassTask *CreateNoCounterTask(int prio,VOIDFUNC exe,VOIDFUNC kill)
00134 {
00135     return CreateTask(prio,NO_NEXT_COUNTER,exe,kill);
00136 }
00137 
00138 
00139 class dKingyoTaskPriorityMap : public dkutil::memory::dKingyoDataWrapperMap<int,TasClassTask *>{
00140 public:
00141     dKingyoTaskPriorityMap(){}
00142     virtual ~dKingyoTaskPriorityMap(){}
00143     virtual TasClassTask *GetData(int key){
00144         int prev_prio=HIGHEST_PRIORITY_OF_SYSTEM_TASK;//前のプライオリティを保存
00145         for(map_it it=m.begin();it!=m.end();it++){
00146             //前の優先順序  取得したいイテレータ(int keyの入れるべき場所) < 今の優先順序
00147             if((*it).first >= key && prev_prio <= key){
00148                 return m[(*it).first];
00149             }
00150             prev_prio=(*it).first;
00151             it++;
00152         }
00153         //throw -1;//not found
00154         return NULL;
00155     }
00156 };
00157 
00158 /*      
00159 typedef dKingyoDataWrapperOverwriteAcceptMap<int,TasClassTask *> GAMEMANAGER_TASK_SERVER
00160 
00161 inline  GAMEMANAGER_TASK_SERVER *GetGameManagerTaskServer(){
00162     //dkutil::singleton<dKingyoDataWrapperMap<int,VOIDFUNC> > m;
00163     static GAMEMANAGER_TASK_SERVER m;
00164     return &m;
00165 }
00166 */
00167 
00168 
00169 
00170         
00171 
00172 
00173 
00174 //このクラスはシングルトンで生成すること!!
00175 class dKingyoGameManager_v01 : IdKingyoGameManager<TasClassTask>{
00176 protected:
00177     OnlyForTaskMemoryPool mpool;//タスクメモリプール。
00178     //番兵さんを格納しておきます^^ int = priority , TasClassTask * = 番兵
00179     dKingyoTaskPriorityMap msentinel;
00180     //KillingTime用のキューです^^;
00181     typedef std::deque<TasClassTask *> KILLQUEUE;
00182     KILLQUEUE killq;
00183 
00184     TasClassTask *top,*tail;//頭と尻尾
00185     TasClassTask *now;//今、実行中のタスク
00186     DWORD topID,tailID;
00187     bool m_InitedFlag;
00188 
00189     //番兵たちによって探させる
00190     inline TasClassTask *SarchInsertTopbySentinel(int dekaprio){
00191             TasClassTask *temp;
00192             return (NULL != (temp=msentinel.GetData(dekaprio))) ? temp : GetTop();
00193             //アホ値が帰ってきたら最初からサーチすることになる。
00194     }
00195     
00196     int InitGameManager_v01();
00197 public:
00198     dKingyoGameManager_v01(){
00199         m_InitedFlag=false;
00200         topID=tailID=NULL;
00201         top=tail=now=NULL;
00202         InitGameManager_v01();
00203     }
00204     virtual ~dKingyoGameManager_v01(){}
00205     virtual int KillingTime();
00206     virtual bool isOK(){return m_InitedFlag;}
00207     virtual int TaskRun();
00208     virtual int OpeningRun();
00209     inline TasClassTask *GetTop(){return top;}
00210     inline TasClassTask *GetTail(){return tail;}
00211     inline TasClassTask *GetNowTask(){return now;}
00212     //番兵を入れる
00213     inline bool SetSentinelTask(TasClassTask *syuzin){
00214     #ifdef _DEBUG
00215         if(false==msentinel.SetData(syuzin->m_prio,syuzin)){
00216             MB("Please break!!! sentinel data overlapped!!!Don't push the ok buttom!!!");
00217         }
00218         return true;
00219     #else
00220         return msentinel.SetData(syuzin->m_prio,syuzin);
00221     #endif
00222     }
00223     
00224     virtual int InsertTask( TasClassTask *item){
00225         //int i;
00226         TasClassTask *t;
00227         
00228         t=SarchInsertTopbySentinel(item->m_prio);
00229 
00230         //効率が悪いが簡単なので順次探索^^;(最近のPCの性能ならば大丈夫であろう
00231         while (t != NULL) {
00232             // 指定プライオリティの最後尾にタスクを追加します
00233             if (t->m_prio >  item->m_prio) {
00234                 //ODS("OKOK\n");
00235                 item->prev         = t->prev;   // 追加タスクの前部リンクに検索タスクの前部リンクをセット
00236                 item->next        = t;      // 追加タスクの後部リンクに検索タスクをセット
00237                 t->prev->next = item;           // 検索タスクの前タスクの後部リンクに追加タスクをセット
00238                 t->prev       = item;           // 検索タスクの前部リンクに追加タスクをセット
00239                 return 0;//成功
00240             }
00241             // 検索位置を進める     
00242             t = t->next;
00243         }
00244         return -1;//ダメです。
00245     }
00246     inline TasClassTask *SarchTask(int get_prio_min,int get_prio_max,int *GetNum,TasClassTask *confirmation=NULL)
00247     {
00248         TasClassTask *t;
00249         int prio_reserved;
00250         TasClassTask *t_reserved=NULL;
00251         bool findflag=false;
00252         int count=0;
00253         t=SarchInsertTopbySentinel(get_prio_min);
00254         while (t != NULL) {
00255             //普通にアドレス指定で見つけたらこれ。
00256             if (t == confirmation){
00257                 return t;
00258             }
00259             //ひとつだけを検索しろと言われたら
00260             if(get_prio_min==get_prio_max){
00261                 if(findflag==false){//まだ見つけていない。
00262                     if(t->m_prio == get_prio_min){ //同じプライオリティを見つけた。
00263                         prio_reserved=get_prio_min;//保存しておく
00264                         t_reserved=t;//保存しておく
00265                         findflag=true;//すでに見つけたことを知らせる
00266                         count++;
00267                     }
00268                 }else{//すでに見つけている場合。
00269                     if(t->m_prio ==get_prio_min){//同じプライオリティを探す
00270                         count++;
00271                     }else{
00272                         *GetNum=count;//いくつ見つけたか?を入れる。
00273                         return t_reserved;//最初に見つけたポインタを返す。
00274                     }
00275                 }
00276                 //いやいや、min <= 対象 <= maxまでを検索しろ!といわれたら。
00277             }else{
00278                 if(findflag==false){//まだ見つけていない
00279                     if(t->m_prio >= get_prio_min && t->m_prio <= get_prio_max)
00280                     {//min < t < max
00281                         t_reserved=t;//保存しておく
00282                         findflag=true;
00283                         count++;
00284                     }
00285                 }else{
00286                     if(t->m_prio >= get_prio_min && t->m_prio <= get_prio_max){
00287                         count++;
00288                     }else{
00289                         *GetNum = count;
00290                         return t_reserved;
00291                     }
00292                 }
00293             }
00294             // 検索位置を進める     
00295             t = t->next;
00296         }
00297         return NULL;//ダメです。
00298     }
00299     virtual int DeleteTask( TasClassTask *item){
00300         killq.push_back(item);
00301         return 0;
00302     }
00303     virtual int DeleteTask(int priority){
00304         return DeletePriorityTask(priority,priority);
00305     }
00306     virtual int DeletePriorityTask(int min,int max)
00307     {//minからmaxまでのプライオリティ範囲のタスクを消す
00308         TasClassTask *temp;
00309         int GetNum;
00310         if(NULL == (temp=SarchTask(min,max,&GetNum))) return -1;
00311         //GetNumでもらった数だけループする。
00312         for(int i=0;i<GetNum;i++){
00313             killq.push_back(temp);
00314             temp = temp->next;
00315         }   
00316         return 0;
00317     }
00318 
00319     virtual int DebugWindow(int,int){return 0;};
00320     virtual int GetVersion(){return 0x0001;}//Version0.01らしい。
00321     inline OnlyForTaskMemoryPool *GetTaskPool(){return &mpool;}
00322 };
00323 
00325 inline dkutil::memory::dKingyoFrameMemory &GetGlobalDoamin(){
00326     static dkutil::memory::dKingyoFrameMemory m_fm;
00327     static bool flag =(bool) m_fm.Construct(dkutil::DTL::GetKB<3>::value);
00328     return m_fm;
00329 }
00330 inline int InitOnceGlobal(){
00331     static bool flag=false;
00332     if(flag==false){
00333         DKUTIL_MULTITHREAD_SYNDROME_LOCK();
00334         if(false==flag){
00335             GAMEMANAGER_DEFAULTGLOBALDATA dd;
00336             GetSingletonGlobalProcess().DefaultGameManagerGlobalMemorySetting(&dd);
00337             GetGlobalDoamin().AttachFrame((void *)&dd,sizeof(dd),enuDefaultTasClassData,
00338                 NULL,"GlobalMemory of dKingyoGameManager(TasClass)");
00339             flag=true;
00340             DKUTIL_MULTITHREAD_SYNDROME_UNLOCK();
00341             return 0;
00342         }
00343         DKUTIL_MULTITHREAD_SYNDROME_UNLOCK();
00344         return -1;
00345     }
00346     return -1;
00347 }
00348 
00349 
00350 inline int AttachGlobal(void *mem,size_t size,DWORD ID,
00351                                         VOIDFUNC ReleaseFunc,char *error_str="no comment"){
00352     return GetGlobalDoamin().AttachFrame(mem,size,ID,ReleaseFunc,error_str);
00353 }
00354 inline int PopTailGlobal(char *error_str="no comment"){
00355     return GetGlobalDoamin().PopTailFrame(error_str);
00356 }
00357 inline void *ChangeFromIDGlobal(DWORD id){
00358     return NULL;//実装してない^^;    
00359 }
00360 
00361 
00362 #ifdef USE_DKINGYO_OBJECT_TEST
00363 
00364 extern void dKingyoGameManager_v01_Test();
00365 
00366 #endif //end of USE_DKINGYO_OBJECT_TEST
00367 
00368 
00369 }//end of dkingyoGM
00370 }//end of dkutil
00371 
00372 #endif // end of once include
00373 
00374 #endif // end of  DONOT_USE_GAMEMANAGER__

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