D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/cl_template_manager.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_template_manager.h
00025  * \brief Template manager to manage all kind of object.
00026  * \author Yannick Allusse
00027 */
00028 #ifndef SG_TLS_CL_TEMPLATE_MANAGER_H
00029 #define SG_TLS_CL_TEMPLATE_MANAGER_H
00030 
00031 #include "SugoiTools\config.h"
00032 #include "SugoiTools\cl_base_obj.h"
00033 #include "SugoiTools\tools.h"
00034 #include "SugoiTools\logger_main.h"
00035 #include "SugoiTools\smart_ptr.h"
00036 #include <map>  // Include algorithms
00037 
00038 namespace SGE{
00046 template <class T, typename P=std::string>  
00047           class CL_TEMPLATE_OBJECT_MANAGER
00048           {
00049           public:
00050                     typedef typename T *                                                            TplHdle;  
00051                     typedef   P&                                                                                        IDRef;              
00052                     typedef   const P&                                                              IDRefCst; 
00053                     typedef typename std::map<P,  TplHdle>  TplMap;             
00054                     typedef typename std::pair<P, TplHdle>  TplPair;  
00055                     typedef typename TplMap::iterator                           iterator; 
00056                     typedef   CL_TEMPLATE_OBJECT_MANAGER<T,P>                   TplManager;
00057 
00058           public:
00059 
00060           //=============================================================
00061           //================= Base Functions ============================
00062           //=============================================================
00069                     _SG_TLS_INLINE
00070                     CL_TEMPLATE_OBJECT_MANAGER(TplManager *_RemoteManager):
00071                               RemoteManager(_RemoteManager),//set this manager to local or remote
00072                               DfltObj(NULL),
00073                               LastAdded(0)
00074                     {
00075                     };
00076 
00077                     
00081                     _SG_TLS_INLINE 
00082                     ~CL_TEMPLATE_OBJECT_MANAGER   (void)
00083                     {
00084                               DeleteAllLocal();
00085                     };
00086 
00087           //=============================================================
00088           //================= Add Functions  ============================
00089           //=============================================================
00090           protected:
00097                     virtual 
00098                     _SG_TLS_INLINE
00099                               bool IsObjValide(TplHdle _Obj)
00100                               {
00101                                         if (_Obj)
00102                                                   return true;
00103                                         return false;
00104                               }
00105 
00112                     //
00113                     virtual 
00114                     _SG_TLS_INLINE
00115                     TplHdle CreateObj(IDRefCst _ID, std::string _ObjType="")
00116                               {
00117                                         return new T(_ID);
00118                               }
00119 
00120 
00121           public:
00128                     _SG_TLS_INLINE 
00129                     TplHdle   
00130                     Add(IDRefCst _ID)
00131                     {
00132                               TplHdle Obj = AddRemote(_ID);
00133                               if (!IsObjValide(Obj))
00134                                          Obj = AddLocal(_ID);
00135                               //else
00136                               SG_Assert(IsObjValide(Obj), "Could not create new object");
00137                               LastAdded = Obj;
00138                               return Obj;
00139                     }
00140 
00147                     _SG_TLS_INLINE 
00148                     TplHdle   
00149                     AddObj(TplHdle _Obj)
00150                     {
00151                               TplHdle Obj = AddRemoteObj(_Obj);
00152                               if (!IsObjValide(Obj))
00153                                          Obj = AddLocalObj(_Obj);
00154                               //else
00155                               SG_Assert(IsObjValide(Obj), "Could not add existing object");
00156                               LastAdded = _Obj;
00157                               return _Obj;
00158                     }
00159 
00164                     _SG_TLS_INLINE 
00165                     TplHdle             
00166                     GetLastAdded()
00167                     {
00168                               return LastAdded;
00169                     }
00170 
00171           protected:
00178                     _SG_TLS_INLINE 
00179                     TplHdle   
00180                     AddLocal(IDRefCst _ID)
00181                     {
00182                               TplHdle Obj = FindLocal(_ID);
00183                               if (IsObjValide(Obj))
00184                               {
00185                                         _SG_DEBUG_MNGR(SG_NOTICE_LOG("CL_TEMPLATE_OBJECT_MANAGER<T,P>::AddLocal()=> object already exist."));
00186                                         return Obj;//already exist
00187                               }
00188                               //we create it:
00189                               Obj = CreateObj(_ID);
00190                               TMap.insert( TplPair(_ID, Obj));
00191                               LastAdded = Obj;
00192                               
00193                               //else
00194                               return Obj;
00195                     }
00196 
00203                     _SG_TLS_INLINE 
00204                     TplHdle   
00205                     AddLocalObj(TplHdle _Obj)
00206                     {
00207                               TplHdle Obj = FindLocal(_Obj->GetID());
00208                               if (IsObjValide(Obj))
00209                               {
00210                                         _SG_DEBUG_MNGR(SG_NOTICE_LOG("CL_TEMPLATE_OBJECT_MANAGER<T,P>::AddLocal()=> object already exist."));
00211                                         return Obj;//already exist
00212                               }
00213                               //we insert it:
00214                               TMap.insert(TplPair(_Obj->GetID(), _Obj));
00215                               LastAdded = _Obj;
00216                               //else
00217                               return _Obj;
00218                     }
00219 
00226                     _SG_TLS_INLINE 
00227                     TplHdle
00228                     AddRemote(IDRefCst _ID)
00229                     {
00230                               if(!RemoteManager)
00231                                         return NULL;
00232                               return RemoteManager->Add(_ID);         //ask for remote manager
00233                     }
00234 
00241                     _SG_TLS_INLINE 
00242                     TplHdle
00243                     AddRemoteObj(TplHdle _Obj)
00244                     {
00245                               if(!RemoteManager)
00246                                         return NULL;
00247                               return RemoteManager->AddObj(_Obj);     //ask for remote manager
00248                     }
00249 
00250           //=============================================================
00251           //================= Find Functions ============================
00252           //=============================================================
00253           public:
00260                     _SG_TLS_INLINE 
00261                     TplHdle   
00262                     Find(IDRefCst _ID)
00263                     {
00264                               TplHdle Obj = FindLocal(_ID);
00265                               if (IsObjValide(Obj))
00266                                         return Obj;
00267                               //else
00268                               return FindRemote(_ID);
00269                     }
00270 
00277                     _SG_TLS_INLINE 
00278                     iterator
00279                     FindLocalPair(IDRefCst _ID)
00280                     {
00281                               return TMap.find(_ID);
00282                     }
00283 
00284           protected:
00291                     _SG_TLS_INLINE 
00292                     TplHdle   
00293                     FindLocal(IDRefCst _ID)
00294                     {
00295                               iterator iObj = TMap.find(_ID);
00296                               if (iObj!= TMap.end())
00297                                         return (*iObj).second;//found
00298                               //else
00299                               return NULL;
00300                     }
00301 
00308                     _SG_TLS_INLINE 
00309                     TplHdle
00310                     FindRemote(IDRefCst _ID)
00311                     {
00312                               if(!RemoteManager)
00313                                         return NULL;
00314 
00315                               return RemoteManager->Find(_ID);        //ask for remote manager
00316                     }
00317 
00318 
00319           //=============================================================
00320           //================= Delete Functions    =======================
00321           //=============================================================
00322                     
00323           public:
00329                     virtual _SG_TLS_INLINE  
00330                     size_t 
00331                     Delete(IDRefCst _ID)
00332                     {
00333                               TplHdle iHdle = FindLocal(_ID);
00334                               if (iHdle != GetDefaultObj())
00335                               {
00336                                         //delete(iHdle);//Erase the data
00337                                         return TMap.erase(_ID);       //erase the pair
00338                               }
00339                               return 0;
00340                     }
00341 
00347                     virtual _SG_TLS_INLINE  
00348                     size_t    
00349                     DeleteAllLocal()
00350                     {
00351                               //we delete all local TplSmartP objects containing T* objects
00352                               //if no other manager point to T*, the T* is deleted with the TplSmartP
00353                               //else only the TplSmartP is deleted
00354 
00355                               iterator it_T;
00356                               //delete all TplSmartP
00357                               //for ( it_T = GetFirstIter( ) ; it_T!= GetLastIter( ) ;it_T++ )
00358                               //        delete((*it_T).second);
00359 
00360                               //erase all pairs
00361                               size_t Nbr = TMap.size();
00362                               if(Nbr)
00363                                         TMap.clear();
00364                               return Nbr;
00365                     }
00366 
00367           //=============================================================
00368           //================= Management Functions=======================
00369           //=============================================================
00370           public:
00376                     _SG_TLS_INLINE 
00377                     TplHdle
00378                     Get(IDRefCst _ID)
00379                     {
00380                               TplHdle Result = Find(_ID);
00381                               if (!IsObjValide(Result))
00382                                         return DfltObj;
00383                               return Result;
00384                     }
00385 
00392                     _SG_TLS_INLINE 
00393                     TplHdle   
00394                     operator [] (IDRefCst _ID)
00395                     {
00396                               return Get(_ID);
00397                     }
00398 
00399           public:
00403           //        _SG_TLS_INLINE 
00404                     iterator
00405                     GetFirstIter()
00406                     {
00407                               //if (TMap.size())
00408                                         return TMap.begin();
00409                               //else
00410                               //        return NULL;
00411                     }
00412 
00416           //        _SG_TLS_INLINE 
00417                     iterator
00418                     GetLastIter()
00419                     {
00420                               //if (GetCount())
00421                                         return TMap.end();
00422                               //else
00423                               //        return NULL;
00424                     }
00425 
00429           //        _SG_TLS_INLINE 
00430                     TplHdle
00431                     GetFirstObj()
00432                     {
00433                               iterator  iObj = GetFirstIter();
00434                               if (iObj != GetLastIter())
00435                                         if (IsObjValide((*iObj).second))
00436                                                   return (*iObj).second;
00437                               
00438                               return NULL;
00439                     }
00440 
00444           //        _SG_TLS_INLINE 
00445                     TplHdle
00446                     GetLastObj()
00447                     {
00448                               iterator  iObj = GetLastIter();
00449                               if (IsObjValide((*iObj).second))
00450                                         return (*iObj).second;
00451                               else
00452                                         return NULL;
00453                     }
00454 
00458                     _SG_TLS_INLINE 
00459                     size_t
00460                     GetCount()
00461                     {
00462                               return TMap.size();
00463                     }
00464 
00472                     _SG_TLS_INLINE 
00473                     TplHdle 
00474                     SetDefaultObj(IDRefCst _ID)
00475                     {
00476                               return DfltObj = Add(_ID);
00477                     }
00478 
00484                     _SG_TLS_INLINE 
00485                     TplHdle 
00486                     GetDefaultObj()
00487                     {
00488                               return DfltObj;
00489                     }
00490 
00494                     _SG_TLS_INLINE 
00495                     void
00496                     PrintAllObjects()
00497                     {
00498                               iterator  iObj;
00499                               for (iObj = GetFirstIter(); iObj != GetLastIter(); iObj++)
00500                                         std::cout << "ID: " << (*iObj).second->GetIDStr() << " => " << (*iObj).second->GetValStr() <<  endl;
00501                     }
00502 
00506                     _SG_TLS_INLINE 
00507                     bool
00508                     SwapID(IDRefCst _ID1, IDRefCst _ID2)
00509                     {
00510                               iterator it1 = FindLocalPair(_ID1);
00511                               iterator it2 = FindLocalPair(_ID2);
00512 
00513                               if (it1!= TMap.end() & 
00514                                         it2!= TMap.end())   
00515                               {
00516                                         TplHdle TempObj = (*it1).second;
00517                                         
00518                                         (*it1).second       = (*it2).second;
00519                                         (*it2).second       = TempObj;
00520 
00521                                         P TempID = (*it1).second->ID;
00522                                         (*it1).second->ID = (*it1).first;
00523                                         (*it2).second->ID = (*it2).first;
00524 
00525                                         return true;
00526                               }
00527                               return false;
00528                     }
00529 
00533                     _SG_TLS_INLINE 
00534                     bool
00535                     SwapID(TplHdle _Obj1, TplHdle _Obj2)
00536                     {
00537                               return SwapID(_Obj1->ID, _Obj2->ID);
00538                     }
00539 
00540 
00548                     _SG_TLS_INLINE 
00549                     TplHdle   
00550                     UpdateID(IDRefCst _IDsrc, IDRefCst _IDnew)
00551                     {         
00552                               TplHdle Obj = NULL;
00553                               //if we have a remote manager, we start from here
00554                               if (RemoteManager)
00555                               {
00556                                         Obj = RemoteManager->UpdateID(_IDsrc,_IDnew);
00557                                         if (Obj)
00558                                         {//delete the local version
00559                                                   Delete(_IDsrc);
00560                                                   AddObj(Obj);//add the new version
00561                                                   return Obj;
00562                                         }
00563                                         return NULL;
00564                               }
00565 
00566                               //no Remote manager
00567 
00568                               //check the new ID.?
00569                               if(Get(_IDnew))
00570                                         return NULL;
00571 
00572                               //proceed update
00573                               Obj = Get(_IDsrc);
00574                               if(!Obj)
00575                                         return NULL;
00576 
00577                               //delete local version
00578                               Delete(_IDsrc);
00579 
00580                               //update the ID
00581                               Obj->SetID(_IDnew);
00582                               AddObj(Obj);
00583                               return Obj;
00584                     }
00585           private:
00586                     TplHdle                       LastAdded;                    
00587                     TplHdle                       DfltObj;            
00588                     TplManager *        RemoteManager;      
00589                     TplMap                        TMap;                         
00590 };
00591 
00592 
00593 
00594 
00595 
00596 
00597 template <class T, typename P=std::string>
00598           class CL_SMART_MANAGER :
00599                     public  CL_TEMPLATE_OBJECT_MANAGER<CSmartPtr<T>, P>
00600 {
00601 public:
00602           typedef   CSmartPtr<T>                                                TplSmartP;
00603           typedef TplSmartP                                                     TplHdle;
00604           typedef typename std::map<P,  TplHdle>  TplMap;
00605           typedef typename std::pair<P, TplHdle>  TplPair;
00606           typedef typename TplMap::iterator                 iterator;
00607           //typedef P&                                                                              IDRef;
00608           //typedef const P&                                                    IDRefCst;
00609           typedef   CL_SMART_MANAGER<T,P>         TplSmartManager;
00610 
00611 public:
00612 //=============================================================
00613 //================= Base Functions ============================
00614 //=============================================================
00621           _SG_TLS_INLINE
00622           CL_SMART_MANAGER(TplSmartManager *_RemoteManager):
00623                     CL_TEMPLATE_OBJECT_MANAGER<CSmartPtr<T>, P>(_RemoteManager)
00624           {
00625           };
00626 
00627           
00631           _SG_TLS_INLINE 
00632           ~CL_SMART_MANAGER   (void)
00633           {
00634           };
00635 
00636 public:
00637           virtual 
00638           _SG_TLS_INLINE
00639                     bool IsObjValide(TplHdle _Obj)
00640                     {
00641                               return _Obj.IsValide();
00642                     }
00643 
00644 
00645 //=============================================================
00646 //================= Delete Functions    =======================
00647 //=============================================================
00648           
00649 public:
00655           _SG_TLS_INLINE 
00656           size_t 
00657           Delete(IDRefCst _ID)
00658           {
00659                     TplHdle iHdle = FindLocal(_ID);
00660                     if (iHdle != GetDefaultObj())
00661                     {
00662                               //delete(iHdle);//Erase the data
00663                               return TMap.erase(_ID);       //erase the pair
00664                     }
00665                     return 0;
00666           }
00667 
00668           _SG_TLS_INLINE 
00669           int       
00670           DeleteAllLocal()
00671           {
00672                     //we delete all local TplSmartP objects containing T* objects
00673                     //if no other manager point to T*, the T* is deleted with the TplSmartP
00674                     //else only the TplSmartP is deleted
00675 
00676                     iterator it_T;
00677                     //delete all TplSmartP
00678                     //for ( it_T = GetFirstIter( ) ; it_T!= GetLastIter( ) ;it_T++ )
00679                     //        delete((*it_T).second);
00680 
00681                     //erase all pairs
00682                     int Nbr = TMap.size();
00683                     if(Nbr)
00684                               TMap.clear();
00685                     return Nbr;
00686           }
00687 
00688 };
00689 };//namespace SGE
00690 #endif //SG_TLS_CL_TEMPLATE_MANAGER_H

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