Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Revision:
5:2b74510900da
Parent:
0:099f76422485
Child:
6:cd7ba1ddb664
--- a/Aggregator.cpp	Wed Jul 09 15:37:19 2014 +0200
+++ b/Aggregator.cpp	Thu Jul 10 16:20:31 2014 +0200
@@ -30,26 +30,26 @@
 #include <stdlib.h>
 
 #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
-Aggregator::Aggregator(size_t capacity, bool growing, bool copy)
+Aggregator::Aggregator(size_t capacity, bool growing, bool managed) :
+    _initial(capacity),
+    _growing(growing),
+    _managed(managed)
 {
-    _alloc = copy;
-    _growing = growing;
     _capacity = 0;
     _list = NULL;
-    _initial = capacity;
     _length = 0;
 }
 #else
-Aggregator::Aggregator(bool copy)
+Aggregator::Aggregator(bool managed) :
+    _managed(managed)
 {
-    _alloc = copy;
     _length = 0;
 }
 #endif
 
 Aggregator::~Aggregator()
 {
-    if (_alloc) {
+    if (_managed) {
         for (size_t n = 0; n < _length; n++)
             delete _list[n];
     }
@@ -59,10 +59,8 @@
     #endif
 }
 
-bool Aggregator::add(const DataGenerator& generator)
+bool Aggregator::add(const Record& record)
 {
-    if (this == &generator)
-        return false;
     #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
     if (_length == _capacity) {
         size_t capacity;
@@ -75,8 +73,8 @@
         else
             capacity = _capacity + SMARTREST_AGGREGATOR_MEMORY_INCREMENT;
 
-        const DataGenerator **list = (const DataGenerator**)realloc(_list,
-            capacity*sizeof(DataGenerator*));
+        const Record **list = (const Record**)realloc(_list,
+            capacity*sizeof(Record*));
         if (list == NULL)
             return false;
         _list = list;
@@ -87,20 +85,25 @@
         return false;
     #endif
 
-    if (_alloc) {
-        DataGenerator *copy = generator.copy();
+    if (_managed) {
+        Record *copy = record.copy();
         if (copy == NULL)
             return false;
         _list[_length++] = copy;
     } else {
-        _list[_length++] = &generator;
+        _list[_length++] = &record;
     }
     return true;
 }
 
+const Record& Aggregator::get(size_t index)
+{
+    return *_list[index];
+}
+
 void Aggregator::clear()
 {
-    if (_alloc) {
+    if (_managed) {
         for (size_t n = 0; n < _length; n++)
             delete _list[n];
     }
@@ -137,6 +140,11 @@
     #endif
 }
 
+bool Aggregator::isManaged()
+{
+    return _managed;
+}
+
 size_t Aggregator::writeTo(AbstractDataSink& sink) const
 {
     size_t len = 0;
@@ -153,7 +161,7 @@
     return len;
 }
 
-DataGenerator* Aggregator::copy() const
+Aggregator* Aggregator::copy() const
 {
     #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
     Aggregator *copy = new Aggregator(_length, _growing, true);