Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

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 <stdlib.h>
00030 #include <string.h>
00031 #include "ComposedRecord.h"
00032 
00033 /*-------------------------------------------------------------------------*/
00034 ComposedRecord::ComposedRecord(bool copy)
00035 {
00036     _alloc = copy;
00037 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00038     _capacity = 0;
00039     _values = NULL;
00040 #endif
00041     _count = 0;
00042 }
00043 /*-------------------------------------------------------------------------*/
00044 ComposedRecord::~ComposedRecord()
00045 {
00046     if (_alloc)
00047     {
00048         for (size_t n = 0; n < _count; n++)
00049             delete _values[n];
00050     }
00051 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00052     if (_values != NULL)
00053         free(_values);
00054 #endif
00055 }
00056 /*-------------------------------------------------------------------------*/
00057 bool ComposedRecord::add(const Value& value)
00058 {
00059 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00060     if (_capacity == _count)
00061     {
00062         size_t capacity;
00063         if (_capacity == 0)
00064             capacity = SMARTREST_COMPOSED_INITIAL_CAPACITY;
00065         else
00066             capacity = _capacity + SMARTREST_COMPOSED_MEMORY_INCREMENT;
00067 
00068         const Value **values = (const Value**)realloc(_values,
00069                 capacity*sizeof(Value*));
00070         if (values == NULL)
00071             return false;
00072         _values = values;
00073         _capacity = capacity;
00074     }
00075 #else
00076     if (_count == SMARTREST_COMPOSED_FIXED_SIZE)
00077         return false;
00078 #endif
00079 
00080     if (_alloc)
00081     {
00082         Value *copy = value.copy();
00083         if (copy == NULL)
00084             return false;
00085         _values[_count++] = copy;
00086     }
00087     else
00088     {
00089         _values[_count++] = &value;
00090     }
00091     return true;
00092 }
00093 /*-------------------------------------------------------------------------*/
00094 void ComposedRecord::clear()
00095 {
00096     if (_alloc)
00097     {
00098         for (size_t n = 0; n < _count; n++)
00099             delete _values[n];
00100     }
00101     _count = 0;
00102 
00103 #ifndef SMARTREST_COMPOSED_FIXED_SIZE
00104     if (_values != NULL)
00105         free(_values);
00106     _values = NULL;
00107     _capacity = 0;
00108 #endif
00109 }
00110 /*-------------------------------------------------------------------------*/
00111 size_t ComposedRecord::values() const
00112 {
00113     return _count;
00114 }
00115 /*-------------------------------------------------------------------------*/
00116 const Value& ComposedRecord::value(size_t index) const
00117 {
00118     return *_values[index];
00119 }
00120 /*-------------------------------------------------------------------------*/
00121 ComposedRecord* ComposedRecord::copy() const
00122 {
00123     ComposedRecord *copy = new ComposedRecord(true);
00124     for (size_t n = 0; n < _count; n++)
00125         copy->add(*_values[n]);
00126     return copy;
00127 }
00128 /*-------------------------------------------------------------------------*/