Generic SmartRest library

Dependents:   SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming

Committer:
vwochnik
Date:
Mon Mar 24 09:57:40 2014 +0000
Revision:
0:744801d5734d
fix;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vwochnik 0:744801d5734d 1 /*
vwochnik 0:744801d5734d 2 * ComposedRecord.cpp
vwochnik 0:744801d5734d 3 *
vwochnik 0:744801d5734d 4 * Created on: Nov 1, 2013
vwochnik 0:744801d5734d 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
vwochnik 0:744801d5734d 6 *
vwochnik 0:744801d5734d 7 * Copyright (c) 2013 Cumulocity GmbH
vwochnik 0:744801d5734d 8 *
vwochnik 0:744801d5734d 9 * Permission is hereby granted, free of charge, to any person obtaining
vwochnik 0:744801d5734d 10 * a copy of this software and associated documentation files (the
vwochnik 0:744801d5734d 11 * "Software"), to deal in the Software without restriction, including
vwochnik 0:744801d5734d 12 * without limitation the rights to use, copy, modify, merge, publish,
vwochnik 0:744801d5734d 13 * distribute, sublicense, and/or sell copies of the Software, and to
vwochnik 0:744801d5734d 14 * permit persons to whom the Software is furnished to do so, subject to
vwochnik 0:744801d5734d 15 * the following conditions:
vwochnik 0:744801d5734d 16 *
vwochnik 0:744801d5734d 17 * The above copyright notice and this permission notice shall be
vwochnik 0:744801d5734d 18 * included in all copies or substantial portions of the Software.
vwochnik 0:744801d5734d 19 *
vwochnik 0:744801d5734d 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
vwochnik 0:744801d5734d 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
vwochnik 0:744801d5734d 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
vwochnik 0:744801d5734d 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
vwochnik 0:744801d5734d 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
vwochnik 0:744801d5734d 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
vwochnik 0:744801d5734d 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
vwochnik 0:744801d5734d 27 */
vwochnik 0:744801d5734d 28
vwochnik 0:744801d5734d 29 #include "ComposedRecord.h"
vwochnik 0:744801d5734d 30 #include "NullValue.h"
vwochnik 0:744801d5734d 31 #include <stdlib.h>
vwochnik 0:744801d5734d 32 #include <string.h>
vwochnik 0:744801d5734d 33
vwochnik 0:744801d5734d 34 ComposedRecord::ComposedRecord(bool copy)
vwochnik 0:744801d5734d 35 {
vwochnik 0:744801d5734d 36 _alloc = copy;
vwochnik 0:744801d5734d 37 #ifndef COMPOSED_FIXED_SIZE
vwochnik 0:744801d5734d 38 _capacity = COMPOSED_INITIAL_CAPACITY;
vwochnik 0:744801d5734d 39 _values = (const Value**)malloc(_capacity*sizeof(Value*));
vwochnik 0:744801d5734d 40 #endif
vwochnik 0:744801d5734d 41 _count = 0;
vwochnik 0:744801d5734d 42 }
vwochnik 0:744801d5734d 43
vwochnik 0:744801d5734d 44 ComposedRecord::~ComposedRecord()
vwochnik 0:744801d5734d 45 {
vwochnik 0:744801d5734d 46 if (_alloc) {
vwochnik 0:744801d5734d 47 for (size_t n = 0; n < _count; n++)
vwochnik 0:744801d5734d 48 delete _values[n];
vwochnik 0:744801d5734d 49 }
vwochnik 0:744801d5734d 50 #ifndef COMPOSED_FIXED_SIZE
vwochnik 0:744801d5734d 51 free(_values);
vwochnik 0:744801d5734d 52 #endif
vwochnik 0:744801d5734d 53 }
vwochnik 0:744801d5734d 54
vwochnik 0:744801d5734d 55 ComposedRecord& ComposedRecord::add(const Value& value)
vwochnik 0:744801d5734d 56 {
vwochnik 0:744801d5734d 57 #ifndef COMPOSED_FIXED_SIZE
vwochnik 0:744801d5734d 58 if (_capacity == _count) {
vwochnik 0:744801d5734d 59 size_t capacity = _capacity + COMPOSED_MEMORY_INCREMENT;
vwochnik 0:744801d5734d 60 const Value **values = (const Value**)realloc(_values,
vwochnik 0:744801d5734d 61 capacity*sizeof(Value*));
vwochnik 0:744801d5734d 62 if (values == NULL)
vwochnik 0:744801d5734d 63 return *this;
vwochnik 0:744801d5734d 64 _values = values;
vwochnik 0:744801d5734d 65 _capacity = capacity;
vwochnik 0:744801d5734d 66 }
vwochnik 0:744801d5734d 67 #else
vwochnik 0:744801d5734d 68 if (_count == COMPOSED_FIXED_SIZE)
vwochnik 0:744801d5734d 69 return *this;
vwochnik 0:744801d5734d 70 #endif
vwochnik 0:744801d5734d 71
vwochnik 0:744801d5734d 72 if (_alloc) {
vwochnik 0:744801d5734d 73 Value *copy = value.copy();
vwochnik 0:744801d5734d 74 if (copy == NULL)
vwochnik 0:744801d5734d 75 return *this;
vwochnik 0:744801d5734d 76 _values[_count++] = copy;
vwochnik 0:744801d5734d 77 } else {
vwochnik 0:744801d5734d 78 _values[_count++] = &value;
vwochnik 0:744801d5734d 79 }
vwochnik 0:744801d5734d 80 return *this;
vwochnik 0:744801d5734d 81 }
vwochnik 0:744801d5734d 82
vwochnik 0:744801d5734d 83 void ComposedRecord::clear()
vwochnik 0:744801d5734d 84 {
vwochnik 0:744801d5734d 85 _count = 0;
vwochnik 0:744801d5734d 86 #ifndef COMPOSED_FIXED_SIZE
vwochnik 0:744801d5734d 87 if (_capacity > COMPOSED_INITIAL_CAPACITY) {
vwochnik 0:744801d5734d 88 Value** values = (Value**)realloc(_values,
vwochnik 0:744801d5734d 89 COMPOSED_INITIAL_CAPACITY*sizeof(Value*));
vwochnik 0:744801d5734d 90 if (values == NULL)
vwochnik 0:744801d5734d 91 return;
vwochnik 0:744801d5734d 92 _capacity = COMPOSED_INITIAL_CAPACITY;
vwochnik 0:744801d5734d 93 }
vwochnik 0:744801d5734d 94 #endif
vwochnik 0:744801d5734d 95 }
vwochnik 0:744801d5734d 96
vwochnik 0:744801d5734d 97 size_t ComposedRecord::values() const
vwochnik 0:744801d5734d 98 {
vwochnik 0:744801d5734d 99 return _count;
vwochnik 0:744801d5734d 100 }
vwochnik 0:744801d5734d 101
vwochnik 0:744801d5734d 102 const Value& ComposedRecord::value(size_t index) const
vwochnik 0:744801d5734d 103 {
vwochnik 0:744801d5734d 104 if (index >= _count)
vwochnik 0:744801d5734d 105 return aNullValue;
vwochnik 0:744801d5734d 106 return *_values[index];
vwochnik 0:744801d5734d 107 }
vwochnik 0:744801d5734d 108
vwochnik 0:744801d5734d 109 DataGenerator* ComposedRecord::copy() const
vwochnik 0:744801d5734d 110 {
vwochnik 0:744801d5734d 111 ComposedRecord *copy = new ComposedRecord(true);
vwochnik 0:744801d5734d 112 for (size_t n = 0; n < _count; n++)
vwochnik 0:744801d5734d 113 copy->add(*_values[n]);
vwochnik 0:744801d5734d 114 return copy;
vwochnik 0:744801d5734d 115 }
vwochnik 0:744801d5734d 116