OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
containerstore.hpp
Go to the documentation of this file.
1 #ifndef GAME_MWWORLD_CONTAINERSTORE_H
2 #define GAME_MWWORLD_CONTAINERSTORE_H
3 
4 #include <iterator>
5 #include <map>
6 #include <utility>
7 
20 
21 #include "ptr.hpp"
22 #include "cellreflist.hpp"
23 
24 namespace ESM
25 {
26  struct InventoryList;
27  struct InventoryState;
28 }
29 
30 namespace MWWorld
31 {
32  class ContainerStoreIterator;
33 
35  {
36  public:
37  virtual void itemAdded(const ConstPtr& item, int count) {}
38  virtual void itemRemoved(const ConstPtr& item, int count) {}
39  };
40 
42  {
43  public:
44 
45  static const int Type_Potion = 0x0001;
46  static const int Type_Apparatus = 0x0002;
47  static const int Type_Armor = 0x0004;
48  static const int Type_Book = 0x0008;
49  static const int Type_Clothing = 0x0010;
50  static const int Type_Ingredient = 0x0020;
51  static const int Type_Light = 0x0040;
52  static const int Type_Lockpick = 0x0080;
53  static const int Type_Miscellaneous = 0x0100;
54  static const int Type_Probe = 0x0200;
55  static const int Type_Repair = 0x0400;
56  static const int Type_Weapon = 0x0800;
57 
58  static const int Type_Last = Type_Weapon;
59 
60  static const int Type_All = 0xffff;
61 
62  static const std::string sGoldId;
63 
64  private:
65 
78 
79  std::map<std::pair<std::string, std::string>, int> mLevelledItemMap;
82 
84 
85  mutable float mCachedWeight;
86  mutable bool mWeightUpToDate;
87  ContainerStoreIterator addImp (const Ptr& ptr, int count);
88  void addInitialItem (const std::string& id, const std::string& owner, int count, bool topLevel=true, const std::string& levItem = "");
89 
90  template<typename T>
92  const ESM::ObjectState& state);
93 
94  template<typename T>
95  void storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state) const;
96 
97  template<typename T>
98  void storeStates (const CellRefList<T>& collection,
99  ESM::InventoryState& inventory, int& index,
100  bool equipable = false) const;
101 
102  virtual void storeEquipmentState (const MWWorld::LiveCellRefBase& ref, int index, ESM::InventoryState& inventory) const;
103 
104  virtual void readEquipmentState (const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory);
105  public:
106 
107  ContainerStore();
108 
109  virtual ~ContainerStore();
110 
111  virtual ContainerStore* clone() { return new ContainerStore(*this); }
112 
114 
116 
117  virtual ContainerStoreIterator add (const Ptr& itemPtr, int count, const Ptr& actorPtr, bool setOwner=false);
128 
129  ContainerStoreIterator add(const std::string& id, int count, const Ptr& actorPtr);
131 
132  int remove(const std::string& itemId, int count, const Ptr& actor);
136 
137  virtual int remove(const Ptr& item, int count, const Ptr& actor);
141 
142  ContainerStoreIterator unstack (const Ptr& ptr, const Ptr& container, int count = 1);
146 
151 
153  int count (const std::string& id);
154 
156  void setContListener(ContainerStoreListener* listener);
157 
158  protected:
161 
162  virtual void flagAsModified();
163 
164  public:
165 
166  virtual bool stacks (const ConstPtr& ptr1, const ConstPtr& ptr2);
168 
169  void fill (const ESM::InventoryList& items, const std::string& owner);
171 
172  void restock (const ESM::InventoryList& items, const MWWorld::Ptr& ptr, const std::string& owner);
173 
174  virtual void clear();
176 
177  float getWeight() const;
179 
180  static int getType (const ConstPtr& ptr);
183 
184  Ptr search (const std::string& id);
185 
186  virtual void writeState (ESM::InventoryState& state) const;
187 
188  virtual void readState (const ESM::InventoryState& state);
189 
191  };
192 
197  : public std::iterator<std::forward_iterator_tag, Ptr, std::ptrdiff_t, Ptr *, Ptr&>
198  {
199  int mType;
200  int mMask;
202  mutable Ptr mPtr;
203 
216 
217  private:
218 
221 
222  ContainerStoreIterator (int mask, ContainerStore *container);
224 
225  // construct iterator using a CellRefList iterator
238 
239  void copy (const ContainerStoreIterator& src);
240 
241  void incType();
242 
243  void nextType();
244 
245  bool resetIterator();
249 
250  bool incIterator();
254 
255  public:
256 
258 
259  Ptr *operator->() const;
260 
261  Ptr operator*() const;
262 
264 
266 
268 
269  bool isEqual (const ContainerStoreIterator& iter) const;
270 
271  int getType() const;
272 
273  const ContainerStore *getContainerStore() const;
274 
275  friend class ContainerStore;
276  };
277 
278  bool operator== (const ContainerStoreIterator& left, const ContainerStoreIterator& right);
279  bool operator!= (const ContainerStoreIterator& left, const ContainerStoreIterator& right);
280 }
281 
282 #endif
Iteration over a subset of objects in a ContainerStore.
Definition: containerstore.hpp:196
Used to create pointers to hold any type of LiveCellRef<> object.
Definition: livecellref.hpp:22
ContainerStoreListener * getContListener() const
Definition: containerstore.cpp:139
MWWorld::CellRefList< ESM::Apparatus > appas
Definition: containerstore.hpp:67
virtual void writeState(ESM::InventoryState &state) const
Definition: containerstore.cpp:729
MWWorld::CellRefList< ESM::Apparatus >::List::iterator mApparatus
Definition: containerstore.hpp:205
virtual ContainerStore * clone()
Definition: containerstore.hpp:111
void incType()
Definition: containerstore.cpp:837
MWWorld::CellRefList< ESM::Miscellaneous > miscItems
Definition: containerstore.hpp:74
static const int Type_Clothing
Definition: containerstore.hpp:49
static const int Type_Light
Definition: containerstore.hpp:51
MWWorld::CellRefList< ESM::Probe > probes
Definition: containerstore.hpp:75
ContainerStoreIterator begin(int mask=Type_All)
Definition: containerstore.cpp:120
MWWorld::CellRefList< ESM::Potion >::List::iterator mPotion
Definition: containerstore.hpp:204
bool operator==(const LiveCellRef< X > &ref, int pRefnum)
Definition: cellstore.cpp:205
virtual void flagAsModified()
Definition: containerstore.cpp:577
void restock(const ESM::InventoryList &items, const MWWorld::Ptr &ptr, const std::string &owner)
Definition: containerstore.cpp:484
virtual bool stacks(const ConstPtr &ptr1, const ConstPtr &ptr2)
Definition: containerstore.cpp:192
ContainerStoreIterator addNewStack(const ConstPtr &ptr, int count)
Add the item to this container (do not try to stack it onto existing items)
Definition: containerstore.cpp:355
MWWorld::CellRefList< ESM::Armor > armors
Definition: containerstore.hpp:68
MWWorld::CellRefList< ESM::Ingredient >::List::iterator mIngredient
Definition: containerstore.hpp:209
int getType() const
Definition: containerstore.cpp:1079
bool mWeightUpToDate
Definition: containerstore.hpp:86
InventoryList, NPCO subrecord.
Definition: loadcont.hpp:26
virtual void readEquipmentState(const MWWorld::ContainerStoreIterator &iter, int index, const ESM::InventoryState &inventory)
Definition: containerstore.cpp:86
static const int Type_Armor
Definition: containerstore.hpp:47
static const int Type_Ingredient
Definition: containerstore.hpp:50
virtual ~ContainerStore()
Definition: containerstore.cpp:118
Definition: livecellref.hpp:77
static const int Type_Apparatus
Definition: containerstore.hpp:46
static const int Type_Weapon
Definition: containerstore.hpp:56
MWWorld::CellRefList< ESM::Book > books
Definition: containerstore.hpp:69
MWWorld::CellRefList< ESM::Clothing >::List::iterator mClothing
Definition: containerstore.hpp:208
bool isEqual(const ContainerStoreIterator &iter) const
Definition: containerstore.cpp:1051
ContainerStoreListener * mListener
Definition: containerstore.hpp:83
static const int Type_Last
Definition: containerstore.hpp:58
static const int Type_Repair
Definition: containerstore.hpp:55
static const std::string sGoldId
Definition: containerstore.hpp:62
MWWorld::CellRefList< ESM::Miscellaneous >::List::iterator mMiscellaneous
Definition: containerstore.hpp:212
MWWorld::CellRefList< ESM::Repair >::List::iterator mRepair
Definition: containerstore.hpp:214
Ptr operator*() const
Definition: containerstore.cpp:1004
void fill(const ESM::InventoryList &items, const std::string &owner)
Insert items into *this.
Definition: containerstore.cpp:422
int mMask
Definition: containerstore.hpp:200
MWWorld::CellRefList< ESM::Repair > repairs
Definition: containerstore.hpp:76
virtual void itemAdded(const ConstPtr &item, int count)
Definition: containerstore.hpp:37
MWWorld::CellRefList< ESM::Book >::List::iterator mBook
Definition: containerstore.hpp:207
float getWeight() const
Return total weight of the items contained in *this.
Definition: containerstore.cpp:582
ContainerStoreIterator & operator++()
Definition: containerstore.cpp:1032
Ptr search(const std::string &id)
Definition: containerstore.cpp:652
MWWorld::CellRefList< ESM::Ingredient > ingreds
Definition: containerstore.hpp:71
static const int Type_Book
Definition: containerstore.hpp:48
MWWorld::CellRefList< ESM::Weapon > weapons
Definition: containerstore.hpp:77
Definition: containerstore.hpp:34
ContainerStoreIterator addImp(const Ptr &ptr, int count)
Definition: containerstore.cpp:312
<
Definition: objectstate.hpp:19
MWWorld::CellRefList< ESM::Clothing > clothes
Definition: containerstore.hpp:70
void nextType()
Definition: containerstore.cpp:850
virtual void clear()
Empty container.
Definition: containerstore.cpp:569
MWWorld::ContainerStoreIterator restack(const MWWorld::Ptr &item)
Definition: containerstore.cpp:164
Pointer to a const LiveCellRef.
Definition: ptr.hpp:90
virtual ContainerStoreIterator add(const Ptr &itemPtr, int count, const Ptr &actorPtr, bool setOwner=false)
Definition: containerstore.cpp:236
MWWorld::CellRefList< ESM::Lockpick > lockpicks
Definition: containerstore.hpp:73
static const int Type_Probe
Definition: containerstore.hpp:54
State for inventories and containers.
Definition: inventorystate.hpp:16
MWWorld::CellRefList< ESM::Potion > potions
Definition: containerstore.hpp:66
void setContListener(ContainerStoreListener *listener)
Definition: containerstore.cpp:145
Definition: containerstore.hpp:41
ContainerStoreIterator end()
Definition: containerstore.cpp:125
ContainerStoreIterator getState(CellRefList< T > &collection, const ESM::ObjectState &state)
static const int Type_Potion
Definition: containerstore.hpp:45
virtual void storeEquipmentState(const MWWorld::LiveCellRefBase &ref, int index, ESM::InventoryState &inventory) const
Definition: containerstore.cpp:82
static const int Type_All
Definition: containerstore.hpp:60
ContainerStore * mContainer
Definition: containerstore.hpp:201
void storeState(const LiveCellRef< T > &ref, ESM::ObjectState &state) const
Definition: containerstore.cpp:91
Ptr mPtr
Definition: containerstore.hpp:202
const ContainerStore * getContainerStore() const
Definition: containerstore.cpp:1084
int mType
Definition: containerstore.hpp:199
ContainerStore()
Definition: containerstore.cpp:116
static int getType(const ConstPtr &ptr)
Definition: containerstore.cpp:607
std::map< std::pair< std::string, std::string >, int > mLevelledItemMap
Definition: containerstore.hpp:79
void addInitialItem(const std::string &id, const std::string &owner, int count, bool topLevel=true, const std::string &levItem="")
Definition: containerstore.cpp:434
Pointer to a LiveCellRef.
Definition: ptr.hpp:19
MWWorld::CellRefList< ESM::Probe >::List::iterator mProbe
Definition: containerstore.hpp:213
bool incIterator()
Definition: containerstore.cpp:930
bool operator!=(const CellStore &left, const CellStore &right)
Definition: cellstore.cpp:935
MWWorld::CellRefList< ESM::Weapon >::List::iterator mWeapon
Definition: containerstore.hpp:215
MWWorld::CellRefList< ESM::Light > lights
Definition: containerstore.hpp:72
ContainerStoreIterator unstack(const Ptr &ptr, const Ptr &container, int count=1)
Definition: containerstore.cpp:150
MWWorld::CellRefList< ESM::Light >::List::iterator mLight
Definition: containerstore.hpp:210
static const int Type_Miscellaneous
Definition: containerstore.hpp:53
virtual void itemRemoved(const ConstPtr &item, int count)
Definition: containerstore.hpp:38
int count(const std::string &id)
Definition: containerstore.cpp:130
MWWorld::CellRefList< ESM::Armor >::List::iterator mArmor
Definition: containerstore.hpp:206
void storeStates(const CellRefList< T > &collection, ESM::InventoryState &inventory, int &index, bool equipable=false) const
Definition: containerstore.cpp:97
float mCachedWeight
Definition: containerstore.hpp:85
static const int Type_Lockpick
Definition: containerstore.hpp:52
ContainerStoreIterator(ContainerStore *container)
End-iterator.
Definition: containerstore.cpp:792
bool resetIterator()
Definition: containerstore.cpp:862
ContainerStoreIterator & operator=(const ContainerStoreIterator &rhs)
Definition: containerstore.cpp:1115
Ptr * operator->() const
Definition: containerstore.cpp:998
void copy(const ContainerStoreIterator &src)
Definition: containerstore.cpp:1089
virtual void readState(const ESM::InventoryState &state)
Definition: containerstore.cpp:750
MWWorld::CellRefList< ESM::Lockpick >::List::iterator mLockpick
Definition: containerstore.hpp:211