mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mvector.h Source File

m2mvector.h

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef M2M_VECTOR_H
00018 #define M2M_VECTOR_H
00019 
00020 #include <stdio.h>
00021 
00022 namespace m2m
00023 {
00024 
00025 template <typename ObjectTemplate>
00026 
00027 class Vector
00028 {
00029   public:
00030     explicit Vector( int init_size = 0)
00031             : _size( init_size ),
00032               _index(0),
00033               _capacity( init_size + MORE_SIZE ){
00034         _object_template = new ObjectTemplate[ _capacity ];
00035     }
00036 
00037     Vector(const Vector & rhs ): _object_template(0) {
00038         operator=(rhs);
00039     }
00040 
00041     ~Vector() {
00042         delete [] _object_template;
00043       _object_template = NULL;
00044     }
00045 
00046     const Vector & operator=(const Vector & rhs) {
00047         if(this != &rhs) {
00048             delete[] _object_template;
00049             _size = rhs.size();
00050             _index = rhs._index;
00051             _capacity = rhs._capacity;
00052 
00053             _object_template = new ObjectTemplate[capacity()];
00054             for(int k = 0; k < size(); k++) {
00055                 _object_template[k] = rhs._object_template[k];
00056             }
00057         }
00058         return *this;
00059     }
00060 
00061     void resize(int new_size) {
00062         if(new_size > _capacity) {
00063             reserve(new_size * 2 + 1);
00064         }
00065         _size = new_size;
00066     }
00067 
00068     void reserve(int new_capacity) {
00069         if(new_capacity < _size) {
00070             return;
00071         }
00072         ObjectTemplate *old_array = _object_template;
00073 
00074         _object_template = new ObjectTemplate[new_capacity];
00075         for(int k = 0; k < _size; k++) {
00076             _object_template[k] = old_array[k];
00077         }
00078         _capacity = new_capacity;
00079         delete [] old_array;
00080     }
00081 
00082     ObjectTemplate & operator[](int idx) {
00083         return _object_template[idx];
00084     }
00085 
00086     const ObjectTemplate& operator[](int idx) const {
00087         return _object_template[idx];
00088     }
00089 
00090     bool empty() const{
00091         return size() == 0;
00092     }
00093 
00094     int size() const {
00095         return _size;
00096     }
00097 
00098     int capacity() const {
00099         return _capacity;
00100     }
00101 
00102     void push_back(const ObjectTemplate& x) {
00103         if(_size == _capacity) {
00104             reserve(2 * _capacity + 1);
00105         }
00106         _object_template[_index++] = x;
00107         _size++;
00108     }
00109 
00110     void pop_back() {
00111         _size--;
00112         _index--;
00113     }
00114 
00115     void clear() {
00116         _size = 0;
00117         _index = 0;
00118     }
00119 
00120     const ObjectTemplate& back() const {
00121         return _object_template[_index- 1];
00122     }
00123 
00124     typedef ObjectTemplate* iterator;
00125     typedef const ObjectTemplate* const_iterator;
00126 
00127     iterator begin() {
00128         return &_object_template[0];
00129     }
00130 
00131     const_iterator begin() const {
00132         return &_object_template[0];
00133     }
00134 
00135     iterator end() {
00136         return &_object_template[_index];
00137     }
00138 
00139     const_iterator end() const {
00140         return &_object_template[_index];
00141     }
00142 
00143     void erase(int position) {
00144         if(position <= _size) {
00145             _object_template[position] = 0;
00146             _size--;
00147             if(position < _index) {
00148                 for(int k = position; k < _index; k++) {
00149                     _object_template[k] = _object_template[k+1];
00150                 }
00151             }
00152             _index--;
00153         }
00154     }
00155 
00156     enum {
00157         MORE_SIZE = 32
00158     };
00159 
00160   private:
00161     int                 _size;
00162     int                 _index;
00163     int                 _capacity;
00164     ObjectTemplate*     _object_template;
00165 };
00166 
00167 } // namespace
00168 
00169 #endif // M2M_VECTOR_H