Procházet zdrojové kódy

cmData.h/c: cmDataPrepend/Insert/AppendChild() now automatically unlinks the

new child prior to relinking it to it's new parent.

Fixed bug in cmDataMakePair() where the key and value were never actually
linked in.
master
kevin před 10 roky
rodič
revize
80498f4f79
2 změnil soubory, kde provedl 29 přidání a 4 odebrání
  1. 13
    0
      cmData.c
  2. 16
    4
      cmData.h

+ 13
- 0
cmData.c Zobrazit soubor

@@ -1284,8 +1284,12 @@ cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p )
1284 1284
 {
1285 1285
   assert( cmDataIsStruct(p) );
1286 1286
   
1287
+
1288
+  cmDataUnlink(p);
1289
+
1287 1290
   p->u.child    = parent->u.child;
1288 1291
   parent->u.child = p;
1292
+  p->parent = parent;
1289 1293
   return p;
1290 1294
 }
1291 1295
 
@@ -1294,6 +1298,8 @@ cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p )
1294 1298
   assert( cmDataIsStruct(parent) );
1295 1299
   assert( parent->tid != kRecordDtId || (parent->tid == kRecordDtId && p->tid==kPairDtId));
1296 1300
 
1301
+  cmDataUnlink(p);
1302
+
1297 1303
   cmData_t* cp = parent->u.child;
1298 1304
   if( cp == NULL )
1299 1305
     parent->u.child = p;
@@ -1307,6 +1313,7 @@ cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p )
1307 1313
       }
1308 1314
   }
1309 1315
 
1316
+  p->parent  = parent;
1310 1317
   p->sibling = NULL;
1311 1318
   return p;
1312 1319
 }
@@ -1316,6 +1323,8 @@ cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p )
1316 1323
   if( !cmDataIsStruct(parent) )
1317 1324
     return NULL;
1318 1325
 
1326
+  cmDataUnlink(p);
1327
+
1319 1328
   unsigned  n  = 0;
1320 1329
   cmData_t* cp = parent->u.child;
1321 1330
   cmData_t* pp = NULL;
@@ -1339,6 +1348,8 @@ cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p )
1339 1348
     ++n;
1340 1349
   }
1341 1350
 
1351
+  p->parent = parent;
1352
+
1342 1353
   return p;
1343 1354
   
1344 1355
 }
@@ -1422,6 +1433,8 @@ cmData_t* cmDataMakePair( cmData_t* parent, cmData_t* p, cmData_t* key, cmData_t
1422 1433
   p->parent = parent;
1423 1434
   p->flags   = 0;
1424 1435
   p->u.child = NULL;
1436
+  cmDataAppendChild(p,key);
1437
+  cmDataAppendChild(p,value);
1425 1438
   return p;
1426 1439
 }
1427 1440
 

+ 16
- 4
cmData.h Zobrazit soubor

@@ -125,7 +125,7 @@ extern "C" {
125 125
 
126 126
   bool cmDataIsValue(  const cmData_t* p );
127 127
   bool cmDataIsPtr(    const cmData_t* p );
128
-  bool cmDataIsStruct( const cmData_t* p );
128
+  bool cmDataIsStruct( const cmData_t* p ); // is a pair,list or record
129 129
 
130 130
   /*
131 131
     TODO:
@@ -367,17 +367,20 @@ extern "C" {
367 367
 
368 368
   // Prepend 'p' to 'parents' child list.
369 369
   // The source node 'p' is not duplicated  it is simply linked in.
370
+  // 'p' is automatically unlinked prior to being prepended.
370 371
   // Returns 'p'. 
371 372
   cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p );
372 373
 
373 374
   // Append 'p' to the end of 'parent' child list.
374 375
   // The source node 'p' is not duplicated it is simply linked in.
375
-  // Returns 'p'.
376
+  // 'p' is automatically unlinked prior to being appended.
377
+  // Returns 'p'. 
376 378
   cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p );
377 379
 
378 380
   // Insert 'p' at index.  Index must be in the range: 
379 381
   // 0 to cmDataChildCount(parent).
380 382
   // The source node 'p' is not duplicated it is simply linked in.
383
+  // 'p' is automatically unlinked prior to being inserted.
381 384
   // Returns 'p'.
382 385
   cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p );
383 386
 
@@ -408,12 +411,18 @@ extern "C" {
408 411
   // The data space for the 'label' string is dynamically allocated.
409 412
   cmData_t* cmDataPairSetKeyLabel(  cmData_t* p, const cmChar_t* label );
410 413
 
414
+  // Create a pair value by assigning a key and value to 'p'.
415
+  // 'p' is unlinked and freed prior to the key value assignment.
416
+  // 'key' and 'value' are simply linked in they are not duplicated or reallocated.
411 417
   cmData_t* cmDataMakePair(       cmData_t* parent, cmData_t* p, cmData_t* key, cmData_t* value );
412 418
 
413
-  // Dynamically allocate a pair node 
419
+  // Dynamically allocate a pair node. Both the key and value nodes are reallocated.
414 420
   cmData_t* cmDataAllocPair(      cmData_t* parent, const cmData_t* key,  const cmData_t* value );
421
+
422
+  // Dynamically allocate the id but link (w/o realloc) the value.
415 423
   cmData_t* cmDataAllocPairId(    cmData_t* parent, unsigned  keyId,       cmData_t* value );
416
-  // The data space for the 'label' string is dynamically allocated.
424
+
425
+  // Dynamically allocate the label but link (w/o realloc) the value.
417 426
   cmData_t* cmDataAllocPairLabel( cmData_t* parent, const cmChar_t* label, cmData_t* value );
418 427
 
419 428
   //----------------------------------------------------------------------------
@@ -476,6 +485,9 @@ extern "C" {
476 485
   cmData_t*       cmRecdMake( cmData_t* parent, cmData_t* p );
477 486
   cmData_t*       cmRecdAlloc( cmData_t* parent );
478 487
   
488
+  // Append a pair node by linking the pair node 'pair' to the record node 'p'.
489
+  // 'pair' is simply linked to 'p' via cmDataAppendChild() no 
490
+  // reallocation or duplicattion takes place.
479 491
   cmData_t*       cmRecdAppendPair( cmData_t* p, cmData_t* pair );
480 492
 
481 493
 

Načítá se…
Zrušit
Uložit