Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Mon Aug 10 10:39:53 2015 +0000
Revision:
26:9c36af176d91
Parent:
11:e1bee9a77652
removed traffic accounting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * Aggregator.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Nov 1, 2013
Cumulocity 0:099f76422485 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
Cumulocity 0:099f76422485 6 *
Cumulocity 0:099f76422485 7 * Copyright (c) 2013 Cumulocity GmbH
Cumulocity 0:099f76422485 8 *
Cumulocity 0:099f76422485 9 * Permission is hereby granted, free of charge, to any person obtaining
Cumulocity 0:099f76422485 10 * a copy of this software and associated documentation files (the
Cumulocity 0:099f76422485 11 * "Software"), to deal in the Software without restriction, including
Cumulocity 0:099f76422485 12 * without limitation the rights to use, copy, modify, merge, publish,
Cumulocity 0:099f76422485 13 * distribute, sublicense, and/or sell copies of the Software, and to
Cumulocity 0:099f76422485 14 * permit persons to whom the Software is furnished to do so, subject to
Cumulocity 0:099f76422485 15 * the following conditions:
Cumulocity 0:099f76422485 16 *
Cumulocity 0:099f76422485 17 * The above copyright notice and this permission notice shall be
Cumulocity 0:099f76422485 18 * included in all copies or substantial portions of the Software.
Cumulocity 0:099f76422485 19 *
Cumulocity 0:099f76422485 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Cumulocity 0:099f76422485 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Cumulocity 0:099f76422485 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Cumulocity 0:099f76422485 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Cumulocity 0:099f76422485 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Cumulocity 0:099f76422485 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Cumulocity 0:099f76422485 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Cumulocity 0:099f76422485 27 */
Cumulocity 0:099f76422485 28
Cumulocity 0:099f76422485 29 #include "Aggregator.h"
Cumulocity 0:099f76422485 30 #include <stdlib.h>
Cumulocity 0:099f76422485 31
Cumulocity 11:e1bee9a77652 32 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 33 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
Cumulocity 5:2b74510900da 34 Aggregator::Aggregator(size_t capacity, bool growing, bool managed) :
Cumulocity 11:e1bee9a77652 35 _initial(capacity),
Cumulocity 11:e1bee9a77652 36 _growing(growing),
Cumulocity 11:e1bee9a77652 37 _managed(managed)
Cumulocity 0:099f76422485 38 {
Cumulocity 11:e1bee9a77652 39 _capacity = 0;
Cumulocity 11:e1bee9a77652 40 _list = NULL;
Cumulocity 11:e1bee9a77652 41 _length = 0;
Cumulocity 0:099f76422485 42 }
Cumulocity 0:099f76422485 43 #else
Cumulocity 5:2b74510900da 44 Aggregator::Aggregator(bool managed) :
Cumulocity 11:e1bee9a77652 45 _managed(managed)
Cumulocity 0:099f76422485 46 {
Cumulocity 11:e1bee9a77652 47 _length = 0;
Cumulocity 0:099f76422485 48 }
Cumulocity 0:099f76422485 49 #endif
Cumulocity 11:e1bee9a77652 50 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 51 Aggregator::~Aggregator()
Cumulocity 0:099f76422485 52 {
Cumulocity 11:e1bee9a77652 53 if (_managed)
Cumulocity 11:e1bee9a77652 54 {
Cumulocity 11:e1bee9a77652 55 for (size_t n = 0; n < _length; n++)
Cumulocity 11:e1bee9a77652 56 delete _list[n];
Cumulocity 11:e1bee9a77652 57 }
Cumulocity 11:e1bee9a77652 58 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
Cumulocity 11:e1bee9a77652 59 if (_list != NULL)
Cumulocity 11:e1bee9a77652 60 free(_list);
Cumulocity 11:e1bee9a77652 61 #endif
Cumulocity 0:099f76422485 62 }
Cumulocity 11:e1bee9a77652 63 /*-------------------------------------------------------------------------*/
Cumulocity 5:2b74510900da 64 bool Aggregator::add(const Record& record)
Cumulocity 0:099f76422485 65 {
Cumulocity 11:e1bee9a77652 66 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
Cumulocity 11:e1bee9a77652 67 if (_length == _capacity)
Cumulocity 11:e1bee9a77652 68 {
Cumulocity 11:e1bee9a77652 69 size_t capacity;
Cumulocity 0:099f76422485 70
Cumulocity 11:e1bee9a77652 71 if ((!_growing) && (_capacity != 0))
Cumulocity 11:e1bee9a77652 72 return false;
Cumulocity 0:099f76422485 73
Cumulocity 11:e1bee9a77652 74 if (_capacity == 0)
Cumulocity 11:e1bee9a77652 75 capacity = _initial;
Cumulocity 11:e1bee9a77652 76 else
Cumulocity 11:e1bee9a77652 77 capacity = _capacity + SMARTREST_AGGREGATOR_MEMORY_INCREMENT;
Cumulocity 0:099f76422485 78
Cumulocity 11:e1bee9a77652 79 const Record **list = (const Record**)realloc(_list,
Cumulocity 11:e1bee9a77652 80 capacity*sizeof(Record*));
Cumulocity 11:e1bee9a77652 81 if (list == NULL)
Cumulocity 11:e1bee9a77652 82 return false;
Cumulocity 11:e1bee9a77652 83 _list = list;
Cumulocity 11:e1bee9a77652 84 _capacity = capacity;
Cumulocity 11:e1bee9a77652 85 }
Cumulocity 11:e1bee9a77652 86 #else
Cumulocity 11:e1bee9a77652 87 if (_length == SMARTREST_AGGREGATOR_FIXED_SIZE)
Cumulocity 11:e1bee9a77652 88 return false;
Cumulocity 11:e1bee9a77652 89 #endif
Cumulocity 0:099f76422485 90
Cumulocity 11:e1bee9a77652 91 if (_managed)
Cumulocity 11:e1bee9a77652 92 {
Cumulocity 11:e1bee9a77652 93 Record *copy = record.copy();
Cumulocity 11:e1bee9a77652 94 if (copy == NULL)
Cumulocity 11:e1bee9a77652 95 return false;
Cumulocity 11:e1bee9a77652 96 _list[_length++] = copy;
Cumulocity 11:e1bee9a77652 97 }
Cumulocity 11:e1bee9a77652 98 else
Cumulocity 11:e1bee9a77652 99 {
Cumulocity 11:e1bee9a77652 100 _list[_length++] = &record;
Cumulocity 11:e1bee9a77652 101 }
Cumulocity 11:e1bee9a77652 102 return true;
Cumulocity 0:099f76422485 103 }
Cumulocity 11:e1bee9a77652 104 /*-------------------------------------------------------------------------*/
Cumulocity 6:cd7ba1ddb664 105 const Record& Aggregator::get(size_t index) const
Cumulocity 5:2b74510900da 106 {
Cumulocity 11:e1bee9a77652 107 return *_list[index];
Cumulocity 5:2b74510900da 108 }
Cumulocity 11:e1bee9a77652 109 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 110 void Aggregator::clear()
Cumulocity 0:099f76422485 111 {
Cumulocity 11:e1bee9a77652 112 if (_managed)
Cumulocity 11:e1bee9a77652 113 {
Cumulocity 11:e1bee9a77652 114 for (size_t n = 0; n < _length; n++)
Cumulocity 11:e1bee9a77652 115 delete _list[n];
Cumulocity 11:e1bee9a77652 116 }
Cumulocity 11:e1bee9a77652 117 _length = 0;
Cumulocity 0:099f76422485 118
Cumulocity 11:e1bee9a77652 119 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
Cumulocity 11:e1bee9a77652 120 if (_list != NULL)
Cumulocity 11:e1bee9a77652 121 free(_list);
Cumulocity 11:e1bee9a77652 122 _list = NULL;
Cumulocity 11:e1bee9a77652 123 _capacity = 0;
Cumulocity 11:e1bee9a77652 124 #endif
Cumulocity 0:099f76422485 125 }
Cumulocity 11:e1bee9a77652 126 /*-------------------------------------------------------------------------*/
Cumulocity 6:cd7ba1ddb664 127 size_t Aggregator::length() const
Cumulocity 0:099f76422485 128 {
Cumulocity 11:e1bee9a77652 129 return _length;
Cumulocity 0:099f76422485 130 }
Cumulocity 11:e1bee9a77652 131 /*-------------------------------------------------------------------------*/
Cumulocity 6:cd7ba1ddb664 132 bool Aggregator::full() const
Cumulocity 0:099f76422485 133 {
Cumulocity 11:e1bee9a77652 134 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
Cumulocity 11:e1bee9a77652 135 return (_growing) ? false : (_length == _capacity);
Cumulocity 11:e1bee9a77652 136 #else
Cumulocity 11:e1bee9a77652 137 return (_length == SMARTREST_AGGREGATOR_FIXED_SIZE);
Cumulocity 11:e1bee9a77652 138 #endif
Cumulocity 0:099f76422485 139 }
Cumulocity 11:e1bee9a77652 140 /*-------------------------------------------------------------------------*/
Cumulocity 6:cd7ba1ddb664 141 size_t Aggregator::capacity() const
Cumulocity 0:099f76422485 142 {
Cumulocity 11:e1bee9a77652 143 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
Cumulocity 11:e1bee9a77652 144 return (_growing) ? 0 : _capacity;
Cumulocity 11:e1bee9a77652 145 #else
Cumulocity 11:e1bee9a77652 146 return SMARTREST_AGGREGATOR_FIXED_SIZE;
Cumulocity 11:e1bee9a77652 147 #endif
Cumulocity 0:099f76422485 148 }
Cumulocity 11:e1bee9a77652 149 /*-------------------------------------------------------------------------*/
Cumulocity 6:cd7ba1ddb664 150 bool Aggregator::managed() const
Cumulocity 5:2b74510900da 151 {
Cumulocity 11:e1bee9a77652 152 return _managed;
Cumulocity 5:2b74510900da 153 }
Cumulocity 11:e1bee9a77652 154 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 155 size_t Aggregator::writeTo(AbstractDataSink& sink) const
Cumulocity 0:099f76422485 156 {
Cumulocity 11:e1bee9a77652 157 size_t len = 0;
Cumulocity 11:e1bee9a77652 158 for (size_t n = 0; n < _length; n++)
Cumulocity 11:e1bee9a77652 159 len += _list[n]->writeTo(sink);
Cumulocity 11:e1bee9a77652 160 return len;
Cumulocity 0:099f76422485 161 }
Cumulocity 11:e1bee9a77652 162 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 163 size_t Aggregator::writtenLength() const
Cumulocity 0:099f76422485 164 {
Cumulocity 11:e1bee9a77652 165 size_t len = 0;
Cumulocity 11:e1bee9a77652 166 for (size_t n = 0; n < _length; n++)
Cumulocity 11:e1bee9a77652 167 len += _list[n]->writtenLength();
Cumulocity 11:e1bee9a77652 168 return len;
Cumulocity 0:099f76422485 169 }
Cumulocity 11:e1bee9a77652 170 /*-------------------------------------------------------------------------*/
Cumulocity 5:2b74510900da 171 Aggregator* Aggregator::copy() const
Cumulocity 0:099f76422485 172 {
Cumulocity 11:e1bee9a77652 173 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
Cumulocity 11:e1bee9a77652 174 Aggregator *copy = new Aggregator(_length, _growing, true);
Cumulocity 11:e1bee9a77652 175 #else
Cumulocity 11:e1bee9a77652 176 Aggregator *copy = new Aggregator(true);
Cumulocity 11:e1bee9a77652 177 #endif
Cumulocity 11:e1bee9a77652 178 for (size_t n = 0; n < _length; n++)
Cumulocity 11:e1bee9a77652 179 copy->add(*_list[n]);
Cumulocity 11:e1bee9a77652 180 return copy;
Cumulocity 0:099f76422485 181 }
Cumulocity 11:e1bee9a77652 182 /*-------------------------------------------------------------------------*/