Main Page   Modules   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

oscl_vector.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00003 
00004 //                     O S C L _ V E C T O R
00005 
00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00007 
00017 #ifndef OSCL_VECTOR_H_INCLUDED
00018 #define OSCL_VECTOR_H_INCLUDED
00019 
00020 #ifndef OSCL_BASE_H_INCLUDED
00021 #include "oscl_base.h"
00022 #endif
00023 
00024 #ifndef OSCL_MEM_BASIC_FUNCTIONS_H_INCLUDED
00025 #include "oscl_mem_basic_functions.h"
00026 #endif
00027 
00028 #ifndef OSCL_ASSERT_H_INCLUDED
00029 #include "oscl_assert.h"
00030 #endif
00031 
00032 #ifndef OSCL_OPAQUE_TYPE_H_INCLUDED
00033 #include "oscl_opaque_type.h"
00034 #endif
00035 
00036 #ifndef OSCL_DEF_ALLOC_H_INCLUDED
00037 #include "oscl_defalloc.h"
00038 #endif
00039 
00047 class Oscl_Vector_Base
00048 {
00049     public:
00053         uint32 size() const
00054         {
00055             return numelems;
00056         }
00057 
00061         uint32 capacity() const
00062         {
00063             return bufsize;
00064         }
00065 
00069         bool empty() const
00070         {
00071             return numelems == 0;
00072         }
00073 
00082         OSCL_IMPORT_REF void reserve(uint32 n) ;
00083 
00084     protected:
00085 
00086         //for use in default constructor.  vtable isn't available in c'tor so this is a subroutine.
00087         OSCL_IMPORT_REF void construct(Oscl_Opaque_Type_Alloc* aType);
00088 
00089         //for use in constructor with pre-allocation for "n" elements.
00090         OSCL_IMPORT_REF void construct(Oscl_Opaque_Type_Alloc* aType, uint32 n) ;
00091 
00092         //for use in copy constructor.
00093         OSCL_IMPORT_REF void construct(Oscl_Opaque_Type_Alloc* aType, const Oscl_Vector_Base& x);
00094 
00098         virtual ~Oscl_Vector_Base()
00099         {}
00100 
00107         OSCL_IMPORT_REF void push_back(const OsclAny* x) ;
00108 
00112         OSCL_IMPORT_REF void pop_back() ;
00113 
00120         OSCL_IMPORT_REF void push_front(const OsclAny* x) ;
00121 
00127         OSCL_IMPORT_REF OsclAny* insert(OsclAny* pos, const OsclAny* x);
00128 
00135         OSCL_IMPORT_REF OsclAny* erase(OsclAny* pos) ;
00136 
00144         OSCL_IMPORT_REF OsclAny* erase(OsclAny* first, OsclAny* last) ;
00145 
00146         //copy vector x into this vector
00147         OSCL_IMPORT_REF void assign_vector(const Oscl_Vector_Base& x);
00148 
00149         OSCL_IMPORT_REF void destroy();
00150 
00151         uint32 numelems; //number of T items in buffer.
00152         uint32 bufsize; //available bufsize as number of T-sized items.
00153         OsclAny* elems; //buffer
00154         uint32 sizeof_T;
00155 
00156         friend class OsclPriorityQueueBase;
00157 
00158     private:
00159 
00163         OsclAny* increment_T(OsclAny* p_T, int32 n) const
00164         {
00165             return (OsclAny*)((int32)p_T + n*sizeof_T);
00166         }
00167 
00171         OsclAny* begin() const
00172         {
00173             return elems;
00174         }
00175 
00179         OsclAny* end() const
00180         {
00181             return increment_T(elems, numelems);
00182         }
00183 
00184         OsclAny* move(OsclAny* first, OsclAny* last, OsclAny* result) ;
00185 
00186         OsclAny* copy(OsclAny* first, OsclAny* last, OsclAny* result) ;
00187 
00188         OsclAny* uninitialized_copy(OsclAny* first, OsclAny* last, OsclAny* result) ;
00189 
00190         //destroy a range of T*
00191         void destroy(OsclAny* first, OsclAny* last) ;
00192 
00193         Oscl_Opaque_Type_Alloc* pOpaqueType;
00194 };
00195 
00204 template<class T, class Alloc>
00205 class Oscl_Vector
00206             : public Oscl_Vector_Base
00207             , public Oscl_Opaque_Type_Alloc
00208 {
00209 
00210     public:
00211         typedef T value_type;
00212         typedef T* pointer;
00213         typedef T& reference;
00214         typedef const T& const_reference;
00215         typedef T* iterator;
00216         typedef const T* const_iterator;
00217 
00221         Oscl_Vector() : Oscl_Vector_Base(), Oscl_Opaque_Type_Alloc()
00222         {
00223             sizeof_T = sizeof(T);
00224             Oscl_Vector_Base::construct(this);
00225         }
00226 
00235         Oscl_Vector(uint32 n) : Oscl_Vector_Base(), Oscl_Opaque_Type_Alloc()
00236         {
00237             sizeof_T = sizeof(T);
00238             Oscl_Vector_Base::construct(this, n);
00239         }
00240 
00245         Oscl_Vector(const Oscl_Vector<T, Alloc>& x) : Oscl_Vector_Base(), Oscl_Opaque_Type_Alloc()
00246         {
00247             sizeof_T = sizeof(T);
00248             Oscl_Vector_Base::construct(this, x);
00249         }
00250 
00254         virtual ~Oscl_Vector()
00255         {
00256             Oscl_Vector_Base::destroy();
00257         }
00258 
00262         Oscl_Vector<T, Alloc>& operator=(const Oscl_Vector<T, Alloc>& x)
00263         {
00264             if (&x != this)
00265                 Oscl_Vector_Base::assign_vector(x);
00266             return *this;
00267         }
00268 
00275         void push_back(const T& x)
00276         {
00277             Oscl_Vector_Base::push_back(&x);
00278         }
00279 
00286         void push_front(const T& x)
00287         {
00288             Oscl_Vector_Base::push_front(&x);
00289         }
00290 
00296         iterator insert(iterator pos, const T& x)
00297         {
00298             return (iterator)Oscl_Vector_Base::insert(pos, &x);
00299         }
00300 
00305         T& operator[](uint32 n)
00306         {
00307             return (*(begin() + n));
00308         }
00309 
00314         const T& operator[](uint32 n) const
00315         {
00316             return (*(begin() + n));
00317         }
00318 
00322         T& front()
00323         {
00324             return *begin();
00325         }
00326 
00330         const T& front() const
00331         {
00332             return *begin();
00333         }
00334 
00338         T& back()
00339         {
00340             return (*(end() - 1));
00341         }
00342 
00346         const T& back() const
00347         {
00348             return (*(end() - 1));
00349         }
00350 
00354         void pop_back()
00355         {
00356             OSCL_ASSERT(numelems);
00357             numelems--;
00358             destroy(end());
00359         }
00360 
00364         void clear()
00365         {
00366             Oscl_Vector_Base::erase(begin(), end());
00367         }
00368 
00372         void destroy()
00373         {
00374             Oscl_Vector_Base::destroy();
00375         }
00376 
00380         iterator begin() const
00381         {
00382             return (pointer)elems;
00383         }
00384 
00388         iterator end() const
00389         {
00390             return (pointer)elems + numelems;
00391         }
00392 
00399         iterator erase(iterator pos)
00400         {
00401             return (iterator)Oscl_Vector_Base::erase(pos);
00402         }
00403 
00411         iterator erase(iterator first, iterator last)
00412         {
00413             return (iterator)Oscl_Vector_Base::erase(first, last);
00414         }
00415 
00416     private:
00417 
00418         Alloc defAlloc;
00419 
00420         //from Oscl_Opaque_Type_Alloc
00421         OsclAny* allocate(const uint32 size)
00422         {
00423             //prevent zero-size allocations since some allocators don't handle this
00424             return (size) ? defAlloc.ALLOCATE(size) : NULL;
00425         }
00426 
00427         //from Oscl_Opaque_Type_Alloc
00428         void deallocate(OsclAny* p)
00429         {
00430             defAlloc.deallocate(p);
00431         }
00432 
00433         //from Oscl_Opaque_Type_Alloc
00434         void construct(OsclAny* p, const OsclAny* x)
00435         {
00436             OSCL_ASSERT(x);
00437             new(p) value_type(*((T*)x));
00438         }
00439 
00440         //from Oscl_Opaque_Type_Alloc
00441         void destroy(OsclAny* first)
00442         {
00443             OSCL_ASSERT(first);
00444             OSCL_UNUSED_ARG(first);
00445             //note: must use "pointer" instead of "T*" here to avoid ADS 1.2 compile error.
00446             ((pointer)first)->~T();
00447         }
00448 
00449 };
00450 
00451 
00455 #endif
00456 
00457 
00458 
00459 
00460 

OSCL API
Posting Version: OPENCORE_20090310