D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/cl_tmpl_dbl_mngr.h

00001 /* LICENSE_BEGIN
00002 SutoiTools library supply support for debugging, testing and 
00003 setting good bases for you application. It is part of the 
00004 SugoiEngine project.
00005 Copyright (C) 2006 Allusse Yannick (yannick dot allusse at laposte dot net)
00006 
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 LICENSE_END
00022 */
00023 
00024 /* \file SG_TLS_CL_TMPL_DBL_MNGR.h
00025  * \brief Template double manager to manage all kind of object, and access them via two different ID.
00026  * \author Yannick Allusse
00027 */
00028 #ifndef SG_TLS_CL_TMPL_MNGR_H
00029 #define SG_TLS_CL_TMPL_MNGR_H
00030 
00031 
00032 
00033 #include "SugoiTools\config.h"
00034 #include "SugoiTools\cl_base_obj.h"
00035 #include "SugoiTools\cl_template_manager.h"
00036 #include "SugoiTools\cl_base_dbl_obj.h"
00037 
00038 
00039 namespace SGE{
00040 
00041 
00042 template <class T, typename P1=std::string, typename P2=int>  
00043 class CL_TMPL_DBL_MANAGER 
00044 
00045 {
00046           public:
00047                     typedef typename CL_BASE_DBL_OBJ<P1, P2>          TplObj;             
00048                     typedef typename CL_BASE_OBJ<P1>                            TplObj1;  
00049                     typedef typename CL_BASE_OBJ<P2>                            TplObj2;  
00050                     typedef typename CL_TEMPLATE_OBJECT_MANAGER<TplObj1, P1>    TMngr1;
00051                     typedef typename CL_TEMPLATE_OBJECT_MANAGER<TplObj2, P2>    TMngr2;
00052                     typedef   typename CL_TMPL_DBL_MANAGER<T,P1, P2>  TplDblManager;
00053                     typedef typename TplObj *                                             TplHdle;  
00054                     typedef   P1&                                                                                       IDRef1;             
00055                     typedef   P2&                                                                                       IDRef2;             
00056                     typedef   P1                                                                                        IDRef1Cst;          
00057                     typedef   P2                                                                                        IDRef2Cst;          
00058                     typedef typename std::map<P1, TplObj1 *>          TplMap1;  
00059                     typedef typename std::map<P2, TplObj2 *>          TplMap2;  
00060                     typedef typename std::pair<P1,          TplObj1 *>          TplPair1; 
00061                     typedef typename std::pair<P2,          TplObj2 *>          TplPair2; 
00062                     typedef typename TplMap1::iterator                          iterator1;          
00063                     typedef typename TplMap2::iterator                          iterator2;          
00064                     
00065 
00066           protected :
00067                     TMngr1    *Mngr1;   
00068                     TMngr2    *Mngr2; 
00069 
00070 
00071           public:
00072 
00079                     _SG_TLS_INLINE
00080                     CL_TMPL_DBL_MANAGER(TplDblManager *_RemoteManager)
00081                     {
00082                               if (_RemoteManager)
00083                               {
00084                                         Mngr1 = new TMngr1(_RemoteManager->GetMngr1());
00085                                         Mngr2 = new TMngr2(_RemoteManager->GetMngr2());                       
00086                               }
00087                               else
00088                               {
00089                                         Mngr1 = new TMngr1(NULL);
00090                                         Mngr2 = new TMngr2(NULL);                         
00091                               }
00092                     }
00093 
00097                     _SG_TLS_INLINE 
00098                     ~CL_TMPL_DBL_MANAGER          (void)
00099                     {
00100                               delete Mngr1;
00101                               delete Mngr2;
00102                     };
00103 
00107                     _SG_TLS_INLINE 
00108                     TMngr1 *GetMngr1()
00109                     {         return Mngr1;                 }
00110 
00114                     _SG_TLS_INLINE 
00115                     TMngr2 *GetMngr2()
00116                     {         return Mngr2;                 }
00117 
00118 
00119           //=============================================================
00120           //================= Add Functions  ============================
00121           //=============================================================
00128                     virtual 
00129                     _SG_TLS_INLINE
00130                               bool IsObjValide(TplHdle _Obj)
00131                               {
00132                                         if (_Obj)
00133                                                   return true;
00134                                         return false;
00135                               }
00144                     _SG_TLS_INLINE 
00145                     TplHdle   
00146                     Add(IDRef1Cst _ID1, IDRef2Cst _ID2)
00147                     {
00148                               //can't ask the manager 1 and 2 to create the obj
00149                               //so we first look for it, and then create if manually if needed...
00150                               TplObj1 * Obj1 = Mngr1->Get(_ID1);
00151                               TplObj2 * Obj2 = Mngr2->Get(_ID2);
00152                               
00153                               if(!Obj1 && !Obj2)
00154                               {//create the obj and add it
00155                                         TplHdle Obj = new TplObj(_ID1, _ID2);
00156                                         Mngr1->AddObj(Obj);
00157                                         Mngr2->AddObj(Obj);
00158                               }
00159                               else if(Obj1 && !Obj2)
00160                               {
00161                                         Obj2 = Mngr2->AddObj(dynamic_cast<TplObj2* >(Obj1));                                      
00162                                         
00163                               }
00164                               else if(!Obj1 && Obj2)
00165                               {
00166                                         Obj1 = Mngr1->AddObj(dynamic_cast<TplObj1* >(Obj2));                                      
00167                               }
00168 
00169                               return (TplObj*)Obj1;
00170                     }
00171 
00180                     _SG_TLS_INLINE 
00181                     TplHdle   
00182                     Add(IDRef1Cst _ID1)
00183                     {
00184                               //can't ask the manager 1 
00185                               //so we first look for it, and then create if manually if needed...
00186                               TplObj1 * Obj1 = Mngr1->Get(_ID1);
00187                               
00188                               if(!Obj1)
00189                               {//create the obj and add it
00190                                         TplObj1 * Obj1 = new TplObj(_ID1);
00191                                         Mngr1->AddObj(Obj1);
00192 
00193                               }
00194                               return (TplObj*)Obj1;
00195                     }
00196 
00197 
00198 
00203                     _SG_TLS_INLINE 
00204                     TplHdle   
00205                     Add(IDRef2Cst _ID2)
00206                     {
00207                               //can't ask the manager 2
00208                               //so we first look for it, and then create if manually if needed...
00209                               TplObj2 * Obj2 = Mngr2->Get(_ID2);
00210                               
00211                               if(!Obj2)
00212                               {//create the obj and add it
00213                                         TplObj2 * Obj2 = new TplObj(_ID2);
00214                                         Mngr2->AddObj(Obj2);
00215 
00216                               }
00217                               return (TplObj*)Obj2;
00218                     }
00219 
00226                     _SG_TLS_INLINE 
00227                     TplHdle   
00228                     AddObj(TplHdle _Obj)
00229                     {
00230                               TplHdle Obj = Mngr1->AddObj(_Obj);
00231                               if (IsObjValide(Obj))
00232                                          Mngr2->AddObj(Obj);
00233                               SG_Assert(IsObjValide(Obj), "Could not add existing object");
00234                               return Obj;
00235                     }
00236 
00244                     _SG_TLS_INLINE 
00245                     TplHdle   
00246                     UpdateID(IDRef1Cst _IDsrc, IDRef1Cst _IDnew)
00247                     {
00248                               return (TplHdle) Mngr1->UpdateID(_IDsrc, _IDnew);
00249                     }
00250 
00258                     _SG_TLS_INLINE 
00259                     TplHdle   
00260                     UpdateID(IDRef2Cst _IDsrc, IDRef2Cst _IDnew)
00261                     {
00262                               return (TplHdle)Mngr2->UpdateID(_IDsrc, _IDnew);
00263                     }
00264 
00265 
00273                     _SG_TLS_INLINE 
00274                     TplHdle   
00275                     UpdateID(IDRef1Cst _ID1src, IDRef2Cst _ID2new)
00276                     {
00277                               TplObj* Obj = (TplHdle) Mngr1->Get(_ID1src);
00278                               TplObj2* Obj2_new = Mngr2->Get(_ID2new);
00279 
00280                               if(Obj2_new)//new ID exists
00281                                         return NULL;
00282 
00283                               if(!Obj)//object not found
00284                                         return NULL;
00285 
00286                               //get ID 2 for update.
00287                               P2 ID2old = Obj->GetID<P2>();
00288                               return (TplHdle)Mngr2->UpdateID(ID2old, _ID2new);
00289                     }
00290 
00298                     _SG_TLS_INLINE 
00299                     TplHdle   
00300                     UpdateID(IDRef2Cst _ID2src, IDRef1Cst _ID1new)
00301                     {
00302                               TplObj* Obj = (TplHdle) Mngr2->Get(_ID2src);
00303                               TplObj1* Obj1_new = Mngr1->Get(_ID1new);
00304 
00305                               if(Obj1_new)//new ID exists
00306                                         return NULL;
00307 
00308                               if(!Obj2)//object not found
00309                                         return NULL;
00310 
00311                               //get ID 1 for update.
00312                               P1 ID1old = Obj->GetID<P1>();
00313                               return Mngr1->UpdateID(ID1old, _ID1new);
00314                     }
00315 
00320                     _SG_TLS_INLINE 
00321                     TplHdle             
00322                     GetLastAdded()
00323                     {
00324                               return Mngr1->GetLastAdded();
00325                     }
00326 
00327           //=============================================================
00328           //================= Find Functions ============================
00329           //=============================================================
00330           
00331 
00338                     _SG_TLS_INLINE 
00339                     TplHdle   
00340                     Find(IDRef1Cst _ID1)
00341                     {
00342                               TplObj1 * Obj1 = Mngr1->Find(_ID1);
00343                               TplHdle Obj = (TplObj*)Obj1;
00344                               return Obj;
00345                     }
00346 
00353                     _SG_TLS_INLINE 
00354                     TplHdle   
00355                     Find(IDRef2Cst _ID2)
00356                     {
00357                               return (TplObj*)Mngr2->Find(_ID2);
00358                     }
00359 
00366                     virtual _SG_TLS_INLINE  
00367                     size_t 
00368                     Delete(IDRef1Cst _ID1)
00369                     {
00370                               TplHdle iHdle = Find(_ID1);
00371                               if (iHdle)
00372                               {
00373                                         Mngr1->Delete(_ID1);
00374                                         CL_BASE_OBJ<P2> * Obj2 = (CL_BASE_OBJ<P2>*)iHdle->GetObj<CL_BASE_OBJ<P2> >();
00375                                         P2 ID2 =  Obj2->GetID();
00376                                         return Mngr2->Delete(ID2);
00377                               }
00378                               return 0;
00379                     }
00380 
00387                     virtual _SG_TLS_INLINE  
00388                     size_t 
00389                     Delete(IDRef2Cst _ID2)
00390                     {
00391                               TplHdle iHdle = Find(_ID2);
00392                               if (iHdle)
00393                               {
00394                                         Mngr2->Delete(_ID2);
00395                                         CL_BASE_OBJ<P1> * Obj1 = (CL_BASE_OBJ<P1>*)iHdle->GetObj<CL_BASE_OBJ<P1> >();
00396                                         P1 ID1 =  Obj1->GetID();
00397                                         return Mngr1->Delete(ID1);
00398                               }
00399                               return 0;
00400                     }
00401 
00406                     virtual _SG_TLS_INLINE  
00407                     size_t    
00408                     DeleteAllLocal()
00409                     {
00410                               Mngr1->DeleteAllLocal();
00411                               return Mngr2->DeleteAllLocal();;
00412                     }
00413 
00419                     _SG_TLS_INLINE 
00420                     TplHdle
00421                     Get(IDRef1Cst _ID)
00422                     {
00423                               return Find(_ID);
00424                     }
00425 
00431                     _SG_TLS_INLINE 
00432                     TplHdle
00433                     Get(IDRef2Cst _ID)
00434                     {
00435                               return Find(_ID);
00436                     }
00437 
00442                     _SG_TLS_INLINE 
00443                     bool
00444                     SwapID(IDRef1Cst _ID1, IDRef2Cst _ID2)
00445                     {
00446                               TplHdle Obj1 = Get(_ID1);
00447                               TplHdle Obj2 = Get(_ID2);
00448                               if(!Mngr1->SwapID(Obj1, Obj2))
00449                                         return false;
00450                               if(!Mngr2->SwapID(Obj1, Obj2))
00451                                         return false;
00452                               return true;
00453                     }
00454 
00455                     _SG_TLS_INLINE 
00456                     void
00457                     PrintAllObjects()
00458                     {
00459                               std::cout << "Manager 1============" << std::endl;
00460                               Mngr1->PrintAllObjects();
00461                               std::cout << "Manager 1============" << std::endl;
00462                               std::cout << "Manager 2============" << std::endl;
00463                               Mngr2->PrintAllObjects();
00464                               std::cout << "Manager 2============" << std::endl;
00465                     }
00466 };
00467 
00468 }
00469 
00470 
00471 
00472 
00473 #endif

Generated on Mon Mar 19 23:15:10 2007 for SugoiTools by  doxygen 1.4.6-NO