osbptrlist.h

Go to the documentation of this file.
00001 // OSB library ************************************************ -*- C++ -*-
00006 /*
00007   AUTHOR(S): Stephan Broennimann (vb)
00008 
00009   RCS information
00010    $Name: OSB_060808 $
00011    $Revision: 1.7 $
00012 
00013   License
00014    OSB rating and billing library for communication networks
00015    Copyright (C) 2004, 2005, 2006  OSB systems
00016 
00017    This file may be distributed and/or modify under the terms of the
00018    GNU General Public License (GPL) as published by the Free Software
00019    Foundation which is provided in the file LICENSE.GPL included in the
00020    packaging of this file.
00021 
00022    The file is distributed in the hope that it will be useful, but WITHOUT
00023    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00025    for more details.
00026 
00027    Holders of a OSB Commercial License may use this file under the terms
00028    and conditions of this commercial license.
00029  */
00030 #ifndef _OSBPTRLIST_H_
00031 #define _OSBPTRLIST_H_
00032 
00033 // ************************************************************************
00034 // included header files
00035 // + standard includes
00036 #include <list>
00037 #include <memory>
00038 
00039 // + libraries
00040 
00041 // + local headers
00042 
00043 // + class declarations
00044 
00045 // ************************************************************************
00046 // namespace extensions
00047 namespace OSB_LIB {
00048 
00049 // ************************************************************************
00050 // forward declarations
00051 
00052 // ************************************************************************
00053 // type definitions
00054 
00055 // ************************************************************************
00056 // class definitions
00057 
00070     template< typename T, typename Cont = std::list<T*> >
00071     struct PtrList {
00072     public:
00074         typedef typename Cont::size_type      Size;
00076         typedef typename Cont::iterator       Iter;
00078         typedef typename Cont::const_iterator ConstIter;
00080         typedef std::auto_ptr<T> AutoPtr;
00081 
00082     public:
00084         PtrList();
00086         ~PtrList();
00088         PtrList(const PtrList& rhs);
00090         PtrList& operator=(const PtrList& rhs);
00091 
00092     public:
00094         bool empty() const;
00096         Size size() const;
00098         Iter begin();
00100         ConstIter begin() const;
00102         Iter end();
00104         ConstIter end() const;
00105 
00107         void pushBack(AutoPtr e);
00114         Iter erase(Iter pos);
00116         void swap(PtrList& other);
00117 
00118     private:
00120         Cont cont_;
00121     };                                  // struct PtrList
00122 }                                       // namespace OSB_LIB
00123 
00124 // ************************************************************************
00125 // inline definitions
00126 namespace OSB_LIB {
00127     template<typename T, typename C>
00128     inline PtrList<T, C>::PtrList()
00129     {
00130     }
00131 
00132     template<typename T, typename C>
00133     PtrList<T, C>::~PtrList()
00134     {
00135         for (Iter e = cont_.end(),
00136                   i = cont_.begin(); i != e; ++i)
00137         {
00138             delete *i;
00139         }
00140     }
00141 
00142     template<typename T, typename C>
00143     PtrList<T, C>::PtrList(const PtrList& rhs)
00144     {
00145         ConstIter e = rhs.end();
00146         ConstIter i = rhs.begin();
00147         for (; i != e; ++i) {
00148             T* t = *i;
00149             if (0 != t) pushBack(t->clone());
00150             else        cont_.push_back(0);
00151         }
00152     }
00153 
00154     template<typename T, typename C>
00155     PtrList<T, C>& PtrList<T, C>::operator=(const PtrList& rhs)
00156     {
00157         if (this != &rhs) {
00158             PtrList tmp(rhs);
00159             swap(tmp);
00160         }
00161         return *this;
00162     }
00163 
00164     template<typename T, typename C>
00165     inline bool PtrList<T, C>::empty() const
00166     {
00167         return cont_.empty();
00168     }
00169 
00170     template<typename T, typename C>
00171     inline typename PtrList<T, C>::Size PtrList<T, C>::size() const
00172     {
00173         return cont_.size();
00174     }
00175 
00176     template<typename T, typename C>
00177     inline typename PtrList<T, C>::Iter PtrList<T, C>::begin()
00178     {
00179         return cont_.begin();
00180     }
00181 
00182     template<typename T, typename C>
00183     inline typename PtrList<T, C>::ConstIter PtrList<T, C>::begin() const
00184     {
00185         return cont_.begin();
00186     }
00187 
00188     template<typename T, typename C>
00189     inline typename PtrList<T, C>::Iter PtrList<T, C>::end()
00190     {
00191         return cont_.end();
00192     }
00193 
00194     template<typename T, typename C>
00195     inline typename PtrList<T, C>::ConstIter PtrList<T, C>::end() const
00196     {
00197         return cont_.end();
00198     }
00199 
00200     template<typename T, typename C>
00201     void PtrList<T, C>::pushBack(AutoPtr e)
00202     {
00203         cont_.push_back(e.get());
00204         e.release();
00205     }
00206 
00207     template<typename T, typename C>
00208     typename PtrList<T, C>::Iter PtrList<T, C>::erase(Iter pos)
00209     {
00210         T* t = *pos;
00211         delete t;
00212         return cont_.erase(pos);
00213     }
00214 
00215     template<typename T, typename C>
00216     inline void PtrList<T, C>::swap(PtrList& other)
00217     {
00218         cont_.swap(other.cont_);
00219     }
00220 }                                       // namespace OSB_LIB
00221 #endif                                  // #ifndef _OSBPTRLIST_H_

Generated on Sat Sep 2 14:06:33 2006 for OSB Library by  doxygen 1.4.7