retriever_bank.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2001 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #ifndef NL_RETRIEVER_BANK_H
00025 #define NL_RETRIEVER_BANK_H
00026 
00027 #include <vector>
00028 #include <string>
00029 #include <set>
00030 
00031 #include "nel/misc/types_nl.h"
00032 #include "nel/misc/vector.h"
00033 #include "nel/misc/file.h"
00034 #include "nel/misc/common.h"
00035 #include "nel/misc/path.h"
00036 
00037 #include "local_retriever.h"
00038 #include "nel/pacs/u_retriever_bank.h"
00039 
00040 namespace NLPACS
00041 {
00042 
00049 class CRetrieverBank : public URetrieverBank
00050 {
00051     friend class URetrieverBank;
00052 
00053 protected:
00055     std::vector<CLocalRetriever>        _Retrievers;
00056 
00058     bool                                _AllLoaded;
00059 
00061     std::string                         _NamePrefix;
00062 
00064     std::set<uint>                      _LoadedRetrievers;
00065 
00067     bool                                _LrInRBank;
00068 
00069 public:
00071     CRetrieverBank(bool allLoaded = true) : _AllLoaded(allLoaded), _LrInRBank(true) {}
00072 
00074     const std::vector<CLocalRetriever>  &getRetrievers() const { return _Retrievers; }
00075 
00077     uint                                size() const { return _Retrievers.size(); }
00078 
00080     const CLocalRetriever               &getRetriever(uint n) const
00081     {
00082         nlassert(n < _Retrievers.size());
00083         /* if (!_Retrievers[n].isLoaded())
00084             nlwarning("Trying to access rbank '%s', retriever %d not loaded", _NamePrefix.c_str(), n); */
00085         return _Retrievers[n];
00086     }
00087 
00089     uint                                addRetriever(const CLocalRetriever &retriever) { _Retrievers.push_back(retriever); return _Retrievers.size()-1; }
00090 
00092     uint                                addRetriever(const std::string &filename)
00093     {
00094         NLMISC::CIFile  input;
00095         _Retrievers.resize(_Retrievers.size()+1);
00096         CLocalRetriever &localRetriever = _Retrievers.back();
00097         nldebug("load retriever file %s", filename.c_str());
00098         input.open(filename);
00099         localRetriever.serial(input);
00100         input.close();
00101 
00102         return _Retrievers.size()-1;
00103     }
00104 
00106     void                                clean();
00107 
00109     void                                setLrInFileFlag(bool status)    { _LrInRBank = status; }
00110 
00112     void                                serial(NLMISC::IStream &f)
00113     {
00114         /*
00115         Version 0:
00116             - base version.
00117         Version 1:
00118             - saves & loads lr in rbank only if bool LrInFile true
00119         */
00120         uint    ver = f.serialVersion(1);
00121 
00122         bool    lrPresent = true;
00123         if (ver > 0)
00124         {
00125             lrPresent = _LrInRBank;
00126             f.serial(lrPresent);
00127         }
00128 
00129         if (f.isReading())
00130         {
00131             if (!_AllLoaded)
00132             {
00133                 uint32  num = 0;
00134                 f.serial(num);
00135                 nlinfo("Presetting RetrieverBank '%s', %d retriever slots allocated", _NamePrefix.c_str(), num);
00136                 _Retrievers.resize(num);
00137             }
00138             else if (lrPresent)
00139             {
00140                 f.serialCont(_Retrievers);
00141             }
00142             else
00143             {
00144                 uint32  num = 0;
00145                 f.serial(num);
00146                 _Retrievers.resize(num);
00147 
00148                 uint    i;
00149                 for (i=0; i<num; ++i)
00150                 {
00151                     std::string fname = NLMISC::CPath::lookup(_NamePrefix + "_" + NLMISC::toString(i) + ".lr", false, true);
00152                     if (fname == "")
00153                         continue;
00154 
00155                     NLMISC::CIFile  f(fname);
00156                     try
00157                     {
00158                         f.serial(_Retrievers[i]);
00159                     }
00160                     catch (NLMISC::Exception &e)
00161                     {
00162                         nlwarning("Couldn't load retriever file '%s', %s", fname.c_str(), e.what());
00163                         _Retrievers[i].clear();
00164                     }
00165                 }
00166             }
00167         }
00168         else
00169         {
00170             if (lrPresent)
00171             {
00172                 f.serialCont(_Retrievers);
00173             }
00174             else
00175             {
00176                 uint32  num = _Retrievers.size();
00177                 f.serial(num);
00178             }
00179         }
00180     }
00181 
00183     void                                saveRetrievers(const std::string &path, const std::string &bankPrefix)
00184     {
00185         uint    i;
00186         for (i=0; i<_Retrievers.size(); ++i)
00187         {
00188             NLMISC::COFile  f(NLMISC::CPath::standardizePath(path) + bankPrefix + "_" + NLMISC::toString(i) + ".lr");
00189             f.serial(_Retrievers[i]);
00190         }
00191     }
00192 
00194     void                                saveShortBank(const std::string &path, const std::string &bankPrefix, bool saveLr = true)
00195     {
00196         NLMISC::COFile  f(NLMISC::CPath::standardizePath(path) + bankPrefix + ".rbank");
00197 
00198         _LrInRBank = false;
00199 
00200         serial(f);
00201 
00202         if (saveLr)
00203             saveRetrievers(path, bankPrefix);
00204     }
00205 
00207     // @{
00208 
00210     void        diff(const std::set<uint> &newlr, std::set<uint> &in, std::set<uint> &out)
00211     {
00212         std::set<uint>::const_iterator  it;
00213 
00214         for (it=_LoadedRetrievers.begin(); it!=_LoadedRetrievers.end(); ++it)
00215         {
00216             uint    n = *it;
00217             if (n >= _Retrievers.size())
00218                 continue;
00219             _Retrievers[n].LoadCheckFlag = true;
00220         }
00221 
00222         for (it=newlr.begin(); it!=newlr.end(); ++it)
00223         {
00224             uint    n = *it;
00225             if (n >= _Retrievers.size())
00226                 continue;
00227             if (!_Retrievers[n].LoadCheckFlag)
00228                 in.insert(n);
00229             _Retrievers[n].LoadCheckFlag = false;
00230         }
00231 
00232         for (it=_LoadedRetrievers.begin(); it!=_LoadedRetrievers.end(); ++it)
00233         {
00234             uint    n = *it;
00235             if (n >= _Retrievers.size())
00236                 continue;
00237             if (_Retrievers[n].LoadCheckFlag)
00238                 out.insert(n);
00239             _Retrievers[n].LoadCheckFlag = false;
00240         }
00241     }
00242 
00244     void        loadRetriever(uint n, NLMISC::IStream &s)
00245     {
00246         if (_AllLoaded || n >= _Retrievers.size() || _Retrievers[n].isLoaded())
00247         {
00248             nlwarning("RetrieverBank '%s' asked to load retriever %n whereas not needed, aborted", _NamePrefix.c_str(), n);
00249             return;
00250         }
00251 
00252         s.serial(_Retrievers[n]);
00253         _LoadedRetrievers.insert(n);
00254     }
00255 
00257     void        setRetrieverAsLoaded(uint n)
00258     {
00259         _LoadedRetrievers.insert(n);
00260     }
00261 
00263     void        unloadRetriever(uint n)
00264     {
00265         if (_AllLoaded || n >= _Retrievers.size() || !_Retrievers[n].isLoaded())
00266         {
00267             nlwarning("RetrieverBank '%s' asked to unload retriever %n whereas not needed, aborted", _NamePrefix.c_str(), n);
00268             return;
00269         }
00270 
00271         _Retrievers[n].clear();
00272         _LoadedRetrievers.erase(n);
00273     }
00274 
00276     const std::string   &getNamePrefix() const  { return _NamePrefix; }
00277 
00279     void                setNamePrefix(const char *prefix) { _NamePrefix = prefix; }
00280 
00282     bool        allLoaded() const { return _AllLoaded; }
00283 
00285     bool        isLoaded(uint n) const
00286     {
00287         return (n < _Retrievers.size() && _Retrievers[n].isLoaded());
00288     }
00289 
00290     // @}
00291 };
00292 
00293 }; // NLPACS
00294 
00295 #endif // NL_RETRIEVER_BANK_H
00296 
00297 /* End of retriever_bank.h */

Generated on Thu Jan 7 08:26:36 2010 for NeL by  doxygen 1.6.1