mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.

Dependents:   MbedSmartRestMain MbedSmartRestMain

Revision:
0:099f76422485
Child:
5:2b74510900da
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Aggregator.h	Thu Jul 03 20:38:04 2014 +0200
@@ -0,0 +1,155 @@
+/*
+ * Aggregator.h
+ *
+ * Created on: Nov 1, 2013
+ * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
+ *
+ * Copyright (c) 2013 Cumulocity GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef AGGREGATOR_H
+#define AGGREGATOR_H
+
+#include "config.h"
+#include <stddef.h>
+#include "DataGenerator.h"
+
+#ifndef SMARTREST_AGGREGATOR_INITIAL_CAPACITY
+#define SMARTREST_AGGREGATOR_INITIAL_CAPACITY 50
+#endif
+#ifndef SMARTREST_AGGREGATOR_MEMORY_INCREMENT
+#define SMARTREST_AGGREGATOR_MEMORY_INCREMENT 25
+#endif
+//#define SMARTREST_AGGREGATOR_FIXED_SIZE 100
+
+/**
+ * An aggregator of data generators. This class can aggregate instances of
+ * itself.
+ * If the aggregator is set to copying all added data generators using
+ * the copy() method, all objects are being properly deallocated on
+ * destruction.
+ * 
+ * Example:
+ * @code
+ * // Given: A concrete SmartRest implementation.
+ * SmartRest client;
+ * 
+ * // sets up a growing aggregator which copies all added objects
+ * // to the heap
+ * Aggregator buffer(25, true, true);
+ * 
+ * // random data collection
+ * for (uint8_t i = 0; i < 32; ++i) {
+ *     // Note: ComposedRecord is not copying any values into the heap
+ *     // because the availability of its values in guaranteed during the
+ *     // instance's lifetime.
+ *     ComposedRecord record(false);
+ *     
+ *     CharValue msgId(100);
+ *     IntegerValue number(i);
+ *     record.add(msgId).add(number);
+ *     
+ *     // Note: ComposedRecord is now cloned into the heap to extend its
+ *     // lifetime beyond this code block.
+ *     buffer.add(record);
+ * }
+ * 
+ * // send the bulk request
+ * uint8_t ret = client.send(buffer);
+ * if (ret != SMARTREST_SUCCESS) {
+ *     // error handling
+ * }
+ * @encode
+ */
+class Aggregator : public DataGenerator
+{
+public:
+    #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
+    /**
+     * Creates a new Aggregator instance.
+     * @param capacity the initial capacity of the instance
+     * @param growing specifies the capability of this instance to grow
+     * @param copy specifies whether all added data generators shall be
+     *             copied using the copy() method
+     */
+    Aggregator(size_t=SMARTREST_AGGREGATOR_INITIAL_CAPACITY, bool=true, bool=false);
+    #else
+    /**
+     * Creates a new Aggregator instance.
+     * @param copy specifies whether all added data generators shall be
+     *             copied using the copy() method
+     */
+    Aggregator(bool=false);
+    #endif
+    ~Aggregator();
+
+    /**
+     * Adds a data generator to the aggregator.
+     * @param generator the data generator to add
+     * @return true if added, false otherwise.
+     */
+    bool add(const DataGenerator&);
+
+    /**
+     * Clears the aggregator. The capacity will shrink to it's initial
+     * size.
+     */
+    void clear();
+
+    /**
+     * Returns the number of data generators aggregated by this instance.
+     * @return the number of data generators aggregated
+     */
+    size_t length();
+
+    /**
+     * Returns whether the aggregator is full. If growing, this will
+     * always return false.
+     * @return whether the aggregator is full
+     */
+    bool full();
+
+    /**
+     * Returns the capacity of the aggregator. This will always return zero
+     * if the aggregator is growing.
+     */
+    size_t capacity();
+
+    size_t writeTo(AbstractDataSink&) const;
+    size_t writtenLength() const;
+    DataGenerator* copy() const;
+
+private:
+    #ifdef SMARTREST_AGGREGATOR_FIXED_SIZE
+    const DataGenerator *_list[SMARTREST_AGGREGATOR_FIXED_SIZE];
+    #else
+    const DataGenerator **_list;
+    #endif
+    size_t _length;
+    bool _alloc;
+    #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE
+    size_t _capacity, _initial;
+    bool _growing;
+    #endif
+};
+
+#endif