# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

msg_container.cpp

Go to the documentation of this file.
00001 
00006 /* Copyright, 2000 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */
00024 #include "nel/ai/agent/agent.h"
00025 #include "nel/ai/agent/msg_container.h"
00026 
00027 namespace NLAIAGENT
00028 {
00029 
00030 
00031         CVectorMsgContainer::CVectorMsgContainer()
00032         {
00033                 _Size = 0;
00034                 _CurrentGrp = 0;
00035         }
00036 
00037         CVectorMsgContainer::CVectorMsgContainer(const CVectorMsgContainer &cp)
00038         {
00039                 _Size = cp.size();
00040                 _CurrentGrp = cp._CurrentGrp;
00041                 _MsgIndex = cp._MsgIndex;
00042                 sint32 index = 0;
00043 
00044                 std::list<const IMessageBase *>::const_iterator it_msg;
00045 
00046                 while ( index != (sint32)cp._Messages.size() )
00047                 {
00048                         _Messages.push_back( std::list<const IMessageBase *>() );               
00049                         it_msg = cp._Messages[index].begin();
00050 
00051                         while ( it_msg != cp._Messages[index].end() )
00052                         {
00053                                 _Messages[ index ].push_back(  (const IMessageBase *) (*it_msg)->clone() );
00054                                 it_msg++;
00055                         }
00056                         index++;
00057                 }
00058                 _MsgIndex = cp._MsgIndex;
00059         }
00060 
00061         CVectorMsgContainer::~CVectorMsgContainer()
00062         {
00063                 sint32 index = 0;
00064                 std::list<const IMessageBase *>::const_iterator it_msg;
00065 
00066                 while ( index != (sint32)_Messages.size() )     
00067                 {
00068                         if ( ! _Messages[index].empty() )
00069                         {
00070                                 it_msg = _Messages[index].begin();
00071 
00072                                 while ( it_msg != _Messages[index].end() )
00073                                 {
00074                                         const IMessageBase *m = *it_msg;
00075                                         it_msg++;
00076                                         delete m;
00077                                 }
00078                         }
00079                         index++;
00080                 }
00081 
00082                 std::list<const IBasicMessageGroup *>::iterator it_grp = _MsgIndex.begin();
00083                 while ( it_grp != _MsgIndex.end() )
00084                 {
00085                         delete *it_grp;
00086                         it_grp++;
00087                 }
00088                 
00089         }
00090 
00091         const NLAIC::IBasicType *CVectorMsgContainer::clone() const
00092         {
00093                 NLAIC::IBasicType *x = new CVectorMsgContainer( *this );
00094                 return x;
00095         }
00096 
00097         const NLAIC::IBasicType *CVectorMsgContainer::newInstance() const
00098         {
00099                 NLAIC::IBasicType *x = new CVectorMsgContainer( *this );
00100                 return x;
00101         }       
00102         const NLAIC::CIdentType &CVectorMsgContainer::getType() const   
00103         {
00104                 
00105                 return IdVectorMsgContainer;
00106         }
00107 
00108         // Adds a message to its index group
00109         void CVectorMsgContainer::addMessage(IMessageBase *msg)
00110         {
00111                 sint32 index = findIndex( msg->getGroup() );
00112                 if ( index < 0 )
00113                 {
00114                         index = _MsgIndex.size();
00115                         _MsgIndex.push_back( new CMessageGroup( msg->getGroup().getId() )  );
00116                         _Messages.push_back( std::list<const IMessageBase *>() );
00117                 }
00118 #ifdef NL_DEBUG
00119                 sint dbg = _Messages.size();
00120                 dbg = _Messages[ index ].size();
00121 #endif
00122                 _Messages[ index ].push_back( msg );
00123 
00124 #ifdef NL_DEBUG 
00125                 dbg = _Messages[ index ].size();
00126 #endif
00127 
00128                 _Size++;
00129         }
00130 
00131         const IMessageBase *CVectorMsgContainer::getMessage()
00132         {
00133                 // S'il reste des messages
00134                 if ( _Size )
00135                 {
00136                         // Si pas de messages dans le groupe courant on passe au suivant
00137                         if ( _Messages[ _CurrentGrp ].empty() )
00138                         {
00139                                 while ( _CurrentGrp != (sint32)_Messages.size() && _Messages[ _CurrentGrp ].empty() )
00140                                         _CurrentGrp++;
00141                         }
00142                         return _Messages[ _CurrentGrp ].front();
00143                 }
00144                 else 
00145                         return NULL;
00146         }
00147 
00148         void CVectorMsgContainer::popMessage()
00149         {
00150                 if ( _Size )
00151                 {
00152                         IMessageBase *m = (IMessageBase *)_Messages[ _CurrentGrp ].front();
00153                         m->release();
00154                         _Messages[ _CurrentGrp ].pop_front();
00155                         _Size--;
00156                         
00157                         if ( _Size == 0 )
00158                                 _CurrentGrp = 0;
00159                         else
00160                                 if ( _Messages [_CurrentGrp ].empty() )
00161                                         _CurrentGrp++;
00162                 }
00163         }
00164 
00165         std::list<const IMessageBase *> *CVectorMsgContainer::pumpMessages(const IBasicMessageGroup &grp)
00166         {
00167 
00168                 std::list<const IMessageBase *> *msg_list = new std::list<const IMessageBase *>;
00169 
00170                 sint32 index = findIndex( grp );
00171 
00172                 if (index >= 0 ) 
00173                 {
00174                         std::list<const IMessageBase *>::iterator it_msg = _Messages[index].begin();
00175 
00176                         while ( it_msg != _Messages[ index ].end() )
00177                         {
00178                                 msg_list->push_back( (const IMessageBase *) (*it_msg)->clone() );
00179                                 it_msg++;
00180                         }
00181                 }
00182 
00183                 return msg_list;
00184         }
00185 
00186         sint32 CVectorMsgContainer::findIndex(const IBasicMessageGroup &grp)
00187         {
00188                 sint32 i = 0;
00189                 std::list<const IBasicMessageGroup *>::iterator it_grp = _MsgIndex.begin();
00190 
00191                 while ( it_grp != _MsgIndex.end() )
00192                 {
00193                         if ( grp == *it_grp )
00194                         {
00195                                 return i;
00196                         }
00197                         i++;
00198                         it_grp++;
00199                 }
00200                 return -1;
00201         }
00202 
00203         // TODO: changer en référence sur la liste
00204         std::list<const IMessageBase *> *CVectorMsgContainer::operator[](const IBasicMessageGroup &grp)
00205         {
00206                 return pumpMessages( grp );
00207         }
00208 
00209 
00210         void CVectorMsgContainer::save(NLMISC::IStream &os)
00211         {
00212 
00213                 // Sauvegarde de l'index des groupes
00214                 sint32 size = _MsgIndex.size();
00215                 os.serial( size );
00216                 std::list<const IBasicMessageGroup *>::const_iterator it_grp = _MsgIndex.begin();
00217                 while ( it_grp != _MsgIndex.end() )
00218                 {
00219                         os.serial( (NLAIC::CIdentType &) (*it_grp)->getType() );
00220                         ((IBasicMessageGroup *)(*it_grp))->save( os );
00221                         it_grp++;
00222                 }
00223 
00224 
00225                 // Sauvegarde des messages
00226                 size = _Size;
00227                 os.serial( size );
00228                 std::list<const IMessageBase *>::const_iterator it_msg;
00229                 
00230                 if ( _Size )
00231                 {
00232                         sint32 index = 0;
00233                         while ( index != (sint32)_Messages.size() )
00234                         {
00235                                 it_msg =_Messages[index].begin();
00236                                 while ( it_msg != _Messages[index].end() )
00237                                 {
00238                                         IMessageBase *msg = (IMessageBase *)*it_msg;
00239                                         os.serial( (NLAIC::CIdentType &) msg->getType() );
00240                                         msg->save(os);                          
00241                                         it_msg++;
00242                                 }       
00243                                 index++;
00244                         }
00245                 }
00246 
00247         }
00248 
00249         void CVectorMsgContainer::load(NLMISC::IStream &is)
00250         {
00251                 sint32 i;
00252                 NLAIC::CIdentTypeAlloc id;
00253 
00254                 // Chargement de l'index des groupes
00255                 sint32 nb_grps;
00256                 is.serial( nb_grps );
00257                 while ( nb_grps-- )
00258                 {
00259                         is.serial( id );
00260                         IBasicMessageGroup *tmp_grp = (IBasicMessageGroup *) id.allocClass();                                           
00261                         tmp_grp->load( is );
00262                         _MsgIndex.push_back( tmp_grp );
00263                 }
00264 
00265                 // Chargement des messages
00266                 is.serial( i );
00267                 if ( i )
00268                 {
00269                         while ( i-- )
00270                         {                                               
00271                                 is.serial( id );
00272                                 IMessageBase *msg = (IMessageBase *)id.allocClass();
00273                                 msg->load(is);
00274                                 addMessage(msg);
00275                         }
00276                 }
00277 
00278         }
00279 }