-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.cpp
902 lines (761 loc) · 20 KB
/
container.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "container.h"
#include "game.h"
#include "iomap.h"
#include "player.h"
extern Game g_game;
Container::Container(uint16_t type) : Item(type)
{
maxSize = items[type].maxItems;
serializationCount = 0;
totalWeight = 0.0;
}
Container::~Container()
{
for(ItemList::iterator cit = itemlist.begin(); cit != itemlist.end(); ++cit)
{
(*cit)->setParent(NULL);
(*cit)->unRef();
}
itemlist.clear();
}
Item* Container::clone() const
{
Container* _item = static_cast<Container*>(Item::clone());
for(ItemList::const_iterator it = itemlist.begin(); it != itemlist.end(); ++it)
_item->addItem((*it)->clone());
return _item;
}
Container* Container::getParentContainer()
{
if(Cylinder* cylinder = getParent())
{
if(Item* item = cylinder->getItem())
return item->getContainer();
}
return NULL;
}
void Container::addItem(Item* item)
{
itemlist.push_back(item);
item->setParent(this);
}
Attr_ReadValue Container::readAttr(AttrTypes_t attr, PropStream& propStream)
{
switch(attr)
{
case ATTR_CONTAINER_ITEMS:
{
uint32_t count;
if(!propStream.GET_ULONG(count))
return ATTR_READ_ERROR;
serializationCount = count;
return ATTR_READ_END;
}
default:
break;
}
return Item::readAttr(attr, propStream);
}
bool Container::unserializeItemNode(FileLoader& f, NODE node, PropStream& propStream)
{
if(!Item::unserializeItemNode(f, node, propStream))
return false;
uint32_t type;
for(NODE nodeItem = f.getChildNode(node, type); nodeItem; nodeItem = f.getNextNode(nodeItem, type))
{
//load container items
if(type != OTBM_ITEM)
return false;
PropStream itemPropStream;
f.getProps(nodeItem, itemPropStream);
Item* item = Item::CreateItem(itemPropStream);
if(!item)
return false;
if(!item->unserializeItemNode(f, nodeItem, itemPropStream))
return false;
addItem(item);
updateItemWeight(item->getWeight());
}
return true;
}
void Container::updateItemWeight(double diff)
{
totalWeight += diff;
if(Container* parent = getParentContainer())
parent->updateItemWeight(diff);
}
double Container::getWeight() const
{
return Item::getWeight() + totalWeight;
}
std::string Container::getContentDescription() const
{
std::stringstream s;
return getContentDescription(s).str();
}
std::stringstream& Container::getContentDescription(std::stringstream& s) const
{
bool begin = true;
Container* evil = const_cast<Container*>(this);
for(ContainerIterator it = evil->begin(); it != evil->end(); ++it)
{
if(!begin)
s << ", ";
else
begin = false;
s << (*it)->getNameDescription();
}
if(begin)
s << "nothing";
return s;
}
Item* Container::getItem(uint32_t index)
{
size_t n = 0;
for(ItemList::const_iterator cit = getItems(); cit != getEnd(); ++cit)
{
if(n == index)
return *cit;
else
++n;
}
return NULL;
}
uint32_t Container::getItemHoldingCount() const
{
uint32_t counter = 0;
for(ContainerIterator it = begin(); it != end(); ++it)
++counter;
return counter;
}
bool Container::isHoldingItem(const Item* item) const
{
for(ContainerIterator it = begin(); it != end(); ++it)
{
if((*it) == item)
return true;
}
return false;
}
void Container::onAddContainerItem(Item* item)
{
const Position& cylinderMapPos = getPosition();
SpectatorVec list;
SpectatorVec::iterator it;
g_game.getSpectators(list, cylinderMapPos, false, false, 2, 2, 2, 2);
//send to client
Player* player = NULL;
for(it = list.begin(); it != list.end(); ++it)
{
if((player = (*it)->getPlayer()))
player->sendAddContainerItem(this, item);
}
//event methods
for(it = list.begin(); it != list.end(); ++it)
{
if((player = (*it)->getPlayer()))
player->onAddContainerItem(this, item);
}
}
void Container::onUpdateContainerItem(uint32_t index, Item* oldItem, const ItemType& oldType,
Item* newItem, const ItemType& newType)
{
const Position& cylinderMapPos = getPosition();
SpectatorVec list;
SpectatorVec::iterator it;
g_game.getSpectators(list, cylinderMapPos, false, false, 2, 2, 2, 2);
//send to client
Player* player = NULL;
for(it = list.begin(); it != list.end(); ++it)
{
if((player = (*it)->getPlayer()))
player->sendUpdateContainerItem(this, index, oldItem, newItem);
}
//event methods
for(it = list.begin(); it != list.end(); ++it)
{
if((player = (*it)->getPlayer()))
player->onUpdateContainerItem(this, index, oldItem, oldType, newItem, newType);
}
}
void Container::onRemoveContainerItem(uint32_t index, Item* item)
{
const Position& cylinderMapPos = getPosition();
SpectatorVec list;
SpectatorVec::iterator it;
g_game.getSpectators(list, cylinderMapPos, false, false, 2, 2, 2, 2);
//send change to client
Player* player = NULL;
for(it = list.begin(); it != list.end(); ++it)
{
if((player = (*it)->getPlayer()))
player->sendRemoveContainerItem(this, index, item);
}
//event methods
for(it = list.begin(); it != list.end(); ++it)
{
if((player = (*it)->getPlayer()))
player->onRemoveContainerItem(this, index, item);
}
}
ReturnValue Container::__queryAdd(int32_t index, const Thing* thing, uint32_t count,
uint32_t flags) const
{
if(((flags & FLAG_CHILDISOWNER) == FLAG_CHILDISOWNER))
{
//a child container is querying, since we are the top container (not carried by a player)
//just return with no error.
return RET_NOERROR;
}
const Item* item = thing->getItem();
if(!item)
return RET_NOTPOSSIBLE;
if(!item->isPickupable())
return RET_CANNOTPICKUP;
if(item == this)
return RET_THISISIMPOSSIBLE;
if(const Container* container = item->getContainer())
{
for(const Cylinder* cylinder = getParent(); cylinder; cylinder = cylinder->getParent())
{
if(cylinder == container)
return RET_THISISIMPOSSIBLE;
}
}
if(index == INDEX_WHEREEVER && !((flags & FLAG_NOLIMIT) == FLAG_NOLIMIT) && full())
return RET_CONTAINERNOTENOUGHROOM;
const Cylinder* topParent = getTopParent();
if(topParent != this)
return topParent->__queryAdd(INDEX_WHEREEVER, item, count, flags | FLAG_CHILDISOWNER);
return RET_NOERROR;
}
ReturnValue Container::__queryMaxCount(int32_t index, const Thing* thing, uint32_t count,
uint32_t& maxQueryCount, uint32_t flags) const
{
const Item* item = thing->getItem();
if(!item)
{
maxQueryCount = 0;
return RET_NOTPOSSIBLE;
}
if(((flags & FLAG_NOLIMIT) == FLAG_NOLIMIT))
{
maxQueryCount = std::max((uint32_t)1, count);
return RET_NOERROR;
}
int32_t freeSlots = std::max((int32_t)(capacity() - size()), (int32_t)0);
if(item->isStackable())
{
uint32_t n = 0;
if(index != INDEX_WHEREEVER)
{
const Thing* destThing = __getThing(index);
const Item* destItem = NULL;
if(destThing)
destItem = destThing->getItem();
if(destItem && destItem->getID() == item->getID())
n = 100 - destItem->getItemCount();
}
maxQueryCount = freeSlots * 100 + n;
if(maxQueryCount < count)
return RET_CONTAINERNOTENOUGHROOM;
}
else
{
maxQueryCount = freeSlots;
if(maxQueryCount == 0)
return RET_CONTAINERNOTENOUGHROOM;
}
return RET_NOERROR;
}
ReturnValue Container::__queryRemove(const Thing* thing, uint32_t count, uint32_t flags) const
{
int32_t index = __getIndexOfThing(thing);
if(index == -1)
return RET_NOTPOSSIBLE;
const Item* item = thing->getItem();
if(item == NULL)
return RET_NOTPOSSIBLE;
if(count == 0 || (item->isStackable() && count > item->getItemCount()))
return RET_NOTPOSSIBLE;
if(item->isNotMoveable() && !hasBitSet(FLAG_IGNORENOTMOVEABLE, flags))
return RET_NOTMOVEABLE;
return RET_NOERROR;
}
Cylinder* Container::__queryDestination(int32_t& index, const Thing* thing, Item** destItem,
uint32_t& flags)
{
if(index == 254 /*move up*/)
{
index = INDEX_WHEREEVER;
*destItem = NULL;
Container* parentContainer = dynamic_cast<Container*>(getParent());
if(parentContainer)
return parentContainer;
else
return this;
}
else if(index == 255 /*add wherever*/)
{
index = INDEX_WHEREEVER;
*destItem = NULL;
return this;
}
else
{
if(index >= (int32_t)capacity())
{
/*
if you have a container, maximize it to show all 20 slots
then you open a bag that is inside the container you will have a bag with 8 slots
and a "grey" area where the other 12 slots where from the container
if you drop the item on that grey area
the client calculates the slot position as if the bag has 20 slots
*/
index = INDEX_WHEREEVER;
}
if(index != INDEX_WHEREEVER)
{
Thing* destThing = __getThing(index);
if(destThing)
*destItem = destThing->getItem();
if(Cylinder* subCylinder = dynamic_cast<Cylinder*>(*destItem))
{
index = INDEX_WHEREEVER;
*destItem = NULL;
return subCylinder;
}
}
}
return this;
}
void Container::__addThing(Creature* actor, Thing* thing)
{
return __addThing(actor, 0, thing);
}
void Container::__addThing(Creature* actor, int32_t index, Thing* thing)
{
if(index >= (int32_t)capacity())
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__addThing], index:" << index << ", index >= capacity()" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
Item* item = thing->getItem();
if(item == NULL)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__addThing] item == NULL" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
#ifdef __DEBUG_MOVESYS__
if(index != INDEX_WHEREEVER)
{
if(size() >= capacity())
{
std::cout << "Failure: [Container::__addThing] size() >= capacity()" << std::endl;
DEBUG_REPORT
return /*RET_CONTAINERNOTENOUGHROOM*/;
}
}
#endif
item->setParent(this);
itemlist.push_front(item);
totalWeight += item->getWeight();
if(Container* parentContainer = getParentContainer())
parentContainer->updateItemWeight(item->getWeight());
//send change to client
if(getParent() && getParent() != VirtualCylinder::virtualCylinder)
onAddContainerItem(item);
}
void Container::__updateThing(Thing* thing, uint16_t itemId, uint32_t count)
{
int32_t index = __getIndexOfThing(thing);
if(index == -1)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__updateThing] index == -1" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
Item* item = thing->getItem();
if(item == NULL)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__updateThing] item == NULL" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
const ItemType& oldType = Item::items[item->getID()];
const ItemType& newType = Item::items[itemId];
const double oldWeight = item->getWeight();
item->setID(itemId);
item->setSubType(count);
const double diffWeight = -oldWeight + item->getWeight();
totalWeight += diffWeight;
if(Container* parentContainer = getParentContainer())
parentContainer->updateItemWeight(diffWeight);
//send change to client
if(getParent())
onUpdateContainerItem(index, item, oldType, item, newType);
}
void Container::__replaceThing(uint32_t index, Thing* thing)
{
Item* item = thing->getItem();
if(item == NULL)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__replaceThing] item == NULL" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
uint32_t count = 0;
ItemList::iterator cit = itemlist.end();
for(cit = itemlist.begin(); cit != itemlist.end(); ++cit)
{
if(count == index)
break;
else
++count;
}
if(cit == itemlist.end())
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__updateThing] item not found" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
totalWeight -= (*cit)->getWeight();
totalWeight += item->getWeight();
if(Container* parentContainer = getParentContainer())
parentContainer->updateItemWeight(-(*cit)->getWeight() + item->getWeight());
itemlist.insert(cit, item);
item->setParent(this);
//send change to client
if(getParent())
{
const ItemType& oldType = Item::items[(*cit)->getID()];
const ItemType& newType = Item::items[item->getID()];
onUpdateContainerItem(index, *cit, oldType, item, newType);
}
(*cit)->setParent(NULL);
itemlist.erase(cit);
}
void Container::__removeThing(Thing* thing, uint32_t count)
{
Item* item = thing->getItem();
if(item == NULL)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__removeThing] item == NULL" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
int32_t index = __getIndexOfThing(thing);
if(index == -1)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__removeThing] index == -1" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
ItemList::iterator cit = std::find(itemlist.begin(), itemlist.end(), thing);
if(cit == itemlist.end())
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__removeThing] item not found" << std::endl;
DEBUG_REPORT
#endif
return /*RET_NOTPOSSIBLE*/;
}
if(item->isStackable() && count != item->getItemCount())
{
const double oldWeight = -item->getWeight();
item->setItemCount(std::max(0, (int32_t)(item->getItemCount() - count)));
const double diffWeight = oldWeight + item->getWeight();
totalWeight += diffWeight;
//send change to client
if(getParent())
{
if(Container* parentContainer = getParentContainer())
parentContainer->updateItemWeight(diffWeight);
const ItemType& it = Item::items[item->getID()];
onUpdateContainerItem(index, item, it, item, it);
}
}
else
{
//send change to client
if(getParent())
{
if(Container* parentContainer = getParentContainer())
parentContainer->updateItemWeight(-item->getWeight());
onRemoveContainerItem(index, item);
}
totalWeight -= item->getWeight();
item->setParent(NULL);
itemlist.erase(cit);
}
}
Thing* Container::__getThing(uint32_t index) const
{
if(index < 0 || index > size())
return NULL;
uint32_t count = 0;
for(ItemList::const_iterator cit = itemlist.begin(); cit != itemlist.end(); ++cit)
{
if(count == index)
return *cit;
else
++count;
}
return NULL;
}
int32_t Container::__getIndexOfThing(const Thing* thing) const
{
uint32_t index = 0;
for(ItemList::const_iterator cit = getItems(); cit != getEnd(); ++cit)
{
if(*cit == thing)
return index;
else
++index;
}
return -1;
}
int32_t Container::__getFirstIndex() const
{
return 0;
}
int32_t Container::__getLastIndex() const
{
return size();
}
uint32_t Container::__getItemTypeCount(uint16_t itemId, int32_t subType /*= -1*/, bool itemCount /*= true*/) const
{
uint32_t count = 0;
Item* item = NULL;
for(ItemList::const_iterator it = itemlist.begin(); it != itemlist.end(); ++it)
{
item = (*it);
if(item && item->getID() == itemId && (subType == -1 || subType == item->getSubType()))
{
if(!itemCount)
{
if(item->isRune())
count += item->getCharges();
else
count += item->getItemCount();
}
else
count += item->getItemCount();
}
}
return count;
}
std::map<uint32_t, uint32_t>& Container::__getAllItemTypeCount(std::map<uint32_t,
uint32_t>& countMap, bool itemCount /*= true*/) const
{
Item* item = NULL;
for(ItemList::const_iterator it = itemlist.begin(); it != itemlist.end(); ++it)
{
item = (*it);
if(!itemCount)
{
if(item->isRune())
countMap[item->getID()] += item->getCharges();
else
countMap[item->getID()] += item->getItemCount();
}
else
countMap[item->getID()] += item->getItemCount();
}
return countMap;
}
void Container::postAddNotification(Creature* actor, Thing* thing, const Cylinder* oldParent,
int32_t index, cylinderlink_t link /*= LINK_OWNER*/)
{
Cylinder* topParent = getTopParent();
if(!topParent->getCreature())
{
if(topParent == this)
{
//let the tile class notify surrounding players
if(topParent->getParent())
topParent->getParent()->postAddNotification(actor, thing, oldParent, index, LINK_NEAR);
}
else
topParent->postAddNotification(actor, thing, oldParent, index, LINK_PARENT);
}
else
topParent->postAddNotification(actor, thing, oldParent, index, LINK_TOPPARENT);
}
void Container::postRemoveNotification(Creature* actor, Thing* thing, const Cylinder* newParent,
int32_t index, bool isCompleteRemoval, cylinderlink_t link /*= LINK_OWNER*/)
{
Cylinder* topParent = getTopParent();
if(!topParent->getCreature())
{
if(topParent == this)
{
//let the tile class notify surrounding players
if(topParent->getParent())
topParent->getParent()->postRemoveNotification(actor, thing,
newParent, index, isCompleteRemoval, LINK_NEAR);
}
else
topParent->postRemoveNotification(actor, thing, newParent,
index, isCompleteRemoval, LINK_PARENT);
}
else
topParent->postRemoveNotification(actor, thing, newParent,
index, isCompleteRemoval, LINK_TOPPARENT);
}
void Container::__internalAddThing(Thing* thing)
{
__internalAddThing(0, thing);
}
void Container::__internalAddThing(uint32_t index, Thing* thing)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "[Container::__internalAddThing] index: " << index << std::endl;
#endif
if(!thing)
return;
Item* item = thing->getItem();
if(item == NULL)
{
#ifdef __DEBUG_MOVESYS__
std::cout << "Failure: [Container::__internalAddThing] item == NULL" << std::endl;
#endif
return;
}
itemlist.push_front(item);
item->setParent(this);
totalWeight += item->getWeight();
if(Container* parentContainer = getParentContainer())
parentContainer->updateItemWeight(item->getWeight());
}
void Container::__startDecaying()
{
for(ItemList::const_iterator it = itemlist.begin(); it != itemlist.end(); ++it)
(*it)->__startDecaying();
}
ContainerIterator Container::begin()
{
ContainerIterator cit(this);
if(!itemlist.empty())
{
cit.over.push(this);
cit.current = itemlist.begin();
}
return cit;
}
ContainerIterator Container::end()
{
ContainerIterator cit(this);
return cit;
}
ContainerIterator Container::begin() const
{
Container* evil = const_cast<Container*>(this);
return evil->begin();
}
ContainerIterator Container::end() const
{
Container* evil = const_cast<Container*>(this);
return evil->end();
}
ContainerIterator::ContainerIterator():
base(NULL) {}
ContainerIterator::ContainerIterator(Container* _base):
base(_base) {}
ContainerIterator::ContainerIterator(const ContainerIterator& rhs):
base(rhs.base), over(rhs.over), current(rhs.current) {}
bool ContainerIterator::operator==(const ContainerIterator& rhs)
{
return !(*this != rhs);
}
bool ContainerIterator::operator!=(const ContainerIterator& rhs)
{
assert(base);
if(base != rhs.base)
return true;
if(over.empty() && rhs.over.empty())
return false;
if(over.empty())
return true;
if(rhs.over.empty())
return true;
if(over.front() != rhs.over.front())
return true;
return current != rhs.current;
}
ContainerIterator& ContainerIterator::operator=(const ContainerIterator& rhs)
{
this->base = rhs.base;
this->current = rhs.current;
this->over = rhs.over;
return *this;
}
Item* ContainerIterator::operator*()
{
assert(base);
return *current;
}
Item* ContainerIterator::operator->()
{
return *(*this);
}
ContainerIterator& ContainerIterator::operator++()
{
assert(base);
if(Item* item = *current)
{
Container* container = item->getContainer();
if(container && !container->empty())
over.push(container);
}
++current;
if(current == over.front()->itemlist.end())
{
over.pop();
if(over.empty())
return *this;
current = over.front()->itemlist.begin();
}
return *this;
}
ContainerIterator ContainerIterator::operator++(int32_t)
{
ContainerIterator tmp(*this);
++*this;
return tmp;
}