| JSONChildren.h | | JSONChildren.h | |
| #ifndef JSONCHILDREN_H | | #ifndef JSONCHILDREN_H | |
| #define JSONCHILDREN_H | | #define JSONCHILDREN_H | |
| | | | |
|
| | | #ifdef _LINUX_ | |
| | | #include <cstdlib> //for malloc, realloc, and free | |
| | | #include <cstring> //for memmove | |
| | | #endif | |
| | | | |
| #include "Debug.h" //for libJSON_ASSERT macro | | #include "Debug.h" //for libJSON_ASSERT macro | |
| | | | |
| #ifdef DEBUG | | #ifdef DEBUG | |
| extern ErrorCallback Error; //tell the user program that something
went wrong | | extern ErrorCallback Error; //tell the user program that something
went wrong | |
| #endif | | #endif | |
| | | | |
| /* | | /* | |
| This class is essentially a vector that has been heavily optimized for
the specific purpose | | This class is essentially a vector that has been heavily optimized for
the specific purpose | |
| of holding JSONNode children. It acts the same way as a vector, it has
a automatically | | of holding JSONNode children. It acts the same way as a vector, it has
a automatically | |
| expanding array. On destruction, this container automatically destroys
everything contained | | expanding array. On destruction, this container automatically destroys
everything contained | |
| | | | |
| skipping to change at line 56 | | skipping to change at line 61 | |
| } else { | | } else { | |
| mycapacity <<= 1; //double the size of the array | | mycapacity <<= 1; //double the size of the array | |
| array = (JSONNode **)realloc(array, mycapacity * sizeof
(JSONNode *)); | | array = (JSONNode **)realloc(array, mycapacity * sizeof
(JSONNode *)); | |
| libJSON_ASSERT(array, TEXT("reallocing array failed")); | | libJSON_ASSERT(array, TEXT("reallocing array failed")); | |
| } | | } | |
| } | | } | |
| array[mysize++] = item; | | array[mysize++] = item; | |
| } | | } | |
| | | | |
| //gets an item out of the vector by it's position | | //gets an item out of the vector by it's position | |
|
| inline JSONNode * operator[] (size_t position) const { | | inline JSONNode * operator[] (unsigned int position) const { | |
| libJSON_ASSERT(position < mysize, TEXT("Using [] out of bounds"
)); | | libJSON_ASSERT(position < mysize, TEXT("Using [] out of bounds"
)); | |
| libJSON_ASSERT(position < mycapacity, TEXT("Using [] out of bou
nds")); | | libJSON_ASSERT(position < mycapacity, TEXT("Using [] out of bou
nds")); | |
| libJSON_ASSERT(array, TEXT("Array is null")); | | libJSON_ASSERT(array, TEXT("Array is null")); | |
| return array[position]; | | return array[position]; | |
| } | | } | |
| | | | |
| //returns the allocated capacity, but keep in mind that some might
not be valid | | //returns the allocated capacity, but keep in mind that some might
not be valid | |
|
| inline size_t capacity() const { | | inline unsigned int capacity() const { | |
| return mycapacity; | | return mycapacity; | |
| } | | } | |
| | | | |
| //returns the number of valid objects within the vector | | //returns the number of valid objects within the vector | |
|
| inline size_t size() const { | | inline unsigned int size() const { | |
| return mysize; | | return mysize; | |
| } | | } | |
| | | | |
| //tests whether or not the vector is empty | | //tests whether or not the vector is empty | |
| inline bool empty() const { | | inline bool empty() const { | |
| return mysize == 0; | | return mysize == 0; | |
| } | | } | |
| | | | |
|
| | | //tests whether or not the vector is empty | |
| | | inline bool not_empty() const { | |
| | | return mysize != 0; | |
| | | } | |
| | | | |
| //clears (and deletes) everything from the vector and sets it's siz
e to 0 | | //clears (and deletes) everything from the vector and sets it's siz
e to 0 | |
| inline void clear(){ | | inline void clear(){ | |
| if (array){ //don't bother clearing anything if there is nothi
ng in it | | if (array){ //don't bother clearing anything if there is nothi
ng in it | |
| libJSON_ASSERT(mycapacity == 0, TEXT("mycapacity is not zer
o, but array is null")); | | libJSON_ASSERT(mycapacity == 0, TEXT("mycapacity is not zer
o, but array is null")); | |
| deleteAll(); | | deleteAll(); | |
| mysize = 0; | | mysize = 0; | |
| } | | } | |
| libJSON_ASSERT(mysize == 0, TEXT("mysize is not zero after clea
r")); | | libJSON_ASSERT(mysize == 0, TEXT("mysize is not zero after clea
r")); | |
| } | | } | |
| | | | |
| | | | |
| skipping to change at line 106 | | skipping to change at line 116 | |
| } | | } | |
| | | | |
| //This function DOES NOT delete the item it points to | | //This function DOES NOT delete the item it points to | |
| inline void erase(JSONNode * * position){ | | inline void erase(JSONNode * * position){ | |
| libJSON_ASSERT(array, TEXT("erasing something from a null array
")); | | libJSON_ASSERT(array, TEXT("erasing something from a null array
")); | |
| libJSON_ASSERT(position >= array, TEXT("position is beneat the
start of the array")); | | libJSON_ASSERT(position >= array, TEXT("position is beneat the
start of the array")); | |
| libJSON_ASSERT(position < array + mysize, TEXT("erasing out of
bounds")); | | libJSON_ASSERT(position < array + mysize, TEXT("erasing out of
bounds")); | |
| memmove(position, position + 1, (mysize-- - (position - array)
- 1) * sizeof(JSONNode *)); | | memmove(position, position + 1, (mysize-- - (position - array)
- 1) * sizeof(JSONNode *)); | |
| } | | } | |
| | | | |
|
| | | inline void reserve(unsigned int amount){ | |
| | | libJSON_ASSERT(!array, TEXT("reserve is not meant to expand a p | |
| | | reexisting array")); | |
| | | libJSON_ASSERT(!mycapacity, TEXT("reservec is not meant to expa | |
| | | nd a preexisting array")); | |
| | | libJSON_ASSERT(!mysize, TEXT("reserves is not meant to expand a | |
| | | preexisting array")); | |
| | | array = (JSONNode **)malloc(amount * sizeof(JSONNode *)); //8 | |
| | | seems average for JSON, and it's only 64 bytes | |
| | | libJSON_ASSERT(array, TEXT("mallocing array failed")); | |
| | | mycapacity = amount; | |
| | | } | |
| | | | |
| //shrinks the array to only as large as it needs to be to hold ever
ything within it | | //shrinks the array to only as large as it needs to be to hold ever
ything within it | |
| inline void shrink(){ | | inline void shrink(){ | |
| #ifdef JSON_LESS_MEMORY //if memory doesn't matter to the user
, go for speed, leave it the way it is | | #ifdef JSON_LESS_MEMORY //if memory doesn't matter to the user
, go for speed, leave it the way it is | |
| if (mysize == 0){ //size is zero, we should completely fre
e the array | | if (mysize == 0){ //size is zero, we should completely fre
e the array | |
| free(array); //free does checks for a null pointer, so
don't bother checking | | free(array); //free does checks for a null pointer, so
don't bother checking | |
| array = 0; | | array = 0; | |
| } else { //need to shrink it, using realloc | | } else { //need to shrink it, using realloc | |
| libJSON_ASSERT(array, TEXT("shrinking a null array that
is not size 0")); | | libJSON_ASSERT(array, TEXT("shrinking a null array that
is not size 0")); | |
| array = (JSONNode **)realloc(array, mysize); | | array = (JSONNode **)realloc(array, mysize); | |
| } | | } | |
| | | | |
| skipping to change at line 132 | | skipping to change at line 151 | |
| unsigned int GetMemoryUsage(void) const { | | unsigned int GetMemoryUsage(void) const { | |
| return mycapacity * sizeof(JSONNode *); | | return mycapacity * sizeof(JSONNode *); | |
| } | | } | |
| #endif | | #endif | |
| private: | | private: | |
| void deleteAll(void); //implemented in JSONNode.cpp | | void deleteAll(void); //implemented in JSONNode.cpp | |
| | | | |
| JSONNode * * array; //the expandable array | | JSONNode * * array; //the expandable array | |
| #ifdef JSON_LESS_MEMORY | | #ifdef JSON_LESS_MEMORY | |
| //maximum of 1 million children | | //maximum of 1 million children | |
|
| size_t mysize : 20; //the number of valid items | | unsigned int mysize : 20; //the number of valid items | |
| size_t mycapacity : 20; //the number of possible items | | unsigned int mycapacity : 20; //the number of possible items | |
| #else | | #else | |
|
| size_t mysize; //the number of valid items | | unsigned int mysize; //the number of valid items | |
| size_t mycapacity; //the number of possible items | | unsigned int mycapacity; //the number of possible items | |
| #endif | | #endif | |
| }; | | }; | |
| | | | |
| #endif | | #endif | |
| | | | |
End of changes. 8 change blocks. |
| 7 lines changed or deleted | | 30 lines changed or added | |
|