Now you can download a copy of these docs so you can use them offline! Download now
ArrayData.cpp
1 /*
2  * ArrayData.cpp
3  *
4  * Created on: Nov 14, 2012
5  * Author: Mitchell Wills
6  */
7 
8 #include "networktables2/type/ArrayData.h"
9 #ifndef _WRS_KERNEL
10 #include <stdint.h>
11 #endif
12 
13 #include <cstring>
14 #include <stdlib.h>
15 #include <memory>
16 
17 ArrayData::ArrayData(ArrayEntryType& type) : ComplexData(type), m_data_type(type){
18  m_size = 0;
19  data = NULL;
20 }
21 ArrayData::~ArrayData(){
22  free(data);
23 }
24 
25 EntryValue ArrayData::_get(unsigned int index){//TODO bounds checking
26  return data[index];
27 }
28 
29 void ArrayData::_set(unsigned int index, EntryValue value){//TODO bounds checking
30  m_data_type.deleteElement(data[index]);
31  data[index] = m_data_type.copyElement(value);
32 }
33 
35  setSize(size()+1);
36  data[size()-1] = m_data_type.copyElement(value);
37 }
38 
39 void ArrayData::remove(unsigned int index){
40  //if(index<0 || index>=size())
41  // throw IndexOutOfBoundsException();//TODO bounds check
42  m_data_type.deleteElement(data[index]);
43  EntryValue nullValue = {0};
44  data[index] = nullValue;
45  if(index < size()-1){
46  memcpy(data+index, data+index+1, (size()-index-1) * sizeof(EntryValue));
47  }
48  setSize(size()-1);
49 }
50 void ArrayData::setSize(unsigned int newSize){
51  if(newSize==m_size)//TODO bound check greater than max size
52  return;
53  EntryValue* newArray = (EntryValue*)malloc(newSize*sizeof(EntryValue));//TODO cache arrays
54  if(newSize<m_size){
55  memcpy(newArray, data, newSize * sizeof(EntryValue));
56  for(unsigned int i = newSize; i<m_size; ++i)
57  m_data_type.deleteElement(data[i]);
58  }
59  else{
60  if(data!=NULL)
61  memcpy(newArray, data, m_size * sizeof(EntryValue));
62  else
63  m_size = 0;//ensure that the current size is actually 0 otherwise will end up with uninitialized values in the array
64  EntryValue nullValue = {0};
65  for(unsigned int i = m_size; i<newSize; ++i)
66  newArray[i] = nullValue;
67  }
68  if(data!=NULL)
69  free(data);
70  data = newArray;
71  m_size = newSize;
72 }
73 unsigned int ArrayData::size(){
74  return m_size;
75 }
EntryValue copyElement(EntryValue value)
EntryValue _get(unsigned int index)
Definition: ArrayData.cpp:25
void setSize(unsigned int size)
Definition: ArrayData.cpp:50
void _set(unsigned int index, EntryValue value)
Definition: ArrayData.cpp:29
void deleteElement(EntryValue value)
void remove(unsigned int index)
Definition: ArrayData.cpp:39
ArrayData(ArrayEntryType &type)
Definition: ArrayData.cpp:17
void _add(EntryValue value)
Definition: ArrayData.cpp:34
unsigned int size()
Definition: ArrayData.cpp:73
Definition: ITable.h:13

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6