Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
Aggregator.cpp
00001 /* 00002 * Aggregator.cpp 00003 * 00004 * Created on: Nov 1, 2013 00005 * * Authors: Vincent Wochnik <v.wochnik@gmail.com> 00006 * 00007 * Copyright (c) 2013 Cumulocity GmbH 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining 00010 * a copy of this software and associated documentation files (the 00011 * "Software"), to deal in the Software without restriction, including 00012 * without limitation the rights to use, copy, modify, merge, publish, 00013 * distribute, sublicense, and/or sell copies of the Software, and to 00014 * permit persons to whom the Software is furnished to do so, subject to 00015 * the following conditions: 00016 * 00017 * The above copyright notice and this permission notice shall be 00018 * included in all copies or substantial portions of the Software. 00019 * 00020 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00021 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00022 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00023 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00024 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00025 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00026 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00027 */ 00028 00029 #include "Aggregator.h" 00030 #include <stdlib.h> 00031 00032 /*-------------------------------------------------------------------------*/ 00033 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE 00034 Aggregator::Aggregator(size_t capacity, bool growing, bool managed) : 00035 _initial(capacity), 00036 _growing(growing), 00037 _managed(managed) 00038 { 00039 _capacity = 0; 00040 _list = NULL; 00041 _length = 0; 00042 } 00043 #else 00044 Aggregator::Aggregator(bool managed) : 00045 _managed(managed) 00046 { 00047 _length = 0; 00048 } 00049 #endif 00050 /*-------------------------------------------------------------------------*/ 00051 Aggregator::~Aggregator() 00052 { 00053 if (_managed) 00054 { 00055 for (size_t n = 0; n < _length; n++) 00056 delete _list[n]; 00057 } 00058 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE 00059 if (_list != NULL) 00060 free(_list); 00061 #endif 00062 } 00063 /*-------------------------------------------------------------------------*/ 00064 bool Aggregator::add(const Record& record) 00065 { 00066 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE 00067 if (_length == _capacity) 00068 { 00069 size_t capacity; 00070 00071 if ((!_growing) && (_capacity != 0)) 00072 return false; 00073 00074 if (_capacity == 0) 00075 capacity = _initial; 00076 else 00077 capacity = _capacity + SMARTREST_AGGREGATOR_MEMORY_INCREMENT; 00078 00079 const Record **list = (const Record**)realloc(_list, 00080 capacity*sizeof(Record*)); 00081 if (list == NULL) 00082 return false; 00083 _list = list; 00084 _capacity = capacity; 00085 } 00086 #else 00087 if (_length == SMARTREST_AGGREGATOR_FIXED_SIZE) 00088 return false; 00089 #endif 00090 00091 if (_managed) 00092 { 00093 Record *copy = record.copy(); 00094 if (copy == NULL) 00095 return false; 00096 _list[_length++] = copy; 00097 } 00098 else 00099 { 00100 _list[_length++] = &record; 00101 } 00102 return true; 00103 } 00104 /*-------------------------------------------------------------------------*/ 00105 const Record& Aggregator::get(size_t index) const 00106 { 00107 return *_list[index]; 00108 } 00109 /*-------------------------------------------------------------------------*/ 00110 void Aggregator::clear() 00111 { 00112 if (_managed) 00113 { 00114 for (size_t n = 0; n < _length; n++) 00115 delete _list[n]; 00116 } 00117 _length = 0; 00118 00119 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE 00120 if (_list != NULL) 00121 free(_list); 00122 _list = NULL; 00123 _capacity = 0; 00124 #endif 00125 } 00126 /*-------------------------------------------------------------------------*/ 00127 size_t Aggregator::length() const 00128 { 00129 return _length; 00130 } 00131 /*-------------------------------------------------------------------------*/ 00132 bool Aggregator::full() const 00133 { 00134 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE 00135 return (_growing) ? false : (_length == _capacity); 00136 #else 00137 return (_length == SMARTREST_AGGREGATOR_FIXED_SIZE); 00138 #endif 00139 } 00140 /*-------------------------------------------------------------------------*/ 00141 size_t Aggregator::capacity() const 00142 { 00143 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE 00144 return (_growing) ? 0 : _capacity; 00145 #else 00146 return SMARTREST_AGGREGATOR_FIXED_SIZE; 00147 #endif 00148 } 00149 /*-------------------------------------------------------------------------*/ 00150 bool Aggregator::managed() const 00151 { 00152 return _managed; 00153 } 00154 /*-------------------------------------------------------------------------*/ 00155 size_t Aggregator::writeTo(AbstractDataSink& sink) const 00156 { 00157 size_t len = 0; 00158 for (size_t n = 0; n < _length; n++) 00159 len += _list[n]->writeTo(sink); 00160 return len; 00161 } 00162 /*-------------------------------------------------------------------------*/ 00163 size_t Aggregator::writtenLength() const 00164 { 00165 size_t len = 0; 00166 for (size_t n = 0; n < _length; n++) 00167 len += _list[n]->writtenLength(); 00168 return len; 00169 } 00170 /*-------------------------------------------------------------------------*/ 00171 Aggregator* Aggregator::copy() const 00172 { 00173 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE 00174 Aggregator *copy = new Aggregator(_length, _growing, true); 00175 #else 00176 Aggregator *copy = new Aggregator(true); 00177 #endif 00178 for (size_t n = 0; n < _length; n++) 00179 copy->add(*_list[n]); 00180 return copy; 00181 } 00182 /*-------------------------------------------------------------------------*/
Generated on Tue Jul 12 2022 17:00:00 by 1.7.2