- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Containers/DynamicArrayTemplate.h

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #ifndef DYNAMIC_ARRAY_TEMPLATE_HEEADER_FILE
    00015 #define DYNAMIC_ARRAY_TEMPLATE_HEEADER_FILE
    00016
    00017 #include <cassert>
    00018
    00019 #include "DynamicArray.h"
    00020
    00021 namespace Mers {
    00022         namespace Utils {
    00023                 namespace Containers {
    00024
    00036                         template<class Data> class DynamicArrayTemplate : public DynamicArray
    00037                         {
    00038                         public:
    00039                                 //-----------------------------------------
    00040                                 // Constructors
    00041                                 //-----------------------------------------
    00042
    00046                                 DynamicArrayTemplate(bool myOwnershipStatus)
    00047                                         : DynamicArray(DynamicArray::oDynamicArrayTemplate, myOwnershipStatus)
    00048                                 {
    00049                                 }
    00050
    00051                                 //-----------------------------------------
    00052                                 // Copy constructors
    00053                                 //-----------------------------------------
    00054
    00061                                 DynamicArrayTemplate(DynamicArrayTemplate<Data> const &other) : DynamicArray(other)
    00062                                 {
    00063                                         assert(other.getDynamicArrayType() == DynamicArray::oDynamicArrayTemplate);
    00064                                         if(FAILED(copy(other))){
    00065 #ifndef EMBEDDED
    00066                                                 std::cerr << "Unable to copy while constructing a new DynamicArrayTemplate: " << std::endl << other << std::endl;
    00067 #else
    00068                                                 fprintf(stderr, "Error, failed to copy the DynamicArrayTemplate while constructing a new one\n");
    00069 #endif
    00070                                         }
    00071                                 }
    00072
    00073                                 //-----------------------------------------
    00074                                 // Destructors
    00075                                 //-----------------------------------------
    00076
    00080                                 virtual ~DynamicArrayTemplate()
    00081                                 {
    00082                                         makeEmpty();
    00083                                 }
    00084
    00085                                 //-----------------------------------------
    00086                                 // Iterators
    00087                                 //-----------------------------------------
    00088
    00089                                 typedef Data * pointer;
    00090                                 typedef const Data * const_pointer;
    00091                                 typedef DynamicArrayTemplate<Data> ContainerType;
    00092                                 typedef IteratorTemplate<const_pointer, ContainerType> const_iterator;
    00093                                 typedef IteratorTemplate<pointer, ContainerType> iterator;
    00094                                 typedef const_iterator ConstantIterator;
    00095                                 typedef iterator Iterator;
    00096
    00097                                 //-----------------------------------------
    00098                                 // Functions
    00099                                 //-----------------------------------------
    00100
    00106                                 Iterator begin()
    00107                                 {
    00108                                         return Iterator(this, 0);
    00109                                 }
    00116                                 ConstantIterator begin() const
    00117                                 {
    00118                                         return ConstantIterator((ContainerType *)this, 0);
    00119                                 }
    00120
    00127                                 Iterator         end()
    00128                                 {
    00129                                         return Iterator(this, getLength());
    00130                                 }
    00137                                 ConstantIterator end() const
    00138                                 {
    00139                                         return ConstantIterator((ContainerType *)this, getLength());
    00140                                 }
    00141
    00153                                 int pushBack(Data * data)
    00154                                 {
    00155                                         return DynamicArray::pushBack((void *)data);
    00156                                 }
    00157
    00169                                 int push_back(Data * data)
    00170                                 {
    00171                                         return pushBack(data);
    00172                                 }
    00173
    00181                                 Data * popBack()
    00182                                 {
    00183                                         return (Data *) DynamicArray::popBack();
    00184                                 }
    00192                                 Data * pop_back()
    00193                                 {
    00194                                         return popBack();
    00195                                 }
    00196
    00205                                 void insert(unsigned int elementIndex, Data * data)
    00206                                 {
    00207                                         DynamicArray::insert(elementIndex, (void *) data);
    00208                                 }
    00209
    00210
    00211                                 //-----------------------------------------
    00212                                 // Operators
    00213                                 //-----------------------------------------
    00214
    00223                                 Data * & operator[](int elementIndex)
    00224                                 {
    00225                                         return (Data * &) DynamicArray::operator [](elementIndex);
    00226                                 }
    00227
    00236                                 Data * & operator[](unsigned int elementIndex)
    00237                                 {
    00238                                         return (Data * &) DynamicArray::operator [](elementIndex);
    00239                                 }
    00240
    00249                                 Data const * & operator[](int elementIndex) const
    00250                                 {
    00251                                         return (Data const * &) DynamicArray::operator [](elementIndex);
    00252                                 }
    00253
    00262                                 Data const * & operator[](unsigned int elementIndex) const
    00263                                 {
    00264                                         return (Data const * &) DynamicArray::operator [](elementIndex);
    00265                                 }
    00266
    00276                                 DynamicArrayTemplate<Data> const & operator =(DynamicArrayTemplate<Data> const &other)
    00277                                 {
    00278                                         return (DynamicArrayTemplate<Data> const &) DynamicArray::operator =(other);
    00279                                 }
    00280
    00289                                 DynamicArrayTemplate<Data> const & operator +=(DynamicArrayTemplate<Data> const &other)
    00290                                 {
    00291                                         return (DynamicArrayTemplate<Data> const &) DynamicArray::operator +=(other);
    00292                                 }
    00293
    00303                                 DynamicArrayTemplate<Data> const & operator +=(Data const &data)
    00304                                 {
    00305                                         return (DynamicArrayTemplate<Data> const &) DynamicArray::operator +=((void const *)&data);
    00306                                 }
    00307
    00317                                 bool operator ==(DynamicArrayTemplate<Data> const &other) const
    00318                                 {
    00319                                         return DynamicArray::operator ==(other);
    00320                                 }
    00321
    00331                                 bool operator !=(DynamicArrayTemplate<Data> const &other) const
    00332                                 {
    00333                                         return !DynamicArray::operator ==(other);
    00334                                 }
    00335
    00336                         protected:
    00337                                 //-----------------------------------------
    00338                                 // Functions
    00339                                 //-----------------------------------------
    00340
    00341 #ifndef EMBEDDED
    00342
    00351                                 void printElement(std::ostream &out, void const * element) const
    00352                                 {
    00353                                         ((Data *) element)->print(out);
    00354                                 }
    00355
    00356 #endif
    00357
    00365                                 void deleteElement(void * element) const
    00366                                 {
    00367                                         delete (Data *) element;
    00368                                 }
    00369
    00378                                 void * newElement(void const * element) const
    00379                                 {
    00380                                         // Probably want something like this
    00381                                         return ((Data *) element)->duplicate();
    00382                                 }
    00383
    00394                                 bool isEqual(void const * element1, void const * element2) const
    00395                                 {
    00396                                         Data const &e1 = *(Data const *) element1;
    00397                                         Data const &e2 = *(Data const *) element2;
    00398                                         return (e1.operator==(e2));
    00399                                 }
    00400                         };
    00401                 }
    00402         }
    00403 }
    00404
    00405 #endif
    00406