4 #include "JSONMemory.h"
7 #ifdef JSON_LESS_MEMORY
11 #pragma pack(push, jsonChildren, 1)
15 #define json_foreach(chldrn, itrtr)\
16 JSONNode ** itrtr = chldrn -> begin();\
17 for(JSONNode ** itrtr##_end = chldrn -> end(); itrtr != itrtr##_end; ++itrtr)
31 #ifdef JSON_LESS_MEMORY
32 #define childrenVirtual virtual
34 #define childrenVirtual
39 LIBJSON_OBJECT(jsonChildren);
41 jsonChildren(
void) json_nothrow : array(0), mysize(0), mycapacity(0) {
45 #ifdef JSON_LESS_MEMORY
46 jsonChildren(JSONNode** ar, json_index_t si, json_index_t ca) json_nothrow : array(ar), mysize(si), mycapacity(ca) {
52 childrenVirtual ~jsonChildren(
void) json_nothrow {
53 if (json_unlikely(array != 0)){
55 libjson_free<JSONNode*>(array);
61 void inc(json_index_t amount) json_nothrow;
62 void inc(
void) json_nothrow;
65 void push_back(JSONNode * item) json_nothrow {
66 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null push_back"));
68 array[mysize++] = item;
72 void push_front(JSONNode * item) json_nothrow {
73 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null push_front"));
75 std::memmove(array + 1, array, mysize++ *
sizeof(JSONNode *));
80 inline JSONNode * operator[] (json_index_t position)
const json_nothrow {
81 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null []"));
82 JSON_ASSERT(position < mysize, JSON_TEXT(
"Using [] out of bounds"));
83 JSON_ASSERT(position < mycapacity, JSON_TEXT(
"Using [] out of bounds"));
84 JSON_ASSERT(array != 0, JSON_TEXT(
"Array is null"));
85 return array[position];
89 inline json_index_t capacity() const json_nothrow {
90 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null capacity"));
95 inline json_index_t size() const json_nothrow {
96 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null size"));
101 inline bool empty() const json_nothrow {
102 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null empty"));
107 inline void clear() json_nothrow {
108 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null clear"));
109 if (json_likely(array != 0)){
110 JSON_ASSERT(mycapacity != 0, JSON_TEXT(
"mycapacity is not zero, but array is null"));
114 JSON_ASSERT(mysize == 0, JSON_TEXT(
"mysize is not zero after clear"));
118 inline JSONNode ** begin(
void) const json_nothrow {
119 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null begin"));
124 inline JSONNode ** end(
void) const json_nothrow {
125 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null end"));
126 return array + mysize;
130 template <
bool reverse>
131 struct iteratorKeeper {
133 LIBJSON_OBJECT(jsonChildren::iteratorKeeper);
134 iteratorKeeper(jsonChildren * pthis, JSONNode ** & position) json_nothrow :
135 myRelativeOffset(reverse ? (json_index_t)(pthis -> array + (
size_t)pthis -> mysize - position) : (json_index_t)(position - pthis -> array)),
141 ~iteratorKeeper(
void) json_nothrow {
144 myPos = myChildren -> array + myChildren -> mysize - myRelativeOffset;
146 myPos = myChildren -> array + myRelativeOffset;
150 iteratorKeeper(
const iteratorKeeper &);
151 iteratorKeeper & operator = (
const iteratorKeeper &);
153 json_index_t myRelativeOffset;
154 jsonChildren * myChildren;
159 inline void erase(JSONNode ** & position) json_nothrow {
160 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null erase"));
161 JSON_ASSERT(array != 0, JSON_TEXT(
"erasing something from a null array 1"));
162 JSON_ASSERT(position >= array, JSON_TEXT(
"position is beneath the start of the array 1"));
163 JSON_ASSERT(position <= array + mysize, JSON_TEXT(
"erasing out of bounds 1"));
164 std::memmove(position, position + 1, (mysize-- - (position - array) - 1) *
sizeof(JSONNode *));
165 iteratorKeeper<false> ik(
this, position);
170 inline void erase(JSONNode ** & position, json_index_t number) json_nothrow {
171 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null erase 2"));
172 doerase(position, number);
173 iteratorKeeper<false> ik(
this, position);
179 inline void erase(JSONNode ** position, json_index_t number, JSONNode ** & starter) json_nothrow {
180 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null erase 3"));
181 doerase(position, number);
182 iteratorKeeper<false> ik(
this, starter);
187 void insert(JSONNode ** & position, JSONNode * item) json_nothrow{
189 void insert(JSONNode ** & position, JSONNode * item,
bool reverse =
false) json_nothrow {
191 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null insert"));
193 JSON_ASSERT(position >= array, JSON_TEXT(
"position is beneath the start of the array insert 1"));
194 JSON_ASSERT(position <= array + mysize, JSON_TEXT(
"position is above the end of the array insert 1"));
197 iteratorKeeper<true> ik(
this, position);
202 iteratorKeeper<false> ik(
this, position);
206 std::memmove(position + 1, position, (mysize++ - (position - array)) *
sizeof(JSONNode *));
210 void insert(JSONNode ** & position, JSONNode ** items, json_index_t num) json_nothrow {
211 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null insert 2"));
212 JSON_ASSERT(position >= array, JSON_TEXT(
"position is beneath the start of the array insert 2"));
213 JSON_ASSERT(position <= array + mysize, JSON_TEXT(
"position is above the end of the array insert 2"));
215 iteratorKeeper<false> ik(
this, position);
218 const size_t ptrs = ((JSONNode **)(array + mysize)) - position;
219 std::memmove(position + num, position, ptrs *
sizeof(JSONNode *));
220 std::memcpy(position, items, num *
sizeof(JSONNode *));
224 inline void reserve(json_index_t amount) json_nothrow {
225 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null reserve"));
226 JSON_ASSERT(array == 0, JSON_TEXT(
"reserve is not meant to expand a preexisting array"));
227 JSON_ASSERT(mycapacity == 0, JSON_TEXT(
"reservec is not meant to expand a preexisting array"));
228 JSON_ASSERT(mysize == 0, JSON_TEXT(
"reserves is not meant to expand a preexisting array"));
229 array = json_malloc<JSONNode*>(mycapacity = amount);
233 static void reserve2(jsonChildren *& mine, json_index_t amount) json_nothrow;
236 inline childrenVirtual
void shrink() json_nothrow {
237 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null shrink"));
238 if (json_unlikely(mysize == 0)){
239 libjson_free<JSONNode*>(array);
241 #ifdef JSON_LESS_MEMORY
243 JSON_ASSERT(array != 0, JSON_TEXT(
"shrinking a null array that is not size 0"));
244 array = json_realloc<JSONNode*>(array, mysize);
251 inline static void deleteChildren(jsonChildren * ptr) json_nothrow {
252 #ifdef JSON_MEMORY_CALLBACKS
253 ptr -> ~jsonChildren();
254 libjson_free<jsonChildren>(ptr);
260 inline static jsonChildren * newChildren(
void) {
261 #ifdef JSON_MEMORY_CALLBACKS
262 return new(json_malloc<jsonChildren>(1)) jsonChildren();
264 return new jsonChildren();
271 json_index_t mycapacity;
274 jsonChildren(
const jsonChildren &);
275 jsonChildren & operator = (
const jsonChildren &);
277 void deleteAll(
void) json_nothrow json_hot;
278 void doerase(JSONNode ** position, json_index_t number) json_nothrow;
281 #ifdef JSON_LESS_MEMORY
282 class jsonChildren_Reserved :
public jsonChildren {
284 LIBJSON_OBJECT(jsonChildren_Reserved);
285 jsonChildren_Reserved(jsonChildren * orig, json_index_t siz) json_nothrow : jsonChildren(orig -> array, orig -> mysize, orig -> mycapacity), myreserved(siz) {
287 deleteChildren(orig);
290 jsonChildren_Reserved(
const jsonChildren_Reserved & orig) json_nothrow : jsonChildren(orig.array, orig.mysize, orig.mycapacity), myreserved(orig.myreserved){
293 inline virtual ~jsonChildren_Reserved() json_nothrow {
296 inline virtual void shrink() json_nothrow {
297 JSON_ASSERT(
this != 0, JSON_TEXT(
"Children is null shrink reserved"));
298 if (json_unlikely(mysize == 0)){
299 libjson_free<JSONNode*>(array);
301 }
else if (mysize > myreserved){
302 JSON_ASSERT(array != 0, JSON_TEXT(
"shrinking a null array that is not size 0"));
303 array = json_realloc<JSONNode*>(array, mysize);
307 #ifdef JSON_LESS_MEMORY
308 inline static jsonChildren * newChildren_Reserved(jsonChildren * orig, json_index_t siz) json_nothrow {
309 #ifdef JSON_MEMORY_CALLBACKS
310 return new(json_malloc<jsonChildren_Reserved>(1)) jsonChildren_Reserved(orig, siz);
312 return new jsonChildren_Reserved(orig, siz);
317 jsonChildren_Reserved & operator = (
const jsonChildren_Reserved &);
318 json_index_t myreserved;
322 #ifdef JSON_LESS_MEMORY
326 #pragma pack(pop, jsonChildren)