autovector.h

00001 /***************************************************************************
00002 *   Copyright (C) 2005 by                                                 *
00003 *   Alejandro Perez Mendez     alejandro_perez@dif.um.es                  *
00004 *   Pedro J. Fernandez Ruiz    pedroj.fernandez@dif.um.es                 *
00005 *                                                                         *
00006 *   This library is free software; you can redistribute it and/or         *
00007 *   modify it under the terms of the GNU Lesser General Public            *
00008 *   License as published by the Free Software Foundation; either          *
00009 *   version 2.1 of the License, or (at your option) any later version.    *
00010 *                                                                         *
00011 *   This library is distributed in the hope that it will be useful,       *
00012 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014 *   Lesser General Public License for more details.                       *
00015 *                                                                         *
00016 *   You should have received a copy of the GNU Lesser General Public      *
00017 *   License along with this library; if not, write to the Free Software   *
00018 *   Foundation, Inc., 51 Franklin St, Fifth Floor,                        *
00019 *   Boston, MA  02110-1301  USA                                           *
00020 ***************************************************************************/
00021 #ifndef OPENIKEV2VECTOROFPOINTER_H
00022 #define OPENIKEV2VECTOROFPOINTER_H
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include "config.h"
00026 #endif
00027 
00028 #include "printable.h"
00029 #include <vector>
00030 
00031 using namespace std;
00032 
00033 namespace openikev2 {
00034 
00035     template <typename _Tp1>
00036     struct AutoVectorRef {
00037         vector<_Tp1*> internal_vector;
00038         explicit AutoVectorRef( vector<_Tp1*> __p ) throw() : internal_vector( __p ) {};
00039     };
00040 
00045     template <typename T> class AutoVector {
00046             typedef T* element_type;
00047             typedef vector<element_type> collection;
00048 
00049             /****************************** ATTRIBUTES ******************************/
00050         protected:
00051             collection internal_vector;     
00053             /****************************** METHODS ******************************/
00054         public:
00058             AutoVector( ) throw() {};
00059 
00064             explicit AutoVector( collection v ) throw() : internal_vector( v ) {};
00065 
00071             AutoVector( AutoVector< T > &other ) throw() : internal_vector( other.release() ) {};
00072 
00073             AutoVector<T>& operator=( AutoVector<T>& other ) throw() {
00074                 this->reset( other.release() );
00075                 return *this;
00076             }
00077 
00078             virtual ~AutoVector() {
00079                 this->deleteAll();
00080             }
00081 
00082             collection& get() {
00083                 return this->internal_vector;
00084             }
00085 
00086             const collection& get() const {
00087                     return this->internal_vector;
00088                 }
00089 
00090             collection release() {
00091                 collection temp = this->internal_vector;
00092                 this->internal_vector.clear();
00093                 return temp;
00094             }
00095 
00096             void reset( collection v ) {
00097                 this->deleteAll();
00098                 this->internal_vector = v;
00099             }
00100 
00101             collection* operator->() throw() {
00102                 return & this->internal_vector;
00103             }
00104 
00105             void clear(){
00106                 this->deleteAll();
00107                 this->internal_vector.clear();            
00108             }
00109             
00110             const collection* operator->() const throw() {
00111                 return & this->internal_vector;
00112             }
00113 
00114             element_type& operator[] ( int i ) {
00115                 return this->internal_vector[ i ];
00116             }
00117 
00118             AutoVector( AutoVectorRef<T> __ref ) throw() : internal_vector ( __ref.internal_vector ) {}
00119 
00120             AutoVector& operator=( AutoVectorRef<T> __ref ) throw() {
00121                 this->deleteAll();
00122                 this->internal_vector = __ref.internal_vector;
00123                 return *this;
00124             }
00125 
00126             template <typename _Tp1> operator AutoVectorRef< _Tp1 >() throw() {
00127                 return AutoVectorRef<_Tp1>( this->release() );
00128             }
00129 
00130             template <typename _Tp1> operator AutoVector<_Tp1>() throw() {
00131                 return AutoVector<_Tp1>( this->release() );
00132             }
00133 
00134             void deleteAll() {
00135                 typename collection::iterator it;
00136                 for ( it = this->internal_vector.begin(); it != this->internal_vector.end(); it++ )
00137                     delete ( *it );
00138             }
00139 
00140 
00141     };
00142 
00143 }
00144 
00145 #endif

Generated on Fri Jul 27 11:04:44 2007 for libopenikev2 by  doxygen 1.5.1