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

Dependents:   MbedSmartRestMain MbedSmartRestMain

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ComposedRecord.cpp Source File

ComposedRecord.cpp

00001 /*
00002  * ComposedRecord.cpp
00003  *
00004  * Created on: Nov 1, 2013
00005  * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
00006  *
00007  * Copyright (c) 2013 Cumulocity GmbH
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining
00010  * a copy of this software and associated documentation files (the
00011  * "Software"), to deal in the Software without restriction, including
00012  * without limitation the rights to use, copy, modify, merge, publish,
00013  * distribute, sublicense, and/or sell copies of the Software, and to
00014  * permit persons to whom the Software is furnished to do so, subject to
00015  * the following conditions:
00016  *
00017  * The above copyright notice and this permission notice shall be
00018  * included in all copies or substantial portions of the Software.
00019  *
00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00023  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00024  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00025  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00026  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 #include "ComposedRecord.h"
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 
00034 /*-------------------------------------------------------------------------*/
00035 ComposedRecord::ComposedRecord(bool copy)
00036 {
00037     _alloc = copy;
00038 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00039     _capacity = 0;
00040     _values = NULL;
00041 #endif
00042     _count = 0;
00043 }
00044 /*-------------------------------------------------------------------------*/
00045 ComposedRecord::~ComposedRecord()
00046 {
00047     if (_alloc)
00048     {
00049         for (size_t n = 0; n < _count; n++)
00050             delete _values[n];
00051     }
00052 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00053     if (_values != NULL)
00054         free(_values);
00055 #endif
00056 }
00057 /*-------------------------------------------------------------------------*/
00058 bool ComposedRecord::add(const Value& value)
00059 {
00060 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00061     if (_capacity == _count)
00062     {
00063         size_t capacity;
00064         if (_capacity == 0)
00065             capacity = SMARTREST_COMPOSED_INITIAL_CAPACITY;
00066         else
00067             capacity = _capacity + SMARTREST_COMPOSED_MEMORY_INCREMENT;
00068 
00069         const Value **values = (const Value**)realloc(_values,
00070                 capacity*sizeof(Value*));
00071         if (values == NULL)
00072             return false;
00073         _values = values;
00074         _capacity = capacity;
00075     }
00076 #else
00077     if (_count == SMARTREST_COMPOSED_FIXED_SIZE)
00078         return false;
00079 #endif
00080 
00081     if (_alloc)
00082     {
00083         Value *copy = value.copy();
00084         if (copy == NULL)
00085             return false;
00086         _values[_count++] = copy;
00087     }
00088     else
00089     {
00090         _values[_count++] = &value;
00091     }
00092     return true;
00093 }
00094 /*-------------------------------------------------------------------------*/
00095 void ComposedRecord::clear()
00096 {
00097     if (_alloc)
00098     {
00099         for (size_t n = 0; n < _count; n++)
00100             delete _values[n];
00101     }
00102     _count = 0;
00103 
00104 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00105     if (_values != NULL)
00106         free(_values);
00107     _values = NULL;
00108     _capacity = 0;
00109 #endif
00110 }
00111 /*-------------------------------------------------------------------------*/
00112 size_t ComposedRecord::values() const
00113 {
00114     return _count;
00115 }
00116 /*-------------------------------------------------------------------------*/
00117 const Value& ComposedRecord::value(size_t index) const
00118 {
00119     return *_values[index];
00120 }
00121 /*-------------------------------------------------------------------------*/
00122 ComposedRecord* ComposedRecord::copy() const
00123 {
00124     ComposedRecord *copy = new ComposedRecord(true);
00125     for (size_t n = 0; n < _count; n++)
00126         copy->add(*_values[n]);
00127     return copy;
00128 }
00129 /*-------------------------------------------------------------------------*/