ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/object.C
(Generate patch)

Comparing deliantra/server/common/object.C (file contents):
Revision 1.343 by root, Sat Apr 23 04:56:46 2011 UTC vs.
Revision 1.344 by root, Sun May 1 13:18:23 2011 UTC

233 233
234/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 234/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
235static bool 235static bool
236compare_ob_value_lists_one (const object *wants, const object *has) 236compare_ob_value_lists_one (const object *wants, const object *has)
237{ 237{
238 /* n-squared behaviour (see kv_get), but I'm hoping both 238 /* n-squared behaviour (see kv.get), but I'm hoping both
239 * objects with lists are rare, and lists stay short. If not, use a 239 * objects with lists are rare, and lists stay short. If not, use a
240 * different structure or at least keep the lists sorted... 240 * different structure or at least keep the lists sorted...
241 */ 241 */
242 242
243 /* For each field in wants, */ 243 /* For each field in wants, */
244 for (key_value *kv = wants->key_values; kv; kv = kv->next) 244 for (key_value *kv = wants->kv.first; kv; kv = kv->next)
245 if (has->kv_get (kv->key) != kv->value) 245 if (has->kv.get (kv->key) != kv->value)
246 return false; 246 return false;
247 247
248 /* If we get here, every field in wants has a matching field in has. */ 248 /* If we get here, every field in wants has a matching field in has. */
249 return true; 249 return true;
250} 250}
373 if (ob1->level != ob2->level) 373 if (ob1->level != ob2->level)
374 return 0; 374 return 0;
375 break; 375 break;
376 } 376 }
377 377
378 if (ob1->key_values || ob2->key_values) 378 if (!ob1->kv.empty () || !ob2->kv.empty ())
379 { 379 {
380 /* At least one of these has key_values. */ 380 /* At least one of these has key_values. */
381 if ((!ob1->key_values) != (!ob2->key_values)) 381 if (ob1->kv.empty () != ob2->kv.empty ())
382 return 0; /* One has fields, but the other one doesn't. */ 382 return 0; /* One has fields, but the other one doesn't. */
383 383
384 if (!compare_ob_value_lists (ob1, ob2)) 384 if (!compare_ob_value_lists (ob1, ob2))
385 return 0; 385 return 0;
386 } 386 }
599 } 599 }
600 600
601 this->owner = owner; 601 this->owner = owner;
602} 602}
603 603
604/* Zero the key_values on op, decrementing the shared-string
605 * refcounts and freeing the links.
606 */
607static void
608free_key_values (object *op)
609{
610 for (key_value *i = op->key_values; i; )
611 {
612 key_value *next = i->next;
613 delete i;
614
615 i = next;
616 }
617
618 op->key_values = 0;
619}
620
621/* 604/*
622 * copy_to first frees everything allocated by the dst object, 605 * copy_to first frees everything allocated by the dst object,
623 * and then copies the contents of itself into the second 606 * and then copies the contents of itself into the second
624 * object, allocating what needs to be allocated. Basically, any 607 * object, allocating what needs to be allocated. Basically, any
625 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 608 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
629void 612void
630object::copy_to (object *dst) 613object::copy_to (object *dst)
631{ 614{
632 dst->remove (); 615 dst->remove ();
633 *(object_copy *)dst = *this; 616 *(object_copy *)dst = *this;
617
618 // maybe move to object_copy?
619 dst->kv = kv;
620
634 dst->flag [FLAG_REMOVED] = true; 621 dst->flag [FLAG_REMOVED] = true;
635
636 /* Copy over key_values, if any. */
637 if (key_values)
638 {
639 key_value *tail = 0;
640 dst->key_values = 0;
641
642 for (key_value *i = key_values; i; i = i->next)
643 {
644 key_value *new_link = new key_value;
645
646 new_link->next = 0;
647 new_link->key = i->key;
648 new_link->value = i->value;
649
650 /* Try and be clever here, too. */
651 if (!dst->key_values)
652 {
653 dst->key_values = new_link;
654 tail = new_link;
655 }
656 else
657 {
658 tail->next = new_link;
659 tail = new_link;
660 }
661 }
662 }
663
664 dst->activate (); 622 dst->activate ();
665} 623}
666 624
667void 625void
668object::instantiate () 626object::instantiate ()
831 789
832object::~object () 790object::~object ()
833{ 791{
834 unlink (); 792 unlink ();
835 793
836 free_key_values (this); 794 kv.clear ();
837} 795}
838 796
839void object::link () 797void object::link ()
840{ 798{
841 assert (!index);//D 799 assert (!index);//D
2494 return tmp; 2452 return tmp;
2495 2453
2496 return 0; 2454 return 0;
2497} 2455}
2498 2456
2457/* Zero the key_values on op, decrementing the shared-string
2458 * refcounts and freeing the links.
2459 */
2460void
2461key_values::clear ()
2462{
2463 for (key_value *kvp = first; kvp; )
2464 {
2465 key_value *next = kvp->next;
2466 delete kvp;
2467 kvp = next;
2468 }
2469
2470 first = 0;
2471}
2472
2499shstr_tmp 2473shstr_tmp
2500object::kv_get (shstr_tmp key) const 2474key_values::get (shstr_tmp key) const
2501{ 2475{
2502 for (key_value *kv = key_values; kv; kv = kv->next) 2476 for (key_value *kv = first; kv; kv = kv->next)
2503 if (kv->key == key) 2477 if (kv->key == key)
2504 return kv->value; 2478 return kv->value;
2505 2479
2506 return shstr (); 2480 return shstr ();
2507} 2481}
2508 2482
2509void 2483void
2484key_values::add (shstr_tmp key, shstr_tmp value)
2485{
2486 key_value *kv = new key_value;
2487
2488 kv->next = first;
2489 kv->key = key;
2490 kv->value = value;
2491
2492 first = kv;
2493}
2494
2495void
2510object::kv_set (shstr_tmp key, shstr_tmp value) 2496key_values::set (shstr_tmp key, shstr_tmp value)
2511{ 2497{
2512 for (key_value *kv = key_values; kv; kv = kv->next) 2498 for (key_value *kv = first; kv; kv = kv->next)
2513 if (kv->key == key) 2499 if (kv->key == key)
2514 { 2500 {
2515 kv->value = value; 2501 kv->value = value;
2516 return; 2502 return;
2517 } 2503 }
2518 2504
2519 key_value *kv = new key_value; 2505 add (key, value);
2520
2521 kv->next = key_values;
2522 kv->key = key;
2523 kv->value = value;
2524
2525 key_values = kv;
2526} 2506}
2527 2507
2528void 2508void
2529object::kv_del (shstr_tmp key) 2509key_values::del (shstr_tmp key)
2530{ 2510{
2531 for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) 2511 for (key_value **kvp = &first; *kvp; kvp = &(*kvp)->next)
2532 if ((*kvp)->key == key) 2512 if ((*kvp)->key == key)
2533 { 2513 {
2534 key_value *kv = *kvp; 2514 key_value *kv = *kvp;
2535 *kvp = (*kvp)->next; 2515 *kvp = (*kvp)->next;
2536 delete kv; 2516 delete kv;
2537 return; 2517 return;
2538 } 2518 }
2519}
2520
2521void
2522key_values::reverse ()
2523{
2524 key_value *prev = 0;
2525 key_value *head = first;
2526
2527 while (head)
2528 {
2529 key_value *node = head;
2530 head = head->next;
2531 node->next = prev;
2532 prev = node;
2533 }
2534
2535 first = prev;
2536}
2537
2538key_values &
2539key_values::operator =(const key_values &kv)
2540{
2541 clear ();
2542
2543 for (key_value *kvp = kv.first; kvp; kvp = kvp->next)
2544 add (kvp->key, kvp->value);
2545
2546 reverse ();
2539} 2547}
2540 2548
2541object::depth_iterator::depth_iterator (object *container) 2549object::depth_iterator::depth_iterator (object *container)
2542: iterator_base (container) 2550: iterator_base (container)
2543{ 2551{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines