Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

plist.h

00001 // $Id: plist_8h-source.html,v 1.2 2002/06/20 17:09:51 mkrohn5 Exp $
00002 
00003 // Pointer List Header File
00004 // Written by: Marco Krohn <marco.krohn@gmx.de>
00005 
00006 // Copyright (C) 2001 - , Marco Krohn, <marco.krohn@gmx.de>
00007 //
00008 // This program 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 // 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 #ifndef __PLIST_INCLUDED
00019 #define __PLIST_INCLUDED
00020 
00021 #include <algorithm>
00022 #include <list>
00023 #include <string>
00024 
00025 using namespace std;
00026 
00027 #include "common/datapackage.h"
00028 
00029 
00030 
00031 
00032 
00033 //----------------------------------------  class TPList  ----------------------------------------
00034 
00063 template<class X> class TPList : public list<X*>
00064 {
00065   public:
00066     // types:
00067     typedef typename list<X*>::iterator iterator;
00068     typedef typename list<X*>::const_iterator const_iterator;
00069 
00074     TPList<X>( bool autoDelete=false ) : list<X*>()
00075     {
00076       setAutoDeleteMode( autoDelete );
00077     }
00078     TPList<X>( const TPList<X>& src ) : list<X*>( src )
00079     {
00080       *this = src;
00081     }
00085     virtual ~TPList()
00086     {
00087       deleteAll();
00088     }
00089 
00096     void add( X* obj )
00097     {
00098       push_back( obj );
00099     }
00104     void deleteAll()
00105     {
00106       // If you are confused by the meaning of "typename" read
00107       // C.13.5 "Typename and Template" in
00108       // "The C++ Programming Language", Bjarne Stroustrup
00109       // (this is in the gcc world only needed if you use -pedantic switch)
00110       iterator it;
00111 
00112       if ( fAutoDelete == true )  {
00113         for ( it=begin(); it!=end(); it++ )  delete *it;   // deletes all objects
00114       }
00115       clear();                                             // deletes the whole list
00116     }
00121     iterator find( X* obj )
00122     {
00123       return std::find( list<X*>::begin(), list<X*>::end(), obj );
00124     }
00128     X* getFirst() const
00129     {
00130       if ( empty() == true )  return 0;
00131       return *(begin());
00132     }
00140     iterator goTo( int4 n )
00141     {
00142       int4 i;
00143       iterator it, last;
00144 
00145       it = begin();
00146       last = end();
00147 
00148       for ( i=0; i<n; i++ )  {
00149         if ( it == end() )  return end();
00150         it++;
00151       }
00152 
00153       return it;
00154     }
00162     TPList<X>& operator=( const TPList& Src )
00163     {
00164       if ( this == &Src )  return *this;                   // src and dest are the same
00165 
00166       deleteAll();
00167       // the list is now empty
00168       // call base-class version of operator=() => base-class assignment
00169       list<X*>::operator=( Src );
00170       // regardless of the settings of Src: set the auto delete variable to
00171       // false, otherwise the object will be deleted twice
00172       setAutoDeleteMode( false );
00173 
00174       return *this;
00175     }
00184     bool remove( X* obj )
00185     {
00186       iterator it = find( obj );
00187       return remove( it );
00188     }
00197     bool remove( iterator it )
00198     {
00199       if ( it == end() )  return false;                    // no such object
00200       if ( fAutoDelete == true )  delete *it;              // delete the object
00201       erase( it );                                         // remove the object
00202 
00203       return true;                            // object removed --> return true
00204     }
00209     void setAutoDeleteMode( bool autoDelete )
00210     {
00211       fAutoDelete = autoDelete;
00212     }
00213 
00214   protected:
00220     bool fAutoDelete;
00221 };
00222 
00223 
00224 
00225 
00226 
00227 //----------------------------------------  class TPEList  ----------------------------------------
00228 
00242 template<class X> class TPEList : public TPList<X>
00243 {
00244   public:
00254     bool load( TDataPackageList* in, const string& identifier,
00255                bool unique=true )
00256     {
00257       bool ret = true;
00258       X* pNew;
00259       TDataPackageList* pList = new TDataPackageList();
00260 
00261       // read the object => sub list
00262       ret &= in->Get( identifier, pList, unique );
00263 
00264       // read the real data from the sub list
00265       ret &= pList->Get( "autodelete", &fAutoDelete, true );
00266       pList->showContent();
00267 
00268       while( pList->Search("listitem") != 0 )
00269       {
00270         pNew = new X;
00271         // this assumes that load(...) will load the first
00272         // element in the list "pList". Otherwise the order
00273         // of elements is not conserved.
00274         ret &= pNew->load( pList, "listitem", false );
00275         // adds (push_back) the element to the list
00276         add( pNew );
00277       }
00278       delete pList;                                        // clean up
00279 
00280       return ret;
00281     }
00292     virtual bool save( TDataPackageList* out, const string& identifier,
00293                bool unique=true ) const
00294     {
00295       bool ret = true;
00296       typename list<X*>::const_iterator it;
00297 
00298       TDataPackageList* pList = new TDataPackageList();    // create a new datapackage list
00299       // autoDelete=true so that pList is deleted sometime
00300       ret &= out->Add( identifier, pList, unique, true );
00301 
00302       // add the data packages to the new list
00303       ret &= pList->Add( "autodelete", &fAutoDelete, true, false );
00304 
00305       for ( it=begin(); it!=end(); it++ )
00306       {
00307         // note: the "listitem" identifier is not unique
00308         ret &= (*it)->save( pList, "listitem", false );
00309       }
00310       return ret;
00311     }
00312 };
00313 
00314 
00315 
00316 
00317 
00318 #endif        // PLIST_INCLUDED

Generated on Thu Jun 20 18:13:16 2002 for Stellar Legacy by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001