00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #ifndef _TEMPLATE_ARRAY_H
00026 #define _TEMPLATE_ARRAY_H
00027
00028 #include "beecrypt/api.h"
00029
00030 #ifdef __cplusplus
00031
00032 #include <new>
00033
00034 namespace beecrypt {
00035
00036 template <typename T>
00037 class array
00038 {
00039 protected:
00040 T* _data;
00041 size_t _size;
00042
00043 public:
00044 array() throw ()
00045 {
00046 _data = 0;
00047 _size = 0;
00048 }
00049
00050 array(size_t size) throw (std::bad_alloc)
00051 {
00052 if (size)
00053 {
00054 _data = (T*) malloc(size * sizeof(T));
00055 if (_data == 0)
00056 throw std::bad_alloc();
00057 }
00058 else
00059 _data = 0;
00060 _size = size;
00061 }
00062
00063 array(const T* data, size_t size) throw (std::bad_alloc)
00064 {
00065 _data = (T*) malloc(size * sizeof(T));
00066 if (_data == 0)
00067 throw std::bad_alloc();
00068 _size = size;
00069 memcpy(_data, data, _size * sizeof(T));
00070 }
00071
00072 array(const array& _copy) throw (std::bad_alloc)
00073 {
00074 if (_copy._size)
00075 {
00076 _data = (T*) malloc(_copy._size * sizeof(T));
00077 if (_data == 0)
00078 throw std::bad_alloc();
00079 _size = _copy._size;
00080 memcpy(_data, _copy._data, _size * sizeof(T));
00081 }
00082 else
00083 {
00084 _data = 0;
00085 _size = 0;
00086 }
00087 }
00088
00089 ~array() throw ()
00090 {
00091 if (_data)
00092 free(_data);
00093 }
00094
00095 array* clone() const throw (std::bad_alloc)
00096 {
00097 return new array(*this);
00098 }
00099
00100 const array& operator=(const array& _set) throw (std::bad_alloc)
00101 {
00102 resize(_set._size);
00103 if (_size)
00104 memcpy(_data, _set._data, _size * sizeof(T));
00105
00106 return *this;
00107 }
00108
00109 bool operator==(const array& _cmp) const throw ()
00110 {
00111 if (_size != _cmp._size)
00112 return false;
00113
00114 if (_size == 0 && _cmp._size == 0)
00115 return true;
00116
00117 return !memcmp(_data, _cmp._data, _size * sizeof(T));
00118 }
00119
00120 bool operator!=(const array& _cmp) const throw ()
00121 {
00122 if (_size != _cmp._size)
00123 return true;
00124
00125 if (_size == 0 && _cmp._size == 0)
00126 return false;
00127
00128 return memcmp(_data, _cmp._data, _size * sizeof(T));
00129 }
00130
00131 T* data() throw ()
00132 {
00133 return _data;
00134 }
00135
00136 const T* data() const throw ()
00137 {
00138 return _data;
00139 }
00140
00141 size_t size() const throw ()
00142 {
00143 return _size;
00144 }
00145
00146 void replace(T* data, size_t size) throw ()
00147 {
00148 if (_data)
00149 free(_data);
00150
00151 _data = data;
00152 _size = size;
00153 }
00154
00155 void swap(array& _swp) throw ()
00156 {
00157 T* tmp_data = _swp._data;
00158 size_t tmp_size = _swp._size;
00159
00160 _swp._data = _data;
00161 _swp._size = _size;
00162
00163 _data = tmp_data;
00164 _size = tmp_size;
00165 }
00166
00167 void resize(size_t _newsize) throw (std::bad_alloc)
00168 {
00169 if (_newsize)
00170 {
00171 if (_newsize != _size)
00172 {
00173 _data = (T*) (_data ? realloc(_data, _newsize * sizeof(T)) : malloc(_newsize * sizeof(T)));
00174 if (_data == 0)
00175 throw std::bad_alloc();
00176 }
00177 }
00178 else
00179 {
00180 if (_data)
00181 {
00182 free(_data);
00183 _data = 0;
00184 }
00185 }
00186 _size = _newsize;
00187 }
00188
00189 T& operator[](size_t _n) throw ()
00190 {
00191 return _data[_n];
00192 }
00193
00194 const T operator[](size_t _n) const throw ()
00195 {
00196 return _data[_n];
00197 }
00198
00199 const array<T>& operator+=(const array<T>& _rhs) throw ()
00200 {
00201 if (_rhs._size)
00202 {
00203 size_t _curr = _size;
00204 resize(_size+_rhs._size);
00205 memcpy(_data+_curr, _rhs._data, _rhs._size * sizeof(T));
00206 }
00207 return *this;
00208 }
00209 };
00210
00211 template<typename T>
00212 array<T> operator+(const array<T>& _lhs, const array<T>& _rhs)
00213 {
00214 array<T> _con(_lhs);
00215
00216 return _con += _rhs;
00217 };
00218
00219 typedef array<byte> bytearray;
00220 typedef array<javachar> javachararray;
00221 }
00222
00223 #endif
00224
00225 #endif