Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
Diff: Aggregator.cpp
- Revision:
- 11:e1bee9a77652
- Parent:
- 6:cd7ba1ddb664
--- a/Aggregator.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/Aggregator.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -29,147 +29,154 @@ #include "Aggregator.h" #include <stdlib.h> +/*-------------------------------------------------------------------------*/ #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE Aggregator::Aggregator(size_t capacity, bool growing, bool managed) : - _initial(capacity), - _growing(growing), - _managed(managed) + _initial(capacity), + _growing(growing), + _managed(managed) { - _capacity = 0; - _list = NULL; - _length = 0; + _capacity = 0; + _list = NULL; + _length = 0; } #else Aggregator::Aggregator(bool managed) : - _managed(managed) + _managed(managed) { - _length = 0; + _length = 0; } #endif - +/*-------------------------------------------------------------------------*/ Aggregator::~Aggregator() { - if (_managed) { - for (size_t n = 0; n < _length; n++) - delete _list[n]; - } - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - if (_list != NULL) - free(_list); - #endif + if (_managed) + { + for (size_t n = 0; n < _length; n++) + delete _list[n]; + } +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + if (_list != NULL) + free(_list); +#endif } - +/*-------------------------------------------------------------------------*/ bool Aggregator::add(const Record& record) { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - if (_length == _capacity) { - size_t capacity; +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + if (_length == _capacity) + { + size_t capacity; - if ((!_growing) && (_capacity != 0)) - return false; + if ((!_growing) && (_capacity != 0)) + return false; - if (_capacity == 0) - capacity = _initial; - else - capacity = _capacity + SMARTREST_AGGREGATOR_MEMORY_INCREMENT; + if (_capacity == 0) + capacity = _initial; + else + capacity = _capacity + SMARTREST_AGGREGATOR_MEMORY_INCREMENT; - const Record **list = (const Record**)realloc(_list, - capacity*sizeof(Record*)); - if (list == NULL) - return false; - _list = list; - _capacity = capacity; - } - #else - if (_length == SMARTREST_AGGREGATOR_FIXED_SIZE) - return false; - #endif + const Record **list = (const Record**)realloc(_list, + capacity*sizeof(Record*)); + if (list == NULL) + return false; + _list = list; + _capacity = capacity; + } +#else + if (_length == SMARTREST_AGGREGATOR_FIXED_SIZE) + return false; +#endif - if (_managed) { - Record *copy = record.copy(); - if (copy == NULL) - return false; - _list[_length++] = copy; - } else { - _list[_length++] = &record; - } - return true; + if (_managed) + { + Record *copy = record.copy(); + if (copy == NULL) + return false; + _list[_length++] = copy; + } + else + { + _list[_length++] = &record; + } + return true; } - +/*-------------------------------------------------------------------------*/ const Record& Aggregator::get(size_t index) const { - return *_list[index]; + return *_list[index]; } - +/*-------------------------------------------------------------------------*/ void Aggregator::clear() { - if (_managed) { - for (size_t n = 0; n < _length; n++) - delete _list[n]; - } - _length = 0; + if (_managed) + { + for (size_t n = 0; n < _length; n++) + delete _list[n]; + } + _length = 0; - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - if (_list != NULL) - free(_list); - _list = NULL; - _capacity = 0; - #endif +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + if (_list != NULL) + free(_list); + _list = NULL; + _capacity = 0; +#endif } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::length() const { - return _length; + return _length; } - +/*-------------------------------------------------------------------------*/ bool Aggregator::full() const { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - return (_growing) ? false : (_length == _capacity); - #else - return (_length == SMARTREST_AGGREGATOR_FIXED_SIZE); - #endif +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + return (_growing) ? false : (_length == _capacity); +#else + return (_length == SMARTREST_AGGREGATOR_FIXED_SIZE); +#endif } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::capacity() const { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - return (_growing) ? 0 : _capacity; - #else - return SMARTREST_AGGREGATOR_FIXED_SIZE; - #endif +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + return (_growing) ? 0 : _capacity; +#else + return SMARTREST_AGGREGATOR_FIXED_SIZE; +#endif } - +/*-------------------------------------------------------------------------*/ bool Aggregator::managed() const { - return _managed; + return _managed; } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::writeTo(AbstractDataSink& sink) const { - size_t len = 0; - for (size_t n = 0; n < _length; n++) - len += _list[n]->writeTo(sink); - return len; + size_t len = 0; + for (size_t n = 0; n < _length; n++) + len += _list[n]->writeTo(sink); + return len; } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::writtenLength() const { - size_t len = 0; - for (size_t n = 0; n < _length; n++) - len += _list[n]->writtenLength(); - return len; + size_t len = 0; + for (size_t n = 0; n < _length; n++) + len += _list[n]->writtenLength(); + return len; } - +/*-------------------------------------------------------------------------*/ Aggregator* Aggregator::copy() const { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - Aggregator *copy = new Aggregator(_length, _growing, true); - #else - Aggregator *copy = new Aggregator(true); - #endif - for (size_t n = 0; n < _length; n++) - copy->add(*_list[n]); - return copy; +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + Aggregator *copy = new Aggregator(_length, _growing, true); +#else + Aggregator *copy = new Aggregator(true); +#endif + for (size_t n = 0; n < _length; n++) + copy->add(*_list[n]); + return copy; } - +/*-------------------------------------------------------------------------*/