Generic SmartRest library

Dependents:   SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming

Committer:
vwochnik
Date:
Mon Mar 24 09:57:40 2014 +0000
Revision:
0:744801d5734d
fix;

Who changed what in which revision?

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