00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00050 protected:
00051 collection internal_vector;
00053
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