Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // template to access a character's inventory
- // this class automatically handles ignoring invalid (not fully replicated or initialized) inventory items
- // it is also resilient against the currently iterated item being destroyed
- template<typename InvType = AUTInventory> class UNREALTOURNAMENT_API TInventoryIterator
- {
- private:
- const AUTCharacter* Holder;
- AUTInventory* CurrentInv;
- AUTInventory* NextInv;
- int32 Count;
- inline bool IsValidForIteration(AUTInventory* TestInv)
- {
- return (TestInv->GetOwner() == Holder && TestInv->GetUTOwner() == Holder && (/*typeid(InvType) == typeid(AUTInventory) || */TestInv->IsA(InvType::StaticClass())));
- }
- public:
- TInventoryIterator(const AUTCharacter* InHolder)
- : Holder(InHolder), Count(0)
- {
- if (Holder != NULL)
- {
- CurrentInv = Holder->InventoryList;
- if (CurrentInv != NULL)
- {
- NextInv = CurrentInv->NextInventory;
- if (!IsValidForIteration(CurrentInv))
- {
- ++(*this);
- }
- }
- else
- {
- NextInv = NULL;
- }
- }
- else
- {
- CurrentInv = NULL;
- NextInv = NULL;
- }
- }
- void operator++()
- {
- do
- {
- // bound number of items to avoid infinite loops on clients in edge cases with partial replication
- // we could keep track of all the items we've iterated but that's more expensive and probably not worth it
- Count++;
- if (Count > 100)
- {
- CurrentInv = NULL;
- }
- else
- {
- CurrentInv = NextInv;
- if (CurrentInv != NULL)
- {
- NextInv = CurrentInv->NextInventory;
- }
- }
- } while (CurrentInv != NULL && !IsValidForIteration(CurrentInv));
- }
- FORCEINLINE bool IsValid() const
- {
- return CurrentInv != NULL;
- }
- FORCEINLINE operator bool() const
- {
- return IsValid();
- }
- FORCEINLINE InvType* operator*() const
- {
- checkSlow(CurrentInv != NULL && CurrentInv->IsA(InvType::StaticClass()));
- return (InvType*)CurrentInv;
- }
- FORCEINLINE InvType* operator->() const
- {
- checkSlow(CurrentInv != NULL && CurrentInv->IsA(InvType::StaticClass()));
- return (InvType*)CurrentInv;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement